fix(48653): throw an error on an invalid optional chain from new expression (#48656)

This commit is contained in:
Oleksandr T 2022-06-03 00:21:36 +03:00 committed by GitHub
parent c6447f9454
commit 2cb8ee27f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 85 additions and 0 deletions

View File

@ -651,6 +651,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

View File

@ -5942,6 +5942,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);
}

View File

@ -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

View File

@ -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

View File

@ -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))

View File

@ -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

View File

@ -0,0 +1,6 @@
class A {
b() {}
}
new A?.b() // error
new A()?.b() // ok