Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c798489

Browse filesBrowse files
author
Kanchalai Tanglertsampan
committed
Move error report of incorrect grammar in dynamic import to checker
1 parent 91d9ecf commit c798489
Copy full SHA for c798489

6 files changed

+62-46Lines changed: 62 additions & 46 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/checker.ts‎

Copy file name to clipboardExpand all lines: src/compiler/checker.ts
+29-6Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14824,16 +14824,17 @@ namespace ts {
1482414824
// Grammar checking; stop grammar-checking if checkGrammarTypeArguments return true
1482514825
checkGrammarTypeArguments(node, node.typeArguments) || checkGrammarArguments(node, node.arguments);
1482614826

14827+
// Dynamic import is not need to go through regular resolve signature.
14828+
if (node.expression.kind === SyntaxKind.ImportKeyword) {
14829+
return checkImportCallExpression(<ImportCall>node);
14830+
}
14831+
1482714832
const signature = getResolvedSignature(node);
1482814833

1482914834
if (node.expression.kind === SyntaxKind.SuperKeyword) {
1483014835
return voidType;
1483114836
}
1483214837

14833-
if (node.expression.kind === SyntaxKind.ImportKeyword) {
14834-
return checkImportCallExpression(<CallExpression>node);
14835-
}
14836-
1483714838
if (node.kind === SyntaxKind.NewExpression) {
1483814839
const declaration = signature.declaration;
1483914840

@@ -14872,7 +14873,12 @@ namespace ts {
1487214873
return getReturnTypeOfSignature(signature);
1487314874
}
1487414875

14875-
function checkImportCallExpression(node: CallExpression): Type {
14876+
function checkImportCallExpression(node: ImportCall): Type {
14877+
// Check grammar of dynamic import
14878+
if (checkGrammarImportCallExpression(node)) {
14879+
return createPromiseReturnType(node, anyType);
14880+
}
14881+
1487614882
if (modulekind === ModuleKind.ES2015) {
1487714883
grammarErrorOnNode(node, Diagnostics.Dynamic_import_cannot_be_used_when_targeting_ECMAScript_2015_modules);
1487814884
}
@@ -23364,5 +23370,22 @@ namespace ts {
2336423370
});
2336523371
return result;
2336623372
}
23373+
23374+
/**
23375+
*
23376+
* @param node
23377+
*/
23378+
function checkGrammarImportCallExpression(node: ImportCall): boolean {
23379+
const arguments = node.arguments;
23380+
if (arguments.length !== 1) {
23381+
return grammarErrorOnNode(node, Diagnostics.Dynamic_import_must_have_one_specifier_as_an_argument);
23382+
}
23383+
23384+
// see: parseArgumentOrArrayLiteralElement...we use this function which parse arguments of callExpression to parse specifier for dynamic import.
23385+
// parseArgumentOrArrayLiteralElement allows spread element to be in an argument list which is not allowed as specifier in dynamic import.
23386+
if (isSpreadExpression(arguments[0])) {
23387+
return grammarErrorOnNode(arguments[0], Diagnostics.Specifier_of_dynamic_import_cannot_be_spread_element);
23388+
}
23389+
}
2336723390
}
23368-
}
23391+
}
Collapse file

‎src/compiler/diagnosticMessages.json‎

Copy file name to clipboardExpand all lines: src/compiler/diagnosticMessages.json
+9-8Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,15 @@
871871
"category": "Error",
872872
"code": 1320
873873
},
874+
"Dynamic import must have one specifier as an argument.": {
875+
"category": "Error",
876+
"code": 1321
877+
},
878+
"Specifier of dynamic import cannot be spread element.": {
879+
"category": "Error",
880+
"code": 1322
881+
},
882+
874883
"Duplicate identifier '{0}'.": {
875884
"category": "Error",
876885
"code": 2300
@@ -3297,14 +3306,6 @@
32973306
"category": "Error",
32983307
"code": 17013
32993308
},
3300-
"Dynamic import can only have one specifier as an argument.": {
3301-
"category": "Error",
3302-
"code": 17014
3303-
},
3304-
"Specifier of dynamic import cannot be spread element.": {
3305-
"category": "Error",
3306-
"code": 17015
3307-
},
33083309

33093310
"Circularity detected while resolving configuration: {0}": {
33103311
"category": "Error",
Collapse file

‎src/compiler/parser.ts‎

Copy file name to clipboardExpand all lines: src/compiler/parser.ts
-14Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3652,20 +3652,6 @@ namespace ts {
36523652
return finishNode(node);
36533653
}
36543654

3655-
if (isImportCall(expression)) {
3656-
// Check that the argument array is strictly of length 1 and the argument is assignment-expression
3657-
const arguments = expression.arguments;
3658-
if (arguments.length !== 1) {
3659-
parseErrorAtPosition(arguments.pos, arguments.end - arguments.pos, Diagnostics.Dynamic_import_can_only_have_one_specifier_as_an_argument);
3660-
}
3661-
3662-
// see: parseArgumentOrArrayLiteralElement...we use this function which parse arguments of callExpression to parse specifier for dynamic import.
3663-
// parseArgumentOrArrayLiteralElement allows spread element to be in an argument list which is not allowed in dynamic import.
3664-
if (expression.arguments.length >= 1 && isSpreadExpression(arguments[0])) {
3665-
parseErrorAtPosition(arguments.pos, arguments.end - arguments.pos, Diagnostics.Specifier_of_dynamic_import_cannot_be_spread_element);
3666-
}
3667-
}
3668-
36693655
return expression;
36703656
}
36713657

Collapse file

‎src/compiler/program.ts‎

Copy file name to clipboardExpand all lines: src/compiler/program.ts
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,8 +1268,8 @@ namespace ts {
12681268
if (isJavaScriptFile && isRequireCall(node, /*checkArgumentIsStringLiteral*/true)) {
12691269
(imports || (imports = [])).push(<StringLiteral>(<CallExpression>node).arguments[0]);
12701270
}
1271-
// we can safely get the first argument in the list here because we already issue parsing error if the length is not 1
1272-
else if (isImportCall(node) && node.arguments[0].kind === SyntaxKind.StringLiteral) {
1271+
// we have to check the argument list has length of 1. We will still have to process these even though we have parsing error.
1272+
else if (isImportCall(node) && node.arguments.length === 1 && node.arguments[0].kind === SyntaxKind.StringLiteral) {
12731273
(imports || (imports = [])).push(<StringLiteral>(<CallExpression>node).arguments[0]);
12741274
}
12751275
else {
Collapse file

‎src/compiler/transformers/module/module.ts‎

Copy file name to clipboardExpand all lines: src/compiler/transformers/module/module.ts
+21-15Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -543,17 +543,14 @@ namespace ts {
543543
return createNew(
544544
createIdentifier("Promise"),
545545
/*typeArguments*/ undefined,
546-
[
547-
createArrowFunction(
548-
/*modifiers*/undefined,
549-
/*typeParameters*/ undefined,
550-
[createParameter(/*decorator*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, /*name*/ resolve)],
551-
/*type*/ undefined,
552-
createToken(SyntaxKind.EqualsGreaterThanToken),
553-
createCall(createIdentifier("require"), /*typeArguments*/ undefined, [createArrayLiteral([node.arguments[0]]), resolve])
554-
)
555-
]
556-
);
546+
[createArrowFunction(
547+
/*modifiers*/undefined,
548+
/*typeParameters*/ undefined,
549+
[createParameter(/*decorator*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, /*name*/ resolve)],
550+
/*type*/ undefined,
551+
createToken(SyntaxKind.EqualsGreaterThanToken),
552+
createCall(createIdentifier("require"), /*typeArguments*/ undefined, node.arguments.concat([resolve]))
553+
)]);
557554
}
558555

559556
function transformImportCallExpressionCommonJS(node: ImportCall): Expression {
@@ -564,11 +561,20 @@ namespace ts {
564561
// if we simply do require in resolve callback in Promise constructor. We will execute the loading immediately
565562
return createCall(
566563
createPropertyAccess(
567-
createCall(/*expression*/ createPropertyAccess(createIdentifier("Promise"), "resolve"), /*typeArguments*/ undefined, /*argumentsArray*/[]),
568-
"then"),
564+
createCall(
565+
createPropertyAccess(createIdentifier("Promise"), "resolve"),
566+
/*typeArguments*/ undefined,
567+
/*argumentsArray*/[]
568+
), "then"),
569569
/*typeArguments*/ undefined,
570-
[createArrowFunction(/*modifiers*/ undefined, /*typeParameters*/ undefined, /*parameters*/ undefined, /*type*/ undefined, createToken(SyntaxKind.EqualsGreaterThanToken), createCall(createIdentifier("require"), /*typeArguments*/ undefined, [node.arguments[0]]))]
571-
);
570+
[createArrowFunction(
571+
/*modifiers*/ undefined,
572+
/*typeParameters*/ undefined,
573+
/*parameters*/ undefined,
574+
/*type*/ undefined,
575+
createToken(SyntaxKind.EqualsGreaterThanToken),
576+
createCall(createIdentifier("require"), /*typeArguments*/ undefined, node.arguments)
577+
)]);
572578
}
573579

574580
/**
Collapse file

‎src/compiler/transformers/module/system.ts‎

Copy file name to clipboardExpand all lines: src/compiler/transformers/module/system.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,7 @@ namespace ts {
14871487
createIdentifier("import")
14881488
),
14891489
/*typeArguments*/ undefined,
1490-
[node.arguments[0]]
1490+
node.arguments
14911491
);
14921492
}
14931493

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.