Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(53656): Add support for the updated import attributes proposal #54242

Merged
merged 23 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7a05f24
feat(53656): Add support for the revised import attributes proposal
a-tarasyuk Aug 25, 2023
6489c39
update diagnostic message
a-tarasyuk Aug 25, 2023
4c8bad1
update baseline
a-tarasyuk Aug 25, 2023
1a0fb93
mark util function as internal
a-tarasyuk Aug 26, 2023
642667e
emit import attributes
a-tarasyuk Aug 26, 2023
fc2093c
update baseline
a-tarasyuk Aug 26, 2023
4a16bf2
add deprecation diagnostic message
a-tarasyuk Aug 29, 2023
dbd7eb5
update baseline
a-tarasyuk Aug 29, 2023
bebf687
update diagnostic message
a-tarasyuk Sep 12, 2023
a387700
remove the nightly-only restriction of resolution mode override
a-tarasyuk Sep 14, 2023
78a01bd
Merge branch 'main' of https://github.com/microsoft/TypeScript into f…
a-tarasyuk Sep 14, 2023
0113dfe
remove unused diagnostic message
a-tarasyuk Sep 14, 2023
14c6226
AssertClause -> ImportAttributes
a-tarasyuk Sep 21, 2023
2569ec3
fix formatting
a-tarasyuk Sep 21, 2023
8482633
fix tests
a-tarasyuk Sep 21, 2023
ba1f979
baseline update
a-tarasyuk Sep 21, 2023
11d556a
remove token param from public API
a-tarasyuk Sep 21, 2023
e0afc4a
fix factory api signatures
a-tarasyuk Sep 21, 2023
3d3939a
update diagnostic messages
a-tarasyuk Sep 22, 2023
bf36343
rid of ImportTypeAttributes interface
a-tarasyuk Sep 22, 2023
a990ddd
remove deprecation diagnostic message
a-tarasyuk Sep 22, 2023
de897d4
update baseline
a-tarasyuk Sep 22, 2023
82e078f
Merge branch 'main' of https://github.com/microsoft/TypeScript into f…
a-tarasyuk Sep 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 47 additions & 87 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

14 changes: 1 addition & 13 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"category": "Error",
"code": 1007
},
"The 'assert' keyword in import attributes is deprecated and it has been replaced by the 'with' keyword.": {
"The 'assert' keyword in import attributes is deprecated and has been replaced by the 'with' keyword.": {
"category": "Error",
"code": 1008
},
Expand Down Expand Up @@ -3635,10 +3635,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
Expand Down Expand Up @@ -4132,14 +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
},
"'resolution-mode' attributes are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.": {
"category": "Error",
"code": 4126
},

"The current host does not support the '{0}' option.": {
"category": "Error",
Expand Down
52 changes: 6 additions & 46 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
ArrayTypeNode,
ArrowFunction,
AsExpression,
AssertClause,
AssertEntry,
AwaitExpression,
base64encode,
BigIntLiteral,
Expand Down Expand Up @@ -2055,10 +2053,6 @@ 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:
Expand Down Expand Up @@ -2952,25 +2946,12 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
writeKeyword("import");
writePunctuation("(");
emit(node.argument);
if (node.assertions) {
writePunctuation(",");
writeSpace();
writePunctuation("{");
writeSpace();
writeKeyword("assert");
writePunctuation(":");
writeSpace();
const elements = node.assertions.assertClause.elements;
emitList(node.assertions.assertClause, elements, ListFormat.ImportClauseEntries);
writeSpace();
writePunctuation("}");
}
if (node.attributes) {
writePunctuation(",");
writeSpace();
writePunctuation("{");
writeSpace();
writeKeyword("with");
emitTokenWithComment(node.attributes.attributes.token, node.pos, writeKeyword, node);
writePunctuation(":");
writeSpace();
const elements = node.attributes.attributes.elements;
Expand Down Expand Up @@ -4009,8 +3990,8 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
writeSpace();
}
emitExpression(node.moduleSpecifier);
if (node.attributes || node.assertClause) {
emitWithLeadingSpace(node.attributes || node.assertClause);
if (node.attributes) {
emitWithLeadingSpace(node.attributes);
}
writeTrailingSemicolon();
}
Expand Down Expand Up @@ -4084,35 +4065,14 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
writeSpace();
emitExpression(node.moduleSpecifier);
}
if (node.attributes || node.assertClause) {
emitWithLeadingSpace(node.attributes || node.assertClause);
if (node.attributes) {
emitWithLeadingSpace(node.attributes);
}
writeTrailingSemicolon();
}

function emitAssertClause(node: AssertClause) {
emitTokenWithComment(SyntaxKind.AssertKeyword, node.pos, writeKeyword, node);
writeSpace();
const elements = node.elements;
emitList(node, elements, ListFormat.ImportClauseEntries);
}

function emitAssertEntry(node: AssertEntry) {
emit(node.name);
writePunctuation(":");
writeSpace();

const value = node.value;
/** @see {emitPropertyAssignment} */
if ((getEmitFlags(value) & EmitFlags.NoLeadingComments) === 0) {
const commentRange = getCommentRange(value);
emitTrailingCommentsOfPosition(commentRange.pos);
}
emit(value);
}

function emitImportAttributes(node: ImportAttributes) {
emitTokenWithComment(SyntaxKind.WithKeyword, node.pos, writeKeyword, node);
emitTokenWithComment(node.token, node.pos, writeKeyword, node);
writeSpace();
const elements = node.elements;
emitList(node, elements, ListFormat.ImportAttributes);
Expand Down
53 changes: 24 additions & 29 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2651,18 +2651,18 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
}

// @api
function createImportTypeNode(argument: TypeNode, assertions?: ImportTypeAssertionContainer, qualifier?: EntityName, typeArguments?: readonly TypeNode[], isTypeOf?: boolean): ImportTypeNode;
function createImportTypeNode(argument: TypeNode, attributes?: ImportTypeAttributes, qualifier?: EntityName, typeArguments?: readonly TypeNode[], isTypeOf?: boolean): ImportTypeNode;
function createImportTypeNode(
argument: TypeNode,
assertions?: ImportTypeAssertionContainer,
attributes?: ImportTypeAttributes,
attributes?: ImportTypeAssertionContainer | ImportTypeAttributes,
qualifier?: EntityName,
typeArguments?: readonly TypeNode[],
isTypeOf = false,
): ImportTypeNode {
const node = createBaseNode<ImportTypeNode>(SyntaxKind.ImportType);
node.argument = argument;
node.assertions = assertions;
node.attributes = attributes;
node.attributes = node.assertions = attributes;
node.qualifier = qualifier;
node.typeArguments = typeArguments && parenthesizerRules().parenthesizeTypeArguments(typeArguments);
node.isTypeOf = isTypeOf;
Expand All @@ -2671,22 +2671,23 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
}

// @api
function updateImportTypeNode(node: ImportTypeNode, argument: TypeNode, assertions: ImportTypeAssertionContainer | undefined, qualifier: EntityName | undefined, typeArguments: readonly TypeNode[] | undefined, isTypeOf?: boolean | undefined): ImportTypeNode;
function updateImportTypeNode(node: ImportTypeNode, argument: TypeNode, attributes: ImportTypeAttributes | undefined, qualifier: EntityName | undefined, typeArguments: readonly TypeNode[] | undefined, isTypeOf?: boolean | undefined): ImportTypeNode;
function updateImportTypeNode(
node: ImportTypeNode,
argument: TypeNode,
assertions: ImportTypeAssertionContainer | undefined,
attributes: ImportTypeAttributes | undefined,
attributes: ImportTypeAssertionContainer | ImportTypeAttributes | undefined,
qualifier: EntityName | undefined,
typeArguments: readonly TypeNode[] | undefined,
isTypeOf: boolean = node.isTypeOf,
): ImportTypeNode {
return node.argument !== argument
|| node.assertions !== assertions
|| node.assertions !== attributes
|| node.attributes !== attributes
|| node.qualifier !== qualifier
|| node.typeArguments !== typeArguments
|| node.isTypeOf !== isTypeOf
? update(createImportTypeNode(argument, assertions, attributes, qualifier, typeArguments, isTypeOf), node)
? update(createImportTypeNode(argument, attributes, qualifier, typeArguments, isTypeOf), node)
: node;
}

Expand Down Expand Up @@ -4710,15 +4711,13 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
modifiers: readonly ModifierLike[] | undefined,
importClause: ImportClause | undefined,
moduleSpecifier: Expression,
assertClause: AssertClause | undefined,
attributes: ImportAttributes | undefined,
attributes: ImportAttributes | AssertClause | undefined,
): ImportDeclaration {
const node = createBaseNode<ImportDeclaration>(SyntaxKind.ImportDeclaration);
node.modifiers = asNodeArray(modifiers);
node.importClause = importClause;
node.moduleSpecifier = moduleSpecifier;
node.assertClause = assertClause;
node.attributes = attributes;
node.attributes = node.assertClause = attributes;
node.transformFlags |= propagateChildFlags(node.importClause) |
propagateChildFlags(node.moduleSpecifier);
node.transformFlags &= ~TransformFlags.ContainsPossibleTopLevelAwait; // always parsed in an Await context
Expand All @@ -4733,15 +4732,13 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
modifiers: readonly ModifierLike[] | undefined,
importClause: ImportClause | undefined,
moduleSpecifier: Expression,
assertClause: AssertClause | undefined,
attributes: ImportAttributes | undefined,
attributes: ImportAttributes | AssertClause | undefined,
) {
return node.modifiers !== modifiers
|| node.importClause !== importClause
|| node.moduleSpecifier !== moduleSpecifier
|| node.assertClause !== assertClause
|| node.attributes !== attributes
? update(createImportDeclaration(modifiers, importClause, moduleSpecifier, assertClause, attributes), node)
? update(createImportDeclaration(modifiers, importClause, moduleSpecifier, attributes), node)
: node;
}

Expand Down Expand Up @@ -4774,6 +4771,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;
}
Expand Down Expand Up @@ -4822,7 +4820,7 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
// @api
function createImportTypeAttributes(attributes: ImportAttributes, multiLine?: boolean): ImportTypeAttributes {
const node = createBaseNode<ImportTypeAttributes>(SyntaxKind.ImportTypeAttributes);
node.attributes = attributes;
node.attributes = node.assertClause = attributes;
node.multiLine = multiLine;
return node;
}
Expand All @@ -4836,8 +4834,9 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
}

// @api
function createImportAttributes(elements: readonly ImportAttribute[], multiLine?: boolean): ImportAttributes {
function createImportAttributes(token: ImportAttributes["token"], elements: readonly ImportAttribute[], multiLine?: boolean): ImportAttributes {
const node = createBaseNode<ImportAttributes>(SyntaxKind.ImportAttributes);
node.token = token;
node.elements = createNodeArray(elements);
node.multiLine = multiLine;
node.transformFlags |= TransformFlags.ContainsESNext;
Expand All @@ -4848,7 +4847,7 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
function updateImportAttributes(node: ImportAttributes, elements: readonly ImportAttribute[], multiLine?: boolean): ImportAttributes {
return node.elements !== elements
|| node.multiLine !== multiLine
? update(createImportAttributes(elements, multiLine), node)
? update(createImportAttributes(node.token, elements, multiLine), node)
: node;
}

Expand Down Expand Up @@ -4976,16 +4975,14 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
isTypeOnly: boolean,
exportClause: NamedExportBindings | undefined,
moduleSpecifier?: Expression,
assertClause?: AssertClause,
attributes?: ImportAttributes,
attributes?: AssertClause | 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 = attributes;
node.attributes = node.assertClause = attributes;
node.transformFlags |= propagateChildrenFlags(node.modifiers) |
propagateChildFlags(node.exportClause) |
propagateChildFlags(node.moduleSpecifier);
Expand All @@ -5002,16 +4999,14 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
isTypeOnly: boolean,
exportClause: NamedExportBindings | undefined,
moduleSpecifier: Expression | undefined,
assertClause: AssertClause | undefined,
attributes: ImportAttributes | undefined,
attributes: AssertClause | ImportAttributes | undefined,
) {
return node.modifiers !== modifiers
|| node.isTypeOnly !== isTypeOnly
|| node.exportClause !== exportClause
|| node.moduleSpecifier !== moduleSpecifier
|| node.assertClause !== assertClause
|| node.attributes !== attributes
? finishUpdateExportDeclaration(createExportDeclaration(modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause, attributes), node)
? finishUpdateExportDeclaration(createExportDeclaration(modifiers, isTypeOnly, exportClause, moduleSpecifier, attributes), node)
: node;
}

Expand Down Expand Up @@ -7156,9 +7151,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, node.attributes) :
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, node.attributes) :
isExportDeclaration(node) ? updateExportDeclaration(node, modifierArray, node.isTypeOnly, node.exportClause, node.moduleSpecifier, node.attributes) :
Debug.assertNever(node);
}

Expand Down
1 change: 0 additions & 1 deletion src/compiler/factory/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,6 @@ 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);
Expand Down
Loading