diff --git a/tsc/internal/parser/parser.go b/tsc/internal/parser/parser.go index c89104f31263f..ecd87335f1392 100644 --- a/tsc/internal/parser/parser.go +++ b/tsc/internal/parser/parser.go @@ -5589,6 +5589,11 @@ func (p *Parser) parsePropertyAccessExpressionRest(pos int, expression *ast.Expr if isOptionalChain && ast.IsPrivateIdentifier(name) { p.parseErrorAtRange(p.skipRangeTrivia(name.Loc), diagnostics.An_optional_chain_cannot_contain_private_identifiers) } + p.checkInstantiationExpressionBeforePropertyAccess(expression) + return p.finishNode(propertyAccess, pos) +} + +func (p *Parser) checkInstantiationExpressionBeforePropertyAccess(expression *ast.Expression) { if ast.IsExpressionWithTypeArguments(expression) { typeArguments := expression.TypeArgumentList() if typeArguments != nil { @@ -5596,7 +5601,6 @@ func (p *Parser) parsePropertyAccessExpressionRest(pos int, expression *ast.Expr p.parseErrorAtRange(loc, diagnostics.An_instantiation_expression_cannot_be_followed_by_a_property_access) } } - return p.finishNode(propertyAccess, pos) } func (p *Parser) tryReparseOptionalChain(node *ast.Expression) bool { @@ -5629,6 +5633,11 @@ func (p *Parser) parseElementAccessExpressionRest(pos int, expression *ast.Expre argumentExpression = p.parseExpressionAllowIn() } p.parseExpected(ast.KindCloseBracketToken) + if questionDotToken == nil { + // A line break can make `` an instantiation rather than a comparison. + // Require parentheses before a regular element access, like for a dot access. + p.checkInstantiationExpressionBeforePropertyAccess(expression) + } isOptionalChain := questionDotToken != nil || p.tryReparseOptionalChain(expression) return p.finishNode(p.factory.NewElementAccessExpression(expression, questionDotToken, argumentExpression, core.IfElse(isOptionalChain, ast.NodeFlagsOptionalChain, ast.NodeFlagsNone)), pos) } diff --git a/tsc/testdata/baselines/reference/compiler/instantiationExpressionBeforeElementAccess.errors.txt b/tsc/testdata/baselines/reference/compiler/instantiationExpressionBeforeElementAccess.errors.txt new file mode 100644 index 0000000000000..46e6a5d011415 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/instantiationExpressionBeforeElementAccess.errors.txt @@ -0,0 +1,30 @@ +instantiationExpressionBeforeElementAccess.ts(4,2): error TS1477: An instantiation expression cannot be followed by a property access. +instantiationExpressionBeforeElementAccess.ts(7,5): error TS1477: An instantiation expression cannot be followed by a property access. +instantiationExpressionBeforeElementAccess.ts(10,5): error TS1477: An instantiation expression cannot be followed by a property access. + + +==== instantiationExpressionBeforeElementAccess.ts (3 errors) ==== + declare const f: { (): T; label: string }; + declare const a: { f: typeof f }; + + f + ~~~~~~~~ +!!! error TS1477: An instantiation expression cannot be followed by a property access. + ["label"]; + + a?.f + ~~~~~~~~ +!!! error TS1477: An instantiation expression cannot be followed by a property access. + ["label"]; + + a?.f /* + ~~~~~~~~ +!!! error TS1477: An instantiation expression cannot be followed by a property access. + */ ["label"]; + + (f) + ["label"]; + + (a?.f)["label"]; + a?.f?.["label"]; + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/instantiationExpressionBeforeElementAccess.js b/tsc/testdata/baselines/reference/compiler/instantiationExpressionBeforeElementAccess.js new file mode 100644 index 0000000000000..a58726d5dbc77 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/instantiationExpressionBeforeElementAccess.js @@ -0,0 +1,31 @@ +//// [tests/cases/compiler/instantiationExpressionBeforeElementAccess.ts] //// + +//// [instantiationExpressionBeforeElementAccess.ts] +declare const f: { (): T; label: string }; +declare const a: { f: typeof f }; + +f +["label"]; + +a?.f +["label"]; + +a?.f /* +*/ ["label"]; + +(f) +["label"]; + +(a?.f)["label"]; +a?.f?.["label"]; + + +//// [instantiationExpressionBeforeElementAccess.js] +"use strict"; +f["label"]; +(a?.f)["label"]; +(a?.f) /* +*/["label"]; +(f)["label"]; +((a?.f))["label"]; +(a?.f)?.["label"]; diff --git a/tsc/testdata/baselines/reference/compiler/instantiationExpressionBeforeElementAccess.symbols b/tsc/testdata/baselines/reference/compiler/instantiationExpressionBeforeElementAccess.symbols new file mode 100644 index 0000000000000..9737d085bbb46 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/instantiationExpressionBeforeElementAccess.symbols @@ -0,0 +1,54 @@ +//// [tests/cases/compiler/instantiationExpressionBeforeElementAccess.ts] //// + +=== instantiationExpressionBeforeElementAccess.ts === +declare const f: { (): T; label: string }; +>f : Symbol(f, Decl(instantiationExpressionBeforeElementAccess.ts, 0, 13)) +>T : Symbol(T, Decl(instantiationExpressionBeforeElementAccess.ts, 0, 20)) +>T : Symbol(T, Decl(instantiationExpressionBeforeElementAccess.ts, 0, 20)) +>label : Symbol(label, Decl(instantiationExpressionBeforeElementAccess.ts, 0, 28)) + +declare const a: { f: typeof f }; +>a : Symbol(a, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 13)) +>f : Symbol(f, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 18)) +>f : Symbol(f, Decl(instantiationExpressionBeforeElementAccess.ts, 0, 13)) + +f +>f : Symbol(f, Decl(instantiationExpressionBeforeElementAccess.ts, 0, 13)) + +["label"]; +>"label" : Symbol(label, Decl(instantiationExpressionBeforeElementAccess.ts, 0, 28)) + +a?.f +>a?.f : Symbol(f, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 18)) +>a : Symbol(a, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 13)) +>f : Symbol(f, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 18)) + +["label"]; +>"label" : Symbol(label, Decl(instantiationExpressionBeforeElementAccess.ts, 0, 28)) + +a?.f /* +>a?.f : Symbol(f, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 18)) +>a : Symbol(a, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 13)) +>f : Symbol(f, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 18)) + +*/ ["label"]; +>"label" : Symbol(label, Decl(instantiationExpressionBeforeElementAccess.ts, 0, 28)) + +(f) +>f : Symbol(f, Decl(instantiationExpressionBeforeElementAccess.ts, 0, 13)) + +["label"]; +>"label" : Symbol(label, Decl(instantiationExpressionBeforeElementAccess.ts, 0, 28)) + +(a?.f)["label"]; +>a?.f : Symbol(f, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 18)) +>a : Symbol(a, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 13)) +>f : Symbol(f, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 18)) +>"label" : Symbol(label, Decl(instantiationExpressionBeforeElementAccess.ts, 0, 28)) + +a?.f?.["label"]; +>a?.f : Symbol(f, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 18)) +>a : Symbol(a, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 13)) +>f : Symbol(f, Decl(instantiationExpressionBeforeElementAccess.ts, 1, 18)) +>"label" : Symbol(label, Decl(instantiationExpressionBeforeElementAccess.ts, 0, 28)) + diff --git a/tsc/testdata/baselines/reference/compiler/instantiationExpressionBeforeElementAccess.types b/tsc/testdata/baselines/reference/compiler/instantiationExpressionBeforeElementAccess.types new file mode 100644 index 0000000000000..f7735361e3759 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/instantiationExpressionBeforeElementAccess.types @@ -0,0 +1,66 @@ +//// [tests/cases/compiler/instantiationExpressionBeforeElementAccess.ts] //// + +=== instantiationExpressionBeforeElementAccess.ts === +declare const f: { (): T; label: string }; +>f : { (): T; label: string; } +>label : string + +declare const a: { f: typeof f }; +>a : { f: typeof f; } +>f : { (): T; label: string; } +>f : { (): T; label: string; } + +f +>f["label"] : string +>f : { (): number; label: string; } +>f : { (): T; label: string; } + +["label"]; +>"label" : "label" + +a?.f +>a?.f["label"] : string +>a?.f : { (): number; label: string; } +>a?.f : { (): T; label: string; } +>a : { f: typeof f; } +>f : { (): T; label: string; } + +["label"]; +>"label" : "label" + +a?.f /* +>a?.f /**/ ["label"] : string +>a?.f : { (): number; label: string; } +>a?.f : { (): T; label: string; } +>a : { f: typeof f; } +>f : { (): T; label: string; } + +*/ ["label"]; +>"label" : "label" + +(f) +>(f)["label"] : string +>(f) : { (): number; label: string; } +>f : { (): number; label: string; } +>f : { (): T; label: string; } + +["label"]; +>"label" : "label" + +(a?.f)["label"]; +>(a?.f)["label"] : string +>(a?.f) : { (): number; label: string; } +>a?.f : { (): number; label: string; } +>a?.f : { (): T; label: string; } +>a : { f: typeof f; } +>f : { (): T; label: string; } +>"label" : "label" + +a?.f?.["label"]; +>a?.f?.["label"] : string +>a?.f : { (): number; label: string; } +>a?.f : { (): T; label: string; } +>a : { f: typeof f; } +>f : { (): T; label: string; } +>"label" : "label" + diff --git a/tsc/testdata/tests/cases/compiler/instantiationExpressionBeforeElementAccess.ts b/tsc/testdata/tests/cases/compiler/instantiationExpressionBeforeElementAccess.ts new file mode 100644 index 0000000000000..25c0b58a98a18 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/instantiationExpressionBeforeElementAccess.ts @@ -0,0 +1,20 @@ +// @target: es2020 +// @strict: true + +declare const f: { (): T; label: string }; +declare const a: { f: typeof f }; + +f +["label"]; + +a?.f +["label"]; + +a?.f /* +*/ ["label"]; + +(f) +["label"]; + +(a?.f)["label"]; +a?.f?.["label"];