fix(ast/estree): fix serializing RegExpLiteral::value field#9028
fix(ast/estree): fix serializing RegExpLiteral::value field#9028overlookmotel wants to merge 1 commit intomainfrom
RegExpLiteral::value field#9028Conversation
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
de58aec to
d69951a
Compare
f28d817 to
2b47299
Compare
d69951a to
03fd1eb
Compare
CodSpeed Performance ReportMerging #9028 will not alter performanceComparing Summary
|
|
Oh, this is pretty confusing. The reason why local run shows Node 23 seems to catch up with this, so $ node --version
v23.7.0
$ node repro.mjs
Node {
type: 'Literal',
start: 0,
end: 9,
value: /(?i:a)b/,
raw: '/(?i:a)b/',
regex: { pattern: '(?i:a)b', flags: '' }
}
$ node --version
v22.13.1
$ node repro.mjs
Node {
type: 'Literal',
start: 0,
end: 9,
value: null,
raw: '/(?i:a)b/',
regex: { pattern: '(?i:a)b', flags: '' }
}Since serialization assumes json, supporting arbitrary |
|
@hi-ogawa Thanks very much for figuring this out. Right, so there is no canonically correct JSON for Also, when running Acorn from JS, you would never get The code which handles this in Rollup is: function literalRegExp(position, buffer): LiteralRegExpNode {
const flags = buffer.convertString(buffer[position + 2]);
const pattern = buffer.convertString(buffer[position + 3]);
return {
type: 'Literal',
start: buffer[position],
end: buffer[position + 1],
raw: `/${pattern}/${flags}`,
regex: { flags, pattern },
value: new RegExp(pattern, flags)
};
}So rollup's Our primary target is matching Rollup, so here's what I think we should do:
We can improve on Rollup by using this code on JS side: function convertRegexp(regexpLiteral) {
try {
regexpLiteral.value = new RegExp(regexpLiteral.regex.pattern, regexpLiteral.regex.flags);
} catch (_err) {}
}JS-side code: Step 1: The quick-and-dirty first implementation can be a complete AST traversal on JS side, setting the Step 2: Improve performance by compiling a list of "path"s to where Step 3: AST transfer will make the problem go away entirely, since it constructs all AST node I think this is at least an OK battle plan, so am going to go ahead and take the first step now. We can revise approach later on if we can figure out something better. |
Output `null` in JSON for `value` field of `RegExpLiteral`. This PR also bumps the `acorn-estree` submodule, to include commit oxc-project/estree-conformance@bd99d40 which does the same in the JSON snapshots of Acorn's output. This is first step as described in #9028 (comment). A later PR will set the `value` field correctly on JS side.

About half the remaining failing conformance tests relate to
RegExps. This should fix them, but it's not working, and I don't know why.It looks like the
valuefield should be{}for a valid regexp, andnullfor an invalid one. So this PR enables parsing regexps in Oxc's parser, and setsvaluefield accordingly depending on whether the regexp parser says it's valid or not.The weird thing is that some cases which should pass, still don't. e.g.:
https://github.com/tc39/test262/blob/bc5c14176e2b11a78859571eb693f028c8822458/test/built-ins/RegExp/regexp-modifiers/add-ignoreCase.js
This test contains
var re1 = /(?i:a)b/;, which is a valid regexp.Sure enough, AST Explorer shows
value: {}. https://ast.sxzz.dev/#eNoliTsKhDAQhq8if7ULgn2avcSy1TSz4xSRmAlJFES8uwN23+NEQsDCOzepsXSMKB5YrGZncZ5enxj4/Z/czf2kPAwElZV/Wlu0TAgeEndtnTA+v9lWRb9H0WevNm/JmfKF6wZsLyU5But when running Acorn locally (v8.14.0, same as AST Explorer uses), it shows
value: null. Whaaaat?@sxzz As the author of AST Explorer, do you have any insight into this?