mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
feat(53656): Add support for the updated import attributes proposal (#54242)
This commit is contained in:
+76
-76
@@ -340,7 +340,7 @@ import {
|
||||
getPropertyNameForPropertyNameNode,
|
||||
getPropertyNameFromType,
|
||||
getResolutionDiagnostic,
|
||||
getResolutionModeOverrideForClause,
|
||||
getResolutionModeOverride,
|
||||
getResolvedExternalModuleName,
|
||||
getResolveJsonModule,
|
||||
getRestParameterElementType,
|
||||
@@ -393,6 +393,7 @@ import {
|
||||
hasOverrideModifier,
|
||||
hasPossibleExternalModuleReference,
|
||||
hasQuestionToken,
|
||||
hasResolutionModeOverride,
|
||||
hasRestParameter,
|
||||
hasScopeMarker,
|
||||
hasStaticModifier,
|
||||
@@ -405,6 +406,7 @@ import {
|
||||
IdentifierTypePredicate,
|
||||
idText,
|
||||
IfStatement,
|
||||
ImportAttributes,
|
||||
ImportCall,
|
||||
ImportClause,
|
||||
ImportDeclaration,
|
||||
@@ -412,7 +414,6 @@ import {
|
||||
ImportOrExportSpecifier,
|
||||
ImportsNotUsedAsValues,
|
||||
ImportSpecifier,
|
||||
ImportTypeAssertionContainer,
|
||||
ImportTypeNode,
|
||||
IndexedAccessType,
|
||||
IndexedAccessTypeNode,
|
||||
@@ -643,7 +644,6 @@ import {
|
||||
isNamespaceExportDeclaration,
|
||||
isNamespaceReexportDeclaration,
|
||||
isNewExpression,
|
||||
isNightly,
|
||||
isNodeDescendantOf,
|
||||
isNonNullAccess,
|
||||
isNullishCoalesce,
|
||||
@@ -5016,11 +5016,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
}
|
||||
if (moduleResolutionKind === ModuleResolutionKind.Node16 || moduleResolutionKind === ModuleResolutionKind.NodeNext) {
|
||||
const isSyncImport = (currentSourceFile.impliedNodeFormat === ModuleKind.CommonJS && !findAncestor(location, isImportCall)) || !!findAncestor(location, isImportEqualsDeclaration);
|
||||
const overrideClauseHost = findAncestor(location, l => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l)) as ImportTypeNode | ImportDeclaration | ExportDeclaration | undefined;
|
||||
const overrideClause = overrideClauseHost && isImportTypeNode(overrideClauseHost) ? overrideClauseHost.assertions?.assertClause : overrideClauseHost?.assertClause;
|
||||
const overrideHost = findAncestor(location, l => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l)) as ImportTypeNode | ImportDeclaration | ExportDeclaration | undefined;
|
||||
// An override clause will take effect for type-only imports and import types, and allows importing the types across formats, regardless of
|
||||
// normal mode restrictions
|
||||
if (isSyncImport && sourceFile.impliedNodeFormat === ModuleKind.ESNext && !getResolutionModeOverrideForClause(overrideClause)) {
|
||||
if (isSyncImport && sourceFile.impliedNodeFormat === ModuleKind.ESNext && !hasResolutionModeOverride(overrideHost)) {
|
||||
if (findAncestor(location, isImportEqualsDeclaration)) {
|
||||
// ImportEquals in a ESM file resolving to another ESM file
|
||||
error(errorNode, Diagnostics.Module_0_cannot_be_imported_using_this_construct_The_specifier_only_resolves_to_an_ES_module_which_cannot_be_imported_with_require_Use_an_ECMAScript_import_instead, moduleReference);
|
||||
@@ -7147,7 +7146,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
return factory.updateImportTypeNode(
|
||||
root,
|
||||
root.argument,
|
||||
root.assertions,
|
||||
root.attributes,
|
||||
qualifier,
|
||||
typeArguments,
|
||||
root.isTypeOf,
|
||||
@@ -7935,18 +7934,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
const contextFile = getSourceFileOfNode(getOriginalNode(context.enclosingDeclaration));
|
||||
const targetFile = getSourceFileOfModule(chain[0]);
|
||||
let specifier: string | undefined;
|
||||
let assertion: ImportTypeAssertionContainer | undefined;
|
||||
let attributes: ImportAttributes | undefined;
|
||||
if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Node16 || getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeNext) {
|
||||
// An `import` type directed at an esm format file is only going to resolve in esm mode - set the esm mode assertion
|
||||
if (targetFile?.impliedNodeFormat === ModuleKind.ESNext && targetFile.impliedNodeFormat !== contextFile?.impliedNodeFormat) {
|
||||
specifier = getSpecifierForModuleSymbol(chain[0], context, ModuleKind.ESNext);
|
||||
assertion = factory.createImportTypeAssertionContainer(factory.createAssertClause(factory.createNodeArray([
|
||||
factory.createAssertEntry(
|
||||
factory.createStringLiteral("resolution-mode"),
|
||||
factory.createStringLiteral("import"),
|
||||
),
|
||||
])));
|
||||
context.tracker.reportImportTypeNodeResolutionModeOverride?.();
|
||||
attributes = factory.createImportAttributes(
|
||||
factory.createNodeArray([
|
||||
factory.createImportAttribute(
|
||||
factory.createStringLiteral("resolution-mode"),
|
||||
factory.createStringLiteral("import"),
|
||||
),
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
||||
if (!specifier) {
|
||||
@@ -7964,17 +7964,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
specifier = oldSpecifier;
|
||||
}
|
||||
else {
|
||||
assertion = factory.createImportTypeAssertionContainer(factory.createAssertClause(factory.createNodeArray([
|
||||
factory.createAssertEntry(
|
||||
factory.createStringLiteral("resolution-mode"),
|
||||
factory.createStringLiteral(swappedMode === ModuleKind.ESNext ? "import" : "require"),
|
||||
),
|
||||
])));
|
||||
context.tracker.reportImportTypeNodeResolutionModeOverride?.();
|
||||
attributes = factory.createImportAttributes(
|
||||
factory.createNodeArray([
|
||||
factory.createImportAttribute(
|
||||
factory.createStringLiteral("resolution-mode"),
|
||||
factory.createStringLiteral(swappedMode === ModuleKind.ESNext ? "import" : "require"),
|
||||
),
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!assertion) {
|
||||
if (!attributes) {
|
||||
// If ultimately we can only name the symbol with a reference that dives into a `node_modules` folder, we should error
|
||||
// since declaration files with these kinds of references are liable to fail when published :(
|
||||
context.encounteredError = true;
|
||||
@@ -7991,12 +7992,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
const lastId = isIdentifier(nonRootParts) ? nonRootParts : nonRootParts.right;
|
||||
setIdentifierTypeArguments(lastId, /*typeArguments*/ undefined);
|
||||
}
|
||||
return factory.createImportTypeNode(lit, assertion, nonRootParts as EntityName, typeParameterNodes as readonly TypeNode[], isTypeOf);
|
||||
return factory.createImportTypeNode(lit, attributes, nonRootParts as EntityName, typeParameterNodes as readonly TypeNode[], isTypeOf);
|
||||
}
|
||||
else {
|
||||
const splitNode = getTopmostIndexedAccessType(nonRootParts);
|
||||
const qualifier = (splitNode.objectType as TypeReferenceNode).typeName;
|
||||
return factory.createIndexedAccessTypeNode(factory.createImportTypeNode(lit, assertion, qualifier, typeParameterNodes as readonly TypeNode[], isTypeOf), splitNode.indexType);
|
||||
return factory.createIndexedAccessTypeNode(factory.createImportTypeNode(lit, attributes, qualifier, typeParameterNodes as readonly TypeNode[], isTypeOf), splitNode.indexType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8504,7 +8505,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
return factory.updateImportTypeNode(
|
||||
node,
|
||||
factory.updateLiteralTypeNode(node.argument, rewriteModuleSpecifier(node, node.argument.literal)),
|
||||
node.assertions,
|
||||
node.attributes,
|
||||
node.qualifier,
|
||||
visitNodes(node.typeArguments, visitExistingNodeTreeSymbols, isTypeNode),
|
||||
node.isTypeOf,
|
||||
@@ -8729,7 +8730,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
|
||||
function inlineExportModifiers(statements: Statement[]) {
|
||||
// Pass 3: Move all `export {}`'s to `export` modifiers where possible
|
||||
const index = findIndex(statements, d => isExportDeclaration(d) && !d.moduleSpecifier && !d.assertClause && !!d.exportClause && isNamedExports(d.exportClause));
|
||||
const index = findIndex(statements, d => isExportDeclaration(d) && !d.moduleSpecifier && !d.attributes && !!d.exportClause && isNamedExports(d.exportClause));
|
||||
if (index >= 0) {
|
||||
const exportDecl = statements[index] as ExportDeclaration & { readonly exportClause: NamedExports; };
|
||||
const replacements = mapDefined(exportDecl.exportClause.elements, e => {
|
||||
@@ -8761,7 +8762,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
replacements,
|
||||
),
|
||||
exportDecl.moduleSpecifier,
|
||||
exportDecl.assertClause,
|
||||
exportDecl.attributes,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -9483,7 +9484,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
)]),
|
||||
),
|
||||
factory.createStringLiteral(specifier),
|
||||
/*assertClause*/ undefined,
|
||||
/*attributes*/ undefined,
|
||||
),
|
||||
ModifierFlags.None,
|
||||
);
|
||||
@@ -9567,7 +9568,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
/*modifiers*/ undefined,
|
||||
factory.createImportClause(/*isTypeOnly*/ false, factory.createIdentifier(localName), /*namedBindings*/ undefined),
|
||||
specifier,
|
||||
(node as ImportClause).parent.assertClause,
|
||||
(node as ImportClause).parent.attributes,
|
||||
),
|
||||
ModifierFlags.None,
|
||||
);
|
||||
@@ -9581,7 +9582,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
/*modifiers*/ undefined,
|
||||
factory.createImportClause(/*isTypeOnly*/ false, /*name*/ undefined, factory.createNamespaceImport(factory.createIdentifier(localName))),
|
||||
specifier,
|
||||
(node as NamespaceImport).parent.parent.assertClause,
|
||||
(node as ImportClause).parent.attributes,
|
||||
),
|
||||
ModifierFlags.None,
|
||||
);
|
||||
@@ -9616,7 +9617,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
]),
|
||||
),
|
||||
specifier,
|
||||
(node as ImportSpecifier).parent.parent.parent.assertClause,
|
||||
(node as ImportSpecifier).parent.parent.parent.attributes,
|
||||
),
|
||||
ModifierFlags.None,
|
||||
);
|
||||
@@ -39764,18 +39765,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
function checkImportType(node: ImportTypeNode) {
|
||||
checkSourceElement(node.argument);
|
||||
|
||||
if (node.assertions) {
|
||||
const override = getResolutionModeOverrideForClause(node.assertions.assertClause, grammarErrorOnNode);
|
||||
if (override) {
|
||||
if (!isNightly()) {
|
||||
grammarErrorOnNode(node.assertions.assertClause, Diagnostics.resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next);
|
||||
}
|
||||
if (getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Node16 && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.NodeNext) {
|
||||
grammarErrorOnNode(node.assertions.assertClause, Diagnostics.resolution_mode_assertions_are_only_supported_when_moduleResolution_is_node16_or_nodenext);
|
||||
}
|
||||
if (node.attributes) {
|
||||
const override = getResolutionModeOverride(node.attributes, grammarErrorOnNode);
|
||||
const errorNode = node.attributes;
|
||||
if (override && errorNode && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Node16 && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.NodeNext) {
|
||||
grammarErrorOnNode(
|
||||
errorNode,
|
||||
node.attributes.token === SyntaxKind.WithKeyword
|
||||
? Diagnostics.The_resolution_mode_attribute_is_only_supported_when_moduleResolution_is_node16_or_nodenext
|
||||
: Diagnostics.resolution_mode_assertions_are_only_supported_when_moduleResolution_is_node16_or_nodenext,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
checkTypeReferenceOrImport(node);
|
||||
}
|
||||
|
||||
@@ -45014,12 +45015,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!isImportEqualsDeclaration(node) && node.assertClause) {
|
||||
if (!isImportEqualsDeclaration(node) && node.attributes) {
|
||||
const diagnostic = node.attributes.token === SyntaxKind.WithKeyword ? Diagnostics.Import_attribute_values_must_be_string_literal_expressions : Diagnostics.Import_assertion_values_must_be_string_literal_expressions;
|
||||
let hasError = false;
|
||||
for (const clause of node.assertClause.elements) {
|
||||
if (!isStringLiteral(clause.value)) {
|
||||
for (const attr of node.attributes.elements) {
|
||||
if (!isStringLiteral(attr.value)) {
|
||||
hasError = true;
|
||||
error(clause.value, Diagnostics.Import_assertion_values_must_be_string_literal_expressions);
|
||||
error(attr.value, diagnostic);
|
||||
}
|
||||
}
|
||||
return !hasError;
|
||||
@@ -45203,37 +45205,42 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
}
|
||||
}
|
||||
|
||||
function checkAssertClause(declaration: ImportDeclaration | ExportDeclaration) {
|
||||
if (declaration.assertClause) {
|
||||
const validForTypeAssertions = isExclusivelyTypeOnlyImportOrExport(declaration);
|
||||
const override = getResolutionModeOverrideForClause(declaration.assertClause, validForTypeAssertions ? grammarErrorOnNode : undefined);
|
||||
if (validForTypeAssertions && override) {
|
||||
if (!isNightly()) {
|
||||
grammarErrorOnNode(declaration.assertClause, Diagnostics.resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next);
|
||||
}
|
||||
|
||||
function checkImportAttributes(declaration: ImportDeclaration | ExportDeclaration) {
|
||||
const node = declaration.attributes;
|
||||
if (node) {
|
||||
const validForTypeAttributes = isExclusivelyTypeOnlyImportOrExport(declaration);
|
||||
const override = getResolutionModeOverride(node, validForTypeAttributes ? grammarErrorOnNode : undefined);
|
||||
const isImportAttributes = declaration.attributes.token === SyntaxKind.WithKeyword;
|
||||
if (validForTypeAttributes && override) {
|
||||
if (getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Node16 && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.NodeNext) {
|
||||
return grammarErrorOnNode(declaration.assertClause, Diagnostics.resolution_mode_assertions_are_only_supported_when_moduleResolution_is_node16_or_nodenext);
|
||||
return grammarErrorOnNode(
|
||||
node,
|
||||
isImportAttributes
|
||||
? Diagnostics.The_resolution_mode_attribute_is_only_supported_when_moduleResolution_is_node16_or_nodenext
|
||||
: Diagnostics.resolution_mode_assertions_are_only_supported_when_moduleResolution_is_node16_or_nodenext,
|
||||
);
|
||||
}
|
||||
return; // Other grammar checks do not apply to type-only imports with resolution mode assertions
|
||||
}
|
||||
|
||||
const mode = (moduleKind === ModuleKind.NodeNext) && declaration.moduleSpecifier && getUsageModeForExpression(declaration.moduleSpecifier);
|
||||
if (mode !== ModuleKind.ESNext && moduleKind !== ModuleKind.ESNext) {
|
||||
return grammarErrorOnNode(
|
||||
declaration.assertClause,
|
||||
moduleKind === ModuleKind.NodeNext
|
||||
? Diagnostics.Import_assertions_are_not_allowed_on_statements_that_transpile_to_CommonJS_require_calls
|
||||
: Diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext_or_nodenext,
|
||||
);
|
||||
const message = isImportAttributes
|
||||
? moduleKind === ModuleKind.NodeNext
|
||||
? Diagnostics.Import_attributes_are_not_allowed_on_statements_that_transpile_to_CommonJS_require_calls
|
||||
: Diagnostics.Import_attributes_are_only_supported_when_the_module_option_is_set_to_esnext_or_nodenext
|
||||
: moduleKind === ModuleKind.NodeNext
|
||||
? Diagnostics.Import_assertions_are_not_allowed_on_statements_that_transpile_to_CommonJS_require_calls
|
||||
: Diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext_or_nodenext;
|
||||
return grammarErrorOnNode(node, message);
|
||||
}
|
||||
|
||||
if (isImportDeclaration(declaration) ? declaration.importClause?.isTypeOnly : declaration.isTypeOnly) {
|
||||
return grammarErrorOnNode(declaration.assertClause, Diagnostics.Import_assertions_cannot_be_used_with_type_only_imports_or_exports);
|
||||
return grammarErrorOnNode(node, isImportAttributes ? Diagnostics.Import_attributes_cannot_be_used_with_type_only_imports_or_exports : Diagnostics.Import_assertions_cannot_be_used_with_type_only_imports_or_exports);
|
||||
}
|
||||
|
||||
if (override) {
|
||||
return grammarErrorOnNode(declaration.assertClause, Diagnostics.resolution_mode_can_only_be_set_for_type_only_imports);
|
||||
return grammarErrorOnNode(node, Diagnostics.resolution_mode_can_only_be_set_for_type_only_imports);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45269,7 +45276,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
}
|
||||
}
|
||||
}
|
||||
checkAssertClause(node);
|
||||
checkImportAttributes(node);
|
||||
}
|
||||
|
||||
function checkImportEqualsDeclaration(node: ImportEqualsDeclaration) {
|
||||
@@ -45365,7 +45372,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
}
|
||||
}
|
||||
}
|
||||
checkAssertClause(node);
|
||||
checkImportAttributes(node);
|
||||
}
|
||||
|
||||
function checkGrammarExportDeclaration(node: ExportDeclaration): boolean {
|
||||
@@ -49907,13 +49914,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
checkGrammarForDisallowedTrailingComma(nodeArguments);
|
||||
|
||||
if (nodeArguments.length > 1) {
|
||||
const assertionArgument = nodeArguments[1];
|
||||
return grammarErrorOnNode(assertionArgument, Diagnostics.Dynamic_imports_only_support_a_second_argument_when_the_module_option_is_set_to_esnext_node16_or_nodenext);
|
||||
const importAttributesArgument = nodeArguments[1];
|
||||
return grammarErrorOnNode(importAttributesArgument, Diagnostics.Dynamic_imports_only_support_a_second_argument_when_the_module_option_is_set_to_esnext_node16_or_nodenext);
|
||||
}
|
||||
}
|
||||
|
||||
if (nodeArguments.length === 0 || nodeArguments.length > 2) {
|
||||
return grammarErrorOnNode(node, Diagnostics.Dynamic_imports_can_only_accept_a_module_specifier_and_an_optional_assertion_as_arguments);
|
||||
return grammarErrorOnNode(node, Diagnostics.Dynamic_imports_can_only_accept_a_module_specifier_and_an_optional_set_of_attributes_as_arguments);
|
||||
}
|
||||
|
||||
// see: parseArgumentOrArrayLiteralElement...we use this function which parse arguments of callExpression to parse specifier for dynamic import.
|
||||
@@ -50244,13 +50251,6 @@ class SymbolTrackerImpl implements SymbolTracker {
|
||||
}
|
||||
}
|
||||
|
||||
reportImportTypeNodeResolutionModeOverride(): void {
|
||||
if (this.inner?.reportImportTypeNodeResolutionModeOverride) {
|
||||
this.onDiagnosticReported();
|
||||
this.inner.reportImportTypeNodeResolutionModeOverride();
|
||||
}
|
||||
}
|
||||
|
||||
private onDiagnosticReported() {
|
||||
this.context.reportedDiagnostic = true;
|
||||
}
|
||||
|
||||
@@ -1472,7 +1472,7 @@
|
||||
"category": "Message",
|
||||
"code": 1449
|
||||
},
|
||||
"Dynamic imports can only accept a module specifier and an optional assertion as arguments": {
|
||||
"Dynamic imports can only accept a module specifier and an optional set of attributes as arguments": {
|
||||
"category": "Message",
|
||||
"code": 1450
|
||||
},
|
||||
@@ -1520,6 +1520,18 @@
|
||||
"category": "Message",
|
||||
"code": 1461
|
||||
},
|
||||
"The 'resolution-mode' attribute is only supported when 'moduleResolution' is 'node16' or 'nodenext'.": {
|
||||
"category": "Error",
|
||||
"code": 1462
|
||||
},
|
||||
"'resolution-mode' is the only valid key for type import attributes.": {
|
||||
"category": "Error",
|
||||
"code": 1463
|
||||
},
|
||||
"Type import attributes should have exactly one key - 'resolution-mode' - with value 'import' or 'require'.": {
|
||||
"category": "Error",
|
||||
"code": 1464
|
||||
},
|
||||
|
||||
"The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.": {
|
||||
"category": "Error",
|
||||
@@ -1625,6 +1637,10 @@
|
||||
"category": "Error",
|
||||
"code": 1495
|
||||
},
|
||||
"Identifier, string literal, or number literal expected.": {
|
||||
"category": "Error",
|
||||
"code": 1496
|
||||
},
|
||||
|
||||
"The types of '{0}' are incompatible between these types.": {
|
||||
"category": "Error",
|
||||
@@ -3579,6 +3595,10 @@
|
||||
"category": "Error",
|
||||
"code": 2822
|
||||
},
|
||||
"Import attributes are only supported when the '--module' option is set to 'esnext' or 'nodenext'.": {
|
||||
"category": "Error",
|
||||
"code": 2823
|
||||
},
|
||||
"Cannot find namespace '{0}'. Did you mean '{1}'?": {
|
||||
"category": "Error",
|
||||
"code": 2833
|
||||
@@ -3611,10 +3631,6 @@
|
||||
"category": "Error",
|
||||
"code": 2840
|
||||
},
|
||||
"The type of this expression cannot be named without a 'resolution-mode' assertion, which is an unstable feature. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.": {
|
||||
"category": "Error",
|
||||
"code": 2841
|
||||
},
|
||||
"'{0}' is an unused renaming of '{1}'. Did you intend to use it as a type annotation?": {
|
||||
"category": "Error",
|
||||
"code": 2842
|
||||
@@ -3667,6 +3683,18 @@
|
||||
"category": "Error",
|
||||
"code": 2855
|
||||
},
|
||||
"Import attributes are not allowed on statements that transpile to CommonJS 'require' calls.": {
|
||||
"category": "Error",
|
||||
"code": 2856
|
||||
},
|
||||
"Import attributes cannot be used with type-only imports or exports.": {
|
||||
"category": "Error",
|
||||
"code": 2857
|
||||
},
|
||||
"Import attribute values must be string literal expressions.": {
|
||||
"category": "Error",
|
||||
"code": 2858
|
||||
},
|
||||
|
||||
"Import declaration '{0}' is using private name '{1}'.": {
|
||||
"category": "Error",
|
||||
@@ -4100,10 +4128,6 @@
|
||||
"category": "Error",
|
||||
"code": 4124
|
||||
},
|
||||
"'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.": {
|
||||
"category": "Error",
|
||||
"code": 4125
|
||||
},
|
||||
|
||||
"The current host does not support the '{0}' option.": {
|
||||
"category": "Error",
|
||||
|
||||
+18
-18
@@ -7,8 +7,6 @@ import {
|
||||
ArrayTypeNode,
|
||||
ArrowFunction,
|
||||
AsExpression,
|
||||
AssertClause,
|
||||
AssertEntry,
|
||||
AwaitExpression,
|
||||
base64encode,
|
||||
BigIntLiteral,
|
||||
@@ -191,6 +189,8 @@ import {
|
||||
Identifier,
|
||||
idText,
|
||||
IfStatement,
|
||||
ImportAttribute,
|
||||
ImportAttributes,
|
||||
ImportClause,
|
||||
ImportDeclaration,
|
||||
ImportEqualsDeclaration,
|
||||
@@ -2055,10 +2055,10 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
|
||||
return emitNamedExports(node as NamedExports);
|
||||
case SyntaxKind.ExportSpecifier:
|
||||
return emitExportSpecifier(node as ExportSpecifier);
|
||||
case SyntaxKind.AssertClause:
|
||||
return emitAssertClause(node as AssertClause);
|
||||
case SyntaxKind.AssertEntry:
|
||||
return emitAssertEntry(node as AssertEntry);
|
||||
case SyntaxKind.ImportAttributes:
|
||||
return emitImportAttributes(node as ImportAttributes);
|
||||
case SyntaxKind.ImportAttribute:
|
||||
return emitImportAttribute(node as ImportAttribute);
|
||||
case SyntaxKind.MissingDeclaration:
|
||||
return;
|
||||
|
||||
@@ -2948,16 +2948,16 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
|
||||
writeKeyword("import");
|
||||
writePunctuation("(");
|
||||
emit(node.argument);
|
||||
if (node.assertions) {
|
||||
if (node.attributes) {
|
||||
writePunctuation(",");
|
||||
writeSpace();
|
||||
writePunctuation("{");
|
||||
writeSpace();
|
||||
writeKeyword("assert");
|
||||
writeKeyword(node.attributes.token === SyntaxKind.AssertKeyword ? "assert" : "with");
|
||||
writePunctuation(":");
|
||||
writeSpace();
|
||||
const elements = node.assertions.assertClause.elements;
|
||||
emitList(node.assertions.assertClause, elements, ListFormat.ImportClauseEntries);
|
||||
const elements = node.attributes.elements;
|
||||
emitList(node.attributes, elements, ListFormat.ImportAttributes);
|
||||
writeSpace();
|
||||
writePunctuation("}");
|
||||
}
|
||||
@@ -4003,8 +4003,8 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
|
||||
writeSpace();
|
||||
}
|
||||
emitExpression(node.moduleSpecifier);
|
||||
if (node.assertClause) {
|
||||
emitWithLeadingSpace(node.assertClause);
|
||||
if (node.attributes) {
|
||||
emitWithLeadingSpace(node.attributes);
|
||||
}
|
||||
writeTrailingSemicolon();
|
||||
}
|
||||
@@ -4078,20 +4078,20 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
|
||||
writeSpace();
|
||||
emitExpression(node.moduleSpecifier);
|
||||
}
|
||||
if (node.assertClause) {
|
||||
emitWithLeadingSpace(node.assertClause);
|
||||
if (node.attributes) {
|
||||
emitWithLeadingSpace(node.attributes);
|
||||
}
|
||||
writeTrailingSemicolon();
|
||||
}
|
||||
|
||||
function emitAssertClause(node: AssertClause) {
|
||||
emitTokenWithComment(SyntaxKind.AssertKeyword, node.pos, writeKeyword, node);
|
||||
function emitImportAttributes(node: ImportAttributes) {
|
||||
emitTokenWithComment(node.token, node.pos, writeKeyword, node);
|
||||
writeSpace();
|
||||
const elements = node.elements;
|
||||
emitList(node, elements, ListFormat.ImportClauseEntries);
|
||||
emitList(node, elements, ListFormat.ImportAttributes);
|
||||
}
|
||||
|
||||
function emitAssertEntry(node: AssertEntry) {
|
||||
function emitImportAttribute(node: ImportAttribute) {
|
||||
emit(node.name);
|
||||
writePunctuation(":");
|
||||
writeSpace();
|
||||
|
||||
@@ -137,6 +137,9 @@ import {
|
||||
IfStatement,
|
||||
ImmediatelyInvokedArrowFunction,
|
||||
ImmediatelyInvokedFunctionExpression,
|
||||
ImportAttribute,
|
||||
ImportAttributeName,
|
||||
ImportAttributes,
|
||||
ImportClause,
|
||||
ImportDeclaration,
|
||||
ImportEqualsDeclaration,
|
||||
@@ -791,6 +794,10 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
|
||||
updateAssertEntry,
|
||||
createImportTypeAssertionContainer,
|
||||
updateImportTypeAssertionContainer,
|
||||
createImportAttributes,
|
||||
updateImportAttributes,
|
||||
createImportAttribute,
|
||||
updateImportAttribute,
|
||||
createNamespaceImport,
|
||||
updateNamespaceImport,
|
||||
createNamespaceExport,
|
||||
@@ -2643,14 +2650,17 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
|
||||
// @api
|
||||
function createImportTypeNode(
|
||||
argument: TypeNode,
|
||||
assertions?: ImportTypeAssertionContainer,
|
||||
attributes?: ImportAttributes,
|
||||
qualifier?: EntityName,
|
||||
typeArguments?: readonly TypeNode[],
|
||||
isTypeOf = false,
|
||||
): ImportTypeNode {
|
||||
const node = createBaseNode<ImportTypeNode>(SyntaxKind.ImportType);
|
||||
node.argument = argument;
|
||||
node.assertions = assertions;
|
||||
node.attributes = attributes;
|
||||
if (node.assertions && node.assertions.assertClause && node.attributes) {
|
||||
(node.assertions as Mutable<ImportTypeAssertionContainer>).assertClause = node.attributes;
|
||||
}
|
||||
node.qualifier = qualifier;
|
||||
node.typeArguments = typeArguments && parenthesizerRules().parenthesizeTypeArguments(typeArguments);
|
||||
node.isTypeOf = isTypeOf;
|
||||
@@ -2662,17 +2672,17 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
|
||||
function updateImportTypeNode(
|
||||
node: ImportTypeNode,
|
||||
argument: TypeNode,
|
||||
assertions: ImportTypeAssertionContainer | undefined,
|
||||
attributes: ImportAttributes | undefined,
|
||||
qualifier: EntityName | undefined,
|
||||
typeArguments: readonly TypeNode[] | undefined,
|
||||
isTypeOf: boolean = node.isTypeOf,
|
||||
): ImportTypeNode {
|
||||
return node.argument !== argument
|
||||
|| node.assertions !== assertions
|
||||
|| node.attributes !== attributes
|
||||
|| node.qualifier !== qualifier
|
||||
|| node.typeArguments !== typeArguments
|
||||
|| node.isTypeOf !== isTypeOf
|
||||
? update(createImportTypeNode(argument, assertions, qualifier, typeArguments, isTypeOf), node)
|
||||
? update(createImportTypeNode(argument, attributes, qualifier, typeArguments, isTypeOf), node)
|
||||
: node;
|
||||
}
|
||||
|
||||
@@ -4696,13 +4706,13 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
|
||||
modifiers: readonly ModifierLike[] | undefined,
|
||||
importClause: ImportClause | undefined,
|
||||
moduleSpecifier: Expression,
|
||||
assertClause: AssertClause | undefined,
|
||||
attributes: ImportAttributes | undefined,
|
||||
): ImportDeclaration {
|
||||
const node = createBaseNode<ImportDeclaration>(SyntaxKind.ImportDeclaration);
|
||||
node.modifiers = asNodeArray(modifiers);
|
||||
node.importClause = importClause;
|
||||
node.moduleSpecifier = moduleSpecifier;
|
||||
node.assertClause = assertClause;
|
||||
node.attributes = node.assertClause = attributes;
|
||||
node.transformFlags |= propagateChildFlags(node.importClause) |
|
||||
propagateChildFlags(node.moduleSpecifier);
|
||||
node.transformFlags &= ~TransformFlags.ContainsPossibleTopLevelAwait; // always parsed in an Await context
|
||||
@@ -4717,13 +4727,13 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
|
||||
modifiers: readonly ModifierLike[] | undefined,
|
||||
importClause: ImportClause | undefined,
|
||||
moduleSpecifier: Expression,
|
||||
assertClause: AssertClause | undefined,
|
||||
attributes: ImportAttributes | undefined,
|
||||
) {
|
||||
return node.modifiers !== modifiers
|
||||
|| node.importClause !== importClause
|
||||
|| node.moduleSpecifier !== moduleSpecifier
|
||||
|| node.assertClause !== assertClause
|
||||
? update(createImportDeclaration(modifiers, importClause, moduleSpecifier, assertClause), node)
|
||||
|| node.attributes !== attributes
|
||||
? update(createImportDeclaration(modifiers, importClause, moduleSpecifier, attributes), node)
|
||||
: node;
|
||||
}
|
||||
|
||||
@@ -4756,6 +4766,7 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
|
||||
const node = createBaseNode<AssertClause>(SyntaxKind.AssertClause);
|
||||
node.elements = createNodeArray(elements);
|
||||
node.multiLine = multiLine;
|
||||
node.token = SyntaxKind.AssertKeyword;
|
||||
node.transformFlags |= TransformFlags.ContainsESNext;
|
||||
return node;
|
||||
}
|
||||
@@ -4801,6 +4812,43 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
|
||||
: node;
|
||||
}
|
||||
|
||||
// @api
|
||||
function createImportAttributes(elements: readonly ImportAttribute[], multiLine?: boolean): ImportAttributes;
|
||||
function createImportAttributes(elements: readonly ImportAttribute[], multiLine?: boolean, token?: ImportAttributes["token"]): ImportAttributes;
|
||||
function createImportAttributes(elements: readonly ImportAttribute[], multiLine?: boolean, token?: ImportAttributes["token"]): ImportAttributes {
|
||||
const node = createBaseNode<ImportAttributes>(SyntaxKind.ImportAttributes);
|
||||
node.token = token ?? SyntaxKind.WithKeyword;
|
||||
node.elements = createNodeArray(elements);
|
||||
node.multiLine = multiLine;
|
||||
node.transformFlags |= TransformFlags.ContainsESNext;
|
||||
return node;
|
||||
}
|
||||
|
||||
// @api
|
||||
function updateImportAttributes(node: ImportAttributes, elements: readonly ImportAttribute[], multiLine?: boolean): ImportAttributes {
|
||||
return node.elements !== elements
|
||||
|| node.multiLine !== multiLine
|
||||
? update(createImportAttributes(elements, multiLine, node.token), node)
|
||||
: node;
|
||||
}
|
||||
|
||||
// @api
|
||||
function createImportAttribute(name: ImportAttributeName, value: Expression): ImportAttribute {
|
||||
const node = createBaseNode<ImportAttribute>(SyntaxKind.ImportAttribute);
|
||||
node.name = name;
|
||||
node.value = value;
|
||||
node.transformFlags |= TransformFlags.ContainsESNext;
|
||||
return node;
|
||||
}
|
||||
|
||||
// @api
|
||||
function updateImportAttribute(node: ImportAttribute, name: ImportAttributeName, value: Expression): ImportAttribute {
|
||||
return node.name !== name
|
||||
|| node.value !== value
|
||||
? update(createImportAttribute(name, value), node)
|
||||
: node;
|
||||
}
|
||||
|
||||
// @api
|
||||
function createNamespaceImport(name: Identifier): NamespaceImport {
|
||||
const node = createBaseDeclaration<NamespaceImport>(SyntaxKind.NamespaceImport);
|
||||
@@ -4908,14 +4956,14 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
|
||||
isTypeOnly: boolean,
|
||||
exportClause: NamedExportBindings | undefined,
|
||||
moduleSpecifier?: Expression,
|
||||
assertClause?: AssertClause,
|
||||
attributes?: ImportAttributes,
|
||||
) {
|
||||
const node = createBaseDeclaration<ExportDeclaration>(SyntaxKind.ExportDeclaration);
|
||||
node.modifiers = asNodeArray(modifiers);
|
||||
node.isTypeOnly = isTypeOnly;
|
||||
node.exportClause = exportClause;
|
||||
node.moduleSpecifier = moduleSpecifier;
|
||||
node.assertClause = assertClause;
|
||||
node.attributes = node.assertClause = attributes;
|
||||
node.transformFlags |= propagateChildrenFlags(node.modifiers) |
|
||||
propagateChildFlags(node.exportClause) |
|
||||
propagateChildFlags(node.moduleSpecifier);
|
||||
@@ -4932,14 +4980,14 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
|
||||
isTypeOnly: boolean,
|
||||
exportClause: NamedExportBindings | undefined,
|
||||
moduleSpecifier: Expression | undefined,
|
||||
assertClause: AssertClause | undefined,
|
||||
attributes: ImportAttributes | undefined,
|
||||
) {
|
||||
return node.modifiers !== modifiers
|
||||
|| node.isTypeOnly !== isTypeOnly
|
||||
|| node.exportClause !== exportClause
|
||||
|| node.moduleSpecifier !== moduleSpecifier
|
||||
|| node.assertClause !== assertClause
|
||||
? finishUpdateExportDeclaration(createExportDeclaration(modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause), node)
|
||||
|| node.attributes !== attributes
|
||||
? finishUpdateExportDeclaration(createExportDeclaration(modifiers, isTypeOnly, exportClause, moduleSpecifier, attributes), node)
|
||||
: node;
|
||||
}
|
||||
|
||||
@@ -7083,9 +7131,9 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
|
||||
isEnumDeclaration(node) ? updateEnumDeclaration(node, modifierArray, node.name, node.members) :
|
||||
isModuleDeclaration(node) ? updateModuleDeclaration(node, modifierArray, node.name, node.body) :
|
||||
isImportEqualsDeclaration(node) ? updateImportEqualsDeclaration(node, modifierArray, node.isTypeOnly, node.name, node.moduleReference) :
|
||||
isImportDeclaration(node) ? updateImportDeclaration(node, modifierArray, node.importClause, node.moduleSpecifier, node.assertClause) :
|
||||
isImportDeclaration(node) ? updateImportDeclaration(node, modifierArray, node.importClause, node.moduleSpecifier, node.attributes) :
|
||||
isExportAssignment(node) ? updateExportAssignment(node, modifierArray, node.expression) :
|
||||
isExportDeclaration(node) ? updateExportDeclaration(node, modifierArray, node.isTypeOnly, node.exportClause, node.moduleSpecifier, node.assertClause) :
|
||||
isExportDeclaration(node) ? updateExportDeclaration(node, modifierArray, node.isTypeOnly, node.exportClause, node.moduleSpecifier, node.attributes) :
|
||||
Debug.assertNever(node);
|
||||
}
|
||||
|
||||
|
||||
@@ -67,6 +67,8 @@ import {
|
||||
HeritageClause,
|
||||
Identifier,
|
||||
IfStatement,
|
||||
ImportAttribute,
|
||||
ImportAttributes,
|
||||
ImportClause,
|
||||
ImportDeclaration,
|
||||
ImportEqualsDeclaration,
|
||||
@@ -847,14 +849,24 @@ export function isImportTypeAssertionContainer(node: Node): node is ImportTypeAs
|
||||
return node.kind === SyntaxKind.ImportTypeAssertionContainer;
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
export function isAssertClause(node: Node): node is AssertClause {
|
||||
return node.kind === SyntaxKind.AssertClause;
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
export function isAssertEntry(node: Node): node is AssertEntry {
|
||||
return node.kind === SyntaxKind.AssertEntry;
|
||||
}
|
||||
|
||||
export function isImportAttributes(node: Node): node is ImportAttributes {
|
||||
return node.kind === SyntaxKind.ImportAttributes;
|
||||
}
|
||||
|
||||
export function isImportAttribute(node: Node): node is ImportAttribute {
|
||||
return node.kind === SyntaxKind.ImportAttribute;
|
||||
}
|
||||
|
||||
export function isNamespaceImport(node: Node): node is NamespaceImport {
|
||||
return node.kind === SyntaxKind.NamespaceImport;
|
||||
}
|
||||
|
||||
@@ -754,7 +754,7 @@ export function createExternalHelpersImportDeclarationIfNeeded(nodeFactory: Node
|
||||
/*modifiers*/ undefined,
|
||||
nodeFactory.createImportClause(/*isTypeOnly*/ false, /*name*/ undefined, namedBindings),
|
||||
nodeFactory.createStringLiteral(externalHelpersModuleNameText),
|
||||
/*assertClause*/ undefined,
|
||||
/*attributes*/ undefined,
|
||||
);
|
||||
addInternalEmitFlags(externalHelpersImportDeclaration, InternalEmitFlags.NeverApplyImportHelper);
|
||||
return externalHelpersImportDeclaration;
|
||||
|
||||
+56
-58
@@ -9,8 +9,6 @@ import {
|
||||
ArrayTypeNode,
|
||||
ArrowFunction,
|
||||
AsExpression,
|
||||
AssertClause,
|
||||
AssertEntry,
|
||||
AssertionLevel,
|
||||
AsteriskToken,
|
||||
attachFileToDiagnostics,
|
||||
@@ -117,6 +115,8 @@ import {
|
||||
Identifier,
|
||||
idText,
|
||||
IfStatement,
|
||||
ImportAttribute,
|
||||
ImportAttributes,
|
||||
ImportClause,
|
||||
ImportDeclaration,
|
||||
ImportEqualsDeclaration,
|
||||
@@ -689,7 +689,7 @@ const forEachChildTable: ForEachChildTable = {
|
||||
},
|
||||
[SyntaxKind.ImportType]: function forEachChildInImportType<T>(node: ImportTypeNode, cbNode: (node: Node) => T | undefined, cbNodes?: (nodes: NodeArray<Node>) => T | undefined): T | undefined {
|
||||
return visitNode(cbNode, node.argument) ||
|
||||
visitNode(cbNode, node.assertions) ||
|
||||
visitNode(cbNode, node.attributes) ||
|
||||
visitNode(cbNode, node.qualifier) ||
|
||||
visitNodes(cbNode, cbNodes, node.typeArguments);
|
||||
},
|
||||
@@ -928,16 +928,16 @@ const forEachChildTable: ForEachChildTable = {
|
||||
return visitNodes(cbNode, cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, node.importClause) ||
|
||||
visitNode(cbNode, node.moduleSpecifier) ||
|
||||
visitNode(cbNode, node.assertClause);
|
||||
visitNode(cbNode, node.attributes);
|
||||
},
|
||||
[SyntaxKind.ImportClause]: function forEachChildInImportClause<T>(node: ImportClause, cbNode: (node: Node) => T | undefined, _cbNodes?: (nodes: NodeArray<Node>) => T | undefined): T | undefined {
|
||||
return visitNode(cbNode, node.name) ||
|
||||
visitNode(cbNode, node.namedBindings);
|
||||
},
|
||||
[SyntaxKind.AssertClause]: function forEachChildInAssertClause<T>(node: AssertClause, cbNode: (node: Node) => T | undefined, cbNodes?: (nodes: NodeArray<Node>) => T | undefined): T | undefined {
|
||||
[SyntaxKind.ImportAttributes]: function forEachChildInImportAttributes<T>(node: ImportAttributes, cbNode: (node: Node) => T | undefined, cbNodes?: (nodes: NodeArray<Node>) => T | undefined): T | undefined {
|
||||
return visitNodes(cbNode, cbNodes, node.elements);
|
||||
},
|
||||
[SyntaxKind.AssertEntry]: function forEachChildInAssertEntry<T>(node: AssertEntry, cbNode: (node: Node) => T | undefined, _cbNodes?: (nodes: NodeArray<Node>) => T | undefined): T | undefined {
|
||||
[SyntaxKind.ImportAttribute]: function forEachChildInImportAttribute<T>(node: ImportAttribute, cbNode: (node: Node) => T | undefined, _cbNodes?: (nodes: NodeArray<Node>) => T | undefined): T | undefined {
|
||||
return visitNode(cbNode, node.name) ||
|
||||
visitNode(cbNode, node.value);
|
||||
},
|
||||
@@ -957,7 +957,7 @@ const forEachChildTable: ForEachChildTable = {
|
||||
return visitNodes(cbNode, cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, node.exportClause) ||
|
||||
visitNode(cbNode, node.moduleSpecifier) ||
|
||||
visitNode(cbNode, node.assertClause);
|
||||
visitNode(cbNode, node.attributes);
|
||||
},
|
||||
[SyntaxKind.ImportSpecifier]: forEachChildInImportOrExportSpecifier,
|
||||
[SyntaxKind.ExportSpecifier]: forEachChildInImportOrExportSpecifier,
|
||||
@@ -2683,9 +2683,8 @@ namespace Parser {
|
||||
token() === SyntaxKind.NumericLiteral;
|
||||
}
|
||||
|
||||
function isAssertionKey(): boolean {
|
||||
return tokenIsIdentifierOrKeyword(token()) ||
|
||||
token() === SyntaxKind.StringLiteral;
|
||||
function isImportAttributeName(): boolean {
|
||||
return tokenIsIdentifierOrKeyword(token()) || token() === SyntaxKind.StringLiteral;
|
||||
}
|
||||
|
||||
function parsePropertyNameWorker(allowComputedPropertyNames: boolean): PropertyName {
|
||||
@@ -2847,8 +2846,8 @@ namespace Parser {
|
||||
return isLiteralPropertyName();
|
||||
case ParsingContext.ObjectBindingElements:
|
||||
return token() === SyntaxKind.OpenBracketToken || token() === SyntaxKind.DotDotDotToken || isLiteralPropertyName();
|
||||
case ParsingContext.AssertEntries:
|
||||
return isAssertionKey();
|
||||
case ParsingContext.ImportAttributes:
|
||||
return isImportAttributeName();
|
||||
case ParsingContext.HeritageClauseElement:
|
||||
// If we see `{ ... }` then only consume it as an expression if it is followed by `,` or `{`
|
||||
// That way we won't consume the body of a class in its heritage clause.
|
||||
@@ -2979,7 +2978,7 @@ namespace Parser {
|
||||
case ParsingContext.ObjectLiteralMembers:
|
||||
case ParsingContext.ObjectBindingElements:
|
||||
case ParsingContext.ImportOrExportSpecifiers:
|
||||
case ParsingContext.AssertEntries:
|
||||
case ParsingContext.ImportAttributes:
|
||||
return token() === SyntaxKind.CloseBraceToken;
|
||||
case ParsingContext.SwitchClauseStatements:
|
||||
return token() === SyntaxKind.CloseBraceToken || token() === SyntaxKind.CaseKeyword || token() === SyntaxKind.DefaultKeyword;
|
||||
@@ -3442,8 +3441,8 @@ namespace Parser {
|
||||
return parseErrorAtCurrentToken(Diagnostics.Identifier_expected);
|
||||
case ParsingContext.JsxChildren:
|
||||
return parseErrorAtCurrentToken(Diagnostics.Identifier_expected);
|
||||
case ParsingContext.AssertEntries:
|
||||
return parseErrorAtCurrentToken(Diagnostics.Identifier_or_string_literal_expected); // AssertionKey.
|
||||
case ParsingContext.ImportAttributes:
|
||||
return parseErrorAtCurrentToken(Diagnostics.Identifier_or_string_literal_expected);
|
||||
case ParsingContext.JSDocComment:
|
||||
return parseErrorAtCurrentToken(Diagnostics.Identifier_expected);
|
||||
case ParsingContext.Count:
|
||||
@@ -4512,26 +4511,6 @@ namespace Parser {
|
||||
return token() === SyntaxKind.ImportKeyword;
|
||||
}
|
||||
|
||||
function parseImportTypeAssertions(): ImportTypeAssertionContainer {
|
||||
const pos = getNodePos();
|
||||
const openBracePosition = scanner.getTokenStart();
|
||||
parseExpected(SyntaxKind.OpenBraceToken);
|
||||
const multiLine = scanner.hasPrecedingLineBreak();
|
||||
parseExpected(SyntaxKind.AssertKeyword);
|
||||
parseExpected(SyntaxKind.ColonToken);
|
||||
const clause = parseAssertClause(/*skipAssertKeyword*/ true);
|
||||
if (!parseExpected(SyntaxKind.CloseBraceToken)) {
|
||||
const lastError = lastOrUndefined(parseDiagnostics);
|
||||
if (lastError && lastError.code === Diagnostics._0_expected.code) {
|
||||
addRelatedInfo(
|
||||
lastError,
|
||||
createDetachedDiagnostic(fileName, sourceText, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, "{", "}"),
|
||||
);
|
||||
}
|
||||
}
|
||||
return finishNode(factory.createImportTypeAssertionContainer(clause, multiLine), pos);
|
||||
}
|
||||
|
||||
function parseImportType(): ImportTypeNode {
|
||||
sourceFlags |= NodeFlags.PossiblyContainsDynamicImport;
|
||||
const pos = getNodePos();
|
||||
@@ -4539,14 +4518,33 @@ namespace Parser {
|
||||
parseExpected(SyntaxKind.ImportKeyword);
|
||||
parseExpected(SyntaxKind.OpenParenToken);
|
||||
const type = parseType();
|
||||
let assertions: ImportTypeAssertionContainer | undefined;
|
||||
let attributes: ImportAttributes | undefined;
|
||||
if (parseOptional(SyntaxKind.CommaToken)) {
|
||||
assertions = parseImportTypeAssertions();
|
||||
const openBracePosition = scanner.getTokenStart();
|
||||
parseExpected(SyntaxKind.OpenBraceToken);
|
||||
const currentToken = token();
|
||||
if (currentToken === SyntaxKind.WithKeyword || currentToken === SyntaxKind.AssertKeyword) {
|
||||
nextToken();
|
||||
}
|
||||
else {
|
||||
parseErrorAtCurrentToken(Diagnostics._0_expected, tokenToString(SyntaxKind.WithKeyword));
|
||||
}
|
||||
parseExpected(SyntaxKind.ColonToken);
|
||||
attributes = parseImportAttributes(currentToken as SyntaxKind.WithKeyword | SyntaxKind.AssertKeyword, /*skipKeyword*/ true);
|
||||
if (!parseExpected(SyntaxKind.CloseBraceToken)) {
|
||||
const lastError = lastOrUndefined(parseDiagnostics);
|
||||
if (lastError && lastError.code === Diagnostics._0_expected.code) {
|
||||
addRelatedInfo(
|
||||
lastError,
|
||||
createDetachedDiagnostic(fileName, sourceText, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, "{", "}"),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
parseExpected(SyntaxKind.CloseParenToken);
|
||||
const qualifier = parseOptional(SyntaxKind.DotToken) ? parseEntityNameOfTypeReference() : undefined;
|
||||
const typeArguments = parseTypeArgumentsOfTypeReference();
|
||||
return finishNode(factory.createImportTypeNode(type, assertions, qualifier, typeArguments, isTypeOf), pos);
|
||||
return finishNode(factory.createImportTypeNode(type, attributes, qualifier, typeArguments, isTypeOf), pos);
|
||||
}
|
||||
|
||||
function nextTokenIsNumericOrBigIntLiteral() {
|
||||
@@ -8358,34 +8356,33 @@ namespace Parser {
|
||||
parseExpected(SyntaxKind.FromKeyword);
|
||||
}
|
||||
const moduleSpecifier = parseModuleSpecifier();
|
||||
|
||||
let assertClause: AssertClause | undefined;
|
||||
if (token() === SyntaxKind.AssertKeyword && !scanner.hasPrecedingLineBreak()) {
|
||||
assertClause = parseAssertClause();
|
||||
const currentToken = token();
|
||||
let attributes: ImportAttributes | undefined;
|
||||
if ((currentToken === SyntaxKind.WithKeyword || currentToken === SyntaxKind.AssertKeyword) && !scanner.hasPrecedingLineBreak()) {
|
||||
attributes = parseImportAttributes(currentToken);
|
||||
}
|
||||
|
||||
parseSemicolon();
|
||||
const node = factory.createImportDeclaration(modifiers, importClause, moduleSpecifier, assertClause);
|
||||
const node = factory.createImportDeclaration(modifiers, importClause, moduleSpecifier, attributes);
|
||||
return withJSDoc(finishNode(node, pos), hasJSDoc);
|
||||
}
|
||||
|
||||
function parseAssertEntry() {
|
||||
function parseImportAttribute() {
|
||||
const pos = getNodePos();
|
||||
const name = tokenIsIdentifierOrKeyword(token()) ? parseIdentifierName() : parseLiteralLikeNode(SyntaxKind.StringLiteral) as StringLiteral;
|
||||
parseExpected(SyntaxKind.ColonToken);
|
||||
const value = parseAssignmentExpressionOrHigher(/*allowReturnTypeInArrowFunction*/ true);
|
||||
return finishNode(factory.createAssertEntry(name, value), pos);
|
||||
return finishNode(factory.createImportAttribute(name, value), pos);
|
||||
}
|
||||
|
||||
function parseAssertClause(skipAssertKeyword?: true) {
|
||||
function parseImportAttributes(token: SyntaxKind.AssertKeyword | SyntaxKind.WithKeyword, skipKeyword?: true) {
|
||||
const pos = getNodePos();
|
||||
if (!skipAssertKeyword) {
|
||||
parseExpected(SyntaxKind.AssertKeyword);
|
||||
if (!skipKeyword) {
|
||||
parseExpected(token);
|
||||
}
|
||||
const openBracePosition = scanner.getTokenStart();
|
||||
if (parseExpected(SyntaxKind.OpenBraceToken)) {
|
||||
const multiLine = scanner.hasPrecedingLineBreak();
|
||||
const elements = parseDelimitedList(ParsingContext.AssertEntries, parseAssertEntry, /*considerSemicolonAsDelimiter*/ true);
|
||||
const elements = parseDelimitedList(ParsingContext.ImportAttributes, parseImportAttribute, /*considerSemicolonAsDelimiter*/ true);
|
||||
if (!parseExpected(SyntaxKind.CloseBraceToken)) {
|
||||
const lastError = lastOrUndefined(parseDiagnostics);
|
||||
if (lastError && lastError.code === Diagnostics._0_expected.code) {
|
||||
@@ -8395,11 +8392,11 @@ namespace Parser {
|
||||
);
|
||||
}
|
||||
}
|
||||
return finishNode(factory.createAssertClause(elements, multiLine), pos);
|
||||
return finishNode(factory.createImportAttributes(elements, multiLine, token), pos);
|
||||
}
|
||||
else {
|
||||
const elements = createNodeArray([], getNodePos(), /*end*/ undefined, /*hasTrailingComma*/ false);
|
||||
return finishNode(factory.createAssertClause(elements, /*multiLine*/ false), pos);
|
||||
return finishNode(factory.createImportAttributes(elements, /*multiLine*/ false, token), pos);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8602,7 +8599,7 @@ namespace Parser {
|
||||
setAwaitContext(/*value*/ true);
|
||||
let exportClause: NamedExportBindings | undefined;
|
||||
let moduleSpecifier: Expression | undefined;
|
||||
let assertClause: AssertClause | undefined;
|
||||
let attributes: ImportAttributes | undefined;
|
||||
const isTypeOnly = parseOptional(SyntaxKind.TypeKeyword);
|
||||
const namespaceExportPos = getNodePos();
|
||||
if (parseOptional(SyntaxKind.AsteriskToken)) {
|
||||
@@ -8622,12 +8619,13 @@ namespace Parser {
|
||||
moduleSpecifier = parseModuleSpecifier();
|
||||
}
|
||||
}
|
||||
if (moduleSpecifier && token() === SyntaxKind.AssertKeyword && !scanner.hasPrecedingLineBreak()) {
|
||||
assertClause = parseAssertClause();
|
||||
const currentToken = token();
|
||||
if (moduleSpecifier && (currentToken === SyntaxKind.WithKeyword || currentToken === SyntaxKind.AssertKeyword) && !scanner.hasPrecedingLineBreak()) {
|
||||
attributes = parseImportAttributes(currentToken);
|
||||
}
|
||||
parseSemicolon();
|
||||
setAwaitContext(savedAwaitContext);
|
||||
const node = factory.createExportDeclaration(modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause);
|
||||
const node = factory.createExportDeclaration(modifiers, isTypeOnly, exportClause, moduleSpecifier, attributes);
|
||||
return withJSDoc(finishNode(node, pos), hasJSDoc);
|
||||
}
|
||||
|
||||
@@ -8674,9 +8672,9 @@ namespace Parser {
|
||||
TupleElementTypes, // Element types in tuple element type list
|
||||
HeritageClauses, // Heritage clauses for a class or interface declaration.
|
||||
ImportOrExportSpecifiers, // Named import clause's import specifier list,
|
||||
AssertEntries, // Import entries list.
|
||||
ImportAttributes, // Import attributes
|
||||
JSDocComment, // Parsing via JSDocParser
|
||||
Count, // Number of parsing contexts
|
||||
Count, // Number of parsing contexts
|
||||
}
|
||||
|
||||
const enum Tristate {
|
||||
|
||||
+21
-10
@@ -167,6 +167,7 @@ import {
|
||||
HeritageClause,
|
||||
Identifier,
|
||||
identity,
|
||||
ImportAttributes,
|
||||
ImportClause,
|
||||
ImportDeclaration,
|
||||
ImportOrExportSpecifier,
|
||||
@@ -895,14 +896,14 @@ export function getModeForUsageLocation(file: { impliedNodeFormat?: ResolutionMo
|
||||
if ((isImportDeclaration(usage.parent) || isExportDeclaration(usage.parent))) {
|
||||
const isTypeOnly = isExclusivelyTypeOnlyImportOrExport(usage.parent);
|
||||
if (isTypeOnly) {
|
||||
const override = getResolutionModeOverrideForClause(usage.parent.assertClause);
|
||||
const override = getResolutionModeOverride(usage.parent.attributes);
|
||||
if (override) {
|
||||
return override;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (usage.parent.parent && isImportTypeNode(usage.parent.parent)) {
|
||||
const override = getResolutionModeOverrideForClause(usage.parent.parent.assertions?.assertClause);
|
||||
const override = getResolutionModeOverride(usage.parent.parent.attributes);
|
||||
if (override) {
|
||||
return override;
|
||||
}
|
||||
@@ -918,16 +919,26 @@ export function getModeForUsageLocation(file: { impliedNodeFormat?: ResolutionMo
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function getResolutionModeOverrideForClause(clause: AssertClause | undefined, grammarErrorOnNode?: (node: Node, diagnostic: DiagnosticMessage) => void) {
|
||||
if (!clause) return undefined;
|
||||
if (length(clause.elements) !== 1) {
|
||||
grammarErrorOnNode?.(clause, Diagnostics.Type_import_assertions_should_have_exactly_one_key_resolution_mode_with_value_import_or_require);
|
||||
export function getResolutionModeOverride(node: AssertClause | ImportAttributes | undefined, grammarErrorOnNode?: (node: Node, diagnostic: DiagnosticMessage) => void) {
|
||||
if (!node) return undefined;
|
||||
if (length(node.elements) !== 1) {
|
||||
grammarErrorOnNode?.(
|
||||
node,
|
||||
node.token === SyntaxKind.WithKeyword
|
||||
? Diagnostics.Type_import_attributes_should_have_exactly_one_key_resolution_mode_with_value_import_or_require
|
||||
: Diagnostics.Type_import_assertions_should_have_exactly_one_key_resolution_mode_with_value_import_or_require,
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
const elem = clause.elements[0];
|
||||
const elem = node.elements[0];
|
||||
if (!isStringLiteralLike(elem.name)) return undefined;
|
||||
if (elem.name.text !== "resolution-mode") {
|
||||
grammarErrorOnNode?.(elem.name, Diagnostics.resolution_mode_is_the_only_valid_key_for_type_import_assertions);
|
||||
grammarErrorOnNode?.(
|
||||
elem.name,
|
||||
node.token === SyntaxKind.WithKeyword
|
||||
? Diagnostics.resolution_mode_is_the_only_valid_key_for_type_import_attributes
|
||||
: Diagnostics.resolution_mode_is_the_only_valid_key_for_type_import_assertions,
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
if (!isStringLiteralLike(elem.value)) return undefined;
|
||||
@@ -1393,7 +1404,7 @@ export const plainJSErrors = new Set<number>([
|
||||
Diagnostics.Classes_may_not_have_a_field_named_constructor.code,
|
||||
Diagnostics.Did_you_mean_to_use_a_Colon_An_can_only_follow_a_property_name_when_the_containing_object_literal_is_part_of_a_destructuring_pattern.code,
|
||||
Diagnostics.Duplicate_label_0.code,
|
||||
Diagnostics.Dynamic_imports_can_only_accept_a_module_specifier_and_an_optional_assertion_as_arguments.code,
|
||||
Diagnostics.Dynamic_imports_can_only_accept_a_module_specifier_and_an_optional_set_of_attributes_as_arguments.code,
|
||||
Diagnostics.for_await_loops_cannot_be_used_inside_a_class_static_block.code,
|
||||
Diagnostics.JSX_attributes_must_only_be_assigned_a_non_empty_expression.code,
|
||||
Diagnostics.JSX_elements_cannot_have_multiple_attributes_with_the_same_name.code,
|
||||
@@ -3294,7 +3305,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
|
||||
|
||||
function createSyntheticImport(text: string, file: SourceFile) {
|
||||
const externalHelpersModuleReference = factory.createStringLiteral(text);
|
||||
const importDecl = factory.createImportDeclaration(/*modifiers*/ undefined, /*importClause*/ undefined, externalHelpersModuleReference, /*assertClause*/ undefined);
|
||||
const importDecl = factory.createImportDeclaration(/*modifiers*/ undefined, /*importClause*/ undefined, externalHelpersModuleReference, /*attributes*/ undefined);
|
||||
addInternalEmitFlags(importDecl, InternalEmitFlags.NeverApplyImportHelper);
|
||||
setParent(externalHelpersModuleReference, importDecl);
|
||||
setParent(importDecl, file);
|
||||
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
append,
|
||||
ArrayBindingElement,
|
||||
arrayFrom,
|
||||
AssertClause,
|
||||
BindingElement,
|
||||
BindingName,
|
||||
BindingPattern,
|
||||
@@ -70,7 +69,7 @@ import {
|
||||
getOutputPathsFor,
|
||||
getParseTreeNode,
|
||||
getRelativePathToDirectoryOrUrl,
|
||||
getResolutionModeOverrideForClause,
|
||||
getResolutionModeOverride,
|
||||
getResolvedExternalModuleName,
|
||||
getSetAccessorValueParameter,
|
||||
getSourceFileOfNode,
|
||||
@@ -86,6 +85,7 @@ import {
|
||||
hasSyntacticModifier,
|
||||
HeritageClause,
|
||||
Identifier,
|
||||
ImportAttributes,
|
||||
ImportDeclaration,
|
||||
ImportEqualsDeclaration,
|
||||
ImportTypeNode,
|
||||
@@ -130,7 +130,6 @@ import {
|
||||
isMethodSignature,
|
||||
isModifier,
|
||||
isModuleDeclaration,
|
||||
isNightly,
|
||||
isOmittedExpression,
|
||||
isPrivateIdentifier,
|
||||
isPropertySignature,
|
||||
@@ -312,7 +311,6 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
trackExternalModuleSymbolOfImportTypeNode,
|
||||
reportNonlocalAugmentation,
|
||||
reportNonSerializableProperty,
|
||||
reportImportTypeNodeResolutionModeOverride,
|
||||
};
|
||||
let errorNameNode: DeclarationName | undefined;
|
||||
let errorFallbackNode: Declaration | undefined;
|
||||
@@ -456,12 +454,6 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
}
|
||||
}
|
||||
|
||||
function reportImportTypeNodeResolutionModeOverride() {
|
||||
if (!isNightly() && (errorNameNode || errorFallbackNode)) {
|
||||
context.addDiagnostic(createDiagnosticForNode((errorNameNode || errorFallbackNode)!, Diagnostics.The_type_of_this_expression_cannot_be_named_without_a_resolution_mode_assertion_which_is_an_unstable_feature_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next));
|
||||
}
|
||||
}
|
||||
|
||||
function transformDeclarationsForJS(sourceFile: SourceFile, bundled?: boolean) {
|
||||
const oldDiag = getSymbolAccessibilityDiagnostic;
|
||||
getSymbolAccessibilityDiagnostic = s => (s.errorNode && canProduceDiagnostics(s.errorNode) ? createGetSymbolAccessibilityDiagnosticForNode(s.errorNode)(s) : ({
|
||||
@@ -985,7 +977,7 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
decl.modifiers,
|
||||
decl.importClause,
|
||||
rewriteModuleSpecifier(decl, decl.moduleSpecifier),
|
||||
getResolutionModeOverrideForClauseInNightly(decl.assertClause),
|
||||
tryGetResolutionModeOverride(decl.attributes),
|
||||
);
|
||||
}
|
||||
// The `importClause` visibility corresponds to the default's visibility.
|
||||
@@ -1002,7 +994,7 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
/*namedBindings*/ undefined,
|
||||
),
|
||||
rewriteModuleSpecifier(decl, decl.moduleSpecifier),
|
||||
getResolutionModeOverrideForClauseInNightly(decl.assertClause),
|
||||
tryGetResolutionModeOverride(decl.attributes),
|
||||
);
|
||||
}
|
||||
if (decl.importClause.namedBindings.kind === SyntaxKind.NamespaceImport) {
|
||||
@@ -1018,7 +1010,7 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
namedBindings,
|
||||
),
|
||||
rewriteModuleSpecifier(decl, decl.moduleSpecifier),
|
||||
getResolutionModeOverrideForClauseInNightly(decl.assertClause),
|
||||
tryGetResolutionModeOverride(decl.attributes),
|
||||
) : undefined;
|
||||
}
|
||||
// Named imports (optionally with visible default)
|
||||
@@ -1034,7 +1026,7 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
bindingList && bindingList.length ? factory.updateNamedImports(decl.importClause.namedBindings, bindingList) : undefined,
|
||||
),
|
||||
rewriteModuleSpecifier(decl, decl.moduleSpecifier),
|
||||
getResolutionModeOverrideForClauseInNightly(decl.assertClause),
|
||||
tryGetResolutionModeOverride(decl.attributes),
|
||||
);
|
||||
}
|
||||
// Augmentation of export depends on import
|
||||
@@ -1044,21 +1036,15 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
decl.modifiers,
|
||||
/*importClause*/ undefined,
|
||||
rewriteModuleSpecifier(decl, decl.moduleSpecifier),
|
||||
getResolutionModeOverrideForClauseInNightly(decl.assertClause),
|
||||
tryGetResolutionModeOverride(decl.attributes),
|
||||
);
|
||||
}
|
||||
// Nothing visible
|
||||
}
|
||||
|
||||
function getResolutionModeOverrideForClauseInNightly(assertClause: AssertClause | undefined) {
|
||||
const mode = getResolutionModeOverrideForClause(assertClause);
|
||||
if (mode !== undefined) {
|
||||
if (!isNightly()) {
|
||||
context.addDiagnostic(createDiagnosticForNode(assertClause!, Diagnostics.resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next));
|
||||
}
|
||||
return assertClause;
|
||||
}
|
||||
return undefined;
|
||||
function tryGetResolutionModeOverride(node: ImportAttributes | undefined) {
|
||||
const mode = getResolutionModeOverride(node);
|
||||
return node && mode !== undefined ? node : undefined;
|
||||
}
|
||||
|
||||
function transformAndReplaceLatePaintedStatements(statements: NodeArray<Statement>): NodeArray<Statement> {
|
||||
@@ -1329,7 +1315,7 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
return cleanup(factory.updateImportTypeNode(
|
||||
input,
|
||||
factory.updateLiteralTypeNode(input.argument, rewriteModuleSpecifier(input, input.argument.literal)),
|
||||
input.assertions,
|
||||
input.attributes,
|
||||
input.qualifier,
|
||||
visitNodes(input.typeArguments, visitDeclarationSubtree, isTypeNode),
|
||||
input.isTypeOf,
|
||||
@@ -1391,7 +1377,7 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
input.isTypeOnly,
|
||||
input.exportClause,
|
||||
rewriteModuleSpecifier(input, input.moduleSpecifier),
|
||||
getResolutionModeOverrideForClause(input.assertClause) ? input.assertClause : undefined,
|
||||
tryGetResolutionModeOverride(input.attributes),
|
||||
);
|
||||
}
|
||||
case SyntaxKind.ExportAssignment: {
|
||||
|
||||
@@ -170,7 +170,7 @@ export function transformJsx(context: TransformationContext): (x: SourceFile | B
|
||||
for (const [importSource, importSpecifiersMap] of arrayFrom(currentFileState.utilizedImplicitRuntimeImports.entries())) {
|
||||
if (isExternalModule(node)) {
|
||||
// Add `import` statement
|
||||
const importStatement = factory.createImportDeclaration(/*modifiers*/ undefined, factory.createImportClause(/*isTypeOnly*/ false, /*name*/ undefined, factory.createNamedImports(arrayFrom(importSpecifiersMap.values()))), factory.createStringLiteral(importSource), /*assertClause*/ undefined);
|
||||
const importStatement = factory.createImportDeclaration(/*modifiers*/ undefined, factory.createImportClause(/*isTypeOnly*/ false, /*name*/ undefined, factory.createNamedImports(arrayFrom(importSpecifiersMap.values()))), factory.createStringLiteral(importSource), /*attributes*/ undefined);
|
||||
setParentRecursive(importStatement, /*incremental*/ false);
|
||||
statements = insertStatementAfterCustomPrologue(statements.slice(), importStatement);
|
||||
}
|
||||
|
||||
@@ -159,6 +159,7 @@ export function transformECMAScriptModule(context: TransformationContext): (x: S
|
||||
]),
|
||||
),
|
||||
factory.createStringLiteral("module"),
|
||||
/*attributes*/ undefined,
|
||||
);
|
||||
const requireHelperName = factory.createUniqueName("__require", GeneratedIdentifierFlags.Optimistic | GeneratedIdentifierFlags.FileLevel);
|
||||
const requireStatement = factory.createVariableStatement(
|
||||
@@ -265,7 +266,7 @@ export function transformECMAScriptModule(context: TransformationContext): (x: S
|
||||
),
|
||||
),
|
||||
node.moduleSpecifier,
|
||||
node.assertClause,
|
||||
node.attributes,
|
||||
);
|
||||
setOriginalNode(importDecl, node.exportClause);
|
||||
|
||||
|
||||
@@ -2227,7 +2227,7 @@ export function transformTypeScript(context: TransformationContext) {
|
||||
/*modifiers*/ undefined,
|
||||
importClause,
|
||||
node.moduleSpecifier,
|
||||
node.assertClause,
|
||||
node.attributes,
|
||||
)
|
||||
: undefined;
|
||||
}
|
||||
@@ -2323,7 +2323,7 @@ export function transformTypeScript(context: TransformationContext) {
|
||||
node.isTypeOnly,
|
||||
exportClause,
|
||||
node.moduleSpecifier,
|
||||
node.assertClause,
|
||||
node.attributes,
|
||||
)
|
||||
: undefined;
|
||||
}
|
||||
@@ -2393,7 +2393,7 @@ export function transformTypeScript(context: TransformationContext) {
|
||||
/*modifiers*/ undefined,
|
||||
/*importClause*/ undefined,
|
||||
node.moduleReference.expression,
|
||||
/*assertClause*/ undefined,
|
||||
/*attributes*/ undefined,
|
||||
),
|
||||
node,
|
||||
),
|
||||
|
||||
+53
-29
@@ -375,9 +375,12 @@ export const enum SyntaxKind {
|
||||
DefaultClause,
|
||||
HeritageClause,
|
||||
CatchClause,
|
||||
AssertClause,
|
||||
AssertEntry,
|
||||
ImportTypeAssertionContainer,
|
||||
|
||||
ImportAttributes,
|
||||
ImportAttribute,
|
||||
/** @deprecated */ AssertClause = ImportAttributes,
|
||||
/** @deprecated */ AssertEntry = ImportAttribute,
|
||||
/** @deprecated */ ImportTypeAssertionContainer,
|
||||
|
||||
// Property assignments
|
||||
PropertyAssignment,
|
||||
@@ -1128,6 +1131,8 @@ export type HasChildren =
|
||||
| ImportDeclaration
|
||||
| AssertClause
|
||||
| AssertEntry
|
||||
| ImportAttributes
|
||||
| ImportAttribute
|
||||
| ImportClause
|
||||
| NamespaceImport
|
||||
| NamespaceExport
|
||||
@@ -2140,10 +2145,11 @@ export interface KeywordTypeNode<TKind extends KeywordTypeSyntaxKind = KeywordTy
|
||||
readonly kind: TKind;
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
export interface ImportTypeAssertionContainer extends Node {
|
||||
readonly kind: SyntaxKind.ImportTypeAssertionContainer;
|
||||
readonly parent: ImportTypeNode;
|
||||
readonly assertClause: AssertClause;
|
||||
/** @deprecated */ readonly assertClause: AssertClause;
|
||||
readonly multiLine?: boolean;
|
||||
}
|
||||
|
||||
@@ -2151,7 +2157,8 @@ export interface ImportTypeNode extends NodeWithTypeArguments {
|
||||
readonly kind: SyntaxKind.ImportType;
|
||||
readonly isTypeOf: boolean;
|
||||
readonly argument: TypeNode;
|
||||
readonly assertions?: ImportTypeAssertionContainer;
|
||||
/** @deprecated */ readonly assertions?: ImportTypeAssertionContainer;
|
||||
readonly attributes?: ImportAttributes;
|
||||
readonly qualifier?: EntityName;
|
||||
}
|
||||
|
||||
@@ -3601,7 +3608,8 @@ export interface ImportDeclaration extends Statement {
|
||||
readonly importClause?: ImportClause;
|
||||
/** If this is not a StringLiteral it will be a grammar error. */
|
||||
readonly moduleSpecifier: Expression;
|
||||
readonly assertClause?: AssertClause;
|
||||
/** @deprecated */ readonly assertClause?: AssertClause;
|
||||
readonly attributes?: ImportAttributes;
|
||||
}
|
||||
|
||||
export type NamedImportBindings =
|
||||
@@ -3626,19 +3634,29 @@ export interface ImportClause extends NamedDeclaration {
|
||||
readonly namedBindings?: NamedImportBindings;
|
||||
}
|
||||
|
||||
export type AssertionKey = Identifier | StringLiteral;
|
||||
/** @deprecated */
|
||||
export type AssertionKey = ImportAttributeName;
|
||||
|
||||
export interface AssertEntry extends Node {
|
||||
readonly kind: SyntaxKind.AssertEntry;
|
||||
readonly parent: AssertClause;
|
||||
readonly name: AssertionKey;
|
||||
/** @deprecated */
|
||||
export type AssertEntry = ImportAttribute;
|
||||
|
||||
/** @deprecated */
|
||||
export type AssertClause = ImportAttributes;
|
||||
|
||||
export type ImportAttributeName = Identifier | StringLiteral;
|
||||
|
||||
export interface ImportAttribute extends Node {
|
||||
readonly kind: SyntaxKind.ImportAttribute;
|
||||
readonly parent: ImportAttributes;
|
||||
readonly name: ImportAttributeName;
|
||||
readonly value: Expression;
|
||||
}
|
||||
|
||||
export interface AssertClause extends Node {
|
||||
readonly kind: SyntaxKind.AssertClause;
|
||||
export interface ImportAttributes extends Node {
|
||||
readonly token: SyntaxKind.WithKeyword | SyntaxKind.AssertKeyword;
|
||||
readonly kind: SyntaxKind.ImportAttributes;
|
||||
readonly parent: ImportDeclaration | ExportDeclaration;
|
||||
readonly elements: NodeArray<AssertEntry>;
|
||||
readonly elements: NodeArray<ImportAttribute>;
|
||||
readonly multiLine?: boolean;
|
||||
}
|
||||
|
||||
@@ -3671,7 +3689,8 @@ export interface ExportDeclaration extends DeclarationStatement, JSDocContainer
|
||||
readonly exportClause?: NamedExportBindings;
|
||||
/** If this is not a StringLiteral it will be a grammar error. */
|
||||
readonly moduleSpecifier?: Expression;
|
||||
readonly assertClause?: AssertClause;
|
||||
/** @deprecated */ readonly assertClause?: AssertClause;
|
||||
readonly attributes?: ImportAttributes;
|
||||
}
|
||||
|
||||
export interface NamedImports extends Node {
|
||||
@@ -8418,8 +8437,8 @@ export interface NodeFactory {
|
||||
updateConditionalTypeNode(node: ConditionalTypeNode, checkType: TypeNode, extendsType: TypeNode, trueType: TypeNode, falseType: TypeNode): ConditionalTypeNode;
|
||||
createInferTypeNode(typeParameter: TypeParameterDeclaration): InferTypeNode;
|
||||
updateInferTypeNode(node: InferTypeNode, typeParameter: TypeParameterDeclaration): InferTypeNode;
|
||||
createImportTypeNode(argument: TypeNode, assertions?: ImportTypeAssertionContainer, qualifier?: EntityName, typeArguments?: readonly TypeNode[], isTypeOf?: boolean): ImportTypeNode;
|
||||
updateImportTypeNode(node: ImportTypeNode, argument: TypeNode, assertions: ImportTypeAssertionContainer | undefined, qualifier: EntityName | undefined, typeArguments: readonly TypeNode[] | undefined, isTypeOf?: boolean): ImportTypeNode;
|
||||
createImportTypeNode(argument: TypeNode, attributes?: ImportAttributes, qualifier?: EntityName, typeArguments?: readonly TypeNode[], isTypeOf?: boolean): ImportTypeNode;
|
||||
updateImportTypeNode(node: ImportTypeNode, argument: TypeNode, attributes: ImportAttributes | undefined, qualifier: EntityName | undefined, typeArguments: readonly TypeNode[] | undefined, isTypeOf?: boolean): ImportTypeNode;
|
||||
createParenthesizedType(type: TypeNode): ParenthesizedTypeNode;
|
||||
updateParenthesizedType(node: ParenthesizedTypeNode, type: TypeNode): ParenthesizedTypeNode;
|
||||
createThisTypeNode(): ThisTypeNode;
|
||||
@@ -8599,16 +8618,21 @@ export interface NodeFactory {
|
||||
updateNamespaceExportDeclaration(node: NamespaceExportDeclaration, name: Identifier): NamespaceExportDeclaration;
|
||||
createImportEqualsDeclaration(modifiers: readonly ModifierLike[] | undefined, isTypeOnly: boolean, name: string | Identifier, moduleReference: ModuleReference): ImportEqualsDeclaration;
|
||||
updateImportEqualsDeclaration(node: ImportEqualsDeclaration, modifiers: readonly ModifierLike[] | undefined, isTypeOnly: boolean, name: Identifier, moduleReference: ModuleReference): ImportEqualsDeclaration;
|
||||
createImportDeclaration(modifiers: readonly ModifierLike[] | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, assertClause?: AssertClause): ImportDeclaration;
|
||||
updateImportDeclaration(node: ImportDeclaration, modifiers: readonly ModifierLike[] | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, assertClause: AssertClause | undefined): ImportDeclaration;
|
||||
createImportDeclaration(modifiers: readonly ModifierLike[] | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, attributes?: ImportAttributes): ImportDeclaration;
|
||||
updateImportDeclaration(node: ImportDeclaration, modifiers: readonly ModifierLike[] | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, attributes: ImportAttributes | undefined): ImportDeclaration;
|
||||
createImportClause(isTypeOnly: boolean, name: Identifier | undefined, namedBindings: NamedImportBindings | undefined): ImportClause;
|
||||
updateImportClause(node: ImportClause, isTypeOnly: boolean, name: Identifier | undefined, namedBindings: NamedImportBindings | undefined): ImportClause;
|
||||
createAssertClause(elements: NodeArray<AssertEntry>, multiLine?: boolean): AssertClause;
|
||||
updateAssertClause(node: AssertClause, elements: NodeArray<AssertEntry>, multiLine?: boolean): AssertClause;
|
||||
createAssertEntry(name: AssertionKey, value: Expression): AssertEntry;
|
||||
updateAssertEntry(node: AssertEntry, name: AssertionKey, value: Expression): AssertEntry;
|
||||
createImportTypeAssertionContainer(clause: AssertClause, multiLine?: boolean): ImportTypeAssertionContainer;
|
||||
updateImportTypeAssertionContainer(node: ImportTypeAssertionContainer, clause: AssertClause, multiLine?: boolean): ImportTypeAssertionContainer;
|
||||
/** @deprecated */ createAssertClause(elements: NodeArray<AssertEntry>, multiLine?: boolean): AssertClause;
|
||||
/** @deprecated */ updateAssertClause(node: AssertClause, elements: NodeArray<AssertEntry>, multiLine?: boolean): AssertClause;
|
||||
/** @deprecated */ createAssertEntry(name: AssertionKey, value: Expression): AssertEntry;
|
||||
/** @deprecated */ updateAssertEntry(node: AssertEntry, name: AssertionKey, value: Expression): AssertEntry;
|
||||
/** @deprecated */ createImportTypeAssertionContainer(clause: AssertClause, multiLine?: boolean): ImportTypeAssertionContainer;
|
||||
/** @deprecated */ updateImportTypeAssertionContainer(node: ImportTypeAssertionContainer, clause: AssertClause, multiLine?: boolean): ImportTypeAssertionContainer;
|
||||
createImportAttributes(elements: NodeArray<ImportAttribute>, multiLine?: boolean): ImportAttributes;
|
||||
/** @internal */ createImportAttributes(elements: NodeArray<ImportAttribute>, multiLine?: boolean, token?: ImportAttributes["token"]): ImportAttributes;
|
||||
updateImportAttributes(node: ImportAttributes, elements: NodeArray<ImportAttribute>, multiLine?: boolean): ImportAttributes;
|
||||
createImportAttribute(name: ImportAttributeName, value: Expression): ImportAttribute;
|
||||
updateImportAttribute(node: ImportAttribute, name: ImportAttributeName, value: Expression): ImportAttribute;
|
||||
createNamespaceImport(name: Identifier): NamespaceImport;
|
||||
updateNamespaceImport(node: NamespaceImport, name: Identifier): NamespaceImport;
|
||||
createNamespaceExport(name: Identifier): NamespaceExport;
|
||||
@@ -8619,8 +8643,8 @@ export interface NodeFactory {
|
||||
updateImportSpecifier(node: ImportSpecifier, isTypeOnly: boolean, propertyName: Identifier | undefined, name: Identifier): ImportSpecifier;
|
||||
createExportAssignment(modifiers: readonly ModifierLike[] | undefined, isExportEquals: boolean | undefined, expression: Expression): ExportAssignment;
|
||||
updateExportAssignment(node: ExportAssignment, modifiers: readonly ModifierLike[] | undefined, expression: Expression): ExportAssignment;
|
||||
createExportDeclaration(modifiers: readonly ModifierLike[] | undefined, isTypeOnly: boolean, exportClause: NamedExportBindings | undefined, moduleSpecifier?: Expression, assertClause?: AssertClause): ExportDeclaration;
|
||||
updateExportDeclaration(node: ExportDeclaration, modifiers: readonly ModifierLike[] | undefined, isTypeOnly: boolean, exportClause: NamedExportBindings | undefined, moduleSpecifier: Expression | undefined, assertClause: AssertClause | undefined): ExportDeclaration;
|
||||
createExportDeclaration(modifiers: readonly ModifierLike[] | undefined, isTypeOnly: boolean, exportClause: NamedExportBindings | undefined, moduleSpecifier?: Expression, attributes?: ImportAttributes): ExportDeclaration;
|
||||
updateExportDeclaration(node: ExportDeclaration, modifiers: readonly ModifierLike[] | undefined, isTypeOnly: boolean, exportClause: NamedExportBindings | undefined, moduleSpecifier: Expression | undefined, attributes: ImportAttributes | undefined): ExportDeclaration;
|
||||
createNamedExports(elements: readonly ExportSpecifier[]): NamedExports;
|
||||
updateNamedExports(node: NamedExports, elements: readonly ExportSpecifier[]): NamedExports;
|
||||
createExportSpecifier(isTypeOnly: boolean, propertyName: string | Identifier | undefined, name: string | Identifier): ExportSpecifier;
|
||||
@@ -9657,7 +9681,6 @@ export interface SymbolTracker {
|
||||
trackExternalModuleSymbolOfImportTypeNode?(symbol: Symbol): void;
|
||||
reportNonlocalAugmentation?(containingFile: SourceFile, parentSymbol: Symbol, augmentingSymbol: Symbol): void;
|
||||
reportNonSerializableProperty?(propertyName: string): void;
|
||||
reportImportTypeNodeResolutionModeOverride?(): void;
|
||||
}
|
||||
|
||||
export interface TextSpan {
|
||||
@@ -9750,7 +9773,8 @@ export const enum ListFormat {
|
||||
ObjectBindingPatternElements = SingleLine | AllowTrailingComma | SpaceBetweenBraces | CommaDelimited | SpaceBetweenSiblings | NoSpaceIfEmpty,
|
||||
ArrayBindingPatternElements = SingleLine | AllowTrailingComma | CommaDelimited | SpaceBetweenSiblings | NoSpaceIfEmpty,
|
||||
ObjectLiteralExpressionProperties = PreserveLines | CommaDelimited | SpaceBetweenSiblings | SpaceBetweenBraces | Indented | Braces | NoSpaceIfEmpty,
|
||||
ImportClauseEntries = PreserveLines | CommaDelimited | SpaceBetweenSiblings | SpaceBetweenBraces | Indented | Braces | NoSpaceIfEmpty,
|
||||
ImportAttributes = PreserveLines | CommaDelimited | SpaceBetweenSiblings | SpaceBetweenBraces | Indented | Braces | NoSpaceIfEmpty,
|
||||
/** @deprecated */ ImportClauseEntries = ImportAttributes,
|
||||
ArrayLiteralExpressionElements = PreserveLines | CommaDelimited | SpaceBetweenSiblings | AllowTrailingComma | Indented | SquareBrackets,
|
||||
CommaListElements = CommaDelimited | SpaceBetweenSiblings | SingleLine,
|
||||
CallExpressionArguments = CommaDelimited | SpaceBetweenSiblings | SingleLine | Parenthesis,
|
||||
|
||||
@@ -194,6 +194,7 @@ import {
|
||||
getPathComponents,
|
||||
getPathFromPathComponents,
|
||||
getRelativePathToDirectoryOrUrl,
|
||||
getResolutionModeOverride,
|
||||
getRootLength,
|
||||
getSnippetElement,
|
||||
getStringComparer,
|
||||
@@ -553,7 +554,6 @@ import {
|
||||
VariableDeclarationList,
|
||||
VariableLikeDeclaration,
|
||||
VariableStatement,
|
||||
version,
|
||||
WhileStatement,
|
||||
WithStatement,
|
||||
WrappedExpression,
|
||||
@@ -5975,11 +5975,6 @@ export function getIndentSize() {
|
||||
return indentStrings[1].length;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function isNightly() {
|
||||
return version.includes("-dev") || version.includes("-insiders");
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function createTextWriter(newLine: string): EmitTextWriter {
|
||||
// Why var? It avoids TDZ checks in the runtime which can be costly.
|
||||
@@ -10446,3 +10441,11 @@ export function getPropertyNameFromType(type: StringLiteralType | NumberLiteralT
|
||||
export function isExpandoPropertyDeclaration(declaration: Declaration | undefined): declaration is PropertyAccessExpression | ElementAccessExpression | BinaryExpression {
|
||||
return !!declaration && (isPropertyAccessExpression(declaration) || isElementAccessExpression(declaration) || isBinaryExpression(declaration));
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function hasResolutionModeOverride(node: ImportTypeNode | ImportDeclaration | ExportDeclaration | undefined) {
|
||||
if (node === undefined) {
|
||||
return false;
|
||||
}
|
||||
return !!getResolutionModeOverride(node.attributes);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
ArrayBindingOrAssignmentElement,
|
||||
ArrayBindingOrAssignmentPattern,
|
||||
AssertionExpression,
|
||||
AssertionKey,
|
||||
AssignmentDeclarationKind,
|
||||
AssignmentPattern,
|
||||
AutoAccessorPropertyDeclaration,
|
||||
@@ -92,6 +91,7 @@ import {
|
||||
hasSyntacticModifier,
|
||||
HasType,
|
||||
Identifier,
|
||||
ImportAttributeName,
|
||||
ImportClause,
|
||||
ImportEqualsDeclaration,
|
||||
ImportSpecifier,
|
||||
@@ -1518,14 +1518,14 @@ export function isTypeOnlyImportOrExportDeclaration(node: Node): node is TypeOnl
|
||||
return isTypeOnlyImportDeclaration(node) || isTypeOnlyExportDeclaration(node);
|
||||
}
|
||||
|
||||
export function isAssertionKey(node: Node): node is AssertionKey {
|
||||
return isStringLiteral(node) || isIdentifier(node);
|
||||
}
|
||||
|
||||
export function isStringTextContainingNode(node: Node): node is StringLiteral | TemplateLiteralToken {
|
||||
return node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind);
|
||||
}
|
||||
|
||||
export function isImportAttributeName(node: Node): node is ImportAttributeName {
|
||||
return isStringLiteral(node) || isIdentifier(node);
|
||||
}
|
||||
|
||||
// Identifiers
|
||||
|
||||
/** @internal */
|
||||
|
||||
@@ -12,8 +12,6 @@ import {
|
||||
isArray,
|
||||
isArrayBindingElement,
|
||||
isAssertClause,
|
||||
isAssertEntry,
|
||||
isAssertionKey,
|
||||
isAssertsKeyword,
|
||||
isAsteriskToken,
|
||||
isAwaitKeyword,
|
||||
@@ -42,9 +40,11 @@ import {
|
||||
isHeritageClause,
|
||||
isIdentifier,
|
||||
isIdentifierOrThisTypeNode,
|
||||
isImportAttribute,
|
||||
isImportAttributeName,
|
||||
isImportAttributes,
|
||||
isImportClause,
|
||||
isImportSpecifier,
|
||||
isImportTypeAssertionContainer,
|
||||
isJsxAttributeLike,
|
||||
isJsxAttributeName,
|
||||
isJsxAttributes,
|
||||
@@ -894,7 +894,7 @@ const visitEachChildTable: VisitEachChildTable = {
|
||||
return context.factory.updateImportTypeNode(
|
||||
node,
|
||||
Debug.checkDefined(nodeVisitor(node.argument, visitor, isTypeNode)),
|
||||
nodeVisitor(node.assertions, visitor, isImportTypeAssertionContainer),
|
||||
nodeVisitor(node.attributes, visitor, isImportAttributes),
|
||||
nodeVisitor(node.qualifier, visitor, isEntityName),
|
||||
nodesVisitor(node.typeArguments, visitor, isTypeNode),
|
||||
node.isTypeOf,
|
||||
@@ -1524,22 +1524,22 @@ const visitEachChildTable: VisitEachChildTable = {
|
||||
nodesVisitor(node.modifiers, visitor, isModifierLike),
|
||||
nodeVisitor(node.importClause, visitor, isImportClause),
|
||||
Debug.checkDefined(nodeVisitor(node.moduleSpecifier, visitor, isExpression)),
|
||||
nodeVisitor(node.assertClause, visitor, isAssertClause),
|
||||
nodeVisitor(node.attributes, visitor, isImportAttributes),
|
||||
);
|
||||
},
|
||||
|
||||
[SyntaxKind.AssertClause]: function visitEachChildOfAssertClause(node, visitor, context, nodesVisitor, _nodeVisitor, _tokenVisitor) {
|
||||
return context.factory.updateAssertClause(
|
||||
[SyntaxKind.ImportAttributes]: function visitEachChildOfImportAttributes(node, visitor, context, nodesVisitor, _nodeVisitor, _tokenVisitor) {
|
||||
return context.factory.updateImportAttributes(
|
||||
node,
|
||||
nodesVisitor(node.elements, visitor, isAssertEntry),
|
||||
nodesVisitor(node.elements, visitor, isImportAttribute),
|
||||
node.multiLine,
|
||||
);
|
||||
},
|
||||
|
||||
[SyntaxKind.AssertEntry]: function visitEachChildOfAssertEntry(node, visitor, context, _nodesVisitor, nodeVisitor, _tokenVisitor) {
|
||||
return context.factory.updateAssertEntry(
|
||||
[SyntaxKind.ImportAttribute]: function visitEachChildOfImportAttribute(node, visitor, context, _nodesVisitor, nodeVisitor, _tokenVisitor) {
|
||||
return context.factory.updateImportAttribute(
|
||||
node,
|
||||
Debug.checkDefined(nodeVisitor(node.name, visitor, isAssertionKey)),
|
||||
Debug.checkDefined(nodeVisitor(node.name, visitor, isImportAttributeName)),
|
||||
Debug.checkDefined(nodeVisitor(node.value, visitor, isExpression)),
|
||||
);
|
||||
},
|
||||
@@ -1598,7 +1598,7 @@ const visitEachChildTable: VisitEachChildTable = {
|
||||
node.isTypeOnly,
|
||||
nodeVisitor(node.exportClause, visitor, isNamedExportBindings),
|
||||
nodeVisitor(node.moduleSpecifier, visitor, isExpression),
|
||||
nodeVisitor(node.assertClause, visitor, isAssertClause),
|
||||
nodeVisitor(node.attributes, visitor, isImportAttributes),
|
||||
);
|
||||
},
|
||||
|
||||
|
||||
@@ -1133,6 +1133,7 @@ export namespace Completion {
|
||||
interfaceEntry("ImportMeta"),
|
||||
interfaceEntry("ImportCallOptions"),
|
||||
interfaceEntry("ImportAssertions"),
|
||||
interfaceEntry("ImportAttributes"),
|
||||
varEntry("Math"),
|
||||
varEntry("Date"),
|
||||
interfaceEntry("DateConstructor"),
|
||||
|
||||
Vendored
+9
-1
@@ -620,7 +620,8 @@ interface ImportMeta {
|
||||
* augmented via interface merging.
|
||||
*/
|
||||
interface ImportCallOptions {
|
||||
assert?: ImportAssertions;
|
||||
/** @deprecated*/ assert?: ImportAssertions;
|
||||
with?: ImportAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -630,6 +631,13 @@ interface ImportAssertions {
|
||||
[key: string]: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* The type for the `with` property of the optional second argument to `import()`.
|
||||
*/
|
||||
interface ImportAttributes {
|
||||
[key: string]: string;
|
||||
}
|
||||
|
||||
interface Math {
|
||||
/** The mathematical constant e. This is Euler's number, the base of natural logarithms. */
|
||||
readonly E: number;
|
||||
|
||||
@@ -68,14 +68,14 @@ function fixSingleExportDeclaration(changes: textChanges.ChangeTracker, exportSp
|
||||
/*isTypeOnly*/ false,
|
||||
factory.updateNamedExports(exportClause, filter(exportClause.elements, e => !contains(typeExportSpecifiers, e))),
|
||||
exportDeclaration.moduleSpecifier,
|
||||
/*assertClause*/ undefined,
|
||||
/*attributes*/ undefined,
|
||||
);
|
||||
const typeExportDeclaration = factory.createExportDeclaration(
|
||||
/*modifiers*/ undefined,
|
||||
/*isTypeOnly*/ true,
|
||||
factory.createNamedExports(typeExportSpecifiers),
|
||||
exportDeclaration.moduleSpecifier,
|
||||
/*assertClause*/ undefined,
|
||||
/*attributes*/ undefined,
|
||||
);
|
||||
|
||||
changes.replaceNode(context.sourceFile, exportDeclaration, valueExportDeclaration, {
|
||||
|
||||
@@ -125,13 +125,13 @@ function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, de
|
||||
getSynthesizedDeepClones(declaration.modifiers, /*includeTrivia*/ true),
|
||||
factory.createImportClause(/*isTypeOnly*/ true, getSynthesizedDeepClone(importClause.name, /*includeTrivia*/ true), /*namedBindings*/ undefined),
|
||||
getSynthesizedDeepClone(declaration.moduleSpecifier, /*includeTrivia*/ true),
|
||||
getSynthesizedDeepClone(declaration.assertClause, /*includeTrivia*/ true),
|
||||
getSynthesizedDeepClone(declaration.attributes, /*includeTrivia*/ true),
|
||||
),
|
||||
factory.createImportDeclaration(
|
||||
getSynthesizedDeepClones(declaration.modifiers, /*includeTrivia*/ true),
|
||||
factory.createImportClause(/*isTypeOnly*/ true, /*name*/ undefined, getSynthesizedDeepClone(importClause.namedBindings, /*includeTrivia*/ true)),
|
||||
getSynthesizedDeepClone(declaration.moduleSpecifier, /*includeTrivia*/ true),
|
||||
getSynthesizedDeepClone(declaration.assertClause, /*includeTrivia*/ true),
|
||||
getSynthesizedDeepClone(declaration.attributes, /*includeTrivia*/ true),
|
||||
),
|
||||
]);
|
||||
}
|
||||
@@ -142,7 +142,7 @@ function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, de
|
||||
sameMap(importClause.namedBindings.elements, e => factory.updateImportSpecifier(e, /*isTypeOnly*/ false, e.propertyName, e.name)),
|
||||
)
|
||||
: importClause.namedBindings;
|
||||
const importDeclaration = factory.updateImportDeclaration(declaration, declaration.modifiers, factory.updateImportClause(importClause, /*isTypeOnly*/ true, importClause.name, newNamedBindings), declaration.moduleSpecifier, declaration.assertClause);
|
||||
const importDeclaration = factory.updateImportDeclaration(declaration, declaration.modifiers, factory.updateImportClause(importClause, /*isTypeOnly*/ true, importClause.name, newNamedBindings), declaration.moduleSpecifier, declaration.attributes);
|
||||
changes.replaceNode(sourceFile, declaration, importDeclaration);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,6 @@ function getImportTypeNode(sourceFile: SourceFile, pos: number): ImportTypeNode
|
||||
}
|
||||
|
||||
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, importType: ImportTypeNode) {
|
||||
const newTypeNode = factory.updateImportTypeNode(importType, importType.argument, importType.assertions, importType.qualifier, importType.typeArguments, /*isTypeOf*/ true);
|
||||
const newTypeNode = factory.updateImportTypeNode(importType, importType.argument, importType.attributes, importType.qualifier, importType.typeArguments, /*isTypeOf*/ true);
|
||||
changes.replaceNode(sourceFile, importType, newTypeNode);
|
||||
}
|
||||
|
||||
@@ -184,13 +184,13 @@ function updateExport(changes: textChanges.ChangeTracker, program: Program, sour
|
||||
factory.createNodeArray([...namedExports, ...createExportSpecifiers(names, allowTypeModifier)], /*hasTrailingComma*/ namedExports.hasTrailingComma),
|
||||
),
|
||||
node.moduleSpecifier,
|
||||
node.assertClause,
|
||||
node.attributes,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function createExport(changes: textChanges.ChangeTracker, program: Program, sourceFile: SourceFile, names: ExportName[]) {
|
||||
changes.insertNodeAtEndOfScope(sourceFile, sourceFile, factory.createExportDeclaration(/*modifiers*/ undefined, /*isTypeOnly*/ false, factory.createNamedExports(createExportSpecifiers(names, /*allowTypeModifier*/ getIsolatedModules(program.getCompilerOptions()))), /*moduleSpecifier*/ undefined, /*assertClause*/ undefined));
|
||||
changes.insertNodeAtEndOfScope(sourceFile, sourceFile, factory.createExportDeclaration(/*modifiers*/ undefined, /*isTypeOnly*/ false, factory.createNamedExports(createExportSpecifiers(names, /*allowTypeModifier*/ getIsolatedModules(program.getCompilerOptions()))), /*moduleSpecifier*/ undefined, /*attributes*/ undefined));
|
||||
}
|
||||
|
||||
function createExportSpecifiers(names: ExportName[], allowTypeModifier: boolean) {
|
||||
|
||||
@@ -1655,7 +1655,7 @@ function getNewImports(
|
||||
factory.createNamespaceImport(factory.createIdentifier(namespaceLikeImport.name)),
|
||||
),
|
||||
quotedModuleSpecifier,
|
||||
/*assertClause*/ undefined,
|
||||
/*attributes*/ undefined,
|
||||
);
|
||||
statements = combine(statements, declaration);
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, in
|
||||
statement,
|
||||
defaultImportName && !allowSyntheticDefaults
|
||||
? factory.createImportEqualsDeclaration(/*modifiers*/ undefined, /*isTypeOnly*/ false, defaultImportName, factory.createExternalModuleReference(required))
|
||||
: factory.createImportDeclaration(/*modifiers*/ undefined, factory.createImportClause(/*isTypeOnly*/ false, defaultImportName, namedImports), required, /*assertClause*/ undefined),
|
||||
: factory.createImportDeclaration(/*modifiers*/ undefined, factory.createImportClause(/*isTypeOnly*/ false, defaultImportName, namedImports), required, /*attributes*/ undefined),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ function splitTypeOnlyImport(changes: textChanges.ChangeTracker, importDeclarati
|
||||
importDeclaration.modifiers,
|
||||
factory.updateImportClause(importClause, importClause.isTypeOnly, importClause.name, /*namedBindings*/ undefined),
|
||||
importDeclaration.moduleSpecifier,
|
||||
importDeclaration.assertClause,
|
||||
importDeclaration.attributes,
|
||||
),
|
||||
);
|
||||
|
||||
@@ -64,7 +64,7 @@ function splitTypeOnlyImport(changes: textChanges.ChangeTracker, importDeclarati
|
||||
/*modifiers*/ undefined,
|
||||
factory.updateImportClause(importClause, importClause.isTypeOnly, /*name*/ undefined, importClause.namedBindings),
|
||||
importDeclaration.moduleSpecifier,
|
||||
importDeclaration.assertClause,
|
||||
importDeclaration.attributes,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -269,7 +269,7 @@ function removeUnusedImports(oldImports: readonly ImportDeclaration[], sourceFil
|
||||
importDecl.modifiers,
|
||||
/*importClause*/ undefined,
|
||||
moduleSpecifier,
|
||||
/*assertClause*/ undefined,
|
||||
/*attributes*/ undefined,
|
||||
));
|
||||
}
|
||||
// If we're not in a declaration file, we can't remove the import clause even though
|
||||
@@ -530,7 +530,7 @@ function coalesceExportsWorker(exportGroup: readonly ExportDeclaration[], compar
|
||||
factory.updateNamespaceExport(exportDecl.exportClause, exportDecl.exportClause.name)
|
||||
),
|
||||
exportDecl.moduleSpecifier,
|
||||
exportDecl.assertClause,
|
||||
exportDecl.attributes,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -579,7 +579,7 @@ function updateImportDeclarationAndClause(
|
||||
importDeclaration.modifiers,
|
||||
factory.updateImportClause(importDeclaration.importClause!, importDeclaration.importClause!.isTypeOnly, name, namedBindings), // TODO: GH#18217
|
||||
importDeclaration.moduleSpecifier,
|
||||
importDeclaration.assertClause,
|
||||
importDeclaration.attributes,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
DefaultClause,
|
||||
findChildOfKind,
|
||||
getLeadingCommentRanges,
|
||||
ImportAttributes,
|
||||
isAnyImportSyntax,
|
||||
isArrayLiteralExpression,
|
||||
isBinaryExpression,
|
||||
@@ -300,11 +301,12 @@ function getOutliningSpanForNode(n: Node, sourceFile: SourceFile): OutliningSpan
|
||||
return spanForParenthesizedExpression(n as ParenthesizedExpression);
|
||||
case SyntaxKind.NamedImports:
|
||||
case SyntaxKind.NamedExports:
|
||||
case SyntaxKind.ImportAttributes:
|
||||
case SyntaxKind.AssertClause:
|
||||
return spanForNamedImportsOrExportsOrAssertClause(n as NamedImports | NamedExports | AssertClause);
|
||||
return spanForImportExportElements(n as NamedImports | NamedExports | AssertClause | ImportAttributes);
|
||||
}
|
||||
|
||||
function spanForNamedImportsOrExportsOrAssertClause(node: NamedImports | NamedExports | AssertClause) {
|
||||
function spanForImportExportElements(node: NamedImports | NamedExports | AssertClause | ImportAttributes) {
|
||||
if (!node.elements.length) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -275,7 +275,7 @@ function changeDefaultToNamedImport(importingSourceFile: SourceFile, ref: Identi
|
||||
}
|
||||
case SyntaxKind.ImportType:
|
||||
const importTypeNode = parent as ImportTypeNode;
|
||||
changes.replaceNode(importingSourceFile, parent, factory.createImportTypeNode(importTypeNode.argument, importTypeNode.assertions, factory.createIdentifier(exportName), importTypeNode.typeArguments, importTypeNode.isTypeOf));
|
||||
changes.replaceNode(importingSourceFile, parent, factory.createImportTypeNode(importTypeNode.argument, importTypeNode.attributes, factory.createIdentifier(exportName), importTypeNode.typeArguments, importTypeNode.isTypeOf));
|
||||
break;
|
||||
default:
|
||||
Debug.failBadSyntaxKind(parent);
|
||||
|
||||
@@ -280,5 +280,5 @@ function isExportEqualsModule(moduleSpecifier: Expression, checker: TypeChecker)
|
||||
}
|
||||
|
||||
function updateImport(old: ImportDeclaration, defaultImportName: Identifier | undefined, elements: readonly ImportSpecifier[] | undefined): ImportDeclaration {
|
||||
return factory.createImportDeclaration(/*modifiers*/ undefined, factory.createImportClause(/*isTypeOnly*/ false, defaultImportName, elements && elements.length ? factory.createNamedImports(elements) : undefined), old.moduleSpecifier, /*assertClause*/ undefined);
|
||||
return factory.createImportDeclaration(/*modifiers*/ undefined, factory.createImportClause(/*isTypeOnly*/ false, defaultImportName, elements && elements.length ? factory.createNamedImports(elements) : undefined), old.moduleSpecifier, /*attributes*/ undefined);
|
||||
}
|
||||
|
||||
@@ -479,7 +479,7 @@ function updateNamespaceLikeImportNode(node: SupportedImport, newNamespaceName:
|
||||
/*modifiers*/ undefined,
|
||||
factory.createImportClause(/*isTypeOnly*/ false, /*name*/ undefined, factory.createNamespaceImport(newNamespaceId)),
|
||||
newModuleString,
|
||||
/*assertClause*/ undefined,
|
||||
/*attributes*/ undefined,
|
||||
);
|
||||
case SyntaxKind.ImportEqualsDeclaration:
|
||||
return factory.createImportEqualsDeclaration(/*modifiers*/ undefined, /*isTypeOnly*/ false, newNamespaceId, factory.createExternalModuleReference(newModuleString));
|
||||
@@ -772,7 +772,7 @@ export function filterImport(i: SupportedImport, moduleSpecifier: StringLiteralL
|
||||
const defaultImport = clause.name && keep(clause.name) ? clause.name : undefined;
|
||||
const namedBindings = clause.namedBindings && filterNamedBindings(clause.namedBindings, keep);
|
||||
return defaultImport || namedBindings
|
||||
? factory.createImportDeclaration(/*modifiers*/ undefined, factory.createImportClause(clause.isTypeOnly, defaultImport, namedBindings), getSynthesizedDeepClone(moduleSpecifier), /*assertClause*/ undefined)
|
||||
? factory.createImportDeclaration(/*modifiers*/ undefined, factory.createImportClause(clause.isTypeOnly, defaultImport, namedBindings), getSynthesizedDeepClone(moduleSpecifier), /*attributes*/ undefined)
|
||||
: undefined;
|
||||
}
|
||||
case SyntaxKind.ImportEqualsDeclaration:
|
||||
@@ -1198,7 +1198,7 @@ function moveStatementsToTargetFile(changes: textChanges.ChangeTracker, program:
|
||||
}
|
||||
|
||||
if (length(updatedElements) < length(elements)) {
|
||||
changes.replaceNode(targetFile, exportDeclaration, factory.updateExportDeclaration(exportDeclaration, exportDeclaration.modifiers, exportDeclaration.isTypeOnly, factory.updateNamedExports(exportDeclaration.exportClause, factory.createNodeArray(updatedElements, elements.hasTrailingComma)), exportDeclaration.moduleSpecifier, exportDeclaration.assertClause));
|
||||
changes.replaceNode(targetFile, exportDeclaration, factory.updateExportDeclaration(exportDeclaration, exportDeclaration.modifiers, exportDeclaration.isTypeOnly, factory.updateNamedExports(exportDeclaration.exportClause, factory.createNodeArray(updatedElements, elements.hasTrailingComma)), exportDeclaration.moduleSpecifier, exportDeclaration.attributes));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2501,7 +2501,7 @@ export function makeImport(defaultImport: Identifier | undefined, namedImports:
|
||||
? factory.createImportClause(!!isTypeOnly, defaultImport, namedImports && namedImports.length ? factory.createNamedImports(namedImports) : undefined)
|
||||
: undefined,
|
||||
typeof moduleSpecifier === "string" ? makeStringLiteral(moduleSpecifier, quotePreference) : moduleSpecifier,
|
||||
/*assertClause*/ undefined,
|
||||
/*attributes*/ undefined,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -309,7 +309,7 @@ describe("unittests:: TransformAPI", () => {
|
||||
const exports = [{ name: "x" }];
|
||||
const exportSpecifiers = exports.map(e => ts.factory.createExportSpecifier(/*isTypeOnly*/ false, e.name, e.name));
|
||||
const exportClause = ts.factory.createNamedExports(exportSpecifiers);
|
||||
const newEd = ts.factory.updateExportDeclaration(ed, ed.modifiers, ed.isTypeOnly, exportClause, ed.moduleSpecifier, ed.assertClause);
|
||||
const newEd = ts.factory.updateExportDeclaration(ed, ed.modifiers, ed.isTypeOnly, exportClause, ed.moduleSpecifier, ed.attributes);
|
||||
|
||||
return newEd as ts.Node as T;
|
||||
}
|
||||
@@ -347,7 +347,7 @@ describe("unittests:: TransformAPI", () => {
|
||||
ts.factory.createNamespaceImport(ts.factory.createIdentifier("i0")),
|
||||
),
|
||||
/*moduleSpecifier*/ ts.factory.createStringLiteral("./comp1"),
|
||||
/*assertClause*/ undefined,
|
||||
/*attributes*/ undefined,
|
||||
);
|
||||
return ts.factory.updateSourceFile(sf, [importStar]);
|
||||
}
|
||||
|
||||
@@ -376,6 +376,77 @@ describe("unittests:: tsc-watch:: moduleResolution", () => {
|
||||
],
|
||||
});
|
||||
|
||||
verifyTscWatch({
|
||||
scenario: "moduleResolution",
|
||||
subScenario: "module resolutions from files with partially used import attributes",
|
||||
sys: () =>
|
||||
createWatchedSystem([
|
||||
{
|
||||
path: `/user/username/projects/myproject/tsconfig.json`,
|
||||
content: JSON.stringify({
|
||||
compilerOptions: { moduleResolution: "node16" },
|
||||
}),
|
||||
},
|
||||
{
|
||||
path: `/user/username/projects/myproject/index.ts`,
|
||||
content: Utils.dedent`
|
||||
import type { ImportInterface } from "pkg" with { "resolution-mode": "import" };
|
||||
import type { RequireInterface } from "pkg1" with { "resolution-mode": "require" };
|
||||
import {x} from "./a";
|
||||
`,
|
||||
},
|
||||
{
|
||||
path: `/user/username/projects/myproject/a.ts`,
|
||||
content: Utils.dedent`
|
||||
export const x = 10;
|
||||
`,
|
||||
},
|
||||
{
|
||||
path: `/user/username/projects/myproject/node_modules/pkg/package.json`,
|
||||
content: JSON.stringify({
|
||||
name: "pkg",
|
||||
version: "0.0.1",
|
||||
exports: {
|
||||
import: "./import.js",
|
||||
require: "./require.js",
|
||||
},
|
||||
}),
|
||||
},
|
||||
{
|
||||
path: `/user/username/projects/myproject/node_modules/pkg/import.d.ts`,
|
||||
content: `export interface ImportInterface {}`,
|
||||
},
|
||||
{
|
||||
path: `/user/username/projects/myproject/node_modules/pkg/require.d.ts`,
|
||||
content: `export interface RequireInterface {}`,
|
||||
},
|
||||
{
|
||||
path: `/user/username/projects/myproject/node_modules/pkg1/package.json`,
|
||||
content: JSON.stringify({
|
||||
name: "pkg1",
|
||||
version: "0.0.1",
|
||||
exports: {
|
||||
import: "./import.js",
|
||||
require: "./require.js",
|
||||
},
|
||||
}),
|
||||
},
|
||||
{
|
||||
path: `/user/username/projects/myproject/node_modules/pkg1/import.d.ts`,
|
||||
content: `export interface ImportInterface {}`,
|
||||
},
|
||||
libFile,
|
||||
], { currentDirectory: "/user/username/projects/myproject" }),
|
||||
commandLineArgs: ["-w", "--traceResolution"],
|
||||
edits: [
|
||||
{
|
||||
caption: "modify aFile by adding import",
|
||||
edit: sys => sys.appendFile(`/user/username/projects/myproject/a.ts`, `import type { ImportInterface } from "pkg" with { "resolution-mode": "import" }`),
|
||||
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
verifyTscWatch({
|
||||
scenario: "moduleResolution",
|
||||
subScenario: "type reference resolutions reuse",
|
||||
|
||||
Reference in New Issue
Block a user