Use attributes instead of deprecated assertions (#15758)

Co-authored-by: SUZUKI Sosuke <aosukeke@gmail.com>
This commit is contained in:
fisker CheungandSUZUKI Sosuke authored and GitHub committed 2023-12-10 17:13:03 +09:00
1 parent 0d1ffb3a91
commit c97480c9a9
3 files changed
+13 -28

No files matched your search

+8 -25
View File
@@ -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(" ");
@@ -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(
@@ -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",