Skip to content

Commit

Permalink
Merge pull request #701 from czosel/fix-named-arguments-comments
Browse files Browse the repository at this point in the history
Fix named arguments comments
  • Loading branch information
ichiriac committed Apr 4, 2021
2 parents dcbd0f7 + afe817d commit bb89fff
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 20 deletions.
18 changes: 11 additions & 7 deletions src/parser/function.js
Expand Up @@ -319,14 +319,18 @@ module.exports = {
this.token === this.tok.T_STRING ||
Object.values(this.lexer.keywords).includes(this.token)
) {
const backup = [this.token, this.lexer.getState()];
const name = this.text();
this.next();
if (this.token === ":") {
return this.node("namedargument")(name, this.next().read_expr());
const lexerState = this.lexer.getState();
const nextToken = this.lexer.lex();
this.lexer.setState(lexerState);
if (nextToken === ":") {
if (this.version < 800) {
this.raiseError("PHP 8+ is required to use named arguments");
}
return this.node("namedargument")(
this.text(),
this.next().next().read_expr()
);
}
this.lexer.tokens.push(backup);
this.next();
}
return this.read_expr();
},
Expand Down
50 changes: 37 additions & 13 deletions test/snapshot/__snapshots__/call.test.js.snap
Expand Up @@ -135,14 +135,6 @@ Program {
},
],
"kind": "array",
"leadingComments": Array [
CommentLine {
"kind": "commentline",
"offset": 10,
"value": "// comment
",
},
],
"shortForm": false,
},
],
Expand All @@ -163,11 +155,43 @@ Program {
"value": "// comment
",
},
CommentLine {
"kind": "commentline",
"offset": 10,
"value": "// comment
",
],
"errors": Array [],
"kind": "program",
}
`;

exports[`Test call doesnt confused static methods with named arguments 1`] = `
Program {
"children": Array [
ExpressionStatement {
"expression": Call {
"arguments": Array [
Call {
"arguments": Array [],
"kind": "call",
"what": StaticLookup {
"kind": "staticlookup",
"offset": Identifier {
"kind": "identifier",
"name": "bar",
},
"what": Name {
"kind": "name",
"name": "a",
"resolution": "uqn",
},
},
},
],
"kind": "call",
"what": Name {
"kind": "name",
"name": "foo",
"resolution": "uqn",
},
},
"kind": "expressionstatement",
},
],
"errors": Array [],
Expand Down
142 changes: 142 additions & 0 deletions test/snapshot/__snapshots__/comment.test.js.snap
Expand Up @@ -850,6 +850,148 @@ Program {
}
`;

exports[`Test comments issues fix call comments 1`] = `
Program {
"children": Array [
ExpressionStatement {
"expression": Call {
"arguments": Array [
Array {
"items": Array [],
"kind": "array",
"loc": Location {
"end": Position {
"column": 2,
"line": 3,
"offset": 25,
},
"source": "array // comment
()",
"start": Position {
"column": 5,
"line": 2,
"offset": 6,
},
},
"shortForm": false,
},
],
"kind": "call",
"loc": Location {
"end": Position {
"column": 4,
"line": 3,
"offset": 27,
},
"source": "call(array // comment
());",
"start": Position {
"column": 0,
"line": 2,
"offset": 1,
},
},
"what": Name {
"kind": "name",
"loc": Location {
"end": Position {
"column": 4,
"line": 2,
"offset": 5,
},
"source": "call",
"start": Position {
"column": 0,
"line": 2,
"offset": 1,
},
},
"name": "call",
"resolution": "uqn",
"trailingComments": Array [
CommentLine {
"kind": "commentline",
"loc": Location {
"end": Position {
"column": 0,
"line": 3,
"offset": 23,
},
"source": "// comment
",
"start": Position {
"column": 11,
"line": 2,
"offset": 12,
},
},
"offset": 12,
"value": "// comment
",
},
],
},
},
"kind": "expressionstatement",
"loc": Location {
"end": Position {
"column": 4,
"line": 3,
"offset": 27,
},
"source": "call(array // comment
());",
"start": Position {
"column": 0,
"line": 2,
"offset": 1,
},
},
},
],
"comments": Array [
CommentLine {
"kind": "commentline",
"loc": Location {
"end": Position {
"column": 0,
"line": 3,
"offset": 23,
},
"source": "// comment
",
"start": Position {
"column": 11,
"line": 2,
"offset": 12,
},
},
"offset": 12,
"value": "// comment
",
},
],
"errors": Array [],
"kind": "program",
"loc": Location {
"end": Position {
"column": 8,
"line": 4,
"offset": 36,
},
"source": "
call(array // comment
());
",
"start": Position {
"column": 0,
"line": 1,
"offset": 0,
},
},
}
`;

exports[`Test comments issues impl #194 1`] = `
Program {
"children": Array [
Expand Down
19 changes: 19 additions & 0 deletions test/snapshot/call.test.js
Expand Up @@ -253,6 +253,25 @@ describe("Test call", function () {
});
expect(astErr).toMatchSnapshot();
});
it("named arguments are not supported in php 7.2", function () {
expect(() =>
parser.parseEval(`foo(a: $a);`, {
parser: {
version: "7.2",
debug: false,
},
})
).toThrow("PHP 8+ is required to use named arguments");
});
it("doesnt confused static methods with named arguments", function () {
const astErr = parser.parseEval(`foo(a::bar());`, {
parser: {
version: "8.0",
debug: false,
},
});
expect(astErr).toMatchSnapshot();
});
it("keyword as named argument", function () {
const astErr = parser.parseEval(`foo(array: $a);`, {
parser: {
Expand Down
21 changes: 21 additions & 0 deletions test/snapshot/comment.test.js
Expand Up @@ -26,6 +26,27 @@ bar() /* inner */ ;
).toMatchSnapshot();
});

it("fix call comments", function () {
expect(
parser.parseEval(
`
call(array // comment
());
`,
{
parser: {
extractDoc: true,
// debug: true
},
ast: {
withPositions: true,
withSource: true,
},
}
)
).toMatchSnapshot();
});

it("fix #126 : new option", function () {
const ast = parser.parseEval(
`
Expand Down

0 comments on commit bb89fff

Please sign in to comment.