|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | + |
| 9 | +/** |
| 10 | + * @typedef {import('vue-eslint-parser').AST.ESLintExpression} Expression |
| 11 | + */ |
| 12 | + |
| 13 | +/** |
| 14 | + * Checks if the given node value is falsy. |
| 15 | + * @param {Expression} node The node to check |
| 16 | + * @returns {boolean} If `true`, the given node value is falsy. |
| 17 | + */ |
| 18 | +function isFalsy (node) { |
| 19 | + if (node.type === 'Literal') { |
| 20 | + if (node.bigint) { |
| 21 | + return node.bigint === '0' |
| 22 | + } else if (!node.value) { |
| 23 | + return true |
| 24 | + } |
| 25 | + } else if (node.type === 'Identifier') { |
| 26 | + if (node.name === 'undefined' || node.name === 'NaN') { |
| 27 | + return true |
| 28 | + } |
| 29 | + } |
| 30 | + return false |
| 31 | +} |
| 32 | +// ------------------------------------------------------------------------------ |
| 33 | +// Rule Definition |
| 34 | +// ------------------------------------------------------------------------------ |
| 35 | + |
| 36 | +module.exports = { |
| 37 | + meta: { |
| 38 | + type: 'problem', |
| 39 | + docs: { |
| 40 | + description: 'enforce that a return statement is present in emits validator', |
| 41 | + categories: ['vue3-essential'], |
| 42 | + url: 'https://eslint.vuejs.org/rules/return-in-emits-validator.html' |
| 43 | + }, |
| 44 | + fixable: null, // or "code" or "whitespace" |
| 45 | + schema: [] |
| 46 | + }, |
| 47 | + |
| 48 | + create (context) { |
| 49 | + const emitsValidators = [] |
| 50 | + |
| 51 | + // ---------------------------------------------------------------------- |
| 52 | + // Public |
| 53 | + // ---------------------------------------------------------------------- |
| 54 | + |
| 55 | + let scopeStack = null |
| 56 | + |
| 57 | + return Object.assign({}, |
| 58 | + utils.defineVueVisitor(context, |
| 59 | + { |
| 60 | + ObjectExpression (obj, { node: vueNode }) { |
| 61 | + if (obj !== vueNode) { |
| 62 | + return |
| 63 | + } |
| 64 | + for (const emits of utils.getComponentEmits(obj)) { |
| 65 | + const emitsValue = emits.value |
| 66 | + if (!emitsValue) { |
| 67 | + continue |
| 68 | + } |
| 69 | + if (emitsValue.type !== 'FunctionExpression' && emitsValue.type !== 'ArrowFunctionExpression') { |
| 70 | + continue |
| 71 | + } |
| 72 | + emitsValidators.push(emits) |
| 73 | + } |
| 74 | + }, |
| 75 | + ':function' (node) { |
| 76 | + scopeStack = { upper: scopeStack, functionNode: node, hasReturnValue: false, possibleOfReturnTrue: false } |
| 77 | + }, |
| 78 | + ReturnStatement (node) { |
| 79 | + if (node.argument) { |
| 80 | + scopeStack.hasReturnValue = true |
| 81 | + |
| 82 | + if (!isFalsy(node.argument)) { |
| 83 | + scopeStack.possibleOfReturnTrue = true |
| 84 | + } |
| 85 | + } |
| 86 | + }, |
| 87 | + ':function:exit' (node) { |
| 88 | + if (!scopeStack.possibleOfReturnTrue) { |
| 89 | + const emits = emitsValidators.find(e => e.value === node) |
| 90 | + if (emits) { |
| 91 | + context.report({ |
| 92 | + node, |
| 93 | + message: scopeStack.hasReturnValue |
| 94 | + ? 'Expected to return a true value in "{{name}}" emits validator.' |
| 95 | + : 'Expected to return a boolean value in "{{name}}" emits validator.', |
| 96 | + data: { |
| 97 | + name: emits.emitName |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + scopeStack = scopeStack.upper |
| 104 | + } |
| 105 | + } |
| 106 | + ), |
| 107 | + ) |
| 108 | + } |
| 109 | +} |
0 commit comments