-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/Fix rendering comments embedded in imports (#71)
Fixes #9 Fixes #54 Turns out our previous comment-adjuster didn't work for most cases. 🤦🏻♂️ This commit is squashed from PR #71 and: - Adds a bunch of test cases, and clarifies existing test cases to fully exercise the space - [Solves the test cases] - Removes unneeded dependencies: `lodash.isequal` --------- Co-authored-by: Ian VanSchooten <ian.vanschooten@gmail.com>
- Loading branch information
Showing
57 changed files
with
862 additions
and
107 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
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,50 @@ | ||
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], | ||
}); | ||
|
||
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
Oops, something went wrong.