Skip to content

Commit 153efa9

Browse files
committed
fix glayzzle#189 - handle comments on expr
1 parent cc64c70 commit 153efa9

File tree

6 files changed

+171
-36
lines changed

6 files changed

+171
-36
lines changed

src/ast.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,18 @@ AST.prototype.prepare = function(kind, docs, parser) {
294294
/**
295295
* Release a node without using it on the AST
296296
*/
297-
result.destroy = function() {
297+
result.destroy = function(target) {
298298
if (docs) {
299299
// release current docs stack
300-
parser._docIndex = parser._docs.length - docs.length;
300+
if (target) {
301+
if (!target.leadingComments) {
302+
target.leadingComments = docs;
303+
} else {
304+
target.leadingComments = docs.concat(target.leadingComments);
305+
}
306+
} else {
307+
parser._docIndex = parser._docs.length - docs.length;
308+
}
301309
}
302310
};
303311
return result;

src/parser/expr.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module.exports = {
8282
return result("retif", expr, trueArg, this.read_expr());
8383
} else {
8484
// see #193
85-
result.destroy();
85+
result.destroy(expr);
8686
}
8787

8888
return expr;
@@ -122,9 +122,8 @@ module.exports = {
122122
result = result("number", "-" + this.text(), null);
123123
this.next();
124124
return result;
125-
} else {
126-
return result("unary", "-", this.read_expr());
127125
}
126+
return result("unary", "-", this.read_expr());
128127
}
129128

130129
if (this.token === "(") {
@@ -424,7 +423,7 @@ module.exports = {
424423
return result("post", "-", expr);
425424
default:
426425
// see #193
427-
result.destroy();
426+
result.destroy(expr);
428427
}
429428
} else if (this.is("SCALAR")) {
430429
result = this.node();
@@ -436,8 +435,8 @@ module.exports = {
436435
let right = this.next().read_expr();
437436
return result("assign", list, right, "=");
438437
} else {
439-
// see #193
440-
result.destroy();
438+
// see #189 - swap docs on nodes
439+
result.destroy(expr);
441440
}
442441
// classic array
443442
return this.handleDereferencable(expr);

src/parser/scalar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ module.exports = {
258258
const value = this.text();
259259
this.next();
260260
// consider it as string
261+
result.destroy();
261262
result = result("string", false, value, false, value);
262263
}
263264

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6181,6 +6181,27 @@ next:
61816181
},
61826182
Assign {
61836183
"kind": "assign",
6184+
"leadingComments": Array [
6185+
CommentLine {
6186+
"kind": "commentline",
6187+
"loc": Location {
6188+
"end": Position {
6189+
"column": 0,
6190+
"line": 140,
6191+
"offset": 2977,
6192+
},
6193+
"source": "// list version
6194+
",
6195+
"start": Position {
6196+
"column": 2,
6197+
"line": 139,
6198+
"offset": 2961,
6199+
},
6200+
},
6201+
"value": "// list version
6202+
",
6203+
},
6204+
],
61846205
"left": List {
61856206
"arguments": Array [
61866207
null,
@@ -6932,27 +6953,6 @@ FOO
69326953
THE END ...
69336954
",
69346955
"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-
],
69566956
"loc": Location {
69576957
"end": Position {
69586958
"column": 17,

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

Lines changed: 115 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,90 @@ Program {
142142
}
143143
`;
144144

145+
exports[`Test comments issues fix #189 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": "var",
155+
},
156+
"operator": "=",
157+
"right": Bin {
158+
"kind": "bin",
159+
"leadingComments": Array [
160+
CommentLine {
161+
"kind": "commentline",
162+
"value": "// Comment 1
163+
",
164+
},
165+
],
166+
"left": Bin {
167+
"kind": "bin",
168+
"left": String {
169+
"isDoubleQuote": false,
170+
"kind": "string",
171+
"raw": "'string1'",
172+
"unicode": false,
173+
"value": "string1",
174+
},
175+
"right": String {
176+
"isDoubleQuote": false,
177+
"kind": "string",
178+
"raw": "'string2'",
179+
"unicode": false,
180+
"value": "string2",
181+
},
182+
"type": ".",
183+
},
184+
"right": String {
185+
"isDoubleQuote": false,
186+
"kind": "string",
187+
"leadingComments": Array [
188+
CommentLine {
189+
"kind": "commentline",
190+
"value": "// Comment 2
191+
",
192+
},
193+
CommentLine {
194+
"kind": "commentline",
195+
"value": "// Comment 3
196+
",
197+
},
198+
],
199+
"raw": "'string3'",
200+
"unicode": false,
201+
"value": "string3",
202+
},
203+
"type": ".",
204+
},
205+
},
206+
],
207+
"comments": Array [
208+
CommentLine {
209+
"kind": "commentline",
210+
"value": "// Comment 1
211+
",
212+
},
213+
CommentLine {
214+
"kind": "commentline",
215+
"value": "// Comment 2
216+
",
217+
},
218+
CommentLine {
219+
"kind": "commentline",
220+
"value": "// Comment 3
221+
",
222+
},
223+
],
224+
"errors": Array [],
225+
"kind": "program",
226+
}
227+
`;
228+
145229
exports[`Test comments issues fix #193 1`] = `
146230
Program {
147231
"children": Array [
@@ -355,6 +439,16 @@ Program {
355439
"shortForm": false,
356440
"test": Boolean {
357441
"kind": "boolean",
442+
"leadingComments": Array [
443+
CommentBlock {
444+
"kind": "commentblock",
445+
"value": "/* ignore */",
446+
},
447+
CommentBlock {
448+
"kind": "commentblock",
449+
"value": "/* ignore */",
450+
},
451+
],
358452
"raw": "false",
359453
"value": false,
360454
},
@@ -409,20 +503,22 @@ Program {
409503
"kind": "commentblock",
410504
"value": "/* ignore */",
411505
},
506+
],
507+
},
508+
"kind": "if",
509+
"shortForm": false,
510+
"test": Boolean {
511+
"kind": "boolean",
512+
"leadingComments": Array [
412513
CommentBlock {
413514
"kind": "commentblock",
414-
"value": "/* */",
515+
"value": "/* ignore */",
415516
},
416517
CommentBlock {
417518
"kind": "commentblock",
418-
"value": "/* ignore */",
519+
"value": "/* */",
419520
},
420521
],
421-
},
422-
"kind": "if",
423-
"shortForm": false,
424-
"test": Boolean {
425-
"kind": "boolean",
426522
"raw": "true",
427523
"value": true,
428524
},
@@ -884,6 +980,18 @@ Program {
884980
"children": Array [
885981
Assign {
886982
"kind": "assign",
983+
"leadingComments": Array [
984+
CommentLine {
985+
"kind": "commentline",
986+
"value": "# some information
987+
",
988+
},
989+
CommentLine {
990+
"kind": "commentline",
991+
"value": "// another line
992+
",
993+
},
994+
],
887995
"left": Variable {
888996
"byref": false,
889997
"curly": false,

test/snapshot/comment.test.js

Lines changed: 19 additions & 0 deletions
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 #189", function() {
44+
const ast = parser.parseEval(
45+
`
46+
$var = 'string1'
47+
// Comment 1
48+
. 'string2' // Comment 2
49+
// Comment 3
50+
. 'string3';
51+
`,
52+
{
53+
parser: {
54+
extractDoc: true,
55+
// debug: true
56+
}
57+
}
58+
);
59+
expect(ast).toMatchSnapshot();
60+
});
4261

4362
it("fix #193", function() {
4463
const ast = parser.parseEval(

0 commit comments

Comments
 (0)