From 6933b581c0aa7569a37c21d66964f350a22ceba5 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Fri, 10 Mar 2017 16:27:22 -0800 Subject: [PATCH] Wip-type check dynamic import --- src/compiler/binder.ts | 2 +- src/compiler/checker.ts | 10 +++++++++- src/compiler/parser.ts | 12 ++++++------ src/compiler/types.ts | 4 ++-- src/compiler/utilities.ts | 2 +- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 418905e604c..b3554876c14 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -2317,7 +2317,7 @@ namespace ts { // A common practice in node modules is to set 'export = module.exports = {}', this ensures that 'exports' // is still pointing to 'module.exports'. // We do not want to consider this as 'export=' since a module can have only one of these. - // Similarlly we do not want to treat 'module.exports = exports' as an 'export='. + // Similarly we do not want to treat 'module.exports = exports' as an 'export='. const assignedExpression = getRightMostAssignedExpression(node.right); if (isEmptyObjectLiteral(assignedExpression) || isExportsOrModuleExportsOrAlias(assignedExpression)) { // Mark it as a module in case there are no other exports in the file diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 00498191f28..f9645b0986c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14862,6 +14862,14 @@ namespace ts { return getReturnTypeOfSignature(signature); } + function checkImportCallExpression(node: ImportCallExpression): Type { + // resolveExternalModuleName will return undefined if the moduleReferenceExpression is not a string literal + const moduleSymbol = resolveExternalModuleName(node, node.specifier); + if (moduleSymbol) { + } + return createPromiseReturnType(node, anyType); + } + function isCommonJsRequire(node: Node) { if (!isRequireCall(node, /*checkArgumentIsStringLiteral*/true)) { return false; @@ -15068,7 +15076,7 @@ namespace ts { return emptyObjectType; } - function createPromiseReturnType(func: FunctionLikeDeclaration, promisedType: Type) { + function createPromiseReturnType(func: FunctionLikeDeclaration | ImportCallExpression, promisedType: Type) { const promiseType = createPromiseType(promisedType); if (promiseType === emptyObjectType) { error(func, Diagnostics.An_async_function_or_method_must_return_a_Promise_Make_sure_you_have_a_declaration_for_Promise_or_include_ES2015_in_your_lib_option); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index c1a8f293418..5eb0bc60e68 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -168,7 +168,7 @@ namespace ts { visitNodes(cbNodes, (node).typeArguments) || visitNodes(cbNodes, (node).arguments); case SyntaxKind.ImportCallExpression: - return visitNode(cbNode, (node).expression); + return visitNode(cbNode, (node).specifier); case SyntaxKind.TaggedTemplateExpression: return visitNode(cbNode, (node).tag) || visitNode(cbNode, (node).template); @@ -3695,8 +3695,8 @@ namespace ts { // 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); + if (importCall.specifier.kind === SyntaxKind.StringLiteral) { + (sourceFile.imports || (sourceFile.imports = [])).push(importCall.specifier as StringLiteral); } return importCall; } @@ -3774,11 +3774,11 @@ namespace ts { return finishNode(node); } - function parseImportCall(): ImportCall { - const importCallExpr = createNode(SyntaxKind.ImportCallExpression); + function parseImportCall(): ImportCallExpression { + const importCallExpr = createNode(SyntaxKind.ImportCallExpression); parseExpected(SyntaxKind.ImportKeyword); parseExpected(SyntaxKind.OpenParenToken); - importCallExpr.expression = parseAssignmentExpressionOrHigher(); + importCallExpr.specifier = parseAssignmentExpressionOrHigher(); parseExpected(SyntaxKind.CloseParenToken); return finishNode(importCallExpr); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index d3ecdef2799..8a19e4d62a1 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1447,9 +1447,9 @@ namespace ts { expression: SuperExpression; } - export interface ImportCall extends LeftHandSideExpression, Declaration { + export interface ImportCallExpression extends LeftHandSideExpression, Declaration { kind: SyntaxKind.ImportCallExpression; - expression: Expression; + specifier: Expression; } export interface ExpressionWithTypeArguments extends TypeNode { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 98a95e9e861..db512cfffe7 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -662,7 +662,7 @@ namespace ts { return n.kind === SyntaxKind.CallExpression && (n).expression.kind === SyntaxKind.SuperKeyword; } - export function isImportCall(n: Node): n is ImportCall { + export function isImportCall(n: Node): n is ImportCallExpression { return n.kind === SyntaxKind.ImportCallExpression; }