-
Notifications
You must be signed in to change notification settings - Fork 21
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
Fix behavior around comments embedded in imports #71
Merged
Merged
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
4b0fd09
Add irritating test cases for comments around imports
fbartho 919b5da
Change test-cases so top-of-file comments have a gap (otherwise they …
fbartho 0a96f12
Initial types & constants for ImportComments Preservation
fbartho c516cf2
Fix get-all-comments
fbartho b69eb21
Change test-cases so top-of-file comments have a gap (more tests)
fbartho bda233c
Feature: Update Import-adjacent Comment Adjustment to handle all the …
fbartho 0efd6c3
Remove a no-longer-needed dependency lodash.isequal
fbartho 18c3045
Formatting tweaks
fbartho cec6f65
Minor test cleanup
fbartho 743e16a
lol forgot to format
fbartho 2f300ef
Merge branch 'next' into fb/fix-import-comments
fbartho 8fcda28
Update for new Babel, and remove workarounds
fbartho b6965f8
Finish patch cleanup
fbartho 6eadf55
Unnecessary let
fbartho ec11257
Use `node.loc.end.line` instead of `start.line` in certain comparison…
fbartho fca0377
PR Feedback: only-same-line-comments should be treated as trailing
fbartho 90bb398
Merge branch 'next' into fb/fix-import-comments
fbartho 2d54d8e
Seeing CI-flaking? Disable the yarn-cache in-case that's it
fbartho 8b22a8e
Revert "Seeing CI-flaking? Disable the yarn-cache in-case that's it"
fbartho aae640e
Accidentally stashed this critical line!
fbartho c68ca80
Small test tweaks
IanVS 58c8d01
Rename id -> commentId for clarity
IanVS 375be16
Clean up lastImport logic
IanVS cbf6839
Add test for multi-line comment block spanning lines
IanVS 7bd1f5e
Simplify readme just a tad
IanVS File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,52 @@ | ||
import { | ||
addComments, | ||
removeComments, | ||
type ImportDeclaration, | ||
} from '@babel/types'; | ||
import clone from 'lodash.clone'; | ||
import isEqual from 'lodash.isequal'; | ||
import { removeComments, type ImportDeclaration } from '@babel/types'; | ||
|
||
import { ImportOrLine } from '../types'; | ||
import { | ||
attachCommentsToOutputNodes, | ||
getCommentRegistryFromImportDeclarations, | ||
} from './get-comment-registry'; | ||
|
||
/** | ||
* Takes the original nodes before sorting and the final nodes after sorting. | ||
* Adjusts the comments on the final nodes so that they match the comments as | ||
* they were in the original nodes. | ||
* @param nodes A list of nodes in the order as they were originally. | ||
* @param originalDeclarationNodes A list of nodes in the order as they were originally. | ||
* @param finalNodes The same set of nodes, but in the final sorting order. | ||
* @returns A copied and adjusted set of nodes, containing comments | ||
*/ | ||
export const adjustCommentsOnSortedNodes = ( | ||
nodes: ImportDeclaration[], | ||
originalDeclarationNodes: ImportDeclaration[], | ||
finalNodes: ImportOrLine[], | ||
) => { | ||
// We will mutate a copy of the finalNodes, and extract comments from the original | ||
const finalNodesClone = finalNodes.map(clone); | ||
|
||
const firstNodesComments = nodes[0].leadingComments; | ||
|
||
// Remove all comments from sorted nodes | ||
finalNodesClone.forEach(removeComments); | ||
const outputNodes: ImportDeclaration[] = finalNodes.filter( | ||
(n) => n.type === 'ImportDeclaration', | ||
) as ImportDeclaration[]; | ||
if (originalDeclarationNodes.length === 0 || outputNodes.length === 0) { | ||
// Nothing to do, because there are no ImportDeclarations! | ||
return finalNodes; | ||
} | ||
|
||
// insert comments other than the first comments | ||
finalNodesClone.forEach((node, index) => { | ||
if (isEqual(nodes[0].loc, node.loc)) return; | ||
const registry = getCommentRegistryFromImportDeclarations({ | ||
outputNodes, | ||
firstImport: originalDeclarationNodes[0], | ||
lastImport: | ||
originalDeclarationNodes[originalDeclarationNodes.length - 1], | ||
}); | ||
|
||
addComments(node, 'leading', finalNodes[index].leadingComments || []); | ||
// Make a copy of the nodes for easier debugging & remove the existing comments to reattach them | ||
// (removeComments clones the nodes internally, so we don't need to do that ourselves) | ||
const finalNodesClone = finalNodes.map((n) => { | ||
const noDirectCommentsNode = removeComments(n); | ||
if (noDirectCommentsNode.type === 'ImportDeclaration') { | ||
// Remove comments isn't recursive, so we need to clone/modify the specifiers manually | ||
noDirectCommentsNode.specifiers = ( | ||
noDirectCommentsNode.specifiers || [] | ||
).map((s) => removeComments(s)); | ||
} | ||
return noDirectCommentsNode; | ||
}); | ||
|
||
if (firstNodesComments) { | ||
addComments(finalNodesClone[0], 'leading', firstNodesComments); | ||
} | ||
attachCommentsToOutputNodes(registry, finalNodesClone); | ||
|
||
return finalNodesClone; | ||
}; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doesn't comment c1 move? In your note in the readme, it says"
So I would assume since there's no gap, that c1 and c2 would both move with the import from "c".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I see how I confused things particularly with my variable names and the term “gap”.
I didn’t mean “gap” as in blank line — I have to consider each comment independently (or I need even more richness in my data model/scan-passes). — So gap is “if this comment and the import-node were the only things that existed, would there be a gap?”
So “leading gap” really was “is this specific 1-line comment ending more than 1 line before the import.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed the variables and comments, in particular because the change from #71 (comment) also further twisted the meaning of one of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, this makes sense, thanks. It seems like a pretty rare thing to happen, and has an easy solution if someone wants both to stay at the top (add a blank line underneath).