@@ -933,6 +933,7 @@ namespace ts {
933
933
let deferredGlobalTemplateStringsArrayType: ObjectType | undefined;
934
934
let deferredGlobalImportMetaType: ObjectType;
935
935
let deferredGlobalImportMetaExpressionType: ObjectType;
936
+ let deferredGlobalImportCallOptionsType: ObjectType | undefined;
936
937
let deferredGlobalExtractSymbol: Symbol | undefined;
937
938
let deferredGlobalOmitSymbol: Symbol | undefined;
938
939
let deferredGlobalAwaitedSymbol: Symbol | undefined;
@@ -6568,7 +6569,7 @@ namespace ts {
6568
6569
6569
6570
function inlineExportModifiers(statements: Statement[]) {
6570
6571
// Pass 3: Move all `export {}`'s to `export` modifiers where possible
6571
- const index = findIndex(statements, d => isExportDeclaration(d) && !d.moduleSpecifier && !!d.exportClause && isNamedExports(d.exportClause));
6572
+ const index = findIndex(statements, d => isExportDeclaration(d) && !d.moduleSpecifier && !d.assertClause && ! !d.exportClause && isNamedExports(d.exportClause));
6572
6573
if (index >= 0) {
6573
6574
const exportDecl = statements[index] as ExportDeclaration & { readonly exportClause: NamedExports };
6574
6575
const replacements = mapDefined(exportDecl.exportClause.elements, e => {
@@ -6600,7 +6601,8 @@ namespace ts {
6600
6601
exportDecl.exportClause,
6601
6602
replacements
6602
6603
),
6603
- exportDecl.moduleSpecifier
6604
+ exportDecl.moduleSpecifier,
6605
+ exportDecl.assertClause
6604
6606
);
6605
6607
}
6606
6608
}
@@ -7260,7 +7262,8 @@ namespace ts {
7260
7262
propertyName && isIdentifier(propertyName) ? factory.createIdentifier(idText(propertyName)) : undefined,
7261
7263
factory.createIdentifier(localName)
7262
7264
)])),
7263
- factory.createStringLiteral(specifier)
7265
+ factory.createStringLiteral(specifier),
7266
+ /*importClause*/ undefined
7264
7267
), ModifierFlags.None);
7265
7268
break;
7266
7269
}
@@ -7336,15 +7339,17 @@ namespace ts {
7336
7339
// We use `target.parent || target` below as `target.parent` is unset when the target is a module which has been export assigned
7337
7340
// And then made into a default by the `esModuleInterop` or `allowSyntheticDefaultImports` flag
7338
7341
// In such cases, the `target` refers to the module itself already
7339
- factory.createStringLiteral(getSpecifierForModuleSymbol(target.parent || target, context))
7342
+ factory.createStringLiteral(getSpecifierForModuleSymbol(target.parent || target, context)),
7343
+ /*assertClause*/ undefined
7340
7344
), ModifierFlags.None);
7341
7345
break;
7342
7346
case SyntaxKind.NamespaceImport:
7343
7347
addResult(factory.createImportDeclaration(
7344
7348
/*decorators*/ undefined,
7345
7349
/*modifiers*/ undefined,
7346
7350
factory.createImportClause(/*isTypeOnly*/ false, /*importClause*/ undefined, factory.createNamespaceImport(factory.createIdentifier(localName))),
7347
- factory.createStringLiteral(getSpecifierForModuleSymbol(target, context))
7351
+ factory.createStringLiteral(getSpecifierForModuleSymbol(target, context)),
7352
+ /*assertClause*/ undefined
7348
7353
), ModifierFlags.None);
7349
7354
break;
7350
7355
case SyntaxKind.NamespaceExport:
@@ -7369,7 +7374,8 @@ namespace ts {
7369
7374
factory.createIdentifier(localName)
7370
7375
)
7371
7376
])),
7372
- factory.createStringLiteral(getSpecifierForModuleSymbol(target.parent || target, context))
7377
+ factory.createStringLiteral(getSpecifierForModuleSymbol(target.parent || target, context)),
7378
+ /*assertClause*/ undefined
7373
7379
), ModifierFlags.None);
7374
7380
break;
7375
7381
case SyntaxKind.ExportSpecifier:
@@ -13455,6 +13461,10 @@ namespace ts {
13455
13461
return deferredGlobalImportMetaExpressionType;
13456
13462
}
13457
13463
13464
+ function getGlobalImportCallOptionsType(reportErrors: boolean) {
13465
+ return (deferredGlobalImportCallOptionsType ||= getGlobalType("ImportCallOptions" as __String, /*arity*/ 0, reportErrors)) || emptyObjectType;
13466
+ }
13467
+
13458
13468
function getGlobalESSymbolConstructorSymbol(reportErrors: boolean): Symbol | undefined {
13459
13469
return deferredGlobalESSymbolConstructorSymbol ||= getGlobalValueSymbol("Symbol" as __String, reportErrors);
13460
13470
}
@@ -25547,6 +25557,12 @@ namespace ts {
25547
25557
}
25548
25558
25549
25559
function getContextualTypeForArgumentAtIndex(callTarget: CallLikeExpression, argIndex: number): Type {
25560
+ if (isImportCall(callTarget)) {
25561
+ return argIndex === 0 ? stringType :
25562
+ argIndex === 1 ? getGlobalImportCallOptionsType(/*reportErrors*/ false) :
25563
+ anyType;
25564
+ }
25565
+
25550
25566
// If we're already in the process of resolving the given signature, don't resolve again as
25551
25567
// that could cause infinite recursion. Instead, return anySignature.
25552
25568
const signature = getNodeLinks(callTarget).resolvedSignature === resolvingSignature ? resolvingSignature : getResolvedSignature(callTarget);
@@ -26007,10 +26023,6 @@ namespace ts {
26007
26023
case SyntaxKind.AwaitExpression:
26008
26024
return getContextualTypeForAwaitOperand(parent as AwaitExpression, contextFlags);
26009
26025
case SyntaxKind.CallExpression:
26010
- if ((parent as CallExpression).expression.kind === SyntaxKind.ImportKeyword) {
26011
- return stringType;
26012
- }
26013
- /* falls through */
26014
26026
case SyntaxKind.NewExpression:
26015
26027
return getContextualTypeForArgument(parent as CallExpression | NewExpression, node);
26016
26028
case SyntaxKind.TypeAssertionExpression:
@@ -30606,17 +30618,26 @@ namespace ts {
30606
30618
if (node.arguments.length === 0) {
30607
30619
return createPromiseReturnType(node, anyType);
30608
30620
}
30621
+
30609
30622
const specifier = node.arguments[0];
30610
30623
const specifierType = checkExpressionCached(specifier);
30624
+ const optionsType = node.arguments.length > 1 ? checkExpressionCached(node.arguments[1]) : undefined;
30611
30625
// Even though multiple arguments is grammatically incorrect, type-check extra arguments for completion
30612
- for (let i = 1 ; i < node.arguments.length; ++i) {
30626
+ for (let i = 2 ; i < node.arguments.length; ++i) {
30613
30627
checkExpressionCached(node.arguments[i]);
30614
30628
}
30615
30629
30616
30630
if (specifierType.flags & TypeFlags.Undefined || specifierType.flags & TypeFlags.Null || !isTypeAssignableTo(specifierType, stringType)) {
30617
30631
error(specifier, Diagnostics.Dynamic_import_s_specifier_must_be_of_type_string_but_here_has_type_0, typeToString(specifierType));
30618
30632
}
30619
30633
30634
+ if (optionsType) {
30635
+ const importCallOptionsType = getGlobalImportCallOptionsType(/*reportErrors*/ true);
30636
+ if (importCallOptionsType !== emptyObjectType) {
30637
+ checkTypeAssignableTo(optionsType, getNullableType(importCallOptionsType, TypeFlags.Undefined), node.arguments[1]);
30638
+ }
30639
+ }
30640
+
30620
30641
// resolveExternalModuleName will return undefined if the moduleReferenceExpression is not a string literal
30621
30642
const moduleSymbol = resolveExternalModuleName(node, specifier);
30622
30643
if (moduleSymbol) {
@@ -39039,6 +39060,18 @@ namespace ts {
39039
39060
}
39040
39061
}
39041
39062
39063
+ function checkAssertClause(declaration: ImportDeclaration | ExportDeclaration) {
39064
+ if (declaration.assertClause) {
39065
+ if (moduleKind !== ModuleKind.ESNext) {
39066
+ return grammarErrorOnNode(declaration.assertClause, Diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext);
39067
+ }
39068
+
39069
+ if (isImportDeclaration(declaration) ? declaration.importClause?.isTypeOnly : declaration.isTypeOnly) {
39070
+ return grammarErrorOnNode(declaration.assertClause, Diagnostics.Import_assertions_cannot_be_used_with_type_only_imports_or_exports);
39071
+ }
39072
+ }
39073
+ }
39074
+
39042
39075
function checkImportDeclaration(node: ImportDeclaration) {
39043
39076
if (checkGrammarModuleElementContext(node, Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) {
39044
39077
// If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
@@ -39070,7 +39103,7 @@ namespace ts {
39070
39103
}
39071
39104
}
39072
39105
}
39073
-
39106
+ checkAssertClause(node);
39074
39107
}
39075
39108
39076
39109
function checkImportEqualsDeclaration(node: ImportEqualsDeclaration) {
@@ -39165,6 +39198,7 @@ namespace ts {
39165
39198
}
39166
39199
}
39167
39200
}
39201
+ checkAssertClause(node);
39168
39202
}
39169
39203
39170
39204
function checkGrammarExportDeclaration(node: ExportDeclaration): boolean {
@@ -43201,14 +43235,25 @@ namespace ts {
43201
43235
}
43202
43236
43203
43237
const nodeArguments = node.arguments;
43204
- if (nodeArguments.length !== 1) {
43205
- return grammarErrorOnNode(node, Diagnostics.Dynamic_import_must_have_one_specifier_as_an_argument);
43238
+ if (moduleKind !== ModuleKind.ESNext) {
43239
+ // We are allowed trailing comma after proposal-import-assertions.
43240
+ checkGrammarForDisallowedTrailingComma(nodeArguments);
43241
+
43242
+ if (nodeArguments.length > 1) {
43243
+ const assertionArgument = nodeArguments[1];
43244
+ return grammarErrorOnNode(assertionArgument, Diagnostics.Dynamic_imports_only_support_a_second_argument_when_the_module_option_is_set_to_esnext);
43245
+ }
43206
43246
}
43207
- checkGrammarForDisallowedTrailingComma(nodeArguments);
43247
+
43248
+ if (nodeArguments.length === 0 || nodeArguments.length > 2) {
43249
+ return grammarErrorOnNode(node, Diagnostics.Dynamic_imports_can_only_accept_a_module_specifier_and_an_optional_assertion_as_arguments);
43250
+ }
43251
+
43208
43252
// see: parseArgumentOrArrayLiteralElement...we use this function which parse arguments of callExpression to parse specifier for dynamic import.
43209
43253
// parseArgumentOrArrayLiteralElement allows spread element to be in an argument list which is not allowed as specifier in dynamic import.
43210
- if (isSpreadElement(nodeArguments[0])) {
43211
- return grammarErrorOnNode(nodeArguments[0], Diagnostics.Specifier_of_dynamic_import_cannot_be_spread_element);
43254
+ const spreadElement = find(nodeArguments, isSpreadElement);
43255
+ if (spreadElement) {
43256
+ return grammarErrorOnNode(spreadElement, Diagnostics.Argument_of_dynamic_import_cannot_be_spread_element);
43212
43257
}
43213
43258
return false;
43214
43259
}
0 commit comments