-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from blutorange/issue-110-preserver-side-effe…
…ct-import-order Do not reorder side effect nodes #110
- Loading branch information
Showing
26 changed files
with
1,472 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/utils/__tests__/adjust-comments-on-sorted-nodes.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { ImportDeclaration } from '@babel/types'; | ||
|
||
import { adjustCommentsOnSortedNodes } from '../adjust-comments-on-sorted-nodes'; | ||
import { getImportNodes } from '../get-import-nodes'; | ||
|
||
function leadingComments(node: ImportDeclaration): string[] { | ||
return node.leadingComments?.map((c) => c.value) ?? []; | ||
} | ||
|
||
function trailingComments(node: ImportDeclaration): string[] { | ||
return node.trailingComments?.map((c) => c.value) ?? []; | ||
} | ||
|
||
test('it preserves the single leading comment for each import declaration', () => { | ||
const importNodes = getImportNodes(` | ||
import {x} from "c"; | ||
// comment b | ||
import {y} from "b"; | ||
// comment a | ||
import {z} from "a"; | ||
`); | ||
expect(importNodes).toHaveLength(3); | ||
const finalNodes = [importNodes[2], importNodes[1], importNodes[0]]; | ||
adjustCommentsOnSortedNodes(importNodes, finalNodes); | ||
expect(finalNodes).toHaveLength(3); | ||
expect(leadingComments(finalNodes[0])).toEqual([' comment a']); | ||
expect(trailingComments(finalNodes[0])).toEqual([]); | ||
expect(leadingComments(finalNodes[1])).toEqual([' comment b']); | ||
expect(trailingComments(finalNodes[1])).toEqual([]); | ||
expect(leadingComments(finalNodes[2])).toEqual([]); | ||
expect(trailingComments(finalNodes[2])).toEqual([]); | ||
}); | ||
|
||
test('it preserves multiple leading comments for each import declaration', () => { | ||
const importNodes = getImportNodes(` | ||
import {x} from "c"; | ||
// comment b1 | ||
// comment b2 | ||
// comment b3 | ||
import {y} from "b"; | ||
// comment a1 | ||
// comment a2 | ||
// comment a3 | ||
import {z} from "a"; | ||
`); | ||
expect(importNodes).toHaveLength(3); | ||
const finalNodes = [importNodes[2], importNodes[1], importNodes[0]]; | ||
adjustCommentsOnSortedNodes(importNodes, finalNodes); | ||
expect(finalNodes).toHaveLength(3); | ||
expect(leadingComments(finalNodes[0])).toEqual([ | ||
' comment a1', | ||
' comment a2', | ||
' comment a3', | ||
]); | ||
expect(trailingComments(finalNodes[0])).toEqual([]); | ||
expect(leadingComments(finalNodes[1])).toEqual([ | ||
' comment b1', | ||
' comment b2', | ||
' comment b3', | ||
]); | ||
expect(trailingComments(finalNodes[1])).toEqual([]); | ||
expect(leadingComments(finalNodes[2])).toEqual([]); | ||
expect(trailingComments(finalNodes[2])).toEqual([]); | ||
}); | ||
|
||
test('it does not move comments at before all import declarations', () => { | ||
const importNodes = getImportNodes(` | ||
// comment c1 | ||
// comment c2 | ||
import {x} from "c"; | ||
import {y} from "b"; | ||
import {z} from "a"; | ||
`); | ||
expect(importNodes).toHaveLength(3); | ||
const finalNodes = [importNodes[2], importNodes[1], importNodes[0]]; | ||
adjustCommentsOnSortedNodes(importNodes, finalNodes); | ||
expect(finalNodes).toHaveLength(3); | ||
expect(leadingComments(finalNodes[0])).toEqual([ | ||
' comment c1', | ||
' comment c2', | ||
]); | ||
expect(trailingComments(finalNodes[0])).toEqual([]); | ||
expect(leadingComments(finalNodes[1])).toEqual([]); | ||
expect(trailingComments(finalNodes[1])).toEqual([]); | ||
expect(leadingComments(finalNodes[2])).toEqual([]); | ||
expect(trailingComments(finalNodes[2])).toEqual([]); | ||
}); | ||
|
||
test('it does not affect comments after all import declarations', () => { | ||
const importNodes = getImportNodes(` | ||
import {x} from "c"; | ||
import {y} from "b"; | ||
import {z} from "a"; | ||
// comment final 1 | ||
// comment final 2 | ||
`); | ||
expect(importNodes).toHaveLength(3); | ||
const finalNodes = [importNodes[2], importNodes[1], importNodes[0]]; | ||
adjustCommentsOnSortedNodes(importNodes, finalNodes); | ||
expect(finalNodes).toHaveLength(3); | ||
expect(leadingComments(finalNodes[0])).toEqual([]); | ||
expect(trailingComments(finalNodes[0])).toEqual([]); | ||
expect(leadingComments(finalNodes[1])).toEqual([]); | ||
expect(trailingComments(finalNodes[1])).toEqual([]); | ||
expect(leadingComments(finalNodes[2])).toEqual([]); | ||
expect(trailingComments(finalNodes[2])).toEqual([]); | ||
}); |
Oops, something went wrong.