Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/TypeScript/tsc/internal/fourslash"
"github.com/microsoft/TypeScript/tsc/internal/testutil"
)

func TestDocumentHighlightsDecoratorOnNonClassExpression(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @Filename: /main.ts
declare function /*1*/dec(x: any): any;
const x = @/*2*/dec 1;
// @Filename: /multiple.ts
const y = @/*3*/dec @/*4*/dec 1;`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyBaselineDocumentHighlights(t, nil /*preferences*/, "1", "2", "3", "4")
}
23 changes: 22 additions & 1 deletion tsc/internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5942,7 +5942,28 @@ func (p *Parser) parseDecoratedExpression() *ast.Expression {
return p.parseClassDeclarationOrExpression(pos, jsdoc, modifiers, ast.KindClassExpression)
}
p.parseErrorAt(p.nodePos(), p.nodePos(), diagnostics.Expression_expected)
return p.createMissingIdentifier()
// Decorators must be followed by a class expression. Rather than dropping the parsed decorators (which would
// leave their expressions untracked in the trivia of an enclosing node), recover by returning the decorator
// expressions themselves. Multiple decorators are joined by synthetic comma operators and wrapped in a
// synthetic parenthesized expression, so the result is still valid where a primary expression is expected.
var result *ast.Expression
for _, modifier := range modifiers.Nodes {
if modifier.Kind != ast.KindDecorator {
continue
}
expression := modifier.Expression()
if result == nil {
result = expression
continue
}
operatorToken := p.factory.NewToken(ast.KindCommaToken)
operatorToken.Loc = core.NewTextRange(expression.Pos(), expression.Pos())
result = p.finishNodeWithEnd(p.factory.NewBinaryExpression(nil /*modifiers*/, result, nil /*typeNode*/, operatorToken, expression), result.Pos(), expression.End())
}
if result.Kind == ast.KindBinaryExpression {
result = p.finishNode(p.factory.NewParenthesizedExpression(result), pos)
}
return result
}

func (p *Parser) unparseExpressionWithTypeArguments(expression *ast.Node, typeArguments *ast.NodeList, result *ast.Node) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,74 @@
assignment.ts(2,6): error TS2304: Cannot find name 'dec'.
assignment.ts(2,9): error TS1109: Expression expected.
assignment.ts(2,10): error TS1005: ';' expected.
assignment.ts(3,7): error TS2304: Cannot find name 'a'.
assignment.ts(3,8): error TS1109: Expression expected.
assignment.ts(3,9): error TS1005: ';' expected.
assignment.ts(4,6): error TS2304: Cannot find name 'a'.
assignment.ts(4,6): error TS2695: Left side of comma operator is unused and has no side effects.
assignment.ts(4,9): error TS2304: Cannot find name 'b'.
assignment.ts(4,10): error TS1109: Expression expected.
assignment.ts(4,11): error TS1005: ';' expected.
assignment.ts(5,6): error TS2304: Cannot find name 'a'.
assignment.ts(5,6): error TS2695: Left side of comma operator is unused and has no side effects.
assignment.ts(5,9): error TS2304: Cannot find name 'b'.
assignment.ts(5,10): error TS1109: Expression expected.
destructuring.ts(1,7): error TS2304: Cannot find name 'd'.
destructuring.ts(1,8): error TS1109: Expression expected.
forAwait.ts(2,17): error TS2304: Cannot find name 'd'.
forAwait.ts(2,18): error TS1109: Expression expected.


==== assignment.ts (4 errors) ====
==== assignment.ts (15 errors) ====
let x;
x = @dec 1;
~~~
!!! error TS2304: Cannot find name 'dec'.

!!! error TS1109: Expression expected.
~
!!! error TS1005: ';' expected.
x = @@a 1;
~
!!! error TS2304: Cannot find name 'a'.

!!! error TS1109: Expression expected.
~
!!! error TS1005: ';' expected.
x = @a @b 1;
~
!!! error TS2304: Cannot find name 'a'.
~
!!! error TS2695: Left side of comma operator is unused and has no side effects.
~
!!! error TS2304: Cannot find name 'b'.

!!! error TS1109: Expression expected.
~
!!! error TS1005: ';' expected.
x = @a @b[0];
~
!!! error TS2304: Cannot find name 'a'.
~
!!! error TS2695: Left side of comma operator is unused and has no side effects.
~
!!! error TS2304: Cannot find name 'b'.

!!! error TS1109: Expression expected.

==== forAwait.ts (1 errors) ====
==== forAwait.ts (2 errors) ====
async function f(y: any) {
for await (@d of y) {}
~
!!! error TS2304: Cannot find name 'd'.

!!! error TS1109: Expression expected.
}

==== destructuring.ts (1 errors) ====
==== destructuring.ts (2 errors) ====
({...@d} = {});
~
!!! error TS2304: Cannot find name 'd'.

!!! error TS1109: Expression expected.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
let x;
x = @dec 1;
x = @@a 1;
x = @a @b 1;
x = @a @b[0];

//// [forAwait.ts]
async function f(y: any) {
Expand All @@ -17,10 +19,13 @@ async function f(y: any) {
//// [assignment.js]
"use strict";
let x;
x = ;
x = dec;
1;
x = ;
x = a;
1;
x = (a, b);
1;
x = (a, b)[0];
//// [forAwait.js]
"use strict";
var __asyncValues = (this && this.__asyncValues) || function (o) {
Expand All @@ -36,7 +41,7 @@ async function f(y) {
for (var _d = true, y_1 = __asyncValues(y), y_1_1; y_1_1 = await y_1.next(), _a = y_1_1.done, !_a; _d = true) {
_c = y_1_1.value;
_d = false;
= _c;
d = _c;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
Expand All @@ -60,4 +65,4 @@ var __rest = (this && this.__rest) || function (s, e) {
}
return t;
};
( = __rest({}, []));
(d = __rest({}, []));
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ x = @dec 1;
x = @@a 1;
>x : Symbol(x, Decl(assignment.ts, 0, 3))

x = @a @b 1;
>x : Symbol(x, Decl(assignment.ts, 0, 3))

x = @a @b[0];
>x : Symbol(x, Decl(assignment.ts, 0, 3))

=== forAwait.ts ===
async function f(y: any) {
>f : Symbol(f, Decl(forAwait.ts, 0, 0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,41 @@ let x;
x = @dec 1;
>x = @dec : any
>x : any
> : any
>dec : any
>1 : 1

x = @@a 1;
>x = @@a : any
>x : any
> : any
>a : any
>1 : 1

x = @a @b 1;
>x = @a @b : any
>x : any
>@a @b : any
>a @b : any
>a : any
>b : any
>1 : 1

x = @a @b[0];
>x = @a @b[0] : any
>x : any
>@a @b[0] : any
>@a @b : any
>a @b : any
>a : any
>b : any
>0 : 0

=== forAwait.ts ===
async function f(y: any) {
>f : (y: any) => Promise<void>
>y : any

for await (@d of y) {}
> : any
>d : any
>y : any
}

Expand All @@ -31,6 +50,6 @@ async function f(y: any) {
>({...@d} = {}) : {}
>{...@d} = {} : {}
>{...@d} : any
> : any
>d : any
>{} : {}

Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
decoratorOnArrowFunction.ts(3,10): error TS2554: Expected 1 arguments, but got 0.
decoratorOnArrowFunction.ts(3,16): error TS1109: Expression expected.
decoratorOnArrowFunction.ts(3,16): error TS7006: Parameter '(Missing)' implicitly has an 'any' type.
decoratorOnArrowFunction.ts(3,17): error TS1005: ';' expected.


==== decoratorOnArrowFunction.ts (2 errors) ====
==== decoratorOnArrowFunction.ts (3 errors) ====
declare function dec<T>(target: T): T;

var F = @dec () => {
~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 decoratorOnArrowFunction.ts:1:25: An argument for 'target' was not provided.

!!! error TS1109: Expression expected.

!!! error TS7006: Parameter '(Missing)' implicitly has an 'any' type.
~~
!!! error TS1005: ';' expected.
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ var F = @dec () => {

//// [decoratorOnArrowFunction.js]
"use strict";
var F = () => {
};
var F = dec();
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ declare function dec<T>(target: T): T;

var F = @dec () => {
>F : Symbol(F, Decl(decoratorOnArrowFunction.ts, 2, 3))
> : Symbol((Missing), Decl(decoratorOnArrowFunction.ts, 2, 15))
>dec : Symbol(dec, Decl(decoratorOnArrowFunction.ts, 0, 0))
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare function dec<T>(target: T): T;
>target : T

var F = @dec () => {
>F : (: any) => void
>@dec () => {} : (: any) => void
> : any
>F : unknown
>dec () : unknown
>dec : <T>(target: T) => T
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ var F = @dec function () {

//// [decoratorOnFunctionExpression.js]
"use strict";
var F = ;
var F = dec;
function () {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ declare function dec<T>(target: T): T;

var F = @dec function () {
>F : Symbol(F, Decl(decoratorOnFunctionExpression.ts, 2, 3))
>dec : Symbol(dec, Decl(decoratorOnFunctionExpression.ts, 0, 0))
> : Symbol((Missing), Decl(decoratorOnFunctionExpression.ts, 2, 12))
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare function dec<T>(target: T): T;
>target : T

var F = @dec function () {
>F : any
> : any
>F : <T>(target: T) => T
>dec : <T>(target: T) => T
> : () => void
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// === documentHighlights ===
// === /main.ts ===
// declare function /*HIGHLIGHTS*/[|dec|](x: any): any;
// const x = @[|dec|] 1;



// === documentHighlights ===
// === /main.ts ===
// declare function [|dec|](x: any): any;
// const x = @/*HIGHLIGHTS*/[|dec|] 1;



// === documentHighlights ===
// === /multiple.ts ===
// const y = @/*HIGHLIGHTS*/[|dec|] @[|dec|] 1;



// === documentHighlights ===
// === /multiple.ts ===
// const y = @[|dec|] @/*HIGHLIGHTS*/[|dec|] 1;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
let x;
x = @dec 1;
x = @@a 1;
x = @a @b 1;
x = @a @b[0];

// @filename: forAwait.ts
async function f(y: any) {
Expand Down