Skip to content

Commit 6e4be2f

Browse files
committed
js parser: recover from bad #private identifiers
1 parent c9c6357 commit 6e4be2f

File tree

3 files changed

+7
-12
lines changed

3 files changed

+7
-12
lines changed

internal/js_parser/js_parser.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,6 @@ type parser struct {
382382
shouldFoldTypeScriptConstantExpressions bool
383383

384384
allowIn bool
385-
allowPrivateIdentifiers bool
386385
hasTopLevelReturn bool
387386
latestReturnHadSemicolon bool
388387
messageAboutThisIsUndefined bool
@@ -3538,7 +3537,7 @@ func (p *parser) parsePrefix(level js_ast.L, errors *deferredErrors, flags exprF
35383537
return js_ast.Expr{Loc: loc, Data: js_ast.EThisShared}
35393538

35403539
case js_lexer.TPrivateIdentifier:
3541-
if !p.allowPrivateIdentifiers || !p.allowIn || level >= js_ast.LCompare {
3540+
if !p.allowIn || level >= js_ast.LCompare {
35423541
p.lexer.Unexpected()
35433542
}
35443543

@@ -4263,7 +4262,7 @@ func (p *parser) parseSuffix(left js_ast.Expr, level js_ast.L, errors *deferredE
42634262
case js_lexer.TDot:
42644263
p.lexer.Next()
42654264

4266-
if p.lexer.Token == js_lexer.TPrivateIdentifier && p.allowPrivateIdentifiers {
4265+
if p.lexer.Token == js_lexer.TPrivateIdentifier {
42674266
// "a.#b"
42684267
// "a?.b.#c"
42694268
if _, ok := left.Data.(*js_ast.ESuper); ok {
@@ -4373,7 +4372,7 @@ func (p *parser) parseSuffix(left js_ast.Expr, level js_ast.L, errors *deferredE
43734372
}}
43744373

43754374
default:
4376-
if p.lexer.Token == js_lexer.TPrivateIdentifier && p.allowPrivateIdentifiers {
4375+
if p.lexer.Token == js_lexer.TPrivateIdentifier {
43774376
// "a?.#b"
43784377
name := p.lexer.Identifier
43794378
nameLoc := p.lexer.Loc()
@@ -6450,9 +6449,7 @@ func (p *parser) parseClass(classKeyword logger.Range, name *ast.LocRef, classOp
64506449

64516450
// Allow "in" and private fields inside class bodies
64526451
oldAllowIn := p.allowIn
6453-
oldAllowPrivateIdentifiers := p.allowPrivateIdentifiers
64546452
p.allowIn = true
6455-
p.allowPrivateIdentifiers = true
64566453

64576454
// A scope is needed for private identifiers
64586455
scopeIndex := p.pushScopeForParsePass(js_ast.ScopeClassBody, bodyLoc)
@@ -6512,7 +6509,6 @@ func (p *parser) parseClass(classKeyword logger.Range, name *ast.LocRef, classOp
65126509
}
65136510

65146511
p.allowIn = oldAllowIn
6515-
p.allowPrivateIdentifiers = oldAllowPrivateIdentifiers
65166512

65176513
closeBraceLoc := p.saveExprCommentsHere()
65186514
p.lexer.Expect(js_lexer.TCloseBrace)

internal/js_parser/js_parser_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6065,10 +6065,10 @@ func TestPreserveOptionalChainParentheses(t *testing.T) {
60656065
}
60666066

60676067
func TestPrivateIdentifiers(t *testing.T) {
6068-
expectParseError(t, "#foo", "<stdin>: ERROR: Unexpected \"#foo\"\n")
6069-
expectParseError(t, "#foo in this", "<stdin>: ERROR: Unexpected \"#foo\"\n")
6070-
expectParseError(t, "this.#foo", "<stdin>: ERROR: Expected identifier but found \"#foo\"\n")
6071-
expectParseError(t, "this?.#foo", "<stdin>: ERROR: Expected identifier but found \"#foo\"\n")
6068+
expectParseError(t, "#foo", "<stdin>: ERROR: Expected \"in\" but found end of file\n")
6069+
expectParseError(t, "#foo in this", "<stdin>: ERROR: Private name \"#foo\" must be declared in an enclosing class\n")
6070+
expectParseError(t, "this.#foo", "<stdin>: ERROR: Private name \"#foo\" must be declared in an enclosing class\n")
6071+
expectParseError(t, "this?.#foo", "<stdin>: ERROR: Private name \"#foo\" must be declared in an enclosing class\n")
60726072
expectParseError(t, "({ #foo: 1 })", "<stdin>: ERROR: Expected identifier but found \"#foo\"\n")
60736073
expectParseError(t, "class Foo { x = { #foo: 1 } }", "<stdin>: ERROR: Expected identifier but found \"#foo\"\n")
60746074
expectParseError(t, "class Foo { x = #foo }", "<stdin>: ERROR: Expected \"in\" but found \"}\"\n")

internal/js_parser/ts_parser.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,6 @@ func (originalParser *parser) isTypeScriptArrowReturnTypeAfterQuestionAndBeforeC
979979
p := newParser(logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug, nil), originalParser.source, originalParser.lexer, &originalParser.options)
980980

981981
// Clone all state that the parser needs to parse this arrow function body
982-
p.allowPrivateIdentifiers = originalParser.allowPrivateIdentifiers
983982
p.allowIn = originalParser.allowIn
984983
p.lexer.IsLogDisabled = true
985984
p.pushScopeForParsePass(js_ast.ScopeEntry, logger.Loc{Start: 0})

0 commit comments

Comments
 (0)