|
| 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 utils = require('../utils') |
| 12 | +const { findVariable } = require('eslint-utils') |
| 13 | + |
| 14 | +/** |
| 15 | + * @typedef {import('vue-eslint-parser').AST.ESLintMemberExpression} MemberExpression |
| 16 | + * @typedef {import('vue-eslint-parser').AST.ESLintIdentifier} Identifier |
| 17 | + * @typedef {import('vue-eslint-parser').AST.ESLintExpression} Expression |
| 18 | + */ |
| 19 | + |
| 20 | +// ------------------------------------------------------------------------------ |
| 21 | +// Rule Definition |
| 22 | +// ------------------------------------------------------------------------------ |
| 23 | + |
| 24 | +module.exports = { |
| 25 | + meta: { |
| 26 | + type: 'problem', |
| 27 | + docs: { |
| 28 | + description: 'enforce properties of `$slots` to be used as a function', |
| 29 | + categories: ['vue3-essential'], |
| 30 | + url: 'https://eslint.vuejs.org/rules/require-slots-as-functions.html' |
| 31 | + }, |
| 32 | + fixable: null, |
| 33 | + schema: [], |
| 34 | + messages: { |
| 35 | + unexpected: 'Property in `$slots` should be used as function.' |
| 36 | + } |
| 37 | + }, |
| 38 | + |
| 39 | + create(context) { |
| 40 | + /** |
| 41 | + * Verify the given node |
| 42 | + * @param {MemberExpression | Identifier} node The node to verify |
| 43 | + * @param {Expression} reportNode The node to report |
| 44 | + */ |
| 45 | + function verify(node, reportNode) { |
| 46 | + const parent = node.parent |
| 47 | + |
| 48 | + if ( |
| 49 | + parent.type === 'VariableDeclarator' && |
| 50 | + parent.id.type === 'Identifier' |
| 51 | + ) { |
| 52 | + // const children = this.$slots.foo |
| 53 | + verifyReferences(parent.id, reportNode) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + if ( |
| 58 | + parent.type === 'AssignmentExpression' && |
| 59 | + parent.right === node && |
| 60 | + parent.left.type === 'Identifier' |
| 61 | + ) { |
| 62 | + // children = this.$slots.foo |
| 63 | + verifyReferences(parent.left, reportNode) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + if ( |
| 68 | + // this.$slots.foo.xxx |
| 69 | + parent.type === 'MemberExpression' || |
| 70 | + // var [foo] = this.$slots.foo |
| 71 | + parent.type === 'VariableDeclarator' || |
| 72 | + // [...this.$slots.foo] |
| 73 | + parent.type === 'SpreadElement' || |
| 74 | + // [this.$slots.foo] |
| 75 | + parent.type === 'ArrayExpression' |
| 76 | + ) { |
| 77 | + context.report({ |
| 78 | + node: reportNode, |
| 79 | + messageId: 'unexpected' |
| 80 | + }) |
| 81 | + } |
| 82 | + } |
| 83 | + /** |
| 84 | + * Verify the references of the given node. |
| 85 | + * @param {Identifier} node The node to verify |
| 86 | + * @param {Expression} reportNode The node to report |
| 87 | + */ |
| 88 | + function verifyReferences(node, reportNode) { |
| 89 | + // @ts-ignore |
| 90 | + const variable = findVariable(context.getScope(), node) |
| 91 | + if (!variable) { |
| 92 | + return |
| 93 | + } |
| 94 | + for (const reference of variable.references) { |
| 95 | + if (!reference.isRead()) { |
| 96 | + continue |
| 97 | + } |
| 98 | + /** @type {Identifier} */ |
| 99 | + const id = reference.identifier |
| 100 | + verify(id, reportNode) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return utils.defineVueVisitor(context, { |
| 105 | + /** @param {MemberExpression} node */ |
| 106 | + MemberExpression(node) { |
| 107 | + const object = node.object |
| 108 | + if (object.type !== 'MemberExpression') { |
| 109 | + return |
| 110 | + } |
| 111 | + if ( |
| 112 | + object.property.type !== 'Identifier' || |
| 113 | + object.property.name !== '$slots' |
| 114 | + ) { |
| 115 | + return |
| 116 | + } |
| 117 | + if (!utils.isThis(object.object, context)) { |
| 118 | + return |
| 119 | + } |
| 120 | + verify(node, node.property) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments