From 11a03ab7d5925f4386561df775e3359178321ab9 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sat, 17 Sep 2022 03:23:22 +0900 Subject: [PATCH 1/2] Fix false positives for script setup with ts --- lib/rules/no-undef-components.js | 24 +++++++++- tests/lib/rules/no-undef-components.js | 45 +++++++++++++++++++ .../util-types/ast/es-ast.ts | 1 + 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/lib/rules/no-undef-components.js b/lib/rules/no-undef-components.js index dc58fd2e9..3ffe9cc3b 100644 --- a/lib/rules/no-undef-components.js +++ b/lib/rules/no-undef-components.js @@ -133,7 +133,29 @@ module.exports = { (scope) => scope.type === 'module' ) for (const variable of (moduleScope && moduleScope.variables) || []) { - if (variable.isTypeVariable) { + if ( + // Check for type definitions. e.g. type Foo = {} + (variable.isTypeVariable && !variable.isValueVariable) || + // type-only import seems to have isValueVariable set to true. So we need to check the actual Node. + (variable.defs.length > 0 && + variable.defs.every((def) => { + if (def.type !== 'ImportBinding') { + return false + } + if (def.parent.importKind === 'type') { + // check for `import type Foo from './xxx'` + return true + } + if ( + def.node.type === 'ImportSpecifier' && + def.node.importKind === 'type' + ) { + // check for `import { type Foo } from './xxx'` + return true + } + return false + })) + ) { scriptTypeOnlyNames.add(variable.name) } else { scriptVariableNames.add(variable.name) diff --git a/tests/lib/rules/no-undef-components.js b/tests/lib/rules/no-undef-components.js index fc3461e5a..00b8701ab 100644 --- a/tests/lib/rules/no-undef-components.js +++ b/tests/lib/rules/no-undef-components.js @@ -598,6 +598,26 @@ tester.run('no-undef-components', rule, { ` + }, + { + filename: 'test.vue', + code: ` + + + + `, + parserOptions: { + ecmaVersion: 6, + sourceType: 'module', + parser: require.resolve('@typescript-eslint/parser') + }, + parser: require.resolve('vue-eslint-parser') } ], invalid: [ @@ -721,6 +741,31 @@ tester.run('no-undef-components', rule, { } ] }, + { + filename: 'test.vue', + code: ` + + + + `, + parserOptions: { + ecmaVersion: 6, + sourceType: 'module', + parser: require.resolve('@typescript-eslint/parser') + }, + parser: require.resolve('vue-eslint-parser'), + errors: [ + { + message: + "The '' component has been used, but 'Foo' only refers to a type.", + line: 7 + } + ] + }, // options API { diff --git a/typings/eslint-plugin-vue/util-types/ast/es-ast.ts b/typings/eslint-plugin-vue/util-types/ast/es-ast.ts index f4dd208c3..b3d5b6e2e 100644 --- a/typings/eslint-plugin-vue/util-types/ast/es-ast.ts +++ b/typings/eslint-plugin-vue/util-types/ast/es-ast.ts @@ -274,6 +274,7 @@ export interface ImportSpecifier extends HasParentNode { type: 'ImportSpecifier' imported: Identifier local: Identifier + importKind?: 'type' | 'value' } export interface ImportDefaultSpecifier extends HasParentNode { type: 'ImportDefaultSpecifier' From b169a43056138eae7a81888372bb6515208cf100 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sat, 17 Sep 2022 03:29:19 +0900 Subject: [PATCH 2/2] fix test for ts eslint v4 --- tests/lib/rules/no-undef-components.js | 92 ++++++++++++++------------ 1 file changed, 50 insertions(+), 42 deletions(-) diff --git a/tests/lib/rules/no-undef-components.js b/tests/lib/rules/no-undef-components.js index 00b8701ab..5af9468fe 100644 --- a/tests/lib/rules/no-undef-components.js +++ b/tests/lib/rules/no-undef-components.js @@ -6,6 +6,7 @@ const RuleTester = require('eslint').RuleTester const rule = require('../../../lib/rules/no-undef-components') +const semver = require('semver') const tester = new RuleTester({ parser: require.resolve('vue-eslint-parser'), @@ -681,9 +682,14 @@ tester.run('no-undef-components', rule, { } ] }, - { - filename: 'test.vue', - code: ` + ...(semver.gte( + require('@typescript-eslint/parser/package.json').version, + '5.0.0' + ) + ? [ + { + filename: 'test.vue', + code: `