Skip to content

Commit a577f75

Browse files
committed
fix: tweak generatorHack reprinting
1 parent 43d3eb2 commit a577f75

File tree

8 files changed

+140
-9
lines changed

8 files changed

+140
-9
lines changed

src/babel/reprint.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Comment, Node } from '../types'
33
import { original, rangeWithWhitespace, source } from '../util/symbols'
44

55
const excludedNodeTypes = new Set([
6-
'File',
76
// @babel/generator prints ` ${ and } around TemplateElement
87
// even though their range doesn't include those characters
98
'TemplateElement',
@@ -43,8 +42,7 @@ export default function reprint(
4342
const src = (node as any)[source]
4443

4544
if (orig && src) {
46-
const { start: _start, end } =
47-
(node as any)[rangeWithWhitespace] || orig
45+
const { start: _start, end } = orig
4846
let start = _start
4947
if (
5048
start < lastPrintedRange.end &&

src/util/cloneAstWithOriginals.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ export function cloneAstWithOriginals<T extends Node>(ast: T, src: string): T {
1313
const result: any = { [original]: node, [source]: src }
1414
const { start, end } = node as any
1515
if (typeof start === 'number' && typeof end === 'number') {
16-
result[rangeWithWhitespace] = { start: starts[start], end: ends[end] }
16+
result[rangeWithWhitespace] = {
17+
start: starts[start],
18+
end: ends[end] ?? end,
19+
}
1720
}
1821

1922
for (const field in node) {

test/Astx.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ describe(`Astx`, function () {
197197
const astx = createAstx(`foo + bar; baz + qux, qlomb`).find`$a + $b`()
198198
expect(astx.match).to.equal(astx.matches[0])
199199
})
200-
it.only(`.matched`, function () {
200+
it(`.matched`, function () {
201201
expect(
202202
createAstx(`foo + bar; baz + qux, qlomb`).find`$a + $b`().matched?.$a
203203
).to.exist

test/astx/insertComment.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import dedent from 'dedent-js'
2+
import { TransformOptions } from '../../src'
3+
import { astxTestcase } from '../astxTestcase'
4+
5+
astxTestcase({
6+
file: __filename,
7+
parsers: ['babel', 'babel/tsx'],
8+
input: dedent`
9+
const foo = 1
10+
`,
11+
astx: ({ astx }: TransformOptions): void => {
12+
;(astx.node as any).program.body[0].leadingComments = [
13+
{
14+
type: 'CommentLine',
15+
value: ' @flow',
16+
},
17+
]
18+
},
19+
expected: dedent`
20+
// @flow
21+
const foo = 1;
22+
`,
23+
})

test/astx/noChanges.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { astxTestcase } from '../astxTestcase'
2+
3+
astxTestcase({
4+
file: __filename,
5+
input: `
6+
7+
// foo
8+
const bar = 1
9+
`,
10+
astx: (): void => {
11+
// no-op
12+
},
13+
expected: `
14+
15+
// foo
16+
const bar = 1
17+
`,
18+
})

test/astxTestcase.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export function astxTestcase(testcase: Fixture): void {
9393
: jsParser.bindParserOpts(parserOpts)
9494
const babelBackend = new BabelBackend({
9595
parserOptions: babelParser.parserOpts,
96+
preserveFormat: 'generatorHack',
9697
})
9798
const backend: Backend =
9899
backendName === 'recast'

test/findReplace/bugs_9_duplicateCode.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
export const input = `
2-
/* eslint-disable no-console */
3-
42
const testLib = require('test-lib')
53
64
const funcOne = (foo) => {
@@ -50,12 +48,10 @@ module.exports = func({
5048
`
5149

5250
export const expectedReplace = `
53-
/* eslint-disable no-console */
5451
const {
5552
func
5653
} = require("some-module");
5754
58-
/* eslint-disable no-console */
5955
const testLib = require("test-lib");
6056
6157
const funcOne = foo => {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
export const input = `
2+
/* eslint-disable no-console */
3+
const testLib = require('test-lib')
4+
5+
const funcOne = (foo) => {
6+
console.log('foo', foo)
7+
return testLib(foo)
8+
}
9+
10+
const funcTwo = (bar) => {
11+
console.log('bar', bar)
12+
return testLib(bar)
13+
}
14+
15+
module.exports = (on, config) => {
16+
on('event', () => {
17+
funcOne(config.foo)
18+
funcTwo(config.bar)
19+
})
20+
}
21+
22+
const json = {
23+
"foo": 123,
24+
"bar": true
25+
}
26+
`
27+
28+
export const find = `
29+
$$header
30+
module.exports = (on, config) => {
31+
$$functionBody
32+
}
33+
const json = { $$json }
34+
`
35+
36+
export const replace = `
37+
const { func } = require('some-module')
38+
39+
$$header
40+
41+
module.exports = func({
42+
$$json,
43+
events: {
44+
setupEvents(on, config) {
45+
$$functionBody
46+
}
47+
}
48+
})
49+
`
50+
51+
export const expectedReplace = `
52+
const {
53+
func
54+
} = require("some-module");
55+
56+
/* eslint-disable no-console */
57+
const testLib = require("test-lib");
58+
59+
const funcOne = foo => {
60+
console.log("foo", foo);
61+
return testLib(foo);
62+
};
63+
64+
const funcTwo = bar => {
65+
console.log("bar", bar);
66+
return testLib(bar);
67+
};
68+
69+
module.exports = func({
70+
"foo": 123,
71+
"bar": true,
72+
73+
events: {
74+
setupEvents(on, config) {
75+
on("event", () => {
76+
funcOne(config.foo);
77+
funcTwo(config.bar);
78+
});
79+
}
80+
}
81+
});
82+
`
83+
import { findReplaceTestcase } from '../findReplaceTestcase'
84+
85+
findReplaceTestcase({
86+
file: __filename,
87+
input,
88+
find,
89+
replace,
90+
expectedReplace,
91+
skip: true,
92+
})

0 commit comments

Comments
 (0)