Skip to content

Commit

Permalink
Chore: Add tests and comments (refs #349) (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Apr 29, 2021
1 parent dffb7aa commit 34e1ab9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/espree.js
Expand Up @@ -171,6 +171,19 @@ export default () => Parser => {
program.start = program.body.length ? program.body[0].start : program.start;
program.end = extra.lastToken ? extra.lastToken.end : program.end;

/*
* https://github.com/eslint/espree/issues/349
* Ensure that template elements have correct range information.
* This is one location where Acorn produces a different value
* for its start and end properties vs. the values present in the
* range property. In order to avoid confusion, we set the start
* and end properties to the values that are present in range.
* This is done here, instead of in finishNode(), because Acorn
* uses the values of start and end internally while parsing, making
* it dangerous to change those values while parsing is ongoing.
* By waiting until the end of parsing, we can safely change these
* values without affect any other part of the process.
*/
this[STATE].templateElements.forEach(templateElement => {
const terminalDollarBraceL = this.input.slice(templateElement.end, templateElement.end + 2) === "${";

Expand Down Expand Up @@ -285,6 +298,7 @@ export default () => Parser => {
result.loc.end.column += (terminalDollarBraceL ? 2 : 1);
}

// save template element references to fix start/end later
this[STATE].templateElements.push(result);
}

Expand Down
13 changes: 12 additions & 1 deletion tests/lib/parse.js
Expand Up @@ -89,6 +89,13 @@ describe("parse()", () => {
assert.strictEqual(ast.end, ast.range[1]);
});

it("should output the same value for program.start, end as when ranges are returned and there is a leading/trailing comments using default options", () => {
const ast = espree.parse("/* foo */ bar /* baz */");

assert.strictEqual(ast.start, 10);
assert.strictEqual(ast.end, 13);
});

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,
Expand Down Expand Up @@ -152,11 +159,15 @@ describe("parse()", () => {

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[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].loc.start.column);
assert.strictEqual(ast.body[0].expression.quasis[1].end, ast.body[0].expression.quasis[1].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 output the same value for loc, range and start and end in templateElement", () => {
it("should output the same value for start and end in templateElement as when ranges are present", () => {
const ast = espree.parse("`foo ${bar}`;", {
ecmaVersion: 6
});
Expand Down

0 comments on commit 34e1ab9

Please sign in to comment.