Skip to content

Commit

Permalink
Handle type + value importing better
Browse files Browse the repository at this point in the history
This treats the first node as the "winner", so that our
"currentOwnerIsFirstImport" logic works correctly for comments at top of files
  • Loading branch information
IanVS committed May 15, 2023
1 parent 20a00f2 commit 73eec7b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
22 changes: 21 additions & 1 deletion src/utils/__tests__/get-sorted-import-specifiers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test('should return correct sorted nodes with default import', () => {
]);
});

test('should group type imports after value imports', () => {
test('should group type imports after value imports - typescript', () => {
const code = `import Component, { type TypeB, filter, type TypeA, reduce, eventHandler } from '@server/z';`;
const [importNode] = getImportNodes(code, {
plugins: ['typescript'],
Expand All @@ -50,3 +50,23 @@ test('should group type imports after value imports', () => {
'TypeB',
]);
});

test.only('should group type imports after value imports - flow', () => {
const code = `import Component, { type TypeB, filter, type TypeA, reduce, eventHandler } from '@server/z';`;
const [importNode] = getImportNodes(code, {
plugins: ['flow'],
});
const sortedImportSpecifiers = getSortedImportSpecifiers(importNode);
const specifiersList = getSortedNodesModulesNames(
sortedImportSpecifiers.specifiers,
);

expect(specifiersList).toEqual([
'Component',
'eventHandler',
'filter',
'reduce',
'TypeA',
'TypeB',
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import { Junk } from "junk-group-1";
import "./side-effects1";
// C, E and D will be separated from A, B because side-effects in-between
import { D, type C, type E } from "a";
// prettier-ignore
Expand Down
3 changes: 2 additions & 1 deletion src/utils/get-sorted-import-specifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const getSortedImportSpecifiers = (node: ImportDeclaration) => {
b.type === 'ImportSpecifier' &&
a.importKind !== b.importKind
) {
return a.importKind === 'value' ? -1 : 1;
// flow uses null for value import specifiers
return a.importKind === 'value' || a.importKind == null ? -1 : 1;
}

return naturalSort(a.local.name, b.local.name);
Expand Down
29 changes: 23 additions & 6 deletions src/utils/merge-nodes-with-matching-flavors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ function mergeIsSafe(
/**
* Mutates the modeToKeep, adding the import specifiers and comments from the nodeToForget.
*
* @returns (true to delete node | false to keep node)
* @returns (node to keep if we should delete the other node | null to keep everything)
*/
function mergeNodes(
nodeToKeep: ImportDeclaration,
nodeToForget: ImportDeclaration,
) {
if (!mergeIsSafe(nodeToKeep, nodeToForget)) {
return false;
return null;
}

if (
Expand Down Expand Up @@ -174,7 +174,7 @@ function mergeNodes(
...(nodeToForget.trailingComments || []),
];

return true;
return nodeToKeep;
}

/**
Expand All @@ -192,9 +192,26 @@ function mutateContextAndMerge({
insertableNode: ImportDeclaration;
}) {
const source = selectNodeImportSource(insertableNode);
if (context[source]) {
if (mergeNodes(context[source], insertableNode)) {
nodesToDelete.push(insertableNode);
const existing = context[source];

if (existing) {
// Check if the existing is after the new one in the code.
// If so, we reverse the keep/delete so that the first node is kept.
// Important for top-of-file comment formatting.
if (
existing.start &&
insertableNode.start &&
existing.start > insertableNode.start
) {
const merged = mergeNodes(insertableNode, existing);
if (merged) {
nodesToDelete.push(existing);
context[source] = merged;
}
} else {
if (mergeNodes(existing, insertableNode)) {
nodesToDelete.push(insertableNode);
}
}
} else {
context[source] = insertableNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ import { a } from 'a';
*/
// second comment
import { a, type A } from "a";
// a
import { a, type A } from "a"; // a
import { b } from "b";
`;
Expand Down

0 comments on commit 73eec7b

Please sign in to comment.