benjamn / benjamn/ast-types

node.extra for `RegExpLiteral` appears to produce the wrong value

Open
#900 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1.2k
Forks
194
Avg merge
22h 43m
Merged PRs (30d)
10

Description

I'm currently using `jscodeshift` to implement some codemod. While using `builders.regExpLiteral`, I noticed any kind of regular expression was being output as `/undefined/`.

I managed to get a reproducer using this code:

```ts
import { builders as b, getFieldValue } from 'ast-types';
const node = b.regExpLiteral('test', '');

console.log(getFieldValue(node, 'extra'));
```

The output was:

```js
{ rawValue: /test/, raw: '/undefined/' }
```

The expected output is:

```js
{ rawValue: /test/, raw: '/test/' }
```

Upon investigation, it appears that [this line](https://github.com/benjamn/ast-types/blob/6e4d91213c27b6ba76a747c45200aa0e8498b176/src/def/babel-core.ts#L159) is accessing the `.pattern` property of a RegExp object, which doesn't seem to exist according to MDN. If the `.pattern` access is replaced with `.source`, the output becomes correct.

Version information:
- ast-types: 0.15.2 and 0.16.0.
- Node.js: v18.12.1

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at src/def/babel-core.ts line 159 and inspect how RegExpLiteral populates its extra field. Use the reproducer from the issue with b.regExpLiteral and getFieldValue; done means the resulting raw value is /test/ rather than /undefined/.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
tooling
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.