mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Add support for import.meta in System modules (#32797)
This commit is contained in:
1 parent
2a2866c3a9
commit
bf054ae796
31 files changed
+1883
-75
No files matched your search
@@ -23161,8 +23161,8 @@ namespace ts {
|
||||
}
|
||||
|
||||
function checkImportMetaProperty(node: MetaProperty) {
|
||||
if (languageVersion < ScriptTarget.ESNext || moduleKind < ModuleKind.ESNext) {
|
||||
error(node, Diagnostics.The_import_meta_meta_property_is_only_allowed_using_ESNext_for_the_target_and_module_compiler_options);
|
||||
if (moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.System) {
|
||||
error(node, Diagnostics.The_import_meta_meta_property_is_only_allowed_when_the_module_option_is_esnext_or_system);
|
||||
}
|
||||
const file = getSourceFileOfNode(node);
|
||||
Debug.assert(!!(file.flags & NodeFlags.PossiblyContainsImportMeta), "Containing file is missing import meta node flag.");
|
||||
|
||||
@@ -979,7 +979,7 @@
|
||||
"category": "Error",
|
||||
"code": 1342
|
||||
},
|
||||
"The 'import.meta' meta-property is only allowed using 'ESNext' for the 'target' and 'module' compiler options.": {
|
||||
"The 'import.meta' meta-property is only allowed when the '--module' option is 'esnext' or 'system'.": {
|
||||
"category": "Error",
|
||||
"code": 1343
|
||||
},
|
||||
|
||||
@@ -24,12 +24,14 @@ namespace ts {
|
||||
context.enableSubstitution(SyntaxKind.BinaryExpression); // Substitutes assignments to exported symbols.
|
||||
context.enableSubstitution(SyntaxKind.PrefixUnaryExpression); // Substitutes updates to exported symbols.
|
||||
context.enableSubstitution(SyntaxKind.PostfixUnaryExpression); // Substitutes updates to exported symbols.
|
||||
context.enableSubstitution(SyntaxKind.MetaProperty); // Substitutes 'import.meta'
|
||||
context.enableEmitNotification(SyntaxKind.SourceFile); // Restore state when substituting nodes in a file.
|
||||
|
||||
const moduleInfoMap: ExternalModuleInfo[] = []; // The ExternalModuleInfo for each file.
|
||||
const deferredExports: (Statement[] | undefined)[] = []; // Exports to defer until an EndOfDeclarationMarker is found.
|
||||
const exportFunctionsMap: Identifier[] = []; // The export function associated with a source file.
|
||||
const noSubstitutionMap: boolean[][] = []; // Set of nodes for which substitution rules should be ignored for each file.
|
||||
const contextObjectMap: Identifier[] = []; // The context object associated with a source file.
|
||||
|
||||
let currentSourceFile: SourceFile; // The current file.
|
||||
let moduleInfo: ExternalModuleInfo; // ExternalModuleInfo for the current file.
|
||||
@@ -75,7 +77,7 @@ namespace ts {
|
||||
// existing identifiers.
|
||||
exportFunction = createUniqueName("exports");
|
||||
exportFunctionsMap[id] = exportFunction;
|
||||
contextObject = createUniqueName("context");
|
||||
contextObject = contextObjectMap[id] = createUniqueName("context");
|
||||
|
||||
// Add the body of the module.
|
||||
const dependencyGroups = collectDependencyGroups(moduleInfo.externalImports);
|
||||
@@ -1586,6 +1588,7 @@ namespace ts {
|
||||
moduleInfo = moduleInfoMap[id];
|
||||
exportFunction = exportFunctionsMap[id];
|
||||
noSubstitution = noSubstitutionMap[id];
|
||||
contextObject = contextObjectMap[id];
|
||||
|
||||
if (noSubstitution) {
|
||||
delete noSubstitutionMap[id];
|
||||
@@ -1596,6 +1599,7 @@ namespace ts {
|
||||
currentSourceFile = undefined!;
|
||||
moduleInfo = undefined!;
|
||||
exportFunction = undefined!;
|
||||
contextObject = undefined!;
|
||||
noSubstitution = undefined;
|
||||
}
|
||||
else {
|
||||
@@ -1641,6 +1645,7 @@ namespace ts {
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Substitution for a ShorthandPropertyAssignment whose name that may contain an imported or exported symbol.
|
||||
*
|
||||
@@ -1694,6 +1699,8 @@ namespace ts {
|
||||
case SyntaxKind.PrefixUnaryExpression:
|
||||
case SyntaxKind.PostfixUnaryExpression:
|
||||
return substituteUnaryExpression(<PrefixUnaryExpression | PostfixUnaryExpression>node);
|
||||
case SyntaxKind.MetaProperty:
|
||||
return substituteMetaProperty(<MetaProperty>node);
|
||||
}
|
||||
|
||||
return node;
|
||||
@@ -1830,6 +1837,13 @@ namespace ts {
|
||||
return node;
|
||||
}
|
||||
|
||||
function substituteMetaProperty(node: MetaProperty) {
|
||||
if (isImportMeta(node)) {
|
||||
return createPropertyAccess(contextObject, createIdentifier("meta"));
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the exports of a name.
|
||||
*
|
||||
|
||||
@@ -1864,6 +1864,12 @@ namespace ts {
|
||||
name: Identifier;
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
export interface ImportMetaProperty extends MetaProperty {
|
||||
keywordToken: SyntaxKind.ImportKeyword;
|
||||
name: Identifier & { escapedText: __String & "meta" };
|
||||
}
|
||||
|
||||
/// A JSX expression of the form <TagName attrs>...</TagName>
|
||||
export interface JsxElement extends PrimaryExpression {
|
||||
kind: SyntaxKind.JsxElement;
|
||||
|
||||
@@ -987,6 +987,12 @@ namespace ts {
|
||||
return n.kind === SyntaxKind.CallExpression && (<CallExpression>n).expression.kind === SyntaxKind.ImportKeyword;
|
||||
}
|
||||
|
||||
export function isImportMeta(n: Node): n is ImportMetaProperty {
|
||||
return isMetaProperty(n)
|
||||
&& n.keywordToken === SyntaxKind.ImportKeyword
|
||||
&& n.name.escapedText === "meta";
|
||||
}
|
||||
|
||||
export function isLiteralImportTypeNode(n: Node): n is LiteralImportTypeNode {
|
||||
return isImportTypeNode(n) && isLiteralTypeNode(n.argument) && isStringLiteral(n.argument.literal);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user