Skip to content

Commit

Permalink
Keep curly braces and from keyword in empty import statements (pr…
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored and medikoo committed Feb 16, 2024
1 parent dfc0ac1 commit 15892f1
Show file tree
Hide file tree
Showing 6 changed files with 385 additions and 14 deletions.
16 changes: 16 additions & 0 deletions changelog_unreleased/javascript/15756.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#### Keep curly braces and `from` keyword in empty `import` statements (#15756 by @fisker)

<!-- prettier-ignore -->
```js
// Input
import { } from 'foo';
import { /* comment */ } from 'bar';

// Prettier stable
import {} from "foo";
import /* comment */ "bar";

// Prettier main
import {} from "foo";
import {} from /* comment */ "bar";
```
29 changes: 15 additions & 14 deletions src/language-js/print/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function printModuleSource(path, options, print) {

/** @type{Doc[]} */
const parts = [];
if (!shouldNotPrintSpecifiers(node, options)) {
if (shouldPrintSpecifiers(node, options)) {
parts.push(" from");
}
parts.push(" ", print("source"));
Expand All @@ -159,7 +159,7 @@ function printModuleSource(path, options, print) {
function printModuleSpecifiers(path, options, print) {
const { node } = path;

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

Expand Down Expand Up @@ -231,21 +231,22 @@ function printModuleSpecifiers(path, options, print) {
return parts;
}

function shouldNotPrintSpecifiers(node, options) {
const { type, importKind, source, specifiers } = node;

function shouldPrintSpecifiers(node, options) {
if (
type !== "ImportDeclaration" ||
isNonEmptyArray(specifiers) ||
importKind === "type"
node.type !== "ImportDeclaration" ||
isNonEmptyArray(node.specifiers) ||
node.importKind === "type"
) {
return false;
return true;
}

// TODO: check tokens
return !/{\s*}/.test(
options.originalText.slice(locStart(node), locStart(source)),
const text = getTextWithoutComments(
options,
locStart(node),
locStart(node.source),
);

return text.trimEnd().endsWith("from");
}

function getTextWithoutComments(options, start, end) {
Expand Down Expand Up @@ -288,8 +289,8 @@ function getImportAttributesOrAssertionsKeyword(node, options) {

const textBetweenSourceAndAttributes = getTextWithoutComments(
options,
node.source.range[1],
node.attributes[0].range[0],
locEnd(node.source),
locStart(node.attributes[0]),
);

if (textBetweenSourceAndAttributes.trimStart().startsWith("assert")) {
Expand Down
166 changes: 166 additions & 0 deletions tests/format/js/import/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,92 @@ printWidth: 80
| printWidth
=====================================input======================================
import { } from '@types/googlemaps';
import 'a';
import /* comment */ 'a';
import // comment
'a';
import { /* comment */ } from 'a';
import /* comment */ {} from 'a';
import {} /* comment */ from 'a';
import {} from /* comment */ 'a';
import /* comment */ {/* comment */} /* comment */ from /* comment */ 'a';
import {
// comment
} from 'a';
import // comment
{} from 'a';
import {}
// comment
from 'a';
import {} from
// comment
'a';
import
// comment
{
// comment
}
// comment
from
// comment
'a';
import // {} from
'a';
import {} from // comment ends with from
'a';
import {} from /* comment ends with from */ 'a';
import {} from // comment not ends with from ___
'a';
import {} from /* comment not ends with from ___ */ 'a';
import // comment ends with from
'a';
import /* comment ends with from */ 'a';
import // comment not ends with from ___
'a';
import /* comment not ends with from ___ */ 'a';
=====================================output=====================================
import {} from "@types/googlemaps";
import "a";
import /* comment */ "a";
import // comment
"a";
import {} from /* comment */ "a";
import {} from /* comment */ "a";
import {} from /* comment */ "a";
import {} from /* comment */ "a";
import {} from /* comment */ /* comment */ /* comment */ /* comment */ "a";
import {} from // comment
"a";
import {} from // comment
"a";
import {} from // comment
"a";
import {} from // comment
"a";
import {} from // comment
// comment
// comment
// comment
"a";
import // {} from
"a";
import {} from // comment ends with from
"a";
import {} from /* comment ends with from */ "a";
import {} from // comment not ends with from ___
"a";
import {} from /* comment not ends with from ___ */ "a";
import // comment ends with from
"a";
import /* comment ends with from */ "a";
import // comment not ends with from ___
"a";
import /* comment not ends with from ___ */ "a";
================================================================================
`;
Expand All @@ -300,9 +383,92 @@ printWidth: 80
| printWidth
=====================================input======================================
import { } from '@types/googlemaps';
import 'a';
import /* comment */ 'a';
import // comment
'a';
import { /* comment */ } from 'a';
import /* comment */ {} from 'a';
import {} /* comment */ from 'a';
import {} from /* comment */ 'a';
import /* comment */ {/* comment */} /* comment */ from /* comment */ 'a';
import {
// comment
} from 'a';
import // comment
{} from 'a';
import {}
// comment
from 'a';
import {} from
// comment
'a';
import
// comment
{
// comment
}
// comment
from
// comment
'a';
import // {} from
'a';
import {} from // comment ends with from
'a';
import {} from /* comment ends with from */ 'a';
import {} from // comment not ends with from ___
'a';
import {} from /* comment not ends with from ___ */ 'a';
import // comment ends with from
'a';
import /* comment ends with from */ 'a';
import // comment not ends with from ___
'a';
import /* comment not ends with from ___ */ 'a';
=====================================output=====================================
import {} from "@types/googlemaps";
import "a";
import /* comment */ "a";
import // comment
"a";
import {} from /* comment */ "a";
import {} from /* comment */ "a";
import {} from /* comment */ "a";
import {} from /* comment */ "a";
import {} from /* comment */ /* comment */ /* comment */ /* comment */ "a";
import {} from // comment
"a";
import {} from // comment
"a";
import {} from // comment
"a";
import {} from // comment
"a";
import {} from // comment
// comment
// comment
// comment
"a";
import // {} from
"a";
import {} from // comment ends with from
"a";
import {} from /* comment ends with from */ "a";
import {} from // comment not ends with from ___
"a";
import {} from /* comment not ends with from ___ */ "a";
import // comment ends with from
"a";
import /* comment ends with from */ "a";
import // comment not ends with from ___
"a";
import /* comment not ends with from ___ */ "a";
================================================================================
`;
Expand Down
45 changes: 45 additions & 0 deletions tests/format/js/import/empty-import.js
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
import { } from '@types/googlemaps';
import 'a';
import /* comment */ 'a';
import // comment
'a';
import { /* comment */ } from 'a';
import /* comment */ {} from 'a';
import {} /* comment */ from 'a';
import {} from /* comment */ 'a';
import /* comment */ {/* comment */} /* comment */ from /* comment */ 'a';
import {
// comment
} from 'a';
import // comment
{} from 'a';
import {}
// comment
from 'a';
import {} from
// comment
'a';
import
// comment
{
// comment
}
// comment
from
// comment
'a';

import // {} from
'a';
import {} from // comment ends with from
'a';
import {} from /* comment ends with from */ 'a';
import {} from // comment not ends with from ___
'a';
import {} from /* comment not ends with from ___ */ 'a';

import // comment ends with from
'a';
import /* comment ends with from */ 'a';
import // comment not ends with from ___
'a';
import /* comment not ends with from ___ */ 'a';
Loading

0 comments on commit 15892f1

Please sign in to comment.