Skip to content

Commit

Permalink
Use attributes instead of deprecated assertions (prettier#15758)
Browse files Browse the repository at this point in the history
Co-authored-by: SUZUKI Sosuke <aosukeke@gmail.com>
  • Loading branch information
2 people authored and medikoo committed Feb 16, 2024
1 parent 8959fc5 commit c7c0946
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 28 deletions.
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

0 comments on commit c7c0946

Please sign in to comment.