Skip to content

Commit de01574

Browse files
committed
Fix: wrong location with babel-eslint (refs vuejs/eslint-plugin-vue#208)
1 parent dd216ec commit de01574

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

src/script/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,20 @@ interface ESLintCustomParser {
3535
function postprocess(ast: ESLintProgram, locationCalculator: LocationCalculator): void {
3636
// There are cases which the same node instance appears twice in the tree.
3737
// E.g. `let {a} = {}` // This `a` appears twice at `Property#key` and `Property#value`.
38-
const traversed = new Set<Node>()
38+
const traversed = new Set<Node|number[]>()
3939

4040
traverseNodes(ast, {
4141
enterNode(node, parent) {
4242
if (!traversed.has(node)) {
4343
traversed.add(node)
4444
node.parent = parent
45-
locationCalculator.fixLocation(node)
45+
46+
// `babel-eslint@8` has shared `Node#range` with multiple nodes.
47+
// See also: https://github.com/vuejs/eslint-plugin-vue/issues/208
48+
if (!traversed.has(node.range)) {
49+
traversed.add(node.range)
50+
locationCalculator.fixLocation(node)
51+
}
4652
}
4753
},
4854
leaveNode() {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<style>
2+
</style>
3+
4+
<template>
5+
<div id="app">
6+
<img src="./assets/logo.png">
7+
<hello></hello>
8+
</div>
9+
</template>
10+
11+
<script>
12+
import { a, b } from './deps';
13+
import Hello from './components/Hello';
14+
15+
export default {
16+
name: 'app',
17+
calc (x) {
18+
return x + a + b;
19+
},
20+
components: {
21+
Hello
22+
}
23+
}
24+
</script>

test/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,29 @@ describe("Basic tests", () => {
345345
})
346346
})
347347

348+
describe("About fixtures/location-issue-with-babel-eslint.vue", () => {
349+
it("Identifiers in import declarations should has correct location.", () => {
350+
const cli = new CLIEngine({
351+
cwd: FIXTURE_DIR,
352+
envs: ["browser", "node"],
353+
parser: PARSER_PATH,
354+
parserOptions: {
355+
parser: "babel-eslint",
356+
sourceType: "module",
357+
ecmaVersion: 2017,
358+
},
359+
rules: {
360+
"no-use-before-define": "error",
361+
},
362+
useEslintrc: false,
363+
})
364+
const report = cli.executeOnFiles(["location-issue-with-babel-eslint.vue"])
365+
const messages = report.results[0].messages
366+
367+
assert(messages.length === 0)
368+
})
369+
})
370+
348371
describe("About unexpected-null-character errors", () => {
349372
it("should keep NULL in DATA state.", () => {
350373
const ast = parse("<template>\u0000</template>")

0 commit comments

Comments
 (0)