Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/parser/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,29 @@ module.exports = {
* ```
*/
read_parameter_list: function (is_class_constructor) {
if (this.token != ")") {
if (this.token !== ")") {
let wasVariadic = false;

return this.read_list_with_dangling_comma(
this.read_parameter.bind(this, is_class_constructor)
function () {
const parameter = this.read_parameter(is_class_constructor);
if (parameter) {
// variadic parameters can only be defined at the end of the parameter list
if (wasVariadic) {
this.raiseError(
"Unexpected parameter after a variadic parameter"
);
}
if (parameter.variadic) {
wasVariadic = true;
}
}
return parameter;
}.bind(this),
","
);
}

return [];
},
/*
Expand Down Expand Up @@ -388,10 +406,14 @@ module.exports = {
function () {
const argument = this.read_argument();
if (argument) {
if (wasVariadic) {
this.raiseError("Unexpected argument after a variadic argument");
const isVariadic = argument.kind === "variadic";
// variadic arguments can only be followed by other variadic arguments
if (wasVariadic && !isVariadic) {
this.raiseError(
"Unexpected non-variadic argument after a variadic argument"
);
}
if (argument.kind === "variadic") {
if (isVariadic) {
wasVariadic = true;
}
}
Expand Down
85 changes: 85 additions & 0 deletions test/snapshot/__snapshots__/call.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,91 @@ Program {
}
`;

exports[`Test call handles spread operator at call site 1`] = `
Program {
"children": Array [
ExpressionStatement {
"expression": Call {
"arguments": Array [
variadic {
"kind": "variadic",
"what": Variable {
"curly": false,
"kind": "variable",
"name": "bar",
},
},
],
"kind": "call",
"what": Name {
"kind": "name",
"name": "foo",
"resolution": "uqn",
},
},
"kind": "expressionstatement",
},
ExpressionStatement {
"expression": Call {
"arguments": Array [
Variable {
"curly": false,
"kind": "variable",
"name": "bar",
},
variadic {
"kind": "variadic",
"what": Variable {
"curly": false,
"kind": "variable",
"name": "baz",
},
},
],
"kind": "call",
"what": Name {
"kind": "name",
"name": "foo",
"resolution": "uqn",
},
},
"kind": "expressionstatement",
},
ExpressionStatement {
"expression": Call {
"arguments": Array [
variadic {
"kind": "variadic",
"what": Variable {
"curly": false,
"kind": "variable",
"name": "bar",
},
},
variadic {
"kind": "variadic",
"what": Variable {
"curly": false,
"kind": "variable",
"name": "baz",
},
},
],
"kind": "call",
"what": Name {
"kind": "name",
"name": "foo",
"resolution": "uqn",
},
},
"kind": "expressionstatement",
},
],
"errors": Array [],
"kind": "program",
}
`;

exports[`Test call inside offsetlookup 1`] = `
Program {
"children": Array [
Expand Down
87 changes: 5 additions & 82 deletions test/snapshot/__snapshots__/function.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1722,88 +1722,11 @@ Program {
}
`;

exports[`Function tests test variadic error 1`] = `
Program {
"children": Array [
ExpressionStatement {
"expression": Assign {
"kind": "assign",
"left": Variable {
"curly": false,
"kind": "variable",
"name": "b",
},
"operator": "=",
"right": Call {
"arguments": Array [
variadic {
"kind": "variadic",
"what": Array {
"items": Array [
Entry {
"byRef": false,
"key": null,
"kind": "entry",
"unpack": false,
"value": Number {
"kind": "number",
"value": "1",
},
},
Entry {
"byRef": false,
"key": null,
"kind": "entry",
"unpack": false,
"value": Number {
"kind": "number",
"value": "2",
},
},
Entry {
"byRef": false,
"key": null,
"kind": "entry",
"unpack": false,
"value": Number {
"kind": "number",
"value": "3",
},
},
],
"kind": "array",
"shortForm": true,
},
},
Variable {
"curly": false,
"kind": "variable",
"name": "a",
},
],
"kind": "call",
"what": Name {
"kind": "name",
"name": "foo",
"resolution": "uqn",
},
},
},
"kind": "expressionstatement",
},
],
"errors": Array [
Error {
"expected": undefined,
"kind": "error",
"line": 1,
"message": "Unexpected argument after a variadic argument on line 1",
"token": undefined,
},
],
"kind": "program",
}
`;
exports[`Function tests test variadic call error 1`] = `"Unexpected non-variadic argument after a variadic argument on line 1"`;

exports[`Function tests test variadic function error 1 1`] = `"Unexpected parameter after a variadic parameter on line 1"`;

exports[`Function tests test variadic function error 2 1`] = `"Unexpected parameter after a variadic parameter on line 1"`;

exports[`Function tests test without danging comma in closure use-block php 8.0 1`] = `
Program {
Expand Down
9 changes: 9 additions & 0 deletions test/snapshot/call.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,13 @@ describe("Test call", function () {
);
expect(astErr).toMatchSnapshot();
});
it("handles spread operator at call site", function () {
expect(
parser.parseEval(`
foo(...$bar);
foo($bar, ...$baz);
foo(...$bar, ...$baz);
`)
).toMatchSnapshot();
});
});
23 changes: 16 additions & 7 deletions test/snapshot/function.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,22 @@ describe("Function tests", function () {
expect(ast).toMatchSnapshot();
});

it("test variadic error", function () {
const astErr = parser.parseEval(`$b = foo(...[1, 2, 3], $a);`, {
parser: {
suppressErrors: true,
},
});
expect(astErr).toMatchSnapshot();
it("test variadic call error", function () {
expect(() =>
parser.parseEval(`$b = foo(...[1, 2, 3], $a);`)
).toThrowErrorMatchingSnapshot();
});

it("test variadic function error 1", function () {
expect(() =>
parser.parseEval(`function foo(...$bar, $baz) {}`)
).toThrowErrorMatchingSnapshot();
});

it("test variadic function error 2", function () {
expect(() =>
parser.parseEval(`function foo(...$bar, ...$baz) {}`)
).toThrowErrorMatchingSnapshot();
});

it("test reserved word for function name error", function () {
Expand Down