Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type annotations range #31

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/ast/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface HasParent {
*/
export type Node =
| ESLintNode
| TypeAnnotation
| VNode
| VForExpression
| VOnExpression
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know there are many more possible types, but they are rather children of these top level types. And what we need here at this moment is just the top level type annotation on Identifier. Unless there will be introduced another eslint rule that will respect further cases of typeAnnotation or returnType. What do you think?

}

//------------------------------------------------------------------------------
// Template
//------------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions src/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
},

Expand Down
24 changes: 24 additions & 0 deletions test/fixtures/location-issue-type-annotation.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template>
<div>{{ msg }}</div>
</template>

<script lang="ts">
import Vue from 'vue';

enum Direction {
Up,
Down,
}

const baz : string = 'hi'; // <- wrong range of type annotation

export default Vue.extend({
name: 'HelloWorld',
props: {
msg: String,
},
});
</script>

<style>
</style>
25 changes: 25 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("<template>\u0000</template>")
Expand Down