From c97480c9a96fedf32ae52d58796eaeb3137ad2dd Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 10 Dec 2023 16:13:03 +0800 Subject: [PATCH] Use `attributes` instead of deprecated `assertions` (#15758) Co-authored-by: SUZUKI Sosuke --- src/language-js/print/module.js | 33 +++++-------------- .../traverse/visitor-keys.evaluate.js | 5 +++ tests/unit/__snapshots__/visitor-keys.js.snap | 3 -- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/src/language-js/print/module.js b/src/language-js/print/module.js index 65d37cf4e..fc36cdc13 100644 --- a/src/language-js/print/module.js +++ b/src/language-js/print/module.js @@ -254,7 +254,7 @@ function shouldPrintAttributes(node, options) { return false; } - if (isNonEmptyArray(node.attributes) || isNonEmptyArray(node.assertions)) { + if (isNonEmptyArray(node.attributes)) { return true; } @@ -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")) { @@ -323,10 +315,6 @@ 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; @@ -334,21 +322,16 @@ function printImportAttributes(path, options, print) { 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(" "); diff --git a/src/language-js/traverse/visitor-keys.evaluate.js b/src/language-js/traverse/visitor-keys.evaluate.js index 7e28c32ae..92ce80a1c 100644 --- a/src/language-js/traverse/visitor-keys.evaluate.js +++ b/src/language-js/traverse/visitor-keys.evaluate.js @@ -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( diff --git a/tests/unit/__snapshots__/visitor-keys.js.snap b/tests/unit/__snapshots__/visitor-keys.js.snap index 5b759cf2f..45be1cfc1 100644 --- a/tests/unit/__snapshots__/visitor-keys.js.snap +++ b/tests/unit/__snapshots__/visitor-keys.js.snap @@ -273,7 +273,6 @@ exports[`visitor keys estree 1`] = ` "ExportAllDeclaration": [ "source", "attributes", - "assertions", "exported", ], "ExportDefaultDeclaration": [ @@ -287,7 +286,6 @@ exports[`visitor keys estree 1`] = ` "specifiers", "source", "attributes", - "assertions", ], "ExportNamespaceSpecifier": [ "exported", @@ -366,7 +364,6 @@ exports[`visitor keys estree 1`] = ` "specifiers", "source", "attributes", - "assertions", ], "ImportDefaultSpecifier": [ "local",