-
Notifications
You must be signed in to change notification settings - Fork 161
Do not reorder side effect nodes #110 #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
byara
merged 11 commits into
trivago:v3.2.x
from
blutorange:issue-110-preserver-side-effect-import-order
Jan 19, 2022
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7b8989f
implements #110
blutorange d05dc43
expand tests with multiple successive side effect imports (which shou…
blutorange 5619b22
Use iterative loop instead of for-each loop #110 / #111
blutorange 170b06a
Check for last item in iteration via index #110 / #111
blutorange 61b487b
Fix typo in example file
miles-au 9929ea1
Fix .prettierrc path in DEBUG doc
miles-au e83f3f1
Merge pull request #121 from miles-au/master
ayusharma c6d137b
Merge branch 'master' into issue-110-preserver-side-effect-import-order
byara 5e2a3eb
Refactor test, add test for adjust-comments, run prettier #110 #111
blutorange 9a43ff2
Lower case constant name to follow the project's style guide #110 #111
blutorange 812cbab
Merge remote-tracking branch 'origin/v3.2.x' into issue-110-preserver…
blutorange File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.