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

Use attributes instead of deprecated assertions #15758

Merged
merged 3 commits into from Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 8 additions & 25 deletions src/language-js/print/module.js
Expand Up @@ -254,7 +254,7 @@ function shouldPrintAttributes(node, options) {
return false;
}

if (isNonEmptyArray(node.attributes) || isNonEmptyArray(node.assertions)) {
if (isNonEmptyArray(node.attributes)) {
return true;
}

Expand Down Expand Up @@ -296,24 +296,16 @@ function getTextWithoutComments(options, start, end) {
return text;
}

function getImportAttributesOrAssertionsKeyword(node, options) {
if (
// Babel parser add this property to indicate the keyword is `assert`
node.extra?.deprecatedAssertSyntax ||
(isNonEmptyArray(node.assertions) && !isNonEmptyArray(node.attributes))
) {
function getImportAttributesKeyword(node, options) {
// Babel parser add this property to indicate the keyword is `assert`
if (node.extra?.deprecatedAssertSyntax) {
return "assert";
}

if (!isNonEmptyArray(node.assertions) && isNonEmptyArray(node.attributes)) {
return "with";
}

const firstAttribute = node.attributes?.[0] ?? node.assertions?.[0];
const textBetweenSourceAndAttributes = getTextWithoutComments(
options,
locEnd(node.source),
firstAttribute ? locStart(firstAttribute) : locEnd(node),
node.attributes?.[0] ? locStart(node.attributes[0]) : locEnd(node),
);

if (textBetweenSourceAndAttributes.trimStart().startsWith("assert")) {
Expand All @@ -323,32 +315,23 @@ function getImportAttributesOrAssertionsKeyword(node, options) {
return "with";
}

/**
* Print Import Attributes syntax.
* If old ImportAssertions syntax is used, print them here.
*/
function printImportAttributes(path, options, print) {
const { node } = path;

if (!shouldPrintAttributes(node, options)) {
return "";
}

const keyword = getImportAttributesOrAssertionsKeyword(node, options);
const keyword = getImportAttributesKeyword(node, options);
/** @type{Doc[]} */
const parts = [` ${keyword} {`];

const property = isNonEmptyArray(node.attributes)
? "attributes"
: isNonEmptyArray(node.assertions)
? "assertions"
: undefined;
if (property) {
if (isNonEmptyArray(node.attributes)) {
if (options.bracketSpacing) {
parts.push(" ");
}

parts.push(join(", ", path.map(print, property)));
parts.push(join(", ", path.map(print, "attributes")));

if (options.bracketSpacing) {
parts.push(" ");
Expand Down
5 changes: 5 additions & 0 deletions src/language-js/traverse/visitor-keys.evaluate.js
Expand Up @@ -61,6 +61,11 @@ const excludeKeys = {
// Flow parser changed `.types` to `.elementTypes` https://github.com/facebook/flow/commit/5b60e6a81dc277dfab2e88fa3737a4dc9aafdcab
// TupleTypeAnnotation: ["types"],
PropertyDefinition: ["tsModifiers"],

// Legacy property
ExportAllDeclaration: ["assertions"],
ExportNamedDeclaration: ["assertions"],
ImportDeclaration: ["assertions"],
};

const visitorKeys = Object.fromEntries(
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/__snapshots__/visitor-keys.js.snap
Expand Up @@ -273,7 +273,6 @@ exports[`visitor keys estree 1`] = `
"ExportAllDeclaration": [
"source",
"attributes",
"assertions",
"exported",
],
"ExportDefaultDeclaration": [
Expand All @@ -287,7 +286,6 @@ exports[`visitor keys estree 1`] = `
"specifiers",
"source",
"attributes",
"assertions",
],
"ExportNamespaceSpecifier": [
"exported",
Expand Down Expand Up @@ -366,7 +364,6 @@ exports[`visitor keys estree 1`] = `
"specifiers",
"source",
"attributes",
"assertions",
],
"ImportDefaultSpecifier": [
"local",
Expand Down