Skip to content

Commit cc64c70

Browse files
committed
1 parent dd3e313 commit cc64c70

File tree

8 files changed

+145
-3
lines changed

8 files changed

+145
-3
lines changed

src/ast.js

+9
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ AST.prototype.prepare = function(kind, docs, parser) {
291291
result.setKind = function(newKind) {
292292
kind = newKind;
293293
};
294+
/**
295+
* Release a node without using it on the AST
296+
*/
297+
result.destroy = function() {
298+
if (docs) {
299+
// release current docs stack
300+
parser._docIndex = parser._docs.length - docs.length;
301+
}
302+
};
294303
return result;
295304
};
296305

src/parser.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ parser.prototype.node = function(name) {
353353
if (this._docIndex < this._docs.length) {
354354
const docs = this._docs.slice(this._docIndex);
355355
this._docIndex = this._docs.length;
356+
if (this.debug) {
357+
console.log(new Error("Append docs on " + name));
358+
console.log(docs);
359+
}
356360
return this.ast.prepare(name, docs, this);
357361
}
358362
}
@@ -379,7 +383,7 @@ parser.prototype.expectEndOfStatement = function(node) {
379383
};
380384

381385
/** outputs some debug information on current token **/
382-
const ignoreStack = ["parser.next"];
386+
const ignoreStack = ["parser.next", "parser.node", "parser.showlog"];
383387
parser.prototype.showlog = function() {
384388
const stack = new Error().stack.split("\n");
385389
let line;

src/parser/expr.js

+9
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ module.exports = {
8080
}
8181
this.expect(":") && this.next();
8282
return result("retif", expr, trueArg, this.read_expr());
83+
} else {
84+
// see #193
85+
result.destroy();
8386
}
8487

8588
return expr;
@@ -419,6 +422,9 @@ module.exports = {
419422
if (isConst) this.error("VARIABLE");
420423
this.next();
421424
return result("post", "-", expr);
425+
default:
426+
// see #193
427+
result.destroy();
422428
}
423429
} else if (this.is("SCALAR")) {
424430
result = this.node();
@@ -429,6 +435,9 @@ module.exports = {
429435
if (expr.loc) list.loc = expr.loc;
430436
let right = this.next().read_expr();
431437
return result("assign", list, right, "=");
438+
} else {
439+
// see #193
440+
result.destroy();
432441
}
433442
// classic array
434443
return this.handleDereferencable(expr);

src/parser/scalar.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ module.exports = {
222222
} else if (this.token === this.tok.T_CURLY_OPEN) {
223223
// expression
224224
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1246
225+
result.destroy();
225226
result = this.next().read_variable(false, false, false);
226227
if (result.kind === "variable") {
227228
result.curly = true;
@@ -230,6 +231,7 @@ module.exports = {
230231
} else if (this.token === this.tok.T_VARIABLE) {
231232
// plain variable
232233
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1231
234+
result.destroy();
233235
result = this.read_simple_variable(false);
234236

235237
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1233
@@ -243,8 +245,8 @@ module.exports = {
243245
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1236
244246
if (this.token === this.tok.T_OBJECT_OPERATOR) {
245247
node = this.node("propertylookup");
246-
const what = this.node("constref");
247248
this.next().expect(this.tok.T_STRING);
249+
const what = this.node("constref");
248250
name = this.text();
249251
this.next();
250252
result = node(result, what(name));

src/parser/variable.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module.exports = {
5555
result = result("constref", name);
5656
}
5757
} else {
58+
// @fixme possible #193 bug
5859
result = name;
5960
}
6061
} else if (this.token === this.tok.T_STATIC) {
@@ -296,7 +297,10 @@ module.exports = {
296297
offset = this.next().read_expr();
297298
this.expect("}") && this.next();
298299
result = node("offsetlookup", result, offset);
299-
} else break;
300+
} else {
301+
node.destroy();
302+
break;
303+
}
300304
}
301305
return result;
302306
},

test/snapshot/__snapshots__/acid.test.js.snap

+21
Original file line numberDiff line numberDiff line change
@@ -6932,6 +6932,27 @@ FOO
69326932
THE END ...
69336933
",
69346934
"kind": "halt",
6935+
"leadingComments": Array [
6936+
CommentLine {
6937+
"kind": "commentline",
6938+
"loc": Location {
6939+
"end": Position {
6940+
"column": 0,
6941+
"line": 140,
6942+
"offset": 2977,
6943+
},
6944+
"source": "// list version
6945+
",
6946+
"start": Position {
6947+
"column": 2,
6948+
"line": 139,
6949+
"offset": 2961,
6950+
},
6951+
},
6952+
"value": "// list version
6953+
",
6954+
},
6955+
],
69356956
"loc": Location {
69366957
"end": Position {
69376958
"column": 17,

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

+74
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,68 @@ Program {
142142
}
143143
`;
144144

145+
exports[`Test comments issues fix #193 1`] = `
146+
Program {
147+
"children": Array [
148+
Assign {
149+
"kind": "assign",
150+
"left": Variable {
151+
"byref": false,
152+
"curly": false,
153+
"kind": "variable",
154+
"name": "a",
155+
},
156+
"operator": "=",
157+
"right": Call {
158+
"arguments": Array [],
159+
"kind": "call",
160+
"what": PropertyLookup {
161+
"kind": "propertylookup",
162+
"leadingComments": Array [
163+
CommentLine {
164+
"kind": "commentline",
165+
"value": "// Comment Before
166+
",
167+
},
168+
],
169+
"offset": ConstRef {
170+
"kind": "constref",
171+
"leadingComments": Array [
172+
CommentLine {
173+
"kind": "commentline",
174+
"value": "// Comment After
175+
",
176+
},
177+
],
178+
"name": "each",
179+
},
180+
"what": Variable {
181+
"byref": false,
182+
"curly": false,
183+
"kind": "variable",
184+
"name": "var",
185+
},
186+
},
187+
},
188+
},
189+
],
190+
"comments": Array [
191+
CommentLine {
192+
"kind": "commentline",
193+
"value": "// Comment Before
194+
",
195+
},
196+
CommentLine {
197+
"kind": "commentline",
198+
"value": "// Comment After
199+
",
200+
},
201+
],
202+
"errors": Array [],
203+
"kind": "program",
204+
}
205+
`;
206+
145207
exports[`Test comments multi line comments test function 1`] = `
146208
Program {
147209
"children": Array [
@@ -347,6 +409,14 @@ Program {
347409
"kind": "commentblock",
348410
"value": "/* ignore */",
349411
},
412+
CommentBlock {
413+
"kind": "commentblock",
414+
"value": "/* */",
415+
},
416+
CommentBlock {
417+
"kind": "commentblock",
418+
"value": "/* ignore */",
419+
},
350420
],
351421
},
352422
"kind": "if",
@@ -525,6 +595,10 @@ Program {
525595
"children": Array [],
526596
"kind": "block",
527597
"leadingComments": Array [
598+
CommentBlock {
599+
"kind": "commentblock",
600+
"value": "/* bb */",
601+
},
528602
CommentBlock {
529603
"kind": "commentblock",
530604
"value": "/* dd */",

test/snapshot/comment.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ describe("Test comments", function() {
3939
);
4040
expect(ast).toMatchSnapshot();
4141
});
42+
43+
it("fix #193", function() {
44+
const ast = parser.parseEval(
45+
`
46+
$a = $var
47+
// Comment Before
48+
->
49+
// Comment After
50+
each();
51+
`,
52+
{
53+
parser: {
54+
extractDoc: true,
55+
// debug: true
56+
}
57+
}
58+
);
59+
expect(ast).toMatchSnapshot();
60+
});
4261
});
4362

4463
it("test single line comments", function() {

0 commit comments

Comments
 (0)