123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- 'use strict'
- const vue3ExportNames = new Set(require('../utils/vue3-export-names.json'))
- const TARGET_AT_VUE_MODULES = new Set([
- '@vue/runtime-dom',
- '@vue/runtime-core',
- '@vue/reactivity',
- '@vue/shared'
- ])
- const SUBSET_AT_VUE_MODULES = new Set(['@vue/runtime-dom', '@vue/runtime-core'])
- function* extractImportNames(node) {
- for (const specifier of node.specifiers) {
- if (specifier.type === 'ImportDefaultSpecifier') {
- yield 'default'
- } else if (specifier.type === 'ImportNamespaceSpecifier') {
- yield null
- } else if (specifier.type === 'ImportSpecifier') {
- yield specifier.imported.name
- }
- }
- }
- module.exports = {
- meta: {
- type: 'suggestion',
- docs: {
- description: "enforce import from 'vue' instead of import from '@vue/*'",
-
-
- categories: undefined,
- url: 'https://eslint.vuejs.org/rules/prefer-import-from-vue.html'
- },
- fixable: 'code',
- schema: [],
- messages: {
- importedAtVue: "Import from 'vue' instead of '{{source}}'."
- }
- },
-
- create(context) {
-
- function verifySource(source, fixable) {
- if (!TARGET_AT_VUE_MODULES.has(source.value)) {
- return
- }
- context.report({
- node: source,
- messageId: 'importedAtVue',
- data: { source: source.value },
- fix: fixable()
- ? (fixer) =>
- fixer.replaceTextRange(
- [source.range[0] + 1, source.range[1] - 1],
- 'vue'
- )
- : null
- })
- }
- return {
- ImportDeclaration(node) {
- verifySource(node.source, () => {
- if (SUBSET_AT_VUE_MODULES.has(node.source.value)) {
-
- return true
- }
- for (const name of extractImportNames(node)) {
- if (name == null) {
- return false
- }
- if (!vue3ExportNames.has(name)) {
-
- return false
- }
- }
- return true
- })
- },
- ExportNamedDeclaration(node) {
- if (node.source) {
- verifySource(node.source, () => {
- for (const specifier of node.specifiers) {
- if (!vue3ExportNames.has(specifier.local.name)) {
-
- return false
- }
- }
- return true
- })
- }
- },
- ExportAllDeclaration(node) {
- verifySource(
- node.source,
-
- () => false
- )
- }
- }
- }
- }
|