Skip to content

Commit a3a878b

Browse files
committed
feat: add noRecastWorkaround option and entry point
1 parent 2321ece commit a3a878b

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,22 @@ it('test', async () => {
8787
Although I could possibly fix this for cases where it's easy to determine that the function has
8888
no parameters, there could be cases where it's impossible to determine whether the identifier
8989
`doSomething` is even a function or whether it has parameters.
90+
91+
## Disabling `recast` workaround
92+
93+
At the time I wrote `asyncify`, there were some show-stopping bugs in old version of `recast` that
94+
`jscodeshift` depended on. To avoid this problem, `asyncify` parses with a newer version of `recast` in its
95+
own dependencies, instead of parsing with the `jscodeshift` API. The author of `putout` has asked to be able
96+
to parse with the injected `jscodeshift` API for performance, so you can access that version of the
97+
`jscodeshift` transform as:
98+
99+
```js
100+
import transform from '@codemodsquad/asyncify/noRecastWorkaround'
101+
```
102+
103+
Or there are two ways you can do it when running via `jscodeshift`:
104+
105+
```
106+
jscodeshift -t path/to/asyncify/noRecastWorkaround.js
107+
jscodeshift -t path/to/asyncify/index.js --noRecastWorkaround=true
108+
```

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ module.exports = function index(
1010
api: API,
1111
options: Options
1212
): string | null | undefined | void {
13-
const ast = recast.parse(fileInfo.source, {
14-
parser: require('recast/parsers/babel'),
15-
})
13+
const ast = options.noRecastWorkaround
14+
? api.jscodeshift(fileInfo.source).get()
15+
: recast.parse(fileInfo.source, {
16+
parser: require('recast/parsers/babel'),
17+
})
1618

1719
const ignoreChainsShorterThan = parseInt(options.ignoreChainsShorterThan)
1820
const commentWorkarounds = options.commentWorkarounds

src/noRecastWorkaround.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { API, FileInfo, Options } from 'jscodeshift'
2+
3+
module.exports = function(
4+
fileInfo: FileInfo,
5+
api: API,
6+
options: Options
7+
): string | null | undefined | void {
8+
// eslint-disable-next-line @typescript-eslint/no-var-requires
9+
return require('./index')(fileInfo, api, {
10+
...options,
11+
noRecastWorkaround: true,
12+
})
13+
}

0 commit comments

Comments
 (0)