Skip to content

Commit acc0363

Browse files
committed
add esquery, simplify numeric literals (updated 22:04)
1 parent f30ca40 commit acc0363

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@
4545
"@types/babel__generator": "^7.27.0",
4646
"@types/dedent": "0.7.0",
4747
"@types/eslint": "^8.44.7",
48+
"@types/esquery": "^1.5.4",
4849
"@types/glob": "7.1.3",
4950
"@types/js-yaml": "3.12.5",
5051
"@types/lodash": "^4.14.202",
5152
"@types/node": "^20.0.0",
5253
"cheerio": "^1.0.0",
5354
"dedent": "^1.5.1",
5455
"eslint-plugin-markdown": "^4.0.1",
56+
"esquery": "^1.6.0",
5557
"expect": "^29.7.0",
5658
"fp-ts": "^2.1.0",
5759
"glob": "^10.3.10",

pnpm-lock.yaml

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

src/dependencies.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as babelTypes from '@babel/types'
55
import * as cheerio from 'cheerio'
66
import * as child_process from 'child_process'
77
import dedent from 'dedent'
8+
import esquery from 'esquery'
89
// eslint-disable-next-line no-restricted-imports
910
import * as fs from 'fs'
1011
import * as glob from 'glob'
@@ -33,6 +34,7 @@ export interface PresetDependencies {
3334
cheerio: typeof import('cheerio')
3435
child_process: typeof import('child_process')
3536
dedent: typeof import('dedent').default
37+
esquery: typeof import('esquery')
3638
fetchSync: typeof import('./fetch-sync').fetchSync
3739
fs: typeof import('fs')
3840
glob: Pick<typeof import('glob'), 'globSync'>
@@ -44,6 +46,8 @@ export interface PresetDependencies {
4446
path: typeof import('path')
4547
readPkgUp: Pick<typeof import('read-pkg-up'), 'sync'>
4648
recast: typeof import('recast')
49+
/** require an arbitrary module *from eslint-plugin-codegen* - this means you can require dependencies of the plugin even when they're not your dependencies */
50+
require: <T = unknown>(path: string) => T
4751
simplify: typeof import('./simplify')
4852
unionfs: typeof import('unionfs')
4953
zx: typeof import('zx')
@@ -58,6 +62,7 @@ export const dependencies: PresetDependencies = {
5862
cheerio,
5963
child_process,
6064
dedent: Object.assign(dedent, {default: dedent}), // backcompat: accidentally used `import * as dedent from 'dedent'` previously
65+
esquery,
6166
fetchSync,
6267
fs,
6368
glob,
@@ -69,6 +74,8 @@ export const dependencies: PresetDependencies = {
6974
path,
7075
readPkgUp,
7176
recast,
77+
// eslint-disable-next-line @typescript-eslint/no-require-imports
78+
require: <T>(modulePath: string) => require(modulePath) as T,
7279
simplify,
7380
unionfs,
7481
zx,

src/simplify.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ export const simplifyCodeTraverseOptions: import('@babel/traverse').TraverseOpti
153153
path.node.shorthand = true
154154
}
155155
},
156+
// remove underscores from numbers
157+
NumericLiteral(path) {
158+
if (path.node.extra && typeof path.node.extra.raw === 'string' && path.node.extra.raw.includes('_')) {
159+
path.node.extra.raw = path.node.extra.raw.replaceAll('_', '')
160+
}
161+
},
156162
}
157163

158164
export function stripTypes(typeScriptCode: string) {

test/simplify.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ test('simplifyCode', async () => {
4141

4242
expect(simplifyCode(leftCode)).toBe(simplifyCode(rightCode))
4343
})
44+
45+
test('snapshots', async () => {
46+
expect(simplifyCode(`const a = 123_456`)).toMatchInlineSnapshot(`"const a = 123456;"`)
47+
})

0 commit comments

Comments
 (0)