Skip to content

Commit 7621a52

Browse files
alexander-akaitclaude
authored andcommitted
fix(javascript): read past a class setter with no parameter
parseProgram reads past "setter should have exactly one param", and the rest-parameter check after it then read the missing first parameter and threw a TypeError. It now runs only when there is one parameter, as the object-literal setter already did. The acorn parity test lets ours read on where acorn crashes after an error it noted, so long as ours notes the same errors first. Claude-Session: https://claude.ai/code/session_015gdnghKbLJ7VvJ5nNCNHhH
1 parent 2305a7c commit 7621a52

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

‎lib/javascript/syntax-parser.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,8 +3245,10 @@ class SyntaxParser {
32453245
value.start,
32463246
"setter should have exactly one param"
32473247
);
3248-
}
3249-
if (method.kind === "set" && value.params[0].type === "RestElement") {
3248+
} else if (
3249+
method.kind === "set" &&
3250+
value.params[0].type === "RestElement"
3251+
) {
32503252
this.raiseRecoverable(
32513253
value.params[0].start,
32523254
"Setter cannot use rest params"
@@ -17727,8 +17729,7 @@ class WebpackParser extends BaseParser {
1772717729
value.start,
1772817730
"setter should have exactly one param"
1772917731
);
17730-
}
17731-
if (
17732+
} else if (
1773217733
target.kind === "set" &&
1773317734
this._typeOf(value.params[0]) === "RestElement"
1773417735
) {

‎test/unitCases/javascript/parser.unittest.js‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4619,6 +4619,19 @@ describe("Parser", () => {
46194619
source,
46204620
{ ...given, ranges: Boolean(options.ranges) || lazy }
46214621
);
4622+
// Where acorn crashes after an error it noted, ours may read on
4623+
// past it, noting at least the same errors first.
4624+
if (
4625+
theirs.error === "TypeError" &&
4626+
theirs.noted.length > 0 &&
4627+
ours.error === undefined &&
4628+
JSON.stringify(ours.noted).startsWith(
4629+
JSON.stringify(theirs.noted).slice(0, -1)
4630+
)
4631+
) {
4632+
recovered++;
4633+
continue;
4634+
}
46224635
const tree = firstDifference(ours.ast, theirs.ast, "program");
46234636
if (
46244637
tree ||
@@ -5796,7 +5809,8 @@ describe("parseProgram", () => {
57965809
it.each([
57975810
["let a; let a", ["var-redeclaration", "", "warning", "11"]],
57985811
["/(/", ["invalid-regular-expression", "", "warning", "1"]],
5799-
["class A { #a; #a }", ["private-name-redeclaration", "", "warning", "14"]]
5812+
["class A { #a; #a }", ["private-name-redeclaration", "", "warning", "14"]],
5813+
["class A { set x() {} }", ["bad-setter-arity", "", "warning", "15"]]
58005814
])("reads past %j", (source, error) => {
58015815
const { program, errors } = collect(source);
58025816
expect(program).toBeDefined();

0 commit comments

Comments
 (0)