Skip to content

Commit

Permalink
Keep empty import attributes and assertions (prettier#15757)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored and medikoo committed Feb 16, 2024
1 parent 15892f1 commit 00c9cf2
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 22 deletions.
16 changes: 16 additions & 0 deletions changelog_unreleased/javascript/15757.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#### Keep empty import attributes and assertions (#15757 by @fisker)

<!-- prettier-ignore -->
```js
// Input
import foo from "foo" with {};
import bar from "bar" assert {};

// Prettier stable
import foo from "foo";
import bar from "bar";

// Prettier main
import foo from "foo" with {};
import bar from "bar" assert {};
```
56 changes: 44 additions & 12 deletions src/language-js/print/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,24 @@ function shouldPrintSpecifiers(node, options) {
return text.trimEnd().endsWith("from");
}

function shouldPrintAttributes(node, options) {
if (!node.source) {
return false;
}

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

const text = getTextWithoutComments(
options,
locEnd(node.source),
locEnd(node),
).trimStart();

return text.startsWith("with") || text.startsWith("assert");
}

function getTextWithoutComments(options, start, end) {
let text = options.originalText.slice(start, end);

Expand Down Expand Up @@ -282,15 +300,20 @@ function getImportAttributesOrAssertionsKeyword(node, options) {
if (
// Babel parser add this property to indicate the keyword is `assert`
node.extra?.deprecatedAssertSyntax ||
!isNonEmptyArray(node.attributes)
(isNonEmptyArray(node.assertions) && !isNonEmptyArray(node.attributes))
) {
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),
locStart(node.attributes[0]),
firstAttribute ? locStart(firstAttribute) : locEnd(node),
);

if (textBetweenSourceAndAttributes.trimStart().startsWith("assert")) {
Expand All @@ -307,24 +330,33 @@ function getImportAttributesOrAssertionsKeyword(node, options) {
function printImportAttributes(path, options, print) {
const { node } = path;

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

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

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

if (!property) {
return "";
parts.push(join(", ", path.map(print, property)));

if (options.bracketSpacing) {
parts.push(" ");
}
}
parts.push("}");

const keyword = getImportAttributesOrAssertionsKeyword(node, options);
return [
` ${keyword} {`,
options.bracketSpacing ? " " : "",
join(", ", path.map(print, property)),
options.bracketSpacing ? " " : "",
"}",
];
return parts;
}

function printModuleSpecifier(path, options, print) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ import * as bar from "bar.json" assert { }
import * as baz from "baz.json" assert { /* comment */ }
=====================================output=====================================
export * as foo from "foo.json";
export * as bar from "bar.json";
export * as baz from "baz.json" /* comment */;
export * as bar from "bar.json" assert {};
export * as baz from "baz.json" /* comment */ assert {};
import * as foo from "foo.json";
import * as bar from "bar.json";
import * as baz from "baz.json" /* comment */;
import * as bar from "bar.json" assert {};
import * as baz from "baz.json" /* comment */ assert {};
================================================================================
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ printWidth: 80
=====================================input======================================
export * as bar from "bar.json" assert { }
=====================================output=====================================
export * as bar from "bar.json";
export * as bar from "bar.json" assert {};
================================================================================
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ import * as baz from "baz.json" with { /* comment */ }
=====================================output=====================================
export * as foo from "foo.json";
export * as bar from "bar.json";
export * as baz from "baz.json" /* comment */;
export * as bar from "bar.json" with {};
export * as baz from "baz.json" /* comment */ with {};
import * as foo from "foo.json";
import * as bar from "bar.json";
import * as baz from "baz.json" /* comment */;
import * as bar from "bar.json" with {};
import * as baz from "baz.json" /* comment */ with {};
================================================================================
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ printWidth: 80
=====================================input======================================
export * as bar from "bar.json" with { }
=====================================output=====================================
export * as bar from "bar.json";
export * as bar from "bar.json" with {};
================================================================================
`;
Expand Down

0 comments on commit 00c9cf2

Please sign in to comment.