Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dffb7aa

Browse files
authored
Breaking: syncing start/end with range (fixes #349) (#461)
* Fix: sycing range and loc with start/end (fixes #349) * Chore: independent logic for start end * Chore: updating templateElement's prop using state
1 parent e86f386 commit dffb7aa

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

lib/espree.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ export default () => Parser => {
104104
impliedStrict: ecmaFeatures.impliedStrict === true && this.options.ecmaVersion >= 5,
105105
ecmaVersion: this.options.ecmaVersion,
106106
jsxAttrValueToken: false,
107-
lastToken: null
107+
lastToken: null,
108+
templateElements: []
108109
};
109110
}
110111

@@ -167,6 +168,16 @@ export default () => Parser => {
167168
program.loc.end = extra.lastToken ? extra.lastToken.loc.end : program.loc.end;
168169
}
169170

171+
program.start = program.body.length ? program.body[0].start : program.start;
172+
program.end = extra.lastToken ? extra.lastToken.end : program.end;
173+
174+
this[STATE].templateElements.forEach(templateElement => {
175+
const terminalDollarBraceL = this.input.slice(templateElement.end, templateElement.end + 2) === "${";
176+
177+
templateElement.start--;
178+
templateElement.end += (terminalDollarBraceL ? 2 : 1);
179+
});
180+
170181
return program;
171182
}
172183

@@ -273,6 +284,8 @@ export default () => Parser => {
273284
result.loc.start.column--;
274285
result.loc.end.column += (terminalDollarBraceL ? 2 : 1);
275286
}
287+
288+
this[STATE].templateElements.push(result);
276289
}
277290

278291
if (result.type.includes("Function") && !result.generator) {

tests/lib/parse.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,93 @@ describe("parse()", () => {
8080
assert.deepStrictEqual(tester.getRaw(ast), allPiecesJson);
8181
});
8282

83+
it("should output the same value for program.start, end and range when there is a leading/trailing comments", () => {
84+
const ast = espree.parse("/* foo */ bar /* baz */", {
85+
range: true
86+
});
87+
88+
assert.strictEqual(ast.start, ast.range[0]);
89+
assert.strictEqual(ast.end, ast.range[1]);
90+
});
91+
92+
it("should output the same value for program.start, end and range and loc when there is a leading comments with range and loc true", () => {
93+
const ast = espree.parse("/* foo */ bar", {
94+
range: true,
95+
loc: true
96+
});
97+
98+
assert.strictEqual(ast.start, ast.range[0]);
99+
assert.strictEqual(ast.end, ast.range[1]);
100+
assert.strictEqual(ast.start, ast.loc.start.column);
101+
assert.strictEqual(ast.end, ast.loc.end.column);
102+
});
103+
104+
it("should output the same value for program.start, end and loc when there is a leading comments with loc", () => {
105+
const ast = espree.parse("/* foo */ bar", {
106+
loc: true
107+
});
108+
109+
assert.strictEqual(ast.start, ast.loc.start.column);
110+
assert.strictEqual(ast.end, ast.loc.end.column);
111+
});
112+
113+
it("should output the same value for program.start, end and range when there is a trailing comments", () => {
114+
const ast = espree.parse(" bar /* foo */", {
115+
range: true
116+
});
117+
118+
assert.strictEqual(ast.start, ast.range[0]);
119+
assert.strictEqual(ast.end, ast.range[1]);
120+
});
121+
122+
it("should output the same value for range and start and end in templateElement with range", () => {
123+
const ast = espree.parse("`foo ${bar}`;", {
124+
ecmaVersion: 6,
125+
range: true
126+
});
127+
128+
assert.strictEqual(ast.body[0].expression.quasis[0].start, ast.body[0].expression.quasis[0].range[0]);
129+
assert.strictEqual(ast.body[0].expression.quasis[0].end, ast.body[0].expression.quasis[0].range[1]);
130+
assert.strictEqual(ast.body[0].expression.quasis[1].start, ast.body[0].expression.quasis[1].range[0]);
131+
assert.strictEqual(ast.body[0].expression.quasis[1].end, ast.body[0].expression.quasis[1].range[1]);
132+
});
133+
134+
it("should output the same value for loc and start and end in templateElement with loc", () => {
135+
const ast = espree.parse("`foo ${bar}`;", {
136+
ecmaVersion: 6,
137+
loc: true
138+
});
139+
140+
assert.strictEqual(ast.body[0].expression.quasis[0].start, ast.body[0].expression.quasis[0].loc.start.column);
141+
assert.strictEqual(ast.body[0].expression.quasis[0].end, ast.body[0].expression.quasis[0].loc.end.column);
142+
assert.strictEqual(ast.body[0].expression.quasis[1].start, ast.body[0].expression.quasis[1].loc.start.column);
143+
assert.strictEqual(ast.body[0].expression.quasis[1].end, ast.body[0].expression.quasis[1].loc.end.column);
144+
});
145+
146+
it("should output the same value for loc, range and start and end in templateElement with loc and range", () => {
147+
const ast = espree.parse("`foo ${bar}`;", {
148+
ecmaVersion: 6,
149+
loc: true,
150+
range: true
151+
});
152+
153+
assert.strictEqual(ast.body[0].expression.quasis[0].start, ast.body[0].expression.quasis[0].loc.start.column);
154+
assert.strictEqual(ast.body[0].expression.quasis[0].end, ast.body[0].expression.quasis[0].loc.end.column);
155+
assert.strictEqual(ast.body[0].expression.quasis[1].start, ast.body[0].expression.quasis[1].range[0]);
156+
assert.strictEqual(ast.body[0].expression.quasis[1].end, ast.body[0].expression.quasis[1].range[1]);
157+
});
158+
159+
it("should output the same value for loc, range and start and end in templateElement", () => {
160+
const ast = espree.parse("`foo ${bar}`;", {
161+
ecmaVersion: 6
162+
});
163+
164+
assert.strictEqual(ast.body[0].expression.quasis[0].start, 0);
165+
assert.strictEqual(ast.body[0].expression.quasis[0].end, 7);
166+
assert.strictEqual(ast.body[0].expression.quasis[1].start, 10);
167+
assert.strictEqual(ast.body[0].expression.quasis[1].end, 12);
168+
});
169+
83170
it("should reset lastToken on each parse", () => {
84171
espree.parse("var foo = bar;");
85172
const ast = espree.parse("//foo", {

0 commit comments

Comments
 (0)