diff --git a/tsc/internal/fourslash/tests/documentHighlightsDecoratorOnNonClassExpression_test.go b/tsc/internal/fourslash/tests/documentHighlightsDecoratorOnNonClassExpression_test.go new file mode 100644 index 0000000000000..855a4947fd3b6 --- /dev/null +++ b/tsc/internal/fourslash/tests/documentHighlightsDecoratorOnNonClassExpression_test.go @@ -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") +} diff --git a/tsc/internal/parser/parser.go b/tsc/internal/parser/parser.go index cea0c61f11038..21d94d6c07cb1 100644 --- a/tsc/internal/parser/parser.go +++ b/tsc/internal/parser/parser.go @@ -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) { diff --git a/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.errors.txt b/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.errors.txt index 1758cfc5744e4..eb74263912191 100644 --- a/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.errors.txt @@ -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. \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.js b/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.js index 02e3942892563..f715e55e41669 100644 --- a/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.js +++ b/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.js @@ -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) { @@ -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) { @@ -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 }; } @@ -60,4 +65,4 @@ var __rest = (this && this.__rest) || function (s, e) { } return t; }; -( = __rest({}, [])); +(d = __rest({}, [])); diff --git a/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.symbols b/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.symbols index 554fe9b5c6c3f..2919b480ad4e4 100644 --- a/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.symbols +++ b/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.symbols @@ -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)) diff --git a/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.types b/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.types index 8f2395156cbee..1aba5233e6490 100644 --- a/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.types +++ b/tsc/testdata/baselines/reference/compiler/decoratorMissingClassExpression.types @@ -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 >y : any for await (@d of y) {} -> : any +>d : any >y : any } @@ -31,6 +50,6 @@ async function f(y: any) { >({...@d} = {}) : {} >{...@d} = {} : {} >{...@d} : any -> : any +>d : any >{} : {} diff --git a/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.errors.txt b/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.errors.txt index 1bede95a586da..5ad40ae29d3ed 100644 --- a/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.errors.txt +++ b/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.errors.txt @@ -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(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. } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.js b/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.js index 58e3aa46cc15c..4e4dd0b6c91a1 100644 --- a/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.js +++ b/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.js @@ -8,5 +8,6 @@ var F = @dec () => { //// [decoratorOnArrowFunction.js] "use strict"; -var F = () => { -}; +var F = dec(); +{ +} diff --git a/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.symbols b/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.symbols index 78552e82fb68f..b8138b7af46a9 100644 --- a/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.symbols +++ b/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.symbols @@ -10,5 +10,5 @@ declare function dec(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)) } diff --git a/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.types b/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.types index c7fc3290b340a..38179df62d379 100644 --- a/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.types +++ b/tsc/testdata/baselines/reference/conformance/decoratorOnArrowFunction.types @@ -6,7 +6,7 @@ declare function dec(target: T): T; >target : T var F = @dec () => { ->F : (: any) => void ->@dec () => {} : (: any) => void -> : any +>F : unknown +>dec () : unknown +>dec : (target: T) => T } diff --git a/tsc/testdata/baselines/reference/conformance/decoratorOnFunctionExpression.js b/tsc/testdata/baselines/reference/conformance/decoratorOnFunctionExpression.js index b7c4d11d8bc87..7f6d899a02277 100644 --- a/tsc/testdata/baselines/reference/conformance/decoratorOnFunctionExpression.js +++ b/tsc/testdata/baselines/reference/conformance/decoratorOnFunctionExpression.js @@ -8,6 +8,6 @@ var F = @dec function () { //// [decoratorOnFunctionExpression.js] "use strict"; -var F = ; +var F = dec; function () { } diff --git a/tsc/testdata/baselines/reference/conformance/decoratorOnFunctionExpression.symbols b/tsc/testdata/baselines/reference/conformance/decoratorOnFunctionExpression.symbols index 01ff84d2cb565..f504f567f7d22 100644 --- a/tsc/testdata/baselines/reference/conformance/decoratorOnFunctionExpression.symbols +++ b/tsc/testdata/baselines/reference/conformance/decoratorOnFunctionExpression.symbols @@ -10,5 +10,6 @@ declare function dec(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)) } diff --git a/tsc/testdata/baselines/reference/conformance/decoratorOnFunctionExpression.types b/tsc/testdata/baselines/reference/conformance/decoratorOnFunctionExpression.types index f549eb1a1edd6..11dfacff56402 100644 --- a/tsc/testdata/baselines/reference/conformance/decoratorOnFunctionExpression.types +++ b/tsc/testdata/baselines/reference/conformance/decoratorOnFunctionExpression.types @@ -6,7 +6,7 @@ declare function dec(target: T): T; >target : T var F = @dec function () { ->F : any -> : any +>F : (target: T) => T +>dec : (target: T) => T > : () => void } diff --git a/tsc/testdata/baselines/reference/fourslash/documentHighlights/documentHighlightsDecoratorOnNonClassExpression.baseline.jsonc b/tsc/testdata/baselines/reference/fourslash/documentHighlights/documentHighlightsDecoratorOnNonClassExpression.baseline.jsonc new file mode 100644 index 0000000000000..b97c6ab7cda77 --- /dev/null +++ b/tsc/testdata/baselines/reference/fourslash/documentHighlights/documentHighlightsDecoratorOnNonClassExpression.baseline.jsonc @@ -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; \ No newline at end of file diff --git a/tsc/testdata/tests/cases/compiler/decoratorMissingClassExpression.ts b/tsc/testdata/tests/cases/compiler/decoratorMissingClassExpression.ts index a91e7352423c2..e4c5a6cabb618 100644 --- a/tsc/testdata/tests/cases/compiler/decoratorMissingClassExpression.ts +++ b/tsc/testdata/tests/cases/compiler/decoratorMissingClassExpression.ts @@ -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) {