Skip to content

Commit

Permalink
Do not consider import.defer in import.defer(...) as an expression
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jan 17, 2025
1 parent 455d843 commit 831d771
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 11 deletions.
13 changes: 11 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37607,7 +37607,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

if (node.keywordToken === SyntaxKind.ImportKeyword) {
if (node.name.escapedText === "defer") {
// 'checkGrammarMetaProperty' already reported the error for the standalone import.defer.
Debug.assert(!isCallExpression(node.parent) || node.parent.expression !== node, "Trying to get the type of `import.defer` in `import.defer(...)`");
return errorType;
} else {
return checkImportMetaProperty(node);
Expand Down Expand Up @@ -41023,7 +41023,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
// Optimize for the common case of a call to a function with a single non-generic call
// signature where we can just fetch the return type without checking the arguments.
if (isCallExpression(expr) && expr.expression.kind !== SyntaxKind.SuperKeyword && !isRequireCall(expr, /*requireStringLiteralLikeArgument*/ true) && !isSymbolOrSymbolForCall(expr)) {
if (isCallExpression(expr) && expr.expression.kind !== SyntaxKind.SuperKeyword && !isRequireCall(expr, /*requireStringLiteralLikeArgument*/ true) && !isSymbolOrSymbolForCall(expr) && !isImportCall(expr)) {
return isCallChain(expr) ? getReturnTypeOfSingleNonGenericSignatureOfCallChain(expr) :
getReturnTypeOfSingleNonGenericCallSignature(checkNonNullExpression(expr.expression));
}
Expand Down Expand Up @@ -49681,6 +49681,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return isExportAssignment(node.parent) ? Debug.checkDefined(node.parent.symbol) : undefined;

case SyntaxKind.ImportKeyword:
if (isMetaProperty(node.parent) && node.parent.name.escapedText === "defer") {
return undefined;
}
// falls through
case SyntaxKind.NewKeyword:
return isMetaProperty(node.parent) ? checkMetaPropertyKeyword(node.parent).symbol : undefined;
case SyntaxKind.InstanceOfKeyword:
Expand Down Expand Up @@ -49753,7 +49757,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

if (isExpressionNode(node)) {
try {
return getRegularTypeOfExpression(node as Expression);
} catch (e) {
console.error("Error while getting the type of", isExpressionNode(node), node.kind, (node as MetaProperty).keywordToken !== SyntaxKind.ImportKeyword, (node as MetaProperty).name?.escapedText)
throw e;
}
}

if (classType && !classDecl.isImplements) {
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3589,8 +3589,10 @@ export function isExpressionNode(node: Node): boolean {
case SyntaxKind.JsxFragment:
case SyntaxKind.YieldExpression:
case SyntaxKind.AwaitExpression:
case SyntaxKind.MetaProperty:
return true;
case SyntaxKind.MetaProperty:
// `import.defer` in `import.defer(...)` is not an expression
return !isImportCall(node.parent) || node.parent.expression !== node;
case SyntaxKind.ExpressionWithTypeArguments:
return !isHeritageClause(node.parent) && !isJSDocAugmentsTag(node.parent);
case SyntaxKind.QualifiedName:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import.defer("./a.js").then(ns => {
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^ ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>import.defer("./a.js") : Promise<typeof import("a")>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>import.defer : any
> : ^^^
>defer : any
> : ^^^
>"./a.js" : "./a.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import.defer("./a.js").then(ns => {
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^ ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>import.defer("./a.js") : Promise<typeof import("a")>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>import.defer : any
> : ^^^
>defer : any
> : ^^^
>"./a.js" : "./a.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import.defer("./a.js").then(ns => {
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^ ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>import.defer("./a.js") : Promise<typeof import("a")>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>import.defer : any
> : ^^^
>defer : any
> : ^^^
>"./a.js" : "./a.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import.defer("./a.js").then(ns => {
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^ ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>import.defer("./a.js") : Promise<typeof import("a")>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>import.defer : error
>defer : any
> : ^^^
>"./a.js" : "./a.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import.defer("./a.js").then(ns => {
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^ ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>import.defer("./a.js") : Promise<{ default: typeof import("a"); foo(): void; }>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>import.defer : error
>defer : any
> : ^^^
>"./a.js" : "./a.js"
Expand Down

0 comments on commit 831d771

Please sign in to comment.