Skip to content

Commit 2e9e9a2

Browse files
authored
feat(53656): Add support for the updated import attributes proposal (#54242)
1 parent b12af0f commit 2e9e9a2

File tree

169 files changed

+32223
-374
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+32223
-374
lines changed

src/compiler/checker.ts

Lines changed: 76 additions & 76 deletions
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@
14721472
"category": "Message",
14731473
"code": 1449
14741474
},
1475-
"Dynamic imports can only accept a module specifier and an optional assertion as arguments": {
1475+
"Dynamic imports can only accept a module specifier and an optional set of attributes as arguments": {
14761476
"category": "Message",
14771477
"code": 1450
14781478
},
@@ -1520,6 +1520,18 @@
15201520
"category": "Message",
15211521
"code": 1461
15221522
},
1523+
"The 'resolution-mode' attribute is only supported when 'moduleResolution' is 'node16' or 'nodenext'.": {
1524+
"category": "Error",
1525+
"code": 1462
1526+
},
1527+
"'resolution-mode' is the only valid key for type import attributes.": {
1528+
"category": "Error",
1529+
"code": 1463
1530+
},
1531+
"Type import attributes should have exactly one key - 'resolution-mode' - with value 'import' or 'require'.": {
1532+
"category": "Error",
1533+
"code": 1464
1534+
},
15231535

15241536
"The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.": {
15251537
"category": "Error",
@@ -1625,6 +1637,10 @@
16251637
"category": "Error",
16261638
"code": 1495
16271639
},
1640+
"Identifier, string literal, or number literal expected.": {
1641+
"category": "Error",
1642+
"code": 1496
1643+
},
16281644

16291645
"The types of '{0}' are incompatible between these types.": {
16301646
"category": "Error",
@@ -3579,6 +3595,10 @@
35793595
"category": "Error",
35803596
"code": 2822
35813597
},
3598+
"Import attributes are only supported when the '--module' option is set to 'esnext' or 'nodenext'.": {
3599+
"category": "Error",
3600+
"code": 2823
3601+
},
35823602
"Cannot find namespace '{0}'. Did you mean '{1}'?": {
35833603
"category": "Error",
35843604
"code": 2833
@@ -3611,10 +3631,6 @@
36113631
"category": "Error",
36123632
"code": 2840
36133633
},
3614-
"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'.": {
3615-
"category": "Error",
3616-
"code": 2841
3617-
},
36183634
"'{0}' is an unused renaming of '{1}'. Did you intend to use it as a type annotation?": {
36193635
"category": "Error",
36203636
"code": 2842
@@ -3667,6 +3683,18 @@
36673683
"category": "Error",
36683684
"code": 2855
36693685
},
3686+
"Import attributes are not allowed on statements that transpile to CommonJS 'require' calls.": {
3687+
"category": "Error",
3688+
"code": 2856
3689+
},
3690+
"Import attributes cannot be used with type-only imports or exports.": {
3691+
"category": "Error",
3692+
"code": 2857
3693+
},
3694+
"Import attribute values must be string literal expressions.": {
3695+
"category": "Error",
3696+
"code": 2858
3697+
},
36703698

36713699
"Import declaration '{0}' is using private name '{1}'.": {
36723700
"category": "Error",
@@ -4100,10 +4128,6 @@
41004128
"category": "Error",
41014129
"code": 4124
41024130
},
4103-
"'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.": {
4104-
"category": "Error",
4105-
"code": 4125
4106-
},
41074131

41084132
"The current host does not support the '{0}' option.": {
41094133
"category": "Error",

src/compiler/emitter.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import {
77
ArrayTypeNode,
88
ArrowFunction,
99
AsExpression,
10-
AssertClause,
11-
AssertEntry,
1210
AwaitExpression,
1311
base64encode,
1412
BigIntLiteral,
@@ -191,6 +189,8 @@ import {
191189
Identifier,
192190
idText,
193191
IfStatement,
192+
ImportAttribute,
193+
ImportAttributes,
194194
ImportClause,
195195
ImportDeclaration,
196196
ImportEqualsDeclaration,
@@ -2055,10 +2055,10 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
20552055
return emitNamedExports(node as NamedExports);
20562056
case SyntaxKind.ExportSpecifier:
20572057
return emitExportSpecifier(node as ExportSpecifier);
2058-
case SyntaxKind.AssertClause:
2059-
return emitAssertClause(node as AssertClause);
2060-
case SyntaxKind.AssertEntry:
2061-
return emitAssertEntry(node as AssertEntry);
2058+
case SyntaxKind.ImportAttributes:
2059+
return emitImportAttributes(node as ImportAttributes);
2060+
case SyntaxKind.ImportAttribute:
2061+
return emitImportAttribute(node as ImportAttribute);
20622062
case SyntaxKind.MissingDeclaration:
20632063
return;
20642064

@@ -2948,16 +2948,16 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
29482948
writeKeyword("import");
29492949
writePunctuation("(");
29502950
emit(node.argument);
2951-
if (node.assertions) {
2951+
if (node.attributes) {
29522952
writePunctuation(",");
29532953
writeSpace();
29542954
writePunctuation("{");
29552955
writeSpace();
2956-
writeKeyword("assert");
2956+
writeKeyword(node.attributes.token === SyntaxKind.AssertKeyword ? "assert" : "with");
29572957
writePunctuation(":");
29582958
writeSpace();
2959-
const elements = node.assertions.assertClause.elements;
2960-
emitList(node.assertions.assertClause, elements, ListFormat.ImportClauseEntries);
2959+
const elements = node.attributes.elements;
2960+
emitList(node.attributes, elements, ListFormat.ImportAttributes);
29612961
writeSpace();
29622962
writePunctuation("}");
29632963
}
@@ -4003,8 +4003,8 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
40034003
writeSpace();
40044004
}
40054005
emitExpression(node.moduleSpecifier);
4006-
if (node.assertClause) {
4007-
emitWithLeadingSpace(node.assertClause);
4006+
if (node.attributes) {
4007+
emitWithLeadingSpace(node.attributes);
40084008
}
40094009
writeTrailingSemicolon();
40104010
}
@@ -4078,20 +4078,20 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
40784078
writeSpace();
40794079
emitExpression(node.moduleSpecifier);
40804080
}
4081-
if (node.assertClause) {
4082-
emitWithLeadingSpace(node.assertClause);
4081+
if (node.attributes) {
4082+
emitWithLeadingSpace(node.attributes);
40834083
}
40844084
writeTrailingSemicolon();
40854085
}
40864086

4087-
function emitAssertClause(node: AssertClause) {
4088-
emitTokenWithComment(SyntaxKind.AssertKeyword, node.pos, writeKeyword, node);
4087+
function emitImportAttributes(node: ImportAttributes) {
4088+
emitTokenWithComment(node.token, node.pos, writeKeyword, node);
40894089
writeSpace();
40904090
const elements = node.elements;
4091-
emitList(node, elements, ListFormat.ImportClauseEntries);
4091+
emitList(node, elements, ListFormat.ImportAttributes);
40924092
}
40934093

4094-
function emitAssertEntry(node: AssertEntry) {
4094+
function emitImportAttribute(node: ImportAttribute) {
40954095
emit(node.name);
40964096
writePunctuation(":");
40974097
writeSpace();

src/compiler/factory/nodeFactory.ts

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ import {
137137
IfStatement,
138138
ImmediatelyInvokedArrowFunction,
139139
ImmediatelyInvokedFunctionExpression,
140+
ImportAttribute,
141+
ImportAttributeName,
142+
ImportAttributes,
140143
ImportClause,
141144
ImportDeclaration,
142145
ImportEqualsDeclaration,
@@ -791,6 +794,10 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
791794
updateAssertEntry,
792795
createImportTypeAssertionContainer,
793796
updateImportTypeAssertionContainer,
797+
createImportAttributes,
798+
updateImportAttributes,
799+
createImportAttribute,
800+
updateImportAttribute,
794801
createNamespaceImport,
795802
updateNamespaceImport,
796803
createNamespaceExport,
@@ -2643,14 +2650,17 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
26432650
// @api
26442651
function createImportTypeNode(
26452652
argument: TypeNode,
2646-
assertions?: ImportTypeAssertionContainer,
2653+
attributes?: ImportAttributes,
26472654
qualifier?: EntityName,
26482655
typeArguments?: readonly TypeNode[],
26492656
isTypeOf = false,
26502657
): ImportTypeNode {
26512658
const node = createBaseNode<ImportTypeNode>(SyntaxKind.ImportType);
26522659
node.argument = argument;
2653-
node.assertions = assertions;
2660+
node.attributes = attributes;
2661+
if (node.assertions && node.assertions.assertClause && node.attributes) {
2662+
(node.assertions as Mutable<ImportTypeAssertionContainer>).assertClause = node.attributes;
2663+
}
26542664
node.qualifier = qualifier;
26552665
node.typeArguments = typeArguments && parenthesizerRules().parenthesizeTypeArguments(typeArguments);
26562666
node.isTypeOf = isTypeOf;
@@ -2662,17 +2672,17 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
26622672
function updateImportTypeNode(
26632673
node: ImportTypeNode,
26642674
argument: TypeNode,
2665-
assertions: ImportTypeAssertionContainer | undefined,
2675+
attributes: ImportAttributes | undefined,
26662676
qualifier: EntityName | undefined,
26672677
typeArguments: readonly TypeNode[] | undefined,
26682678
isTypeOf: boolean = node.isTypeOf,
26692679
): ImportTypeNode {
26702680
return node.argument !== argument
2671-
|| node.assertions !== assertions
2681+
|| node.attributes !== attributes
26722682
|| node.qualifier !== qualifier
26732683
|| node.typeArguments !== typeArguments
26742684
|| node.isTypeOf !== isTypeOf
2675-
? update(createImportTypeNode(argument, assertions, qualifier, typeArguments, isTypeOf), node)
2685+
? update(createImportTypeNode(argument, attributes, qualifier, typeArguments, isTypeOf), node)
26762686
: node;
26772687
}
26782688

@@ -4696,13 +4706,13 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
46964706
modifiers: readonly ModifierLike[] | undefined,
46974707
importClause: ImportClause | undefined,
46984708
moduleSpecifier: Expression,
4699-
assertClause: AssertClause | undefined,
4709+
attributes: ImportAttributes | undefined,
47004710
): ImportDeclaration {
47014711
const node = createBaseNode<ImportDeclaration>(SyntaxKind.ImportDeclaration);
47024712
node.modifiers = asNodeArray(modifiers);
47034713
node.importClause = importClause;
47044714
node.moduleSpecifier = moduleSpecifier;
4705-
node.assertClause = assertClause;
4715+
node.attributes = node.assertClause = attributes;
47064716
node.transformFlags |= propagateChildFlags(node.importClause) |
47074717
propagateChildFlags(node.moduleSpecifier);
47084718
node.transformFlags &= ~TransformFlags.ContainsPossibleTopLevelAwait; // always parsed in an Await context
@@ -4717,13 +4727,13 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
47174727
modifiers: readonly ModifierLike[] | undefined,
47184728
importClause: ImportClause | undefined,
47194729
moduleSpecifier: Expression,
4720-
assertClause: AssertClause | undefined,
4730+
attributes: ImportAttributes | undefined,
47214731
) {
47224732
return node.modifiers !== modifiers
47234733
|| node.importClause !== importClause
47244734
|| node.moduleSpecifier !== moduleSpecifier
4725-
|| node.assertClause !== assertClause
4726-
? update(createImportDeclaration(modifiers, importClause, moduleSpecifier, assertClause), node)
4735+
|| node.attributes !== attributes
4736+
? update(createImportDeclaration(modifiers, importClause, moduleSpecifier, attributes), node)
47274737
: node;
47284738
}
47294739

@@ -4756,6 +4766,7 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
47564766
const node = createBaseNode<AssertClause>(SyntaxKind.AssertClause);
47574767
node.elements = createNodeArray(elements);
47584768
node.multiLine = multiLine;
4769+
node.token = SyntaxKind.AssertKeyword;
47594770
node.transformFlags |= TransformFlags.ContainsESNext;
47604771
return node;
47614772
}
@@ -4801,6 +4812,43 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
48014812
: node;
48024813
}
48034814

4815+
// @api
4816+
function createImportAttributes(elements: readonly ImportAttribute[], multiLine?: boolean): ImportAttributes;
4817+
function createImportAttributes(elements: readonly ImportAttribute[], multiLine?: boolean, token?: ImportAttributes["token"]): ImportAttributes;
4818+
function createImportAttributes(elements: readonly ImportAttribute[], multiLine?: boolean, token?: ImportAttributes["token"]): ImportAttributes {
4819+
const node = createBaseNode<ImportAttributes>(SyntaxKind.ImportAttributes);
4820+
node.token = token ?? SyntaxKind.WithKeyword;
4821+
node.elements = createNodeArray(elements);
4822+
node.multiLine = multiLine;
4823+
node.transformFlags |= TransformFlags.ContainsESNext;
4824+
return node;
4825+
}
4826+
4827+
// @api
4828+
function updateImportAttributes(node: ImportAttributes, elements: readonly ImportAttribute[], multiLine?: boolean): ImportAttributes {
4829+
return node.elements !== elements
4830+
|| node.multiLine !== multiLine
4831+
? update(createImportAttributes(elements, multiLine, node.token), node)
4832+
: node;
4833+
}
4834+
4835+
// @api
4836+
function createImportAttribute(name: ImportAttributeName, value: Expression): ImportAttribute {
4837+
const node = createBaseNode<ImportAttribute>(SyntaxKind.ImportAttribute);
4838+
node.name = name;
4839+
node.value = value;
4840+
node.transformFlags |= TransformFlags.ContainsESNext;
4841+
return node;
4842+
}
4843+
4844+
// @api
4845+
function updateImportAttribute(node: ImportAttribute, name: ImportAttributeName, value: Expression): ImportAttribute {
4846+
return node.name !== name
4847+
|| node.value !== value
4848+
? update(createImportAttribute(name, value), node)
4849+
: node;
4850+
}
4851+
48044852
// @api
48054853
function createNamespaceImport(name: Identifier): NamespaceImport {
48064854
const node = createBaseDeclaration<NamespaceImport>(SyntaxKind.NamespaceImport);
@@ -4908,14 +4956,14 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
49084956
isTypeOnly: boolean,
49094957
exportClause: NamedExportBindings | undefined,
49104958
moduleSpecifier?: Expression,
4911-
assertClause?: AssertClause,
4959+
attributes?: ImportAttributes,
49124960
) {
49134961
const node = createBaseDeclaration<ExportDeclaration>(SyntaxKind.ExportDeclaration);
49144962
node.modifiers = asNodeArray(modifiers);
49154963
node.isTypeOnly = isTypeOnly;
49164964
node.exportClause = exportClause;
49174965
node.moduleSpecifier = moduleSpecifier;
4918-
node.assertClause = assertClause;
4966+
node.attributes = node.assertClause = attributes;
49194967
node.transformFlags |= propagateChildrenFlags(node.modifiers) |
49204968
propagateChildFlags(node.exportClause) |
49214969
propagateChildFlags(node.moduleSpecifier);
@@ -4932,14 +4980,14 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
49324980
isTypeOnly: boolean,
49334981
exportClause: NamedExportBindings | undefined,
49344982
moduleSpecifier: Expression | undefined,
4935-
assertClause: AssertClause | undefined,
4983+
attributes: ImportAttributes | undefined,
49364984
) {
49374985
return node.modifiers !== modifiers
49384986
|| node.isTypeOnly !== isTypeOnly
49394987
|| node.exportClause !== exportClause
49404988
|| node.moduleSpecifier !== moduleSpecifier
4941-
|| node.assertClause !== assertClause
4942-
? finishUpdateExportDeclaration(createExportDeclaration(modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause), node)
4989+
|| node.attributes !== attributes
4990+
? finishUpdateExportDeclaration(createExportDeclaration(modifiers, isTypeOnly, exportClause, moduleSpecifier, attributes), node)
49434991
: node;
49444992
}
49454993

@@ -7083,9 +7131,9 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
70837131
isEnumDeclaration(node) ? updateEnumDeclaration(node, modifierArray, node.name, node.members) :
70847132
isModuleDeclaration(node) ? updateModuleDeclaration(node, modifierArray, node.name, node.body) :
70857133
isImportEqualsDeclaration(node) ? updateImportEqualsDeclaration(node, modifierArray, node.isTypeOnly, node.name, node.moduleReference) :
7086-
isImportDeclaration(node) ? updateImportDeclaration(node, modifierArray, node.importClause, node.moduleSpecifier, node.assertClause) :
7134+
isImportDeclaration(node) ? updateImportDeclaration(node, modifierArray, node.importClause, node.moduleSpecifier, node.attributes) :
70877135
isExportAssignment(node) ? updateExportAssignment(node, modifierArray, node.expression) :
7088-
isExportDeclaration(node) ? updateExportDeclaration(node, modifierArray, node.isTypeOnly, node.exportClause, node.moduleSpecifier, node.assertClause) :
7136+
isExportDeclaration(node) ? updateExportDeclaration(node, modifierArray, node.isTypeOnly, node.exportClause, node.moduleSpecifier, node.attributes) :
70897137
Debug.assertNever(node);
70907138
}
70917139

0 commit comments

Comments
 (0)