|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const vue3ExportNames = new Set(require('../utils/vue3-export-names.json')) |
| 12 | + |
| 13 | +// ------------------------------------------------------------------------------ |
| 14 | +// Helpers |
| 15 | +// ------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +const TARGET_AT_VUE_MODULES = new Set([ |
| 18 | + '@vue/runtime-dom', |
| 19 | + '@vue/runtime-core', |
| 20 | + '@vue/reactivity', |
| 21 | + '@vue/shared' |
| 22 | +]) |
| 23 | +// Modules with the names of a subset of vue. |
| 24 | +const SUBSET_AT_VUE_MODULES = new Set(['@vue/runtime-dom', '@vue/runtime-core']) |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {ImportDeclaration} node |
| 28 | + */ |
| 29 | +function* extractImportNames(node) { |
| 30 | + for (const specifier of node.specifiers) { |
| 31 | + if (specifier.type === 'ImportDefaultSpecifier') { |
| 32 | + yield 'default' |
| 33 | + } else if (specifier.type === 'ImportNamespaceSpecifier') { |
| 34 | + yield null // all |
| 35 | + } else if (specifier.type === 'ImportSpecifier') { |
| 36 | + yield specifier.imported.name |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// ------------------------------------------------------------------------------ |
| 42 | +// Rule Definition |
| 43 | +// ------------------------------------------------------------------------------ |
| 44 | + |
| 45 | +module.exports = { |
| 46 | + meta: { |
| 47 | + type: 'suggestion', |
| 48 | + docs: { |
| 49 | + description: "enforce import from 'vue' instead of import from '@vue/*'", |
| 50 | + // TODO We will change it in the next major version. |
| 51 | + // categories: ['vue3-essential'], |
| 52 | + categories: undefined, |
| 53 | + url: 'https://eslint.vuejs.org/rules/prefer-import-from-vue.html' |
| 54 | + }, |
| 55 | + fixable: 'code', |
| 56 | + schema: [], |
| 57 | + messages: { |
| 58 | + importedAtVue: "Import from 'vue' instead of '{{source}}'." |
| 59 | + } |
| 60 | + }, |
| 61 | + /** |
| 62 | + * @param {RuleContext} context |
| 63 | + * @returns {RuleListener} |
| 64 | + */ |
| 65 | + create(context) { |
| 66 | + /** |
| 67 | + * |
| 68 | + * @param {Literal & { value: string }} source |
| 69 | + * @param { () => boolean } fixable |
| 70 | + */ |
| 71 | + function verifySource(source, fixable) { |
| 72 | + if (!TARGET_AT_VUE_MODULES.has(source.value)) { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + context.report({ |
| 77 | + node: source, |
| 78 | + messageId: 'importedAtVue', |
| 79 | + data: { source: source.value }, |
| 80 | + fix: fixable() |
| 81 | + ? (fixer) => |
| 82 | + fixer.replaceTextRange( |
| 83 | + [source.range[0] + 1, source.range[1] - 1], |
| 84 | + 'vue' |
| 85 | + ) |
| 86 | + : null |
| 87 | + }) |
| 88 | + } |
| 89 | + |
| 90 | + return { |
| 91 | + ImportDeclaration(node) { |
| 92 | + verifySource(node.source, () => { |
| 93 | + if (SUBSET_AT_VUE_MODULES.has(node.source.value)) { |
| 94 | + // If the module is a subset of 'vue', we can safely change it to 'vue'. |
| 95 | + return true |
| 96 | + } |
| 97 | + for (const name of extractImportNames(node)) { |
| 98 | + if (name == null) { |
| 99 | + return false // import all |
| 100 | + } |
| 101 | + if (!vue3ExportNames.has(name)) { |
| 102 | + // If there is a name that is not exported from 'vue', it will not be auto-fixed. |
| 103 | + return false |
| 104 | + } |
| 105 | + } |
| 106 | + return true |
| 107 | + }) |
| 108 | + }, |
| 109 | + ExportNamedDeclaration(node) { |
| 110 | + if (node.source) { |
| 111 | + verifySource(node.source, () => { |
| 112 | + for (const specifier of node.specifiers) { |
| 113 | + if (!vue3ExportNames.has(specifier.local.name)) { |
| 114 | + // If there is a name that is not exported from 'vue', it will not be auto-fixed. |
| 115 | + return false |
| 116 | + } |
| 117 | + } |
| 118 | + return true |
| 119 | + }) |
| 120 | + } |
| 121 | + }, |
| 122 | + ExportAllDeclaration(node) { |
| 123 | + verifySource( |
| 124 | + node.source, |
| 125 | + // If we change it to `from 'vue'`, it will export more, so it will not be auto-fixed. |
| 126 | + () => false |
| 127 | + ) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments