benjamn / ast-types

Esprima-compatible implementation of the Mozilla JS Parser API
MIT License
1.14k stars 197 forks source link

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

Open yjwong opened 1 year ago

yjwong commented 1 year ago

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:

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

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

The output was:

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

The expected output is:

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

Upon investigation, it appears that this line 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: