Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(48653): Missing syntax error for new A?.b() #48656

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
@@ -647,6 +647,10 @@
"category": "Error",
"code": 1208
},
"Invalid optional chain from new expression. Did you mean to call '{0}()'?": {
"category": "Error",
"code": 1209
},
"Code contained in a class is evaluated in JavaScript's strict mode which does not allow this use of '{0}'. For more information, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode.": {
"category": "Error",
"code": 1210
3 changes: 3 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
@@ -5970,6 +5970,9 @@ namespace ts {
typeArguments = (expression as ExpressionWithTypeArguments).typeArguments;
expression = (expression as ExpressionWithTypeArguments).expression;
}
if (token() === SyntaxKind.QuestionDotToken) {
parseErrorAtCurrentToken(Diagnostics.Invalid_optional_chain_from_new_expression_Did_you_mean_to_call_0, getTextOfNodeFromSourceText(sourceText, expression));
}
const argumentList = token() === SyntaxKind.OpenParenToken ? parseArgumentList() : undefined;
return finishNode(factory.createNewExpression(expression, typeArguments, argumentList), pos);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tests/cases/compiler/invalidOptionalChainFromNewExpression.ts(5,6): error TS1209: Invalid optional chain from new expression. Did you mean to call 'A()'?


==== tests/cases/compiler/invalidOptionalChainFromNewExpression.ts (1 errors) ====
class A {
b() {}
}

new A?.b() // error
~~
!!! error TS1209: Invalid optional chain from new expression. Did you mean to call 'A()'?
new A()?.b() // ok

19 changes: 19 additions & 0 deletions tests/baselines/reference/invalidOptionalChainFromNewExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [invalidOptionalChainFromNewExpression.ts]
class A {
b() {}
}

new A?.b() // error
new A()?.b() // ok


//// [invalidOptionalChainFromNewExpression.js]
var _a, _b;
var A = /** @class */ (function () {
function A() {
}
A.prototype.b = function () { };
return A;
}());
(_a = new A) === null || _a === void 0 ? void 0 : _a.b(); // error
(_b = new A()) === null || _b === void 0 ? void 0 : _b.b(); // ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/invalidOptionalChainFromNewExpression.ts ===
class A {
>A : Symbol(A, Decl(invalidOptionalChainFromNewExpression.ts, 0, 0))

b() {}
>b : Symbol(A.b, Decl(invalidOptionalChainFromNewExpression.ts, 0, 9))
}

new A?.b() // error
>new A?.b : Symbol(A.b, Decl(invalidOptionalChainFromNewExpression.ts, 0, 9))
>A : Symbol(A, Decl(invalidOptionalChainFromNewExpression.ts, 0, 0))
>b : Symbol(A.b, Decl(invalidOptionalChainFromNewExpression.ts, 0, 9))

new A()?.b() // ok
>new A()?.b : Symbol(A.b, Decl(invalidOptionalChainFromNewExpression.ts, 0, 9))
>A : Symbol(A, Decl(invalidOptionalChainFromNewExpression.ts, 0, 0))
>b : Symbol(A.b, Decl(invalidOptionalChainFromNewExpression.ts, 0, 9))

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/invalidOptionalChainFromNewExpression.ts ===
class A {
>A : A

b() {}
>b : () => void
}

new A?.b() // error
>new A?.b() : void
>new A?.b : () => void
>new A : A
>A : typeof A
>b : () => void

new A()?.b() // ok
>new A()?.b() : void
>new A()?.b : () => void
>new A() : A
>A : typeof A
>b : () => void

6 changes: 6 additions & 0 deletions tests/cases/compiler/invalidOptionalChainFromNewExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class A {
b() {}
}

new A?.b() // error
new A()?.b() // ok