mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Parse import call and use collect its specifier
This commit is contained in:
+20
-18
@@ -167,6 +167,8 @@ namespace ts {
|
||||
return visitNode(cbNode, (<CallExpression>node).expression) ||
|
||||
visitNodes(cbNodes, (<CallExpression>node).typeArguments) ||
|
||||
visitNodes(cbNodes, (<CallExpression>node).arguments);
|
||||
case SyntaxKind.ImportCallExpression:
|
||||
return visitNode(cbNode, (<ImportCall>node).expression);
|
||||
case SyntaxKind.TaggedTemplateExpression:
|
||||
return visitNode(cbNode, (<TaggedTemplateExpression>node).tag) ||
|
||||
visitNode(cbNode, (<TaggedTemplateExpression>node).template);
|
||||
@@ -3686,20 +3688,19 @@ namespace ts {
|
||||
// the last two CallExpression productions. 2) We see 'import' which must start import call.
|
||||
// 3)we have a MemberExpression which either completes the LeftHandSideExpression,
|
||||
// or starts the beginning of the first four CallExpression productions.
|
||||
let expression: LeftHandSideExpression;
|
||||
if (token() === SyntaxKind.SuperKeyword) {
|
||||
expression = parseSuperExpression();
|
||||
}
|
||||
else if (token() === SyntaxKind.ImportKeyword && lookAhead(nextTokenIsOpenParen)) {
|
||||
// We don't want to eagerly consume all import keyword as import call expression.
|
||||
|
||||
if (token() === SyntaxKind.ImportKeyword && lookAhead(nextTokenIsOpenParen)) {
|
||||
// We don't want to eagerly consume all import keyword as import call expression so we look a head to find "("
|
||||
// For example:
|
||||
// var foo3 = require("subfolder//*require0*/
|
||||
// import * as foo1 from "module-from-node//*import_as1*/ -> we want this import to be a statement rather than import call expression
|
||||
expression = parseImportExpression();
|
||||
}
|
||||
else {
|
||||
expression = parseMemberExpressionOrHigher();
|
||||
// var foo3 = require("subfolder
|
||||
// import * as foo1 from "module-from-node -> we want this import to be a statement rather than import call expression
|
||||
const importCall = parseImportCall();
|
||||
if (importCall.expression.kind === SyntaxKind.StringLiteral) {
|
||||
(sourceFile.imports || (sourceFile.imports = [])).push(importCall.expression as StringLiteral);
|
||||
}
|
||||
return importCall;
|
||||
}
|
||||
const expression = token() === SyntaxKind.SuperKeyword ? parseSuperExpression() : parseMemberExpressionOrHigher();
|
||||
|
||||
// Now, we *may* be complete. However, we might have consumed the start of a
|
||||
// CallExpression. As such, we need to consume the rest of it here to be complete.
|
||||
@@ -3773,12 +3774,13 @@ namespace ts {
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
function parseImportExpression(): LeftHandSideExpression {
|
||||
const expression = parseTokenNode<PrimaryExpression>();
|
||||
if (token() === SyntaxKind.OpenParenToken) {
|
||||
return expression;
|
||||
}
|
||||
Debug.assert(token() === SyntaxKind.OpenParenToken, "Parsing import expression expects open parenthesis after import keyword");
|
||||
function parseImportCall(): ImportCall {
|
||||
const importCallExpr = <ImportCall>createNode(SyntaxKind.ImportCallExpression);
|
||||
parseExpected(SyntaxKind.ImportKeyword);
|
||||
parseExpected(SyntaxKind.OpenParenToken);
|
||||
importCallExpr.expression = parseAssignmentExpressionOrHigher();
|
||||
parseExpected(SyntaxKind.CloseParenToken);
|
||||
return finishNode(importCallExpr);
|
||||
}
|
||||
|
||||
function tagNamesAreEquivalent(lhs: JsxTagNameExpression, rhs: JsxTagNameExpression): boolean {
|
||||
|
||||
@@ -1178,15 +1178,12 @@ namespace ts {
|
||||
}
|
||||
|
||||
function collectExternalModuleReferences(file: SourceFile): void {
|
||||
if (file.imports) {
|
||||
return;
|
||||
}
|
||||
|
||||
const isJavaScriptFile = isSourceFileJavaScript(file);
|
||||
const isExternalModuleFile = isExternalModule(file);
|
||||
const isDtsFile = isDeclarationFile(file);
|
||||
|
||||
let imports: LiteralExpression[];
|
||||
// file.imports may not be undefined if there exists dynamic import
|
||||
let imports = file.imports;
|
||||
let moduleAugmentations: LiteralExpression[];
|
||||
let ambientModules: string[];
|
||||
|
||||
|
||||
@@ -391,8 +391,12 @@ namespace ts {
|
||||
MergeDeclarationMarker,
|
||||
EndOfDeclarationMarker,
|
||||
|
||||
// Dynamic import
|
||||
ImportCallExpression,
|
||||
|
||||
// Enum value count
|
||||
Count,
|
||||
|
||||
// Markers
|
||||
FirstAssignment = EqualsToken,
|
||||
LastAssignment = CaretEqualsToken,
|
||||
@@ -1443,8 +1447,9 @@ namespace ts {
|
||||
expression: SuperExpression;
|
||||
}
|
||||
|
||||
export interface ImportCall extends CallExpression {
|
||||
expression: ImportExpression;
|
||||
export interface ImportCall extends LeftHandSideExpression, Declaration {
|
||||
kind: SyntaxKind.ImportCallExpression;
|
||||
expression: Expression;
|
||||
}
|
||||
|
||||
export interface ExpressionWithTypeArguments extends TypeNode {
|
||||
|
||||
@@ -662,6 +662,10 @@ namespace ts {
|
||||
return n.kind === SyntaxKind.CallExpression && (<CallExpression>n).expression.kind === SyntaxKind.SuperKeyword;
|
||||
}
|
||||
|
||||
export function isImportCall(n: Node): n is ImportCall {
|
||||
return n.kind === SyntaxKind.ImportCallExpression;
|
||||
}
|
||||
|
||||
export function isPrologueDirective(node: Node): node is PrologueDirective {
|
||||
return node.kind === SyntaxKind.ExpressionStatement
|
||||
&& (<ExpressionStatement>node).expression.kind === SyntaxKind.StringLiteral;
|
||||
@@ -3886,7 +3890,8 @@ namespace ts {
|
||||
|| kind === SyntaxKind.TrueKeyword
|
||||
|| kind === SyntaxKind.SuperKeyword
|
||||
|| kind === SyntaxKind.NonNullExpression
|
||||
|| kind === SyntaxKind.MetaProperty;
|
||||
|| kind === SyntaxKind.MetaProperty
|
||||
|| kind === SyntaxKind.ImportCallExpression;
|
||||
}
|
||||
|
||||
export function isLeftHandSideExpression(node: Node): node is LeftHandSideExpression {
|
||||
|
||||
Reference in New Issue
Block a user