123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- 'use strict'
- const utils = require('../utils')
- const casing = require('../utils/casing')
- function camelize(str) {
- return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''))
- }
- module.exports = {
- meta: {
- type: 'suggestion',
- docs: {
- description: 'disallow use of undefined components in `<template>`',
- categories: undefined,
- url: 'https://eslint.vuejs.org/rules/no-undef-components.html'
- },
- fixable: null,
- schema: [
- {
- type: 'object',
- properties: {
- ignorePatterns: {
- type: 'array'
- }
- },
- additionalProperties: false
- }
- ],
- messages: {
- undef: "The '<{{name}}>' component has been used, but not defined."
- }
- },
-
- create(context) {
- const options = context.options[0] || {}
-
- const ignorePatterns = options.ignorePatterns || []
-
- function isVerifyTargetComponent(rawName) {
- const kebabCaseName = casing.kebabCase(rawName)
- if (
- utils.isHtmlWellKnownElementName(rawName) ||
- utils.isSvgWellKnownElementName(rawName) ||
- utils.isBuiltInComponentName(kebabCaseName)
- ) {
- return false
- }
- const pascalCaseName = casing.pascalCase(rawName)
-
- if (
- ignorePatterns.some((pattern) => {
- const regExp = new RegExp(pattern)
- return (
- regExp.test(rawName) ||
- regExp.test(kebabCaseName) ||
- regExp.test(pascalCaseName)
- )
- })
- ) {
- return false
- }
- return true
- }
-
- let verifyName
-
- let scriptVisitor = {}
-
- const templateBodyVisitor = {
- VElement(node) {
- if (!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) {
- return
- }
- verifyName(node.rawName, node.startTag)
- },
-
- "VAttribute[directive=false][key.name='is']"(node) {
- if (
- !node.value
- )
- return
- const value = node.value.value.startsWith('vue:')
- ? node.value.value.slice(4)
- : node.value.value
- verifyName(value, node)
- }
- }
- if (utils.isScriptSetup(context)) {
-
-
- const scriptVariableNames = new Set()
- const globalScope = context.getSourceCode().scopeManager.globalScope
- if (globalScope) {
- for (const variable of globalScope.variables) {
- scriptVariableNames.add(variable.name)
- }
- const moduleScope = globalScope.childScopes.find(
- (scope) => scope.type === 'module'
- )
- for (const variable of (moduleScope && moduleScope.variables) || []) {
- scriptVariableNames.add(variable.name)
- }
- }
-
- const existsSetupReference = (name) => {
- if (scriptVariableNames.has(name)) {
- return true
- }
- const camelName = camelize(name)
- if (scriptVariableNames.has(camelName)) {
- return true
- }
- const pascalName = casing.capitalize(camelName)
- if (scriptVariableNames.has(pascalName)) {
- return true
- }
- return false
- }
- verifyName = (rawName, reportNode) => {
- if (!isVerifyTargetComponent(rawName)) {
- return
- }
- if (existsSetupReference(rawName)) {
- return
- }
-
-
- const dotIndex = rawName.indexOf('.')
- if (dotIndex > 0) {
- if (existsSetupReference(rawName.slice(0, dotIndex))) {
- return
- }
- }
- context.report({
- node: reportNode,
- messageId: 'undef',
- data: {
- name: rawName
- }
- })
- }
- } else {
-
-
- const registeredComponentNames = []
-
- const registeredComponentKebabCaseNames = []
-
- const componentsRegisteredAsKebabCase = []
- scriptVisitor = utils.executeOnVue(context, (obj) => {
- registeredComponentNames.push(
- ...utils.getRegisteredComponents(obj).map(({ name }) => name)
- )
- const nameProperty = utils.findProperty(obj, 'name')
- if (nameProperty && utils.isStringLiteral(nameProperty.value)) {
- const name = utils.getStringLiteralValue(nameProperty.value)
- if (name) {
- registeredComponentNames.push(name)
- }
- }
- registeredComponentKebabCaseNames.push(
- ...registeredComponentNames.map((name) => casing.kebabCase(name))
- )
- componentsRegisteredAsKebabCase.push(
- ...registeredComponentNames.filter(
- (name) => name === casing.kebabCase(name)
- )
- )
- })
- verifyName = (rawName, reportNode) => {
- if (!isVerifyTargetComponent(rawName)) {
- return
- }
- if (registeredComponentNames.includes(rawName)) {
- return
- }
- const kebabCaseName = casing.kebabCase(rawName)
- if (registeredComponentKebabCaseNames.includes(kebabCaseName)) {
- if (
-
- !casing.isPascalCase(rawName)
- ) {
- return
- }
- }
- context.report({
- node: reportNode,
- messageId: 'undef',
- data: {
- name: rawName
- }
- })
- }
-
- templateBodyVisitor[
- "VAttribute[directive=true][key.name.name='bind'][key.argument.name='is'], VAttribute[directive=true][key.name.name='is']"
- ] = (node) => {
- if (
- !node.value ||
- node.value.type !== 'VExpressionContainer' ||
- !node.value.expression
- )
- return
- if (node.value.expression.type === 'Literal') {
- verifyName(`${node.value.expression.value}`, node)
- }
- }
- }
- return utils.defineTemplateBodyVisitor(
- context,
- templateBodyVisitor,
- scriptVisitor
- )
- }
- }
|