豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 804d3ed

Browse files
authored
fix: noop location (#1218)
1 parent ee242a5 commit 804d3ed

File tree

4 files changed

+225
-16
lines changed

4 files changed

+225
-16
lines changed

src/ast.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,25 @@ AST.prototype.prepare = function (kind, docs, parser) {
377377
const args = Array.prototype.slice.call(arguments);
378378
args.push(docs);
379379
if (self.withPositions || self.withSource) {
380+
let nodeStart = start;
381+
let nodeEnd = new Position(
382+
parser.prev[0],
383+
parser.prev[1],
384+
parser.prev[2],
385+
);
386+
// Fix swapped positions when no tokens are consumed between node() and ()
387+
// (e.g. noop node in an empty block that contains only comments)
388+
if (nodeStart.offset > nodeEnd.offset) {
389+
const tmp = nodeStart;
390+
nodeStart = nodeEnd;
391+
nodeEnd = tmp;
392+
}
380393
let src = null;
381394
if (self.withSource) {
382-
src = parser.lexer._input.substring(start.offset, parser.prev[2]);
395+
src = parser.lexer._input.substring(nodeStart.offset, nodeEnd.offset);
383396
}
384397
// if with source, need location on swapLocations function
385-
const location = new Location(
386-
src,
387-
start,
388-
new Position(parser.prev[0], parser.prev[1], parser.prev[2]),
389-
);
398+
const location = new Location(src, nodeStart, nodeEnd);
390399
// last argument is always the location
391400
args.push(location);
392401
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,16 +2257,16 @@ Program {
22572257
"kind": "block",
22582258
"loc": Location {
22592259
"end": Position {
2260-
"column": 19,
2261-
"line": 63,
2262-
"offset": 1314,
2260+
"column": 8,
2261+
"line": 64,
2262+
"offset": 1323,
22632263
},
22642264
"source": "
22652265
",
22662266
"start": Position {
2267-
"column": 8,
2268-
"line": 64,
2269-
"offset": 1323,
2267+
"column": 19,
2268+
"line": 63,
2269+
"offset": 1314,
22702270
},
22712271
},
22722272
},
@@ -7619,15 +7619,15 @@ next:
76197619
"kind": "noop",
76207620
"loc": Location {
76217621
"end": Position {
7622-
"column": 11,
7622+
"column": 12,
76237623
"line": 141,
7624-
"offset": 3029,
7624+
"offset": 3030,
76257625
},
76267626
"source": " ",
76277627
"start": Position {
7628-
"column": 12,
7628+
"column": 11,
76297629
"line": 141,
7630-
"offset": 3030,
7630+
"offset": 3029,
76317631
},
76327632
},
76337633
},

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

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,190 @@ Program {
125125
}
126126
`;
127127

128+
exports[`Test locations noop in comment-only body has correct start/end order 1`] = `
129+
Program {
130+
"children": [
131+
_Function {
132+
"arguments": [],
133+
"attrGroups": [],
134+
"body": Block {
135+
"children": [
136+
Noop {
137+
"kind": "noop",
138+
"leadingComments": [
139+
CommentBlock {
140+
"kind": "commentblock",
141+
"loc": Location {
142+
"end": Position {
143+
"column": 17,
144+
"line": 3,
145+
"offset": 35,
146+
},
147+
"source": "/* comment */",
148+
"start": Position {
149+
"column": 4,
150+
"line": 3,
151+
"offset": 22,
152+
},
153+
},
154+
"offset": 22,
155+
"value": "/* comment */",
156+
},
157+
CommentLine {
158+
"kind": "commentline",
159+
"loc": Location {
160+
"end": Position {
161+
"column": 0,
162+
"line": 5,
163+
"offset": 60,
164+
},
165+
"source": "# $m[] = 'declare';
166+
",
167+
"start": Position {
168+
"column": 4,
169+
"line": 4,
170+
"offset": 40,
171+
},
172+
},
173+
"offset": 40,
174+
"value": "# $m[] = 'declare';
175+
",
176+
},
177+
],
178+
"loc": Location {
179+
"end": Position {
180+
"column": 0,
181+
"line": 5,
182+
"offset": 60,
183+
},
184+
"source": "
185+
/* comment */
186+
# $m[] = 'declare';
187+
",
188+
"start": Position {
189+
"column": 1,
190+
"line": 2,
191+
"offset": 17,
192+
},
193+
},
194+
},
195+
],
196+
"kind": "block",
197+
"loc": Location {
198+
"end": Position {
199+
"column": 1,
200+
"line": 5,
201+
"offset": 61,
202+
},
203+
"source": "{
204+
/* comment */
205+
# $m[] = 'declare';
206+
}",
207+
"start": Position {
208+
"column": 0,
209+
"line": 2,
210+
"offset": 16,
211+
},
212+
},
213+
},
214+
"byref": false,
215+
"kind": "function",
216+
"loc": Location {
217+
"end": Position {
218+
"column": 1,
219+
"line": 5,
220+
"offset": 61,
221+
},
222+
"source": "function test()",
223+
"start": Position {
224+
"column": 0,
225+
"line": 1,
226+
"offset": 0,
227+
},
228+
},
229+
"name": Identifier {
230+
"kind": "identifier",
231+
"loc": Location {
232+
"end": Position {
233+
"column": 13,
234+
"line": 1,
235+
"offset": 13,
236+
},
237+
"source": "test",
238+
"start": Position {
239+
"column": 9,
240+
"line": 1,
241+
"offset": 9,
242+
},
243+
},
244+
"name": "test",
245+
},
246+
"nullable": false,
247+
"type": null,
248+
},
249+
],
250+
"comments": [
251+
CommentBlock {
252+
"kind": "commentblock",
253+
"loc": Location {
254+
"end": Position {
255+
"column": 17,
256+
"line": 3,
257+
"offset": 35,
258+
},
259+
"source": "/* comment */",
260+
"start": Position {
261+
"column": 4,
262+
"line": 3,
263+
"offset": 22,
264+
},
265+
},
266+
"offset": 22,
267+
"value": "/* comment */",
268+
},
269+
CommentLine {
270+
"kind": "commentline",
271+
"loc": Location {
272+
"end": Position {
273+
"column": 0,
274+
"line": 5,
275+
"offset": 60,
276+
},
277+
"source": "# $m[] = 'declare';
278+
",
279+
"start": Position {
280+
"column": 4,
281+
"line": 4,
282+
"offset": 40,
283+
},
284+
},
285+
"offset": 40,
286+
"value": "# $m[] = 'declare';
287+
",
288+
},
289+
],
290+
"errors": [],
291+
"kind": "program",
292+
"loc": Location {
293+
"end": Position {
294+
"column": 1,
295+
"line": 5,
296+
"offset": 61,
297+
},
298+
"source": "function test()
299+
{
300+
/* comment */
301+
# $m[] = 'declare';
302+
}",
303+
"start": Position {
304+
"column": 0,
305+
"line": 1,
306+
"offset": 0,
307+
},
308+
},
309+
}
310+
`;
311+
128312
exports[`Test locations test #164 : expr must include ; 1`] = `
129313
Program {
130314
"children": [

test/snapshot/location.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,22 @@ string";`,
241241
).toMatchSnapshot();
242242
});
243243

244+
it("noop in comment-only body has correct start/end order", function () {
245+
expect(
246+
parser.parseEval(
247+
`function test()
248+
{
249+
/* comment */
250+
# $m[] = 'declare';
251+
}`,
252+
{
253+
parser: { extractDoc: true },
254+
ast: { withPositions: true, withSource: true },
255+
},
256+
),
257+
).toMatchSnapshot();
258+
});
259+
244260
it("#164 : expr should avoid ?>", function () {
245261
expect(
246262
parser.parseCode("<?php $a = $b + 1 ?>", {

0 commit comments

Comments
 (0)