Skip to content

Commit 7ebaecb

Browse files
committed
glayzzle#168 : fix ?> locations - ignore T_CLOSE_TAG
1 parent 8165be5 commit 7ebaecb

File tree

4 files changed

+276
-8
lines changed

4 files changed

+276
-8
lines changed

src/parser.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,15 @@ parser.prototype.text = function() {
440440
/** consume the next token **/
441441
parser.prototype.next = function() {
442442
// prepare the back command
443-
this.prev = [
444-
this.lexer.yylloc.last_line,
445-
this.lexer.yylloc.last_column,
446-
this.lexer.offset
447-
];
443+
if (this.token !== ';' || this.lexer.yytext === ';') {
444+
// ignore '?>' from automated resolution
445+
// https://github.com/glayzzle/php-parser/issues/168
446+
this.prev = [
447+
this.lexer.yylloc.last_line,
448+
this.lexer.yylloc.last_column,
449+
this.lexer.offset
450+
];
451+
}
448452

449453
// eating the token
450454
this.lex();

test/snapshot/__snapshots__/if.test.js.snap

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,228 @@ Program {
157157
}
158158
`;
159159

160+
exports[`Test IF statements test issue #168 1`] = `
161+
Program {
162+
"children": Array [
163+
If {
164+
"alternate": null,
165+
"body": null,
166+
"kind": "if",
167+
"loc": Location {
168+
"end": Position {
169+
"column": 16,
170+
"line": 1,
171+
"offset": 16,
172+
},
173+
"source": "if ($foo);",
174+
"start": Position {
175+
"column": 6,
176+
"line": 1,
177+
"offset": 6,
178+
},
179+
},
180+
"shortForm": false,
181+
"test": Variable {
182+
"byref": false,
183+
"curly": false,
184+
"kind": "variable",
185+
"loc": Location {
186+
"end": Position {
187+
"column": 14,
188+
"line": 1,
189+
"offset": 14,
190+
},
191+
"source": "$foo",
192+
"start": Position {
193+
"column": 10,
194+
"line": 1,
195+
"offset": 10,
196+
},
197+
},
198+
"name": "foo",
199+
},
200+
},
201+
Inline {
202+
"kind": "inline",
203+
"loc": Location {
204+
"end": Position {
205+
"column": 6,
206+
"line": 2,
207+
"offset": 26,
208+
},
209+
"source": " ",
210+
"start": Position {
211+
"column": 0,
212+
"line": 2,
213+
"offset": 20,
214+
},
215+
},
216+
"raw": "
217+
",
218+
"value": " ",
219+
},
220+
],
221+
"errors": Array [],
222+
"kind": "program",
223+
"loc": Location {
224+
"end": Position {
225+
"column": 6,
226+
"line": 2,
227+
"offset": 26,
228+
},
229+
"source": "<?php if ($foo); ?>
230+
",
231+
"start": Position {
232+
"column": 0,
233+
"line": 1,
234+
"offset": 0,
235+
},
236+
},
237+
}
238+
`;
239+
240+
exports[`Test IF statements test issue #168 2`] = `
241+
Program {
242+
"children": Array [
243+
If {
244+
"alternate": null,
245+
"body": null,
246+
"kind": "if",
247+
"loc": Location {
248+
"end": Position {
249+
"column": 15,
250+
"line": 1,
251+
"offset": 15,
252+
},
253+
"source": "if ($foo)",
254+
"start": Position {
255+
"column": 6,
256+
"line": 1,
257+
"offset": 6,
258+
},
259+
},
260+
"shortForm": false,
261+
"test": Variable {
262+
"byref": false,
263+
"curly": false,
264+
"kind": "variable",
265+
"loc": Location {
266+
"end": Position {
267+
"column": 14,
268+
"line": 1,
269+
"offset": 14,
270+
},
271+
"source": "$foo",
272+
"start": Position {
273+
"column": 10,
274+
"line": 1,
275+
"offset": 10,
276+
},
277+
},
278+
"name": "foo",
279+
},
280+
},
281+
Inline {
282+
"kind": "inline",
283+
"loc": Location {
284+
"end": Position {
285+
"column": 6,
286+
"line": 2,
287+
"offset": 25,
288+
},
289+
"source": " ",
290+
"start": Position {
291+
"column": 0,
292+
"line": 2,
293+
"offset": 19,
294+
},
295+
},
296+
"raw": "
297+
",
298+
"value": " ",
299+
},
300+
],
301+
"errors": Array [],
302+
"kind": "program",
303+
"loc": Location {
304+
"end": Position {
305+
"column": 6,
306+
"line": 2,
307+
"offset": 25,
308+
},
309+
"source": "<?php if ($foo) ?>
310+
",
311+
"start": Position {
312+
"column": 0,
313+
"line": 1,
314+
"offset": 0,
315+
},
316+
},
317+
}
318+
`;
319+
320+
exports[`Test IF statements test issue #168 3`] = `
321+
Program {
322+
"children": Array [
323+
If {
324+
"alternate": null,
325+
"body": null,
326+
"kind": "if",
327+
"loc": Location {
328+
"end": Position {
329+
"column": 7,
330+
"line": 2,
331+
"offset": 23,
332+
},
333+
"source": "if ($foo)
334+
;",
335+
"start": Position {
336+
"column": 6,
337+
"line": 1,
338+
"offset": 6,
339+
},
340+
},
341+
"shortForm": false,
342+
"test": Variable {
343+
"byref": false,
344+
"curly": false,
345+
"kind": "variable",
346+
"loc": Location {
347+
"end": Position {
348+
"column": 14,
349+
"line": 1,
350+
"offset": 14,
351+
},
352+
"source": "$foo",
353+
"start": Position {
354+
"column": 10,
355+
"line": 1,
356+
"offset": 10,
357+
},
358+
},
359+
"name": "foo",
360+
},
361+
},
362+
],
363+
"errors": Array [],
364+
"kind": "program",
365+
"loc": Location {
366+
"end": Position {
367+
"column": 7,
368+
"line": 2,
369+
"offset": 23,
370+
},
371+
"source": "<?php if ($foo)
372+
;",
373+
"start": Position {
374+
"column": 0,
375+
"line": 1,
376+
"offset": 0,
377+
},
378+
},
379+
}
380+
`;
381+
160382
exports[`Test IF statements test short form 1`] = `
161383
Program {
162384
"children": Array [

test/snapshot/__snapshots__/location.test.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ Program {
208208
"kind": "program",
209209
"loc": Location {
210210
"end": Position {
211-
"column": 20,
211+
"column": 17,
212212
"line": 1,
213-
"offset": 20,
213+
"offset": 17,
214214
},
215-
"source": "<?php $a = $b + 1 ?>",
215+
"source": "<?php $a = $b + 1",
216216
"start": Position {
217217
"column": 0,
218218
"line": 1,

test/snapshot/if.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,46 @@ describe("Test IF statements", function() {
7575
);
7676
expect(ast).toMatchSnapshot();
7777
});
78+
it("test issue #168", () => {
79+
80+
// should be ok
81+
let ast = parser.parseCode(
82+
`<?php if ($foo); ?>
83+
`,
84+
{
85+
ast: {
86+
withPositions: true,
87+
withSource: true
88+
}
89+
}
90+
);
91+
expect(ast).toMatchSnapshot();
92+
93+
// should ignore ?>
94+
ast = parser.parseCode(
95+
`<?php if ($foo) ?>
96+
`,
97+
{
98+
ast: {
99+
withPositions: true,
100+
withSource: true
101+
}
102+
}
103+
);
104+
expect(ast).toMatchSnapshot();
105+
106+
// should include ';'
107+
ast = parser.parseCode(
108+
`<?php if ($foo)
109+
;`,
110+
{
111+
ast: {
112+
withPositions: true,
113+
withSource: true
114+
}
115+
}
116+
);
117+
expect(ast).toMatchSnapshot();
118+
119+
});
78120
});

0 commit comments

Comments
 (0)