123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- 'use strict'
- const utils = require('../utils')
- const { findVariable } = require('eslint-utils')
- module.exports = {
- meta: {
- type: 'problem',
- docs: {
- description: 'enforce properties of `$slots` to be used as a function',
- categories: ['vue3-essential'],
- url: 'https://eslint.vuejs.org/rules/require-slots-as-functions.html'
- },
- fixable: null,
- schema: [],
- messages: {
- unexpected: 'Property in `$slots` should be used as function.'
- }
- },
-
- create(context) {
-
- function verify(node, reportNode) {
- const parent = node.parent
- if (
- parent.type === 'VariableDeclarator' &&
- parent.id.type === 'Identifier'
- ) {
-
- verifyReferences(parent.id, reportNode)
- return
- }
- if (
- parent.type === 'AssignmentExpression' &&
- parent.right === node &&
- parent.left.type === 'Identifier'
- ) {
-
- verifyReferences(parent.left, reportNode)
- return
- }
- if (parent.type === 'ChainExpression') {
-
- verify(parent, reportNode)
- return
- }
- if (
-
- parent.type === 'MemberExpression' ||
-
- parent.type === 'VariableDeclarator' ||
-
- parent.type === 'SpreadElement' ||
-
- parent.type === 'ArrayExpression'
- ) {
- context.report({
- node: reportNode,
- messageId: 'unexpected'
- })
- }
- }
-
- function verifyReferences(node, reportNode) {
- const variable = findVariable(context.getScope(), node)
- if (!variable) {
- return
- }
- for (const reference of variable.references) {
- if (!reference.isRead()) {
- continue
- }
-
- const id = reference.identifier
- verify(id, reportNode)
- }
- }
- return utils.defineVueVisitor(context, {
-
- MemberExpression(node) {
- const object = utils.skipChainExpression(node.object)
- if (object.type !== 'MemberExpression') {
- return
- }
- if (utils.getStaticPropertyName(object) !== '$slots') {
- return
- }
- if (!utils.isThis(object.object, context)) {
- return
- }
- if (node.property.type === 'PrivateIdentifier') {
-
- return
- }
- verify(node, node.property)
- }
- })
- }
- }
|