|
| 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 | + */ |
| 18 | + |
| 19 | +// ------------------------------------------------------------------------------ |
| 20 | +// Rule Definition |
| 21 | +// ------------------------------------------------------------------------------ |
| 22 | + |
| 23 | +module.exports = { |
| 24 | + meta: { |
| 25 | + type: 'problem', |
| 26 | + docs: { |
| 27 | + description: 'disallow to pass multiple arguments to scoped slots', |
| 28 | + categories: ['vue3-recommended', 'recommended'], |
| 29 | + url: 'https://eslint.vuejs.org/rules/no-multiple-slot-args.html' |
| 30 | + }, |
| 31 | + fixable: null, |
| 32 | + schema: [], |
| 33 | + messages: { |
| 34 | + unexpected: 'Unexpected multiple arguments.', |
| 35 | + unexpectedSpread: 'Unexpected spread argument.' |
| 36 | + } |
| 37 | + }, |
| 38 | + |
| 39 | + create(context) { |
| 40 | + /** |
| 41 | + * Verify the given node |
| 42 | + * @param {MemberExpression | Identifier} node The node to verify |
| 43 | + */ |
| 44 | + function verify(node) { |
| 45 | + const parent = node.parent |
| 46 | + |
| 47 | + if ( |
| 48 | + parent.type === 'VariableDeclarator' && |
| 49 | + parent.id.type === 'Identifier' |
| 50 | + ) { |
| 51 | + // const foo = this.$scopedSlots.foo |
| 52 | + verifyReferences(parent.id) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + if ( |
| 57 | + parent.type === 'AssignmentExpression' && |
| 58 | + parent.right === node && |
| 59 | + parent.left.type === 'Identifier' |
| 60 | + ) { |
| 61 | + // foo = this.$scopedSlots.foo |
| 62 | + verifyReferences(parent.left) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + if (parent.type !== 'CallExpression' || parent.arguments.includes(node)) { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + if (!parent.arguments.length) { |
| 71 | + return |
| 72 | + } |
| 73 | + if (parent.arguments.length > 1) { |
| 74 | + context.report({ |
| 75 | + node: parent.arguments[1], |
| 76 | + messageId: 'unexpected' |
| 77 | + }) |
| 78 | + } |
| 79 | + if (parent.arguments[0].type === 'SpreadElement') { |
| 80 | + context.report({ |
| 81 | + node: parent.arguments[0], |
| 82 | + messageId: 'unexpectedSpread' |
| 83 | + }) |
| 84 | + } |
| 85 | + } |
| 86 | + /** |
| 87 | + * Verify the references of the given node. |
| 88 | + * @param {Identifier} node The node to verify |
| 89 | + */ |
| 90 | + function verifyReferences(node) { |
| 91 | + // @ts-ignore |
| 92 | + const variable = findVariable(context.getScope(), node) |
| 93 | + if (!variable) { |
| 94 | + return |
| 95 | + } |
| 96 | + for (const reference of variable.references) { |
| 97 | + if (!reference.isRead()) { |
| 98 | + continue |
| 99 | + } |
| 100 | + /** @type {Identifier} */ |
| 101 | + const id = reference.identifier |
| 102 | + verify(id) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return utils.defineVueVisitor(context, { |
| 107 | + /** @param {MemberExpression} node */ |
| 108 | + MemberExpression(node) { |
| 109 | + const object = node.object |
| 110 | + if (object.type !== 'MemberExpression') { |
| 111 | + return |
| 112 | + } |
| 113 | + if ( |
| 114 | + object.property.type !== 'Identifier' || |
| 115 | + (object.property.name !== '$slots' && |
| 116 | + object.property.name !== '$scopedSlots') |
| 117 | + ) { |
| 118 | + return |
| 119 | + } |
| 120 | + if (!utils.isThis(object.object, context)) { |
| 121 | + return |
| 122 | + } |
| 123 | + verify(node) |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
0 commit comments