Skip to content

Commit

Permalink
JS: Fix import {a as a} and export {a as a} format (#9435)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Oct 20, 2020
1 parent b95df49 commit 140cb14
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 4 deletions.
16 changes: 16 additions & 0 deletions changelog_unreleased/javascript/pr-9435.md
@@ -0,0 +1,16 @@
#### Fix `import {a as a}` and `export {a as a}` format (#9435 by @fisker)

<!-- prettier-ignore -->
```js
// Input
import { a as a } from "a";
export { b as b } from "b";

// Prettier stable
import { a } from "a";
export { b } from "b";

// Prettier master
import { a as a } from "a";
export { b as b } from "b";
```
5 changes: 3 additions & 2 deletions src/language-js/printer-estree.js
Expand Up @@ -69,6 +69,7 @@ const {
hasNewlineBetweenOrAfterDecorators,
hasNgSideEffect,
hasPrettierIgnore,
hasSameLoc,
hasTrailingComment,
hasTrailingLineComment,
identity,
Expand Down Expand Up @@ -803,15 +804,15 @@ function printPathNoParens(path, options, print, args) {

parts.push(path.call(print, "imported"));

if (n.local && n.local.name !== n.imported.name) {
if (n.local && !hasSameLoc(n.local, n.imported, options)) {
parts.push(" as ", path.call(print, "local"));
}

return concat(parts);
case "ExportSpecifier":
parts.push(path.call(print, "local"));

if (n.exported && n.exported.name !== n.local.name) {
if (n.exported && !hasSameLoc(n.local, n.exported, options)) {
parts.push(" as ", path.call(print, "exported"));
}

Expand Down
25 changes: 23 additions & 2 deletions src/language-js/utils.js
Expand Up @@ -342,8 +342,28 @@ function isGetterOrSetter(node) {
* @param {Node} nodeB
* @returns {boolean}
*/
function sameLocStart(nodeA, nodeB, options) {
return options.locStart(nodeA) === options.locStart(nodeB);
function sameLocStart(nodeA, nodeB, { locStart }) {
return locStart(nodeA) === locStart(nodeB);
}

/**
* @param {Node} nodeA
* @param {Node} nodeB
* @returns {boolean}
*/
function sameLocEnd(nodeA, nodeB, { locEnd }) {
return locEnd(nodeA) === locEnd(nodeB);
}

/**
* @param {Node} nodeA
* @param {Node} nodeB
* @returns {boolean}
*/
function hasSameLoc(nodeA, nodeB, options) {
return (
sameLocStart(nodeA, nodeB, options) && sameLocEnd(nodeA, nodeB, options)
);
}

// TODO: This is a bad hack and we need a better way to distinguish between
Expand Down Expand Up @@ -1376,6 +1396,7 @@ module.exports = {
hasNgSideEffect,
hasNode,
hasPrettierIgnore,
hasSameLoc,
hasTrailingComment,
hasTrailingLineComment,
identity,
Expand Down
37 changes: 37 additions & 0 deletions tests/js/export/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -141,3 +141,40 @@ export {} from ".";
================================================================================
`;

exports[`same-local-and-exported.js - {"bracketSpacing":false} format 1`] = `
====================================options=====================================
bracketSpacing: false
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
export {a} from 'a';
export {b as b} from 'b';
export {c as /* comment */c} from 'c';
=====================================output=====================================
export {a} from "a";
export {b as b} from "b";
export {c as /* comment */ c} from "c";
================================================================================
`;

exports[`same-local-and-exported.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
export {a} from 'a';
export {b as b} from 'b';
export {c as /* comment */c} from 'c';
=====================================output=====================================
export { a } from "a";
export { b as b } from "b";
export { c as /* comment */ c } from "c";
================================================================================
`;
3 changes: 3 additions & 0 deletions tests/js/export/same-local-and-exported.js
@@ -0,0 +1,3 @@
export {a} from 'a';
export {b as b} from 'b';
export {c as /* comment */c} from 'c';
37 changes: 37 additions & 0 deletions tests/js/import/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -415,3 +415,40 @@ import a, * as b from "a";
================================================================================
`;

exports[`same-local-and-imported.js - {"bracketSpacing":false} format 1`] = `
====================================options=====================================
bracketSpacing: false
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
import {a} from 'a';
import {b as b} from 'b';
import {c as /* comment */c} from 'c';
=====================================output=====================================
import {a} from "a";
import {b as b} from "b";
import {c as /* comment */ c} from "c";
================================================================================
`;

exports[`same-local-and-imported.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
import {a} from 'a';
import {b as b} from 'b';
import {c as /* comment */c} from 'c';
=====================================output=====================================
import { a } from "a";
import { b as b } from "b";
import { c as /* comment */ c } from "c";
================================================================================
`;
3 changes: 3 additions & 0 deletions tests/js/import/same-local-and-imported.js
@@ -0,0 +1,3 @@
import {a} from 'a';
import {b as b} from 'b';
import {c as /* comment */c} from 'c';

0 comments on commit 140cb14

Please sign in to comment.