Skip to content

Commit

Permalink
reword an experimental decorators error message
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Nov 24, 2023
1 parent f3d5352 commit 7edc83d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
7 changes: 4 additions & 3 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6567,10 +6567,10 @@ func (p *parser) parseDecorators(decoratorScope *js_ast.Scope, classKeyword logg
if p.options.ts.Parse {
if p.options.ts.Config.ExperimentalDecorators == config.True {
if (context & decoratorInClassExpr) != 0 {
p.lexer.AddRangeErrorWithNotes(p.lexer.Range(), "Experimental decorators can only be used with class declarations in TypeScript",
p.lexer.AddRangeErrorWithNotes(p.lexer.Range(), "TypeScript experimental decorators can only be used with class declarations",
[]logger.MsgData{p.tracker.MsgData(classKeyword, "This is a class expression, not a class declaration:")})
} else if (context & decoratorBeforeClassExpr) != 0 {
p.log.AddError(&p.tracker, p.lexer.Range(), "Experimental decorators cannot be used in expression position in TypeScript")
p.log.AddError(&p.tracker, p.lexer.Range(), "TypeScript experimental decorators cannot be used in expression position")
}
} else {
if (context&decoratorInFnArgs) != 0 && p.options.ts.Config.ExperimentalDecorators != config.True {
Expand Down Expand Up @@ -11251,7 +11251,8 @@ func (p *parser) visitDecorators(decorators []js_ast.Decorator, decoratorScope *
if decorators != nil {
// Decorators cause us to temporarily revert to the scope that encloses the
// class declaration, since that's where the generated code for decorators
// will be inserted.
// will be inserted. I believe this currently only matters for parameter
// decorators, where the scope should not be within the argument list.
oldScope := p.currentScope
p.currentScope = decoratorScope

Expand Down
12 changes: 6 additions & 6 deletions internal/js_parser/ts_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1890,9 +1890,9 @@ func TestTSExperimentalDecorator(t *testing.T) {

// Decorators must be forbidden outside class statements
note := "<stdin>: NOTE: This is a class expression, not a class declaration:\n"
expectParseErrorExperimentalDecoratorTS(t, "(class { @dec foo })", "<stdin>: ERROR: Experimental decorators can only be used with class declarations in TypeScript\n"+note)
expectParseErrorExperimentalDecoratorTS(t, "(class { @dec foo() {} })", "<stdin>: ERROR: Experimental decorators can only be used with class declarations in TypeScript\n"+note)
expectParseErrorExperimentalDecoratorTS(t, "(class { foo(@dec x) {} })", "<stdin>: ERROR: Experimental decorators can only be used with class declarations in TypeScript\n"+note)
expectParseErrorExperimentalDecoratorTS(t, "(class { @dec foo })", "<stdin>: ERROR: TypeScript experimental decorators can only be used with class declarations\n"+note)
expectParseErrorExperimentalDecoratorTS(t, "(class { @dec foo() {} })", "<stdin>: ERROR: TypeScript experimental decorators can only be used with class declarations\n"+note)
expectParseErrorExperimentalDecoratorTS(t, "(class { foo(@dec x) {} })", "<stdin>: ERROR: TypeScript experimental decorators can only be used with class declarations\n"+note)
expectParseErrorExperimentalDecoratorTS(t, "({ @dec foo })", "<stdin>: ERROR: Expected identifier but found \"@\"\n")
expectParseErrorExperimentalDecoratorTS(t, "({ @dec foo() {} })", "<stdin>: ERROR: Expected identifier but found \"@\"\n")
expectParseErrorExperimentalDecoratorTS(t, "({ foo(@dec x) {} })", "<stdin>: ERROR: Expected identifier but found \"@\"\n")
Expand Down Expand Up @@ -2007,7 +2007,7 @@ func TestTSExperimentalDecorator(t *testing.T) {
expectParseErrorExperimentalDecoratorTS(t, "@x[y] class Foo {}", "<stdin>: ERROR: Expected \";\" but found \"class\"\n")
expectParseErrorExperimentalDecoratorTS(t, "@() => {} class Foo {}", "<stdin>: ERROR: Unexpected \")\"\n")
expectParseErrorExperimentalDecoratorTS(t, "x = @y function() {}",
"<stdin>: ERROR: Experimental decorators cannot be used in expression position in TypeScript\n"+
"<stdin>: ERROR: TypeScript experimental decorators cannot be used in expression position\n"+
"<stdin>: ERROR: Expected \"class\" but found \"function\"\n")

// Check ASI for "abstract"
Expand All @@ -2023,8 +2023,8 @@ func TestTSExperimentalDecorator(t *testing.T) {
"let stdin_default = class {\n};\nstdin_default = __decorateClass([\n x\n], stdin_default);\nexport {\n stdin_default as default\n};\n")
expectPrintedExperimentalDecoratorTS(t, "@x export default class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n x\n], Foo);\nexport {\n Foo as default\n};\n")
expectPrintedExperimentalDecoratorTS(t, "export default @x class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n x\n], Foo);\nexport {\n Foo as default\n};\n")
expectParseErrorExperimentalDecoratorTS(t, "export default (@x class {})", "<stdin>: ERROR: Experimental decorators cannot be used in expression position in TypeScript\n")
expectParseErrorExperimentalDecoratorTS(t, "export default (@x class Foo {})", "<stdin>: ERROR: Experimental decorators cannot be used in expression position in TypeScript\n")
expectParseErrorExperimentalDecoratorTS(t, "export default (@x class {})", "<stdin>: ERROR: TypeScript experimental decorators cannot be used in expression position\n")
expectParseErrorExperimentalDecoratorTS(t, "export default (@x class Foo {})", "<stdin>: ERROR: TypeScript experimental decorators cannot be used in expression position\n")
expectParseErrorExperimentalDecoratorTS(t, "export @x default class {}", "<stdin>: ERROR: Unexpected \"default\"\n")
expectParseErrorExperimentalDecoratorTS(t, "@x export @y class Foo {}", "<stdin>: ERROR: Decorators are not valid here\n")
expectParseErrorExperimentalDecoratorTS(t, "@x export default abstract", "<stdin>: ERROR: Decorators are not valid here\n")
Expand Down

0 comments on commit 7edc83d

Please sign in to comment.