Skip to content

Commit 9edcf6a

Browse files
authored
Merge pull request #12 from codemodsquad/fix/comments
fix(comments): preserve comments
2 parents 606346a + f8bf002 commit 9edcf6a

File tree

6 files changed

+43
-1
lines changed

6 files changed

+43
-1
lines changed

src/util/insertStatementsBefore.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export default function insertStatementsBefore<
88
>(path: NodePath<any>, statements: Statements): NodePaths<Statements> {
99
let target = parentStatement(path)
1010
const { parentPath } = target
11+
const firstStatement = Array.isArray(statements) ? statements[0] : statements
12+
if (target.node && firstStatement) {
13+
t.inheritLeadingComments(firstStatement, target.node)
14+
target.node.leadingComments = null
15+
}
1116
if (
1217
(parentPath.isBlockParent() || parentPath.isIfStatement()) &&
1318
!parentPath.isBlockStatement()

src/util/recastBugWorkarounds.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { NodePath } from '@babel/traverse'
33

44
export default function recastBugWorkarounds(path: NodePath<any>): void {
55
path.traverse({
6+
exit(path: NodePath<t.Node>) {
7+
const anyNode = path.node as any
8+
anyNode.comments = anyNode.leadingComments
9+
},
610
AwaitExpression(path: NodePath<t.AwaitExpression>) {
711
const argument = path.get('argument')
812
const { parentPath } = path

src/util/replaceWithStatements.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ export default function replaceWithStatements<Statements extends t.Statement[]>(
1515
'path must be a child of a BlockParent, SwitchCase, or IfStatement'
1616
)
1717
}
18+
const oldNode = path.node
19+
if (oldNode) {
20+
if (oldNode && statements[0]) {
21+
t.inheritLeadingComments(statements[0], oldNode)
22+
}
23+
if (oldNode && statements[statements.length - 1]) {
24+
t.inheritTrailingComments(statements[statements.length - 1], oldNode)
25+
}
26+
t.removeComments(oldNode)
27+
}
1828
if (!parentPath.isBlockStatement()) {
1929
return (path.replaceWith(t.blockStatement(statements)) as any)[0].get(
2030
'body'

test/fixtures/bugs_BelongsToMany.create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ class BelongsToMany {
5050
}
5151
}
5252
53+
// Create the related model instance
5354
const newAssociatedObject = await association.target.create(values, options);
5455
await sourceInstance[association.accessors.add](newAssociatedObject, _.omit(options, ['fields']));
55-
// Create the related model instance
5656
return newAssociatedObject;
5757
}
5858
}

test/fixtures/bugs_Sequelize.transaction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Sequelize {
5252
await transaction.commit();
5353
return await result;
5454
} catch (err) {
55+
// Rollback transaction if not already finished (commit, rollback, etc)
56+
// and reject with original error (ignore any error in rollback)
5557
if (!transaction.finished) {
5658
try {
5759
await transaction.rollback();

test/fixtures/leadingComment.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const input = `
2+
function foo() {
3+
// this is a test
4+
return bar.then(baz => {
5+
return process(baz)
6+
}).then(results => {
7+
console.log(results)
8+
})
9+
}
10+
`
11+
12+
export const options = {}
13+
14+
export const expected = `
15+
async function foo() {
16+
// this is a test
17+
const baz = await bar
18+
const results = await process(baz)
19+
console.log(results)
20+
}
21+
`

0 commit comments

Comments
 (0)