-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for import defer
proposal
#60757
Open
nicolo-ribaudo
wants to merge
19
commits into
microsoft:main
Choose a base branch
from
nicolo-ribaudo:import-defer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
bd82b0b
Add support for `import defer` proposal
ryzokuken 4b3c02c
Update AST to unify `type` with other modifiers
nicolo-ribaudo b66fb03
Add test for comments around `defer`
nicolo-ribaudo bdf98f5
Remove unnecessary backwards compat check
nicolo-ribaudo e8d76ea
Add support for `import.defer(...)`
nicolo-ribaudo 68d3d25
Also support `module: NodeNext`
nicolo-ribaudo 066d762
`aren't` -> `are not`
nicolo-ribaudo daf55fe
ImportPhaseModifier -> ImportPhaseModifierSyntaxKind
nicolo-ribaudo ef4eecb
Move import clause checks to checker.ts
nicolo-ribaudo 5f4ca3b
Report `'(' expected` for import.defer
nicolo-ribaudo 84f7535
Make meta property error localizable
nicolo-ribaudo 33f70c0
Move `import.defer` checks to checker.ts
nicolo-ribaudo 2a8e8b6
Unify tests testing different values of `module`
nicolo-ribaudo 455d843
Move `errorType` for `import.defer` handling
nicolo-ribaudo 831d771
Do not consider `import.defer` in `import.defer(...)` as an expression
nicolo-ribaudo b764f3a
fmt
nicolo-ribaudo 8c85100
Sort tokens alphabetically
nicolo-ribaudo e787ee5
Remove unused baselines
nicolo-ribaudo 7b17c31
Properly resolve specifiers in `import.defer(...)`
nicolo-ribaudo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9852,7 +9852,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
factory.createImportDeclaration( | ||
/*modifiers*/ undefined, | ||
factory.createImportClause( | ||
/*isTypeOnly*/ false, | ||
/*phaseModifier*/ undefined, | ||
/*name*/ undefined, | ||
factory.createNamedImports([factory.createImportSpecifier( | ||
/*isTypeOnly*/ false, | ||
|
@@ -9945,7 +9945,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
addResult( | ||
factory.createImportDeclaration( | ||
/*modifiers*/ undefined, | ||
factory.createImportClause(isTypeOnly, factory.createIdentifier(localName), /*namedBindings*/ undefined), | ||
factory.createImportClause( | ||
/* phaseModifier */ isTypeOnly ? SyntaxKind.TypeKeyword : undefined, | ||
factory.createIdentifier(localName), | ||
/*namedBindings*/ undefined, | ||
), | ||
specifier, | ||
attributes, | ||
), | ||
|
@@ -9960,7 +9964,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
addResult( | ||
factory.createImportDeclaration( | ||
/*modifiers*/ undefined, | ||
factory.createImportClause(isTypeOnly, /*name*/ undefined, factory.createNamespaceImport(factory.createIdentifier(localName))), | ||
factory.createImportClause( | ||
/* phaseModifier */ isTypeOnly ? SyntaxKind.TypeKeyword : undefined, | ||
/*name*/ undefined, | ||
factory.createNamespaceImport(factory.createIdentifier(localName)), | ||
), | ||
specifier, | ||
(node as ImportClause).parent.attributes, | ||
), | ||
|
@@ -9987,7 +9995,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
factory.createImportDeclaration( | ||
/*modifiers*/ undefined, | ||
factory.createImportClause( | ||
isTypeOnly, | ||
/* phaseModifier */ isTypeOnly ? SyntaxKind.TypeKeyword : undefined, | ||
/*name*/ undefined, | ||
factory.createNamedImports([ | ||
factory.createImportSpecifier( | ||
|
@@ -37598,6 +37606,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
|
||
if (node.keywordToken === SyntaxKind.ImportKeyword) { | ||
if (node.name.escapedText === "defer") { | ||
Debug.assert(!isCallExpression(node.parent) || node.parent.expression !== node, "Trying to get the type of `import.defer` in `import.defer(...)`"); | ||
return errorType; | ||
} | ||
return checkImportMetaProperty(node); | ||
} | ||
|
||
|
@@ -41010,7 +41022,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)); | ||
} | ||
|
@@ -41164,8 +41176,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
case SyntaxKind.ElementAccessExpression: | ||
return checkIndexedAccess(node as ElementAccessExpression, checkMode); | ||
case SyntaxKind.CallExpression: | ||
if ((node as CallExpression).expression.kind === SyntaxKind.ImportKeyword) { | ||
return checkImportCallExpression(node as ImportCall); | ||
if (isImportCall(node)) { | ||
return checkImportCallExpression(node); | ||
} | ||
// falls through | ||
case SyntaxKind.NewExpression: | ||
|
@@ -49668,6 +49680,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: | ||
|
@@ -49740,7 +49756,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
|
||
if (isExpressionNode(node)) { | ||
return getRegularTypeOfExpression(node as Expression); | ||
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; | ||
Comment on lines
+49762
to
+49764
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this left over from local debugging? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whops 😅 |
||
} | ||
} | ||
|
||
if (classType && !classDecl.isImplements) { | ||
|
@@ -52605,7 +52627,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
break; | ||
case SyntaxKind.ImportKeyword: | ||
if (escapedText !== "meta") { | ||
return grammarErrorOnNode(node.name, Diagnostics._0_is_not_a_valid_meta_property_for_keyword_1_Did_you_mean_2, unescapeLeadingUnderscores(node.name.escapedText), tokenToString(node.keywordToken), "meta"); | ||
const isCallee = isCallExpression(node.parent) && node.parent.expression === node; | ||
if (escapedText === "defer") { | ||
if (!isCallee) { | ||
return grammarErrorAtPos(node, node.end, 0, Diagnostics._0_expected, "("); | ||
} | ||
} | ||
else { | ||
if (isCallee) { | ||
return grammarErrorOnNode(node.name, Diagnostics._0_is_not_a_valid_meta_property_for_keyword_import_Did_you_mean_meta_or_defer, unescapeLeadingUnderscores(node.name.escapedText)); | ||
} | ||
return grammarErrorOnNode(node.name, Diagnostics._0_is_not_a_valid_meta_property_for_keyword_1_Did_you_mean_2, unescapeLeadingUnderscores(node.name.escapedText), tokenToString(node.keywordToken), "meta"); | ||
} | ||
} | ||
break; | ||
} | ||
|
@@ -52864,11 +52897,24 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
|
||
function checkGrammarImportClause(node: ImportClause): boolean { | ||
if (node.isTypeOnly && node.name && node.namedBindings) { | ||
return grammarErrorOnNode(node, Diagnostics.A_type_only_import_can_specify_a_default_import_or_named_bindings_but_not_both); | ||
if (node.phaseModifier === SyntaxKind.TypeKeyword) { | ||
if (node.name && node.namedBindings) { | ||
return grammarErrorOnNode(node, Diagnostics.A_type_only_import_can_specify_a_default_import_or_named_bindings_but_not_both); | ||
} | ||
if (node.namedBindings?.kind === SyntaxKind.NamedImports) { | ||
return checkGrammarNamedImportsOrExports(node.namedBindings); | ||
} | ||
} | ||
if (node.isTypeOnly && node.namedBindings?.kind === SyntaxKind.NamedImports) { | ||
return checkGrammarNamedImportsOrExports(node.namedBindings); | ||
else if (node.phaseModifier === SyntaxKind.DeferKeyword) { | ||
if (node.name) { | ||
return grammarErrorOnNode(node, Diagnostics.Default_imports_are_not_allowed_in_a_deferred_import); | ||
} | ||
if (node.namedBindings?.kind === SyntaxKind.NamedImports) { | ||
return grammarErrorOnNode(node, Diagnostics.Named_imports_are_not_allowed_in_a_deferred_import); | ||
} | ||
if (moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.NodeNext) { | ||
return grammarErrorOnNode(node, Diagnostics.Deferred_imports_are_only_supported_when_the_module_flag_is_set_to_esnext_or_nodenext); | ||
} | ||
} | ||
return false; | ||
} | ||
|
@@ -52891,7 +52937,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
return grammarErrorOnNode(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled); | ||
} | ||
|
||
if (moduleKind === ModuleKind.ES2015) { | ||
if (node.expression.kind === SyntaxKind.MetaProperty) { | ||
if (moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.NodeNext) { | ||
return grammarErrorOnNode(node, Diagnostics.Deferred_imports_are_only_supported_when_the_module_flag_is_set_to_esnext_or_nodenext); | ||
} | ||
} | ||
else if (moduleKind === ModuleKind.ES2015) { | ||
return grammarErrorOnNode(node, Diagnostics.Dynamic_imports_are_only_supported_when_the_module_flag_is_set_to_es2020_es2022_esnext_commonjs_amd_system_umd_node16_node18_or_nodenext); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assertion seems like it could result in an exception in an editor as you type
import.defer
. I'd rather we not crash here.