Skip to content

Commit c97480c

Browse files
fiskersosukesuzuki
andauthored
Use attributes instead of deprecated assertions (#15758)
Co-authored-by: SUZUKI Sosuke <[email protected]>
1 parent 0d1ffb3 commit c97480c

File tree

3 files changed

+13
-28
lines changed

3 files changed

+13
-28
lines changed

src/language-js/print/module.js

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ function shouldPrintAttributes(node, options) {
254254
return false;
255255
}
256256

257-
if (isNonEmptyArray(node.attributes) || isNonEmptyArray(node.assertions)) {
257+
if (isNonEmptyArray(node.attributes)) {
258258
return true;
259259
}
260260

@@ -296,24 +296,16 @@ function getTextWithoutComments(options, start, end) {
296296
return text;
297297
}
298298

299-
function getImportAttributesOrAssertionsKeyword(node, options) {
300-
if (
301-
// Babel parser add this property to indicate the keyword is `assert`
302-
node.extra?.deprecatedAssertSyntax ||
303-
(isNonEmptyArray(node.assertions) && !isNonEmptyArray(node.attributes))
304-
) {
299+
function getImportAttributesKeyword(node, options) {
300+
// Babel parser add this property to indicate the keyword is `assert`
301+
if (node.extra?.deprecatedAssertSyntax) {
305302
return "assert";
306303
}
307304

308-
if (!isNonEmptyArray(node.assertions) && isNonEmptyArray(node.attributes)) {
309-
return "with";
310-
}
311-
312-
const firstAttribute = node.attributes?.[0] ?? node.assertions?.[0];
313305
const textBetweenSourceAndAttributes = getTextWithoutComments(
314306
options,
315307
locEnd(node.source),
316-
firstAttribute ? locStart(firstAttribute) : locEnd(node),
308+
node.attributes?.[0] ? locStart(node.attributes[0]) : locEnd(node),
317309
);
318310

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

326-
/**
327-
* Print Import Attributes syntax.
328-
* If old ImportAssertions syntax is used, print them here.
329-
*/
330318
function printImportAttributes(path, options, print) {
331319
const { node } = path;
332320

333321
if (!shouldPrintAttributes(node, options)) {
334322
return "";
335323
}
336324

337-
const keyword = getImportAttributesOrAssertionsKeyword(node, options);
325+
const keyword = getImportAttributesKeyword(node, options);
338326
/** @type{Doc[]} */
339327
const parts = [` ${keyword} {`];
340328

341-
const property = isNonEmptyArray(node.attributes)
342-
? "attributes"
343-
: isNonEmptyArray(node.assertions)
344-
? "assertions"
345-
: undefined;
346-
if (property) {
329+
if (isNonEmptyArray(node.attributes)) {
347330
if (options.bracketSpacing) {
348331
parts.push(" ");
349332
}
350333

351-
parts.push(join(", ", path.map(print, property)));
334+
parts.push(join(", ", path.map(print, "attributes")));
352335

353336
if (options.bracketSpacing) {
354337
parts.push(" ");

src/language-js/traverse/visitor-keys.evaluate.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ const excludeKeys = {
6161
// Flow parser changed `.types` to `.elementTypes` https://github.com/facebook/flow/commit/5b60e6a81dc277dfab2e88fa3737a4dc9aafdcab
6262
// TupleTypeAnnotation: ["types"],
6363
PropertyDefinition: ["tsModifiers"],
64+
65+
// Legacy property
66+
ExportAllDeclaration: ["assertions"],
67+
ExportNamedDeclaration: ["assertions"],
68+
ImportDeclaration: ["assertions"],
6469
};
6570

6671
const visitorKeys = Object.fromEntries(

tests/unit/__snapshots__/visitor-keys.js.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ exports[`visitor keys estree 1`] = `
273273
"ExportAllDeclaration": [
274274
"source",
275275
"attributes",
276-
"assertions",
277276
"exported",
278277
],
279278
"ExportDefaultDeclaration": [
@@ -287,7 +286,6 @@ exports[`visitor keys estree 1`] = `
287286
"specifiers",
288287
"source",
289288
"attributes",
290-
"assertions",
291289
],
292290
"ExportNamespaceSpecifier": [
293291
"exported",
@@ -366,7 +364,6 @@ exports[`visitor keys estree 1`] = `
366364
"specifiers",
367365
"source",
368366
"attributes",
369-
"assertions",
370367
],
371368
"ImportDefaultSpecifier": [
372369
"local",

0 commit comments

Comments
 (0)