diff --git a/internal/js_parser/js_parser.go b/internal/js_parser/js_parser.go index e41bc4eb37..8c989c477c 100644 --- a/internal/js_parser/js_parser.go +++ b/internal/js_parser/js_parser.go @@ -2070,10 +2070,11 @@ func (p *parser) parseProperty(startLoc logger.Loc, kind js_ast.PropertyKind, op p.lexer.Next() case js_lexer.TPrivateIdentifier: - if !opts.isClass || (p.options.ts.Config.ExperimentalDecorators == config.True && len(opts.decorators) > 0) { + if p.options.ts.Parse && p.options.ts.Config.ExperimentalDecorators == config.True && len(opts.decorators) > 0 { + p.log.AddError(&p.tracker, p.lexer.Range(), "TypeScript experimental decorators cannot be used on private identifiers") + } else if !opts.isClass { p.lexer.Expected(js_lexer.TIdentifier) - } - if opts.tsDeclareRange.Len != 0 { + } else if opts.tsDeclareRange.Len != 0 { p.log.AddError(&p.tracker, opts.tsDeclareRange, "\"declare\" cannot be used with a private identifier") } name := p.lexer.Identifier diff --git a/internal/js_parser/ts_parser_test.go b/internal/js_parser/ts_parser_test.go index 2c478e7d5a..7442a26933 100644 --- a/internal/js_parser/ts_parser_test.go +++ b/internal/js_parser/ts_parser_test.go @@ -890,16 +890,18 @@ func TestTSPrivateIdentifiers(t *testing.T) { expectPrintedTS(t, "class Foo { static set #foo(x) {} }", "class Foo {\n static set #foo(x) {\n }\n}\n") // Decorators are not valid on private members - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo = 1 }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo() {} }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec get #foo() {} }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec set #foo() {x} }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo = 1 }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo() {} }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static get #foo() {} }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static set #foo() {x} }", ": ERROR: Expected identifier but found \"#foo\"\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo = 1 }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo() {} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec get #foo() {} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec set #foo(x) {x} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec accessor #foo }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo = 1 }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo() {} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static get #foo() {} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static set #foo(x) {x} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static accessor #foo }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") // Decorators are now able to access private names, since the TypeScript // compiler was changed to move them into a "static {}" block within the @@ -1919,18 +1921,20 @@ func TestTSExperimentalDecorator(t *testing.T) { expectParseErrorExperimentalDecoratorTS(t, "({ foo(@dec x) {} })", ": ERROR: Expected identifier but found \"@\"\n") // Decorators aren't allowed with private names - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo = 1 }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo() {} }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec *#foo() {} }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec async #foo() {} }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec async* #foo() {} }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo = 1 }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo() {} }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static *#foo() {} }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static async #foo() {} }", ": ERROR: Expected identifier but found \"#foo\"\n") - expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static async* #foo() {} }", ": ERROR: Expected identifier but found \"#foo\"\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo = 1 }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo() {} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec *#foo() {} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec async #foo() {} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec async* #foo() {} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec accessor #foo }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo = 1 }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo() {} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static *#foo() {} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static async #foo() {} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static async* #foo() {} }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") + expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static accessor #foo }", ": ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") // Decorators aren't allowed on class constructors expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec constructor() {} }", ": ERROR: Decorators are not allowed on class constructors\n")