Skip to content

Commit a02edb1

Browse files
author
Yui T
committed
Address PR: don't early exit when there are grammar errors
1 parent faab927 commit a02edb1

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16099,15 +16099,22 @@ namespace ts {
1609916099

1610016100
function checkImportCallExpression(node: ImportCall): Type {
1610116101
// Check grammar of dynamic import
16102-
if (checkGrammarArguments(node, node.arguments) || checkGrammarImportCallExpression(node)) {
16102+
checkGrammarArguments(node, node.arguments) || checkGrammarImportCallExpression(node);
16103+
16104+
if (node.arguments.length === 0) {
1610316105
return createPromiseReturnType(node, anyType);
1610416106
}
16105-
1610616107
const specifier = node.arguments[0];
16107-
const specifierType = checkNonNullExpression(specifier);
16108-
if (!isTypeAssignableTo(specifierType, stringType)) {
16108+
const specifierType = checkExpressionCached(specifier);
16109+
// Even though multiple arugments is grammatically incorrect, type-check extra arguments for completion
16110+
for (let i = 1; i < node.arguments.length; ++i) {
16111+
checkExpressionCached(node.arguments[i]);
16112+
}
16113+
16114+
if (specifierType.flags & TypeFlags.Undefined || specifierType.flags & TypeFlags.Null || !isTypeAssignableTo(specifierType, stringType)) {
1610916115
error(specifier, Diagnostics.Dynamic_import_s_specifier_must_be_of_type_string_but_here_has_type_0, typeToString(specifierType));
1611016116
}
16117+
1611116118
// resolveExternalModuleName will return undefined if the moduleReferenceExpression is not a string literal
1611216119
const moduleSymbol = resolveExternalModuleName(node, specifier);
1611316120
if (moduleSymbol) {

tests/baselines/reference/importCallExpression5ES2018.errors.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/conformance/es2018/dynamicImport/2.ts(3,23): error TS2532: Object is possibly 'undefined'.
2-
tests/cases/conformance/es2018/dynamicImport/2.ts(4,24): error TS2532: Object is possibly 'undefined'.
3-
tests/cases/conformance/es2018/dynamicImport/2.ts(5,24): error TS2531: Object is possibly 'null'.
4-
tests/cases/conformance/es2018/dynamicImport/2.ts(6,24): error TS2531: Object is possibly 'null'.
1+
tests/cases/conformance/es2018/dynamicImport/2.ts(3,23): error TS7036: Dynamic import's specifier must be of type 'string', but here has type '"./0" | undefined'.
2+
tests/cases/conformance/es2018/dynamicImport/2.ts(4,24): error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'undefined'.
3+
tests/cases/conformance/es2018/dynamicImport/2.ts(5,24): error TS7036: Dynamic import's specifier must be of type 'string', but here has type '"./1" | null'.
4+
tests/cases/conformance/es2018/dynamicImport/2.ts(6,24): error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'null'.
55

66

77
==== tests/cases/conformance/es2018/dynamicImport/0.ts (0 errors) ====
@@ -19,13 +19,13 @@ tests/cases/conformance/es2018/dynamicImport/2.ts(6,24): error TS2531: Object is
1919
const specify = bar() ? "./0" : undefined;
2020
let myModule = import(specify);
2121
~~~~~~~
22-
!!! error TS2532: Object is possibly 'undefined'.
22+
!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type '"./0" | undefined'.
2323
let myModule1 = import(undefined);
2424
~~~~~~~~~
25-
!!! error TS2532: Object is possibly 'undefined'.
25+
!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'undefined'.
2626
let myModule2 = import(bar() ? "./1" : null);
2727
~~~~~~~~~~~~~~~~~~~~
28-
!!! error TS2531: Object is possibly 'null'.
28+
!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type '"./1" | null'.
2929
let myModule3 = import(null);
3030
~~~~
31-
!!! error TS2531: Object is possibly 'null'.
31+
!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'null'.

tests/baselines/reference/importCallExpression6ES2018.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/conformance/es2018/dynamicImport/2.ts(4,24): error TS2532: Object is possibly 'undefined'.
2-
tests/cases/conformance/es2018/dynamicImport/2.ts(6,24): error TS2531: Object is possibly 'null'.
1+
tests/cases/conformance/es2018/dynamicImport/2.ts(4,24): error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'undefined'.
2+
tests/cases/conformance/es2018/dynamicImport/2.ts(6,24): error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'null'.
33

44

55
==== tests/cases/conformance/es2018/dynamicImport/0.ts (0 errors) ====
@@ -18,8 +18,8 @@ tests/cases/conformance/es2018/dynamicImport/2.ts(6,24): error TS2531: Object is
1818
let myModule = import(specify);
1919
let myModule1 = import(undefined);
2020
~~~~~~~~~
21-
!!! error TS2532: Object is possibly 'undefined'.
21+
!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'undefined'.
2222
let myModule2 = import(bar() ? "./1" : null);
2323
let myModule3 = import(null);
2424
~~~~
25-
!!! error TS2531: Object is possibly 'null'.
25+
!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'null'.

tests/baselines/reference/importCallExpressionGrammarError.errors.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts
22
tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts(7,17): error TS1325: Specifier of dynamic import cannot be spread element.
33
tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts(8,12): error TS1324: Dynamic import must have one specifier as an argument.
44
tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts(9,19): error TS1135: Argument expression expected.
5+
tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts(9,19): error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'undefined'.
56
tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts(10,12): error TS1324: Dynamic import must have one specifier as an argument.
7+
tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts(10,19): error TS2307: Cannot find module 'pathToModule'.
68

79

8-
==== tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts (5 errors) ====
10+
==== tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts (7 errors) ====
911
declare function getSpecifier(): string;
1012
declare var whatToLoad: boolean;
1113

@@ -23,6 +25,10 @@ tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts
2325
const p3 = import(,);
2426

2527
!!! error TS1135: Argument expression expected.
28+
29+
!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'undefined'.
2630
const p4 = import("pathToModule", "secondModule");
2731
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28-
!!! error TS1324: Dynamic import must have one specifier as an argument.
32+
!!! error TS1324: Dynamic import must have one specifier as an argument.
33+
~~~~~~~~~~~~~~
34+
!!! error TS2307: Cannot find module 'pathToModule'.

0 commit comments

Comments
 (0)