Skip to content

Commit

Permalink
Fix: sycing range and loc with start/end (fixes eslint#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
anikethsaha committed Dec 4, 2020
1 parent 0e09d9a commit 2a993e0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/espree.js
Expand Up @@ -162,10 +162,14 @@ module.exports = () => Parser => {
if (program.range) {
program.range[0] = program.body.length ? program.body[0].range[0] : program.range[0];
program.range[1] = extra.lastToken ? extra.lastToken.range[1] : program.range[1];
program.start = program.range[0];
program.end = program.range[1];
}
if (program.loc) {
program.loc.start = program.body.length ? program.body[0].loc.start : program.loc.start;
program.loc.end = extra.lastToken ? extra.lastToken.loc.end : program.loc.end;
program.start = program.loc.start.column;
program.end = program.loc.end.column;
}

return program;
Expand Down Expand Up @@ -268,11 +272,15 @@ module.exports = () => Parser => {
if (result.range) {
result.range[0]--;
result.range[1] += (terminalDollarBraceL ? 2 : 1);
result.start = result.range[0];
result.end = result.range[1];
}

if (result.loc) {
result.loc.start.column--;
result.loc.end.column += (terminalDollarBraceL ? 2 : 1);
result.start = result.loc.start.column;
result.end = result.loc.end.column;
}
}

Expand Down
76 changes: 76 additions & 0 deletions tests/lib/parse.js
Expand Up @@ -78,6 +78,82 @@ describe("parse()", () => {
assert.deepStrictEqual(tester.getRaw(ast), require("../fixtures/parse/all-pieces.json"));
});

it("should output the same value for program.start, end and range when there is a leading/trailing comments", () => {
const ast = espree.parse("/* foo */ bar /* baz */", {
range: true
});

assert.strictEqual(ast.start, ast.range[0]);
assert.strictEqual(ast.end, ast.range[1]);
});

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", () => {
const ast = espree.parse("/* foo */ bar", {
range: true,
loc: true
});

assert.strictEqual(ast.start, ast.range[0]);
assert.strictEqual(ast.end, ast.range[1]);
assert.strictEqual(ast.start, ast.loc.start.column);
assert.strictEqual(ast.end, ast.loc.end.column);
});

it("should output the same value for program.start, end and loc when there is a leading comments with loc", () => {
const ast = espree.parse("/* foo */ bar", {
loc: true
});

assert.strictEqual(ast.start, ast.loc.start.column);
assert.strictEqual(ast.end, ast.loc.end.column);
});

it("should output the same value for program.start, end and range when there is a trailing comments", () => {
const ast = espree.parse(" bar /* foo */", {
range: true
});

assert.strictEqual(ast.start, ast.range[0]);
assert.strictEqual(ast.end, ast.range[1]);
});

it("should output the same value for range and start and end in templateElement with range", () => {
const ast = espree.parse("`foo ${bar}`;", {
ecmaVersion: 6,
range: true
});

assert.strictEqual(ast.body[0].expression.quasis[0].start, ast.body[0].expression.quasis[0].range[0]);
assert.strictEqual(ast.body[0].expression.quasis[0].end, ast.body[0].expression.quasis[0].range[1]);
assert.strictEqual(ast.body[0].expression.quasis[1].start, ast.body[0].expression.quasis[1].range[0]);
assert.strictEqual(ast.body[0].expression.quasis[1].end, ast.body[0].expression.quasis[1].range[1]);
});

it("should output the same value for loc and start and end in templateElement with loc", () => {
const ast = espree.parse("`foo ${bar}`;", {
ecmaVersion: 6,
loc: true
});

assert.strictEqual(ast.body[0].expression.quasis[0].start, ast.body[0].expression.quasis[0].loc.start.column);
assert.strictEqual(ast.body[0].expression.quasis[0].end, ast.body[0].expression.quasis[0].loc.end.column);
assert.strictEqual(ast.body[0].expression.quasis[1].start, ast.body[0].expression.quasis[1].loc.start.column);
assert.strictEqual(ast.body[0].expression.quasis[1].end, ast.body[0].expression.quasis[1].loc.end.column);
});

it("should output the same value for loc, range and start and end in templateElement with loc and range", () => {
const ast = espree.parse("`foo ${bar}`;", {
ecmaVersion: 6,
loc: true,
range: true
});

assert.strictEqual(ast.body[0].expression.quasis[0].start, ast.body[0].expression.quasis[0].loc.start.column);
assert.strictEqual(ast.body[0].expression.quasis[0].end, ast.body[0].expression.quasis[0].loc.end.column);
assert.strictEqual(ast.body[0].expression.quasis[1].start, ast.body[0].expression.quasis[1].range[0]);
assert.strictEqual(ast.body[0].expression.quasis[1].end, ast.body[0].expression.quasis[1].range[1]);
});

it("should reset lastToken on each parse", () => {
espree.parse("var foo = bar;");
const ast = espree.parse("//foo", {
Expand Down

0 comments on commit 2a993e0

Please sign in to comment.