diff --git a/src/ast/nodes.ts b/src/ast/nodes.ts index f44eb9ef..4c451732 100644 --- a/src/ast/nodes.ts +++ b/src/ast/nodes.ts @@ -24,6 +24,7 @@ export interface HasParent { */ export type Node = | ESLintNode + | TypeAnnotation | VNode | VForExpression | VOnExpression @@ -348,6 +349,7 @@ export type ESLintExpression = export interface ESLintIdentifier extends HasLocation, HasParent { type: "Identifier" name: string + typeAnnotation?: TypeAnnotation | null } export interface ESLintLiteral extends HasLocation, HasParent { @@ -614,6 +616,13 @@ export interface ESLintLegacySpreadProperty extends HasLocation, HasParent { argument: ESLintExpression } +/** + * Top-level type annotation from `typescript-eslint-parser` and `babel-eslint` + */ +export interface TypeAnnotation extends HasLocation, HasParent { + type: "TSTypeAnnotation" | "TypeAnnotation" +} + //------------------------------------------------------------------------------ // Template //------------------------------------------------------------------------------ diff --git a/src/script/index.ts b/src/script/index.ts index 64d8747c..c471cc93 100644 --- a/src/script/index.ts +++ b/src/script/index.ts @@ -82,6 +82,15 @@ function postprocess( traversed.add(node.range) locationCalculator.fixLocation(node) } + + if ( + node.type === "Identifier" && + node.typeAnnotation && + !traversed.has(node.typeAnnotation) + ) { + traversed.add(node.typeAnnotation) + locationCalculator.fixLocation(node.typeAnnotation) + } } }, diff --git a/test/fixtures/location-issue-type-annotation.vue b/test/fixtures/location-issue-type-annotation.vue new file mode 100644 index 00000000..15745494 --- /dev/null +++ b/test/fixtures/location-issue-type-annotation.vue @@ -0,0 +1,24 @@ + + + + + diff --git a/test/index.js b/test/index.js index 9c719eac..fd1a55c8 100644 --- a/test/index.js +++ b/test/index.js @@ -396,6 +396,31 @@ describe("Basic tests", () => { }) }) + describe("About fixtures/location-issue-type-annotation.vue", () => { + it("typeAnnotation node in Identifier should have correct location.", () => { + const cli = new CLIEngine({ + cwd: FIXTURE_DIR, + envs: ["browser", "node"], + parser: PARSER_PATH, + parserOptions: { + parser: "typescript-eslint-parser", + sourceType: "module", + ecmaVersion: 2017, + }, + rules: { + "space-infix-ops": "error", + }, + useEslintrc: false, + }) + const report = cli.executeOnFiles([ + "location-issue-type-annotation.vue", + ]) + const messages = report.results[0].messages + + assert(messages.length === 0) + }) + }) + describe("About unexpected-null-character errors", () => { it("should keep NULL in DATA state.", () => { const ast = parse("")