Skip to content

Commit afe817d

Browse files
committed
fix: comments in named arguments
1 parent 0b7781f commit afe817d

File tree

5 files changed

+230
-20
lines changed

5 files changed

+230
-20
lines changed

src/parser/function.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,18 @@ module.exports = {
294294
this.token === this.tok.T_STRING ||
295295
Object.values(this.lexer.keywords).includes(this.token)
296296
) {
297-
const backup = [this.token, this.lexer.getState()];
298-
const name = this.text();
299-
this.next();
300-
if (this.token === ":") {
301-
return this.node("namedargument")(name, this.next().read_expr());
297+
const lexerState = this.lexer.getState();
298+
const nextToken = this.lexer.lex();
299+
this.lexer.setState(lexerState);
300+
if (nextToken === ":") {
301+
if (this.version < 800) {
302+
this.raiseError("PHP 8+ is required to use named arguments");
303+
}
304+
return this.node("namedargument")(
305+
this.text(),
306+
this.next().next().read_expr()
307+
);
302308
}
303-
this.lexer.tokens.push(backup);
304-
this.next();
305309
}
306310
return this.read_expr();
307311
},

test/snapshot/__snapshots__/call.test.js.snap

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,6 @@ Program {
135135
},
136136
],
137137
"kind": "array",
138-
"leadingComments": Array [
139-
CommentLine {
140-
"kind": "commentline",
141-
"offset": 10,
142-
"value": "// comment
143-
",
144-
},
145-
],
146138
"shortForm": false,
147139
},
148140
],
@@ -163,11 +155,43 @@ Program {
163155
"value": "// comment
164156
",
165157
},
166-
CommentLine {
167-
"kind": "commentline",
168-
"offset": 10,
169-
"value": "// comment
170-
",
158+
],
159+
"errors": Array [],
160+
"kind": "program",
161+
}
162+
`;
163+
164+
exports[`Test call doesnt confused static methods with named arguments 1`] = `
165+
Program {
166+
"children": Array [
167+
ExpressionStatement {
168+
"expression": Call {
169+
"arguments": Array [
170+
Call {
171+
"arguments": Array [],
172+
"kind": "call",
173+
"what": StaticLookup {
174+
"kind": "staticlookup",
175+
"offset": Identifier {
176+
"kind": "identifier",
177+
"name": "bar",
178+
},
179+
"what": Name {
180+
"kind": "name",
181+
"name": "a",
182+
"resolution": "uqn",
183+
},
184+
},
185+
},
186+
],
187+
"kind": "call",
188+
"what": Name {
189+
"kind": "name",
190+
"name": "foo",
191+
"resolution": "uqn",
192+
},
193+
},
194+
"kind": "expressionstatement",
171195
},
172196
],
173197
"errors": Array [],

test/snapshot/__snapshots__/comment.test.js.snap

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,148 @@ Program {
850850
}
851851
`;
852852

853+
exports[`Test comments issues fix call comments 1`] = `
854+
Program {
855+
"children": Array [
856+
ExpressionStatement {
857+
"expression": Call {
858+
"arguments": Array [
859+
Array {
860+
"items": Array [],
861+
"kind": "array",
862+
"loc": Location {
863+
"end": Position {
864+
"column": 2,
865+
"line": 3,
866+
"offset": 25,
867+
},
868+
"source": "array // comment
869+
()",
870+
"start": Position {
871+
"column": 5,
872+
"line": 2,
873+
"offset": 6,
874+
},
875+
},
876+
"shortForm": false,
877+
},
878+
],
879+
"kind": "call",
880+
"loc": Location {
881+
"end": Position {
882+
"column": 4,
883+
"line": 3,
884+
"offset": 27,
885+
},
886+
"source": "call(array // comment
887+
());",
888+
"start": Position {
889+
"column": 0,
890+
"line": 2,
891+
"offset": 1,
892+
},
893+
},
894+
"what": Name {
895+
"kind": "name",
896+
"loc": Location {
897+
"end": Position {
898+
"column": 4,
899+
"line": 2,
900+
"offset": 5,
901+
},
902+
"source": "call",
903+
"start": Position {
904+
"column": 0,
905+
"line": 2,
906+
"offset": 1,
907+
},
908+
},
909+
"name": "call",
910+
"resolution": "uqn",
911+
"trailingComments": Array [
912+
CommentLine {
913+
"kind": "commentline",
914+
"loc": Location {
915+
"end": Position {
916+
"column": 0,
917+
"line": 3,
918+
"offset": 23,
919+
},
920+
"source": "// comment
921+
",
922+
"start": Position {
923+
"column": 11,
924+
"line": 2,
925+
"offset": 12,
926+
},
927+
},
928+
"offset": 12,
929+
"value": "// comment
930+
",
931+
},
932+
],
933+
},
934+
},
935+
"kind": "expressionstatement",
936+
"loc": Location {
937+
"end": Position {
938+
"column": 4,
939+
"line": 3,
940+
"offset": 27,
941+
},
942+
"source": "call(array // comment
943+
());",
944+
"start": Position {
945+
"column": 0,
946+
"line": 2,
947+
"offset": 1,
948+
},
949+
},
950+
},
951+
],
952+
"comments": Array [
953+
CommentLine {
954+
"kind": "commentline",
955+
"loc": Location {
956+
"end": Position {
957+
"column": 0,
958+
"line": 3,
959+
"offset": 23,
960+
},
961+
"source": "// comment
962+
",
963+
"start": Position {
964+
"column": 11,
965+
"line": 2,
966+
"offset": 12,
967+
},
968+
},
969+
"offset": 12,
970+
"value": "// comment
971+
",
972+
},
973+
],
974+
"errors": Array [],
975+
"kind": "program",
976+
"loc": Location {
977+
"end": Position {
978+
"column": 8,
979+
"line": 4,
980+
"offset": 36,
981+
},
982+
"source": "
983+
call(array // comment
984+
());
985+
",
986+
"start": Position {
987+
"column": 0,
988+
"line": 1,
989+
"offset": 0,
990+
},
991+
},
992+
}
993+
`;
994+
853995
exports[`Test comments issues impl #194 1`] = `
854996
Program {
855997
"children": Array [

test/snapshot/call.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,25 @@ describe("Test call", function () {
253253
});
254254
expect(astErr).toMatchSnapshot();
255255
});
256+
it("named arguments are not supported in php 7.2", function () {
257+
expect(() =>
258+
parser.parseEval(`foo(a: $a);`, {
259+
parser: {
260+
version: "7.2",
261+
debug: false,
262+
},
263+
})
264+
).toThrow("PHP 8+ is required to use named arguments");
265+
});
266+
it("doesnt confused static methods with named arguments", function () {
267+
const astErr = parser.parseEval(`foo(a::bar());`, {
268+
parser: {
269+
version: "8.0",
270+
debug: false,
271+
},
272+
});
273+
expect(astErr).toMatchSnapshot();
274+
});
256275
it("keyword as named argument", function () {
257276
const astErr = parser.parseEval(`foo(array: $a);`, {
258277
parser: {

test/snapshot/comment.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ bar() /* inner */ ;
2626
).toMatchSnapshot();
2727
});
2828

29+
it("fix call comments", function () {
30+
expect(
31+
parser.parseEval(
32+
`
33+
call(array // comment
34+
());
35+
`,
36+
{
37+
parser: {
38+
extractDoc: true,
39+
// debug: true
40+
},
41+
ast: {
42+
withPositions: true,
43+
withSource: true,
44+
},
45+
}
46+
)
47+
).toMatchSnapshot();
48+
});
49+
2950
it("fix #126 : new option", function () {
3051
const ast = parser.parseEval(
3152
`

0 commit comments

Comments
 (0)