Skip to content

Commit e66fd7d

Browse files
committed
fix: upgrade to prettier 3
1 parent 5be8dbb commit e66fd7d

File tree

6 files changed

+54
-34
lines changed

6 files changed

+54
-34
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@
189189
"lint-staged": "^10.5.3",
190190
"mocha": "^10.1.0",
191191
"nyc": "^15.1.0",
192-
"prettier": "^2.2.1",
192+
"prettier": "^3.0.3",
193193
"prettier-eslint": "^12.0.0",
194194
"require-glob": "^3.2.0",
195195
"rimraf": "^3.0.2",

pnpm-lock.yaml

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/node/AstxWorker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ export default class AstxWorker {
7373
emitted(worker, 'message', {
7474
filter: (event: any) => event.seq === seq,
7575
rejectionEvents: ['error', 'exit'],
76-
}).catch((reason) =>
77-
typeof reason === 'number'
78-
? `worker exited with code ${reason}`
76+
}).catch((reason) => {
77+
throw typeof reason === 'number'
78+
? new Error(`worker exited with code ${reason}`)
7979
: reason
80-
),
80+
}),
8181
...(signal
8282
? [emitted(signal as any, '', { rejectionEvents: ['abort'] })]
8383
: []),

src/node/runTransformOnFile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const getPrettier = memoize(async (path: string): Promise<any> => {
3636
} catch (error) {
3737
// ignore
3838
}
39-
return null
39+
return await import('prettier')
4040
})
4141

4242
export interface Fs {
@@ -174,7 +174,7 @@ export default async function runTransformOnFile({
174174
const prettierConfig = (await prettier.resolveConfig(file)) || {}
175175
prettierConfig.filepath = file
176176
if (/\.tsx?$/.test(file)) prettierConfig.parser = 'typescript'
177-
transformed = prettier.format(transformed, prettierConfig)
177+
transformed = await prettier.format(transformed, prettierConfig)
178178
}
179179
if (transformed != null) {
180180
transformed = omitBlankLineChanges(source, transformed)

test/Astx.test.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ describe(`Astx`, function () {
1717
new BabelBackend(),
1818
] as Backend<any>[]) {
1919
const prettierOptions = { parser: 'babel-ts' }
20-
const format = (code: string) =>
21-
prettier
22-
.format(code, prettierOptions)
20+
const format = async (code: string) =>
21+
(await prettier.format(code, prettierOptions))
2322
.trim()
2423
.replace(/\n{2,}/gm, '\n')
25-
const reformat = (code: string) =>
26-
format(backend.generate(backend.parse(code)).code)
24+
const reformat = async (code: string) =>
25+
await format(backend.generate(backend.parse(code)).code)
2726

2827
const toSource = (astx: Astx) => backend.generate(astx.nodes[0]).code
2928
let source = ''
@@ -274,41 +273,55 @@ describe(`Astx`, function () {
274273
)
275274
).to.deep.equal([{ node: '1 + 2', captures: { $a: '1', $b: '2' } }])
276275
})
277-
it(`.replace tagged template works`, function () {
276+
it(`.replace tagged template works`, async function () {
278277
const astx = createAstx(`1 + 2; 3 + 4`)
279278
astx.find`$a + $b`().replace`$b + $a`()
280-
expect(reformat(toSource(astx))).to.equal(reformat(`2 + 1; 4 + 3;`))
279+
expect(await reformat(toSource(astx))).to.equal(
280+
await reformat(`2 + 1; 4 + 3;`)
281+
)
281282
})
282-
it(`.replace tagged template after find options works`, function () {
283+
it(`.replace tagged template after find options works`, async function () {
283284
const astx = createAstx(`1 + 2; 3 + 4`)
284285
astx.find`$a + $b`({
285-
where: { $b: (path: any) => path.node.value < 4 },
286+
where: {
287+
$b: (path: any) => path.node.value < 4,
288+
},
286289
}).replace`$b + $a`()
287-
expect(reformat(toSource(astx))).to.equal(reformat(`2 + 1; 3 + 4`))
290+
expect(await reformat(toSource(astx))).to.equal(
291+
await reformat(`2 + 1; 3 + 4`)
292+
)
288293
})
289-
it(`.replace tagged template interpolation works`, function () {
294+
it(`.replace tagged template interpolation works`, async function () {
290295
const astx = createAstx(`1 + 2; 3 + 4`)
291296
astx.find`$a + $b`().replace`$b + ${t.identifier('foo')}`()
292-
expect(reformat(toSource(astx))).to.equal(reformat(`2 + foo; 4 + foo;`))
297+
expect(await reformat(toSource(astx))).to.equal(
298+
await reformat(`2 + foo; 4 + foo;`)
299+
)
293300
})
294-
it(`.replace function returning parse tagged template literal works`, function () {
301+
it(`.replace function returning parse tagged template literal works`, async function () {
295302
const astx = createAstx(`1 + FOO; 3 + BAR`)
296303
astx.find`$a + $b`().replace(
297304
({ $b }, parse) => parse`${$b.code.toLowerCase()} + $a`
298305
)
299-
expect(reformat(toSource(astx))).to.equal(reformat(`foo + 1; bar + 3;`))
306+
expect(await reformat(toSource(astx))).to.equal(
307+
await reformat(`foo + 1; bar + 3;`)
308+
)
300309
})
301-
it(`.replace function returning string works`, function () {
310+
it(`.replace function returning string works`, async function () {
302311
const astx = createAstx(`1 + FOO; 3 + BAR`)
303312
astx.find`$a + $b`().replace(
304313
({ $b }) => `${$b.code.toLowerCase()} + $a`
305314
)
306-
expect(reformat(toSource(astx))).to.equal(reformat(`foo + 1; bar + 3;`))
315+
expect(await reformat(toSource(astx))).to.equal(
316+
await reformat(`foo + 1; bar + 3;`)
317+
)
307318
})
308-
it(`.replace called with string works`, function () {
319+
it(`.replace called with string works`, async function () {
309320
const astx = createAstx(`1 + 2; 3 + 4`)
310321
astx.find('$a + $b').replace('$b + $a')
311-
expect(reformat(toSource(astx))).to.equal(reformat(`2 + 1; 4 + 3;`))
322+
expect(await reformat(toSource(astx))).to.equal(
323+
await reformat(`2 + 1; 4 + 3;`)
324+
)
312325
})
313326
function expectCodeFrameError(
314327
callee: () => any,

test/findReplaceTestcase.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,17 @@ export function findReplaceTestcase(fixture: Fixture): void {
175175
parser: actualParser === 'babel/tsx' ? 'babel-ts' : 'babel-flow',
176176
}
177177

178-
const format = (code: string) =>
178+
const format = async (code: string) =>
179179
_format === false
180180
? code
181-
: prettier
182-
.format(code, prettierOptions)
181+
: (await prettier.format(code, prettierOptions))
183182
.trim()
184183
.replace(/\n{2,}/gm, '\n')
185184

186-
const reformat = (code: string) =>
185+
const reformat = async (code: string) =>
187186
_format === false
188187
? code
189-
: format(backend.generate(backend.parse(code)).code)
188+
: await format(backend.generate(backend.parse(code)).code)
190189

191190
if (expectedFind || !_replace) {
192191
it(`<find> ${parser}`, function () {
@@ -226,7 +225,7 @@ export function findReplaceTestcase(fixture: Fixture): void {
226225
})
227226
}
228227
if (_replace) {
229-
it(`<replace> ${parser}`, function () {
228+
it(`<replace> ${parser}`, async function () {
230229
const ast = backend.parse(input)
231230
const root = new backend.t.NodePath(ast)
232231
const matches = find(root, backend.parsePattern(_find), {
@@ -272,7 +271,9 @@ export function findReplaceTestcase(fixture: Fixture): void {
272271
{ backend }
273272
)
274273
const actual = backend.generate(ast).code
275-
expect(reformat(format(actual))).to.deep.equal(reformat(expected))
274+
expect(await reformat(await format(actual))).to.deep.equal(
275+
await reformat(expected)
276+
)
276277
}
277278
})
278279
}

0 commit comments

Comments
 (0)