|
| 1 | +/** |
| 2 | + * @author 2nofa11 |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const { findVariable } = require('@eslint-community/eslint-utils') |
| 8 | +const utils = require('../utils') |
| 9 | + |
| 10 | +module.exports = { |
| 11 | + meta: { |
| 12 | + type: 'problem', |
| 13 | + docs: { |
| 14 | + description: 'require `MaybeRef` values to be unwrapped with `unref()` before using in conditions', |
| 15 | + categories: undefined, |
| 16 | + url: 'https://eslint.vuejs.org/rules/require-mayberef-unwrap.html' |
| 17 | + }, |
| 18 | + fixable: null, |
| 19 | + hasSuggestions: true, |
| 20 | + schema: [], |
| 21 | + messages: { |
| 22 | + requireUnref: 'MaybeRef should be unwrapped with `unref()` before using in conditions. Use `unref({{name}})` instead.', |
| 23 | + wrapWithUnref: 'Wrap with unref().' |
| 24 | + } |
| 25 | + }, |
| 26 | + /** @param {RuleContext} context */ |
| 27 | + create(context) { |
| 28 | + /** |
| 29 | + * Determine if identifier should be considered MaybeRef |
| 30 | + * @param {Identifier} node |
| 31 | + */ |
| 32 | + function isMaybeRef(node) { |
| 33 | + const variable = findVariable(utils.getScope(context, node), node) |
| 34 | + const id = variable?.defs[0]?.node?.id |
| 35 | + if (id?.type === 'Identifier' && id.typeAnnotation) { |
| 36 | + return isMaybeRefTypeNode(id.typeAnnotation.typeAnnotation) |
| 37 | + } |
| 38 | + return false |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Check TypeScript type node for MaybeRef/MaybeRefOrGetter |
| 43 | + * @param {import('@typescript-eslint/types').TSESTree.TypeNode | undefined} typeNode |
| 44 | + * @returns {boolean} |
| 45 | + */ |
| 46 | + function isMaybeRefTypeNode(typeNode) { |
| 47 | + if (!typeNode) return false |
| 48 | + if (typeNode.type === 'TSTypeReference') { |
| 49 | + if (typeNode.typeName && typeNode.typeName.type === 'Identifier') { |
| 50 | + return ( |
| 51 | + typeNode.typeName.name === 'MaybeRef' || |
| 52 | + typeNode.typeName.name === 'MaybeRefOrGetter' |
| 53 | + ) |
| 54 | + } |
| 55 | + } |
| 56 | + if (typeNode.type === 'TSUnionType') { |
| 57 | + return typeNode.types.some((t) => isMaybeRefTypeNode(t)) |
| 58 | + } |
| 59 | + return false |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Reports if the identifier is a MaybeRef type |
| 64 | + * @param {Identifier} node |
| 65 | + */ |
| 66 | + function reportIfMaybeRef(node) { |
| 67 | + if (!isMaybeRef(node)) return |
| 68 | + |
| 69 | + const sourceCode = context.getSourceCode() |
| 70 | + context.report({ |
| 71 | + node, |
| 72 | + messageId: 'requireUnref', |
| 73 | + data: { name: node.name }, |
| 74 | + suggest: [ |
| 75 | + { |
| 76 | + messageId: 'wrapWithUnref', |
| 77 | + /** @param {*} fixer */ |
| 78 | + fix(fixer) { |
| 79 | + return fixer.replaceText(node, `unref(${sourceCode.getText(node)})`) |
| 80 | + } |
| 81 | + } |
| 82 | + ] |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + return { |
| 87 | + // if (maybeRef) |
| 88 | + /** @param {Identifier} node */ |
| 89 | + 'IfStatement>Identifier'(node) { |
| 90 | + reportIfMaybeRef(node) |
| 91 | + }, |
| 92 | + // maybeRef ? x : y |
| 93 | + /** @param {Identifier & {parent: ConditionalExpression}} node */ |
| 94 | + 'ConditionalExpression>Identifier'(node) { |
| 95 | + if (node.parent.test === node) { |
| 96 | + reportIfMaybeRef(node) |
| 97 | + } |
| 98 | + }, |
| 99 | + // !maybeRef, +maybeRef, -maybeRef, ~maybeRef, typeof maybeRef |
| 100 | + /** @param {Identifier} node */ |
| 101 | + 'UnaryExpression>Identifier'(node) { |
| 102 | + reportIfMaybeRef(node) |
| 103 | + }, |
| 104 | + // maybeRef || other, maybeRef && other, maybeRef ?? other |
| 105 | + /** @param {Identifier & {parent: LogicalExpression}} node */ |
| 106 | + 'LogicalExpression>Identifier'(node) { |
| 107 | + reportIfMaybeRef(node) |
| 108 | + }, |
| 109 | + // maybeRef == x, maybeRef != x, maybeRef === x, maybeRef !== x |
| 110 | + /** @param {Identifier} node */ |
| 111 | + 'BinaryExpression>Identifier'(node) { |
| 112 | + reportIfMaybeRef(node) |
| 113 | + }, |
| 114 | + // Boolean(maybeRef), String(maybeRef) |
| 115 | + /** @param {Identifier} node */ |
| 116 | + 'CallExpression>Identifier'(node) { |
| 117 | + if (node.parent && |
| 118 | + node.parent.type === 'CallExpression' && |
| 119 | + node.parent.callee && |
| 120 | + node.parent.callee.type === 'Identifier' && |
| 121 | + ['Boolean', 'String'].includes(node.parent.callee.name) && |
| 122 | + node.parent.arguments[0] === node) { |
| 123 | + reportIfMaybeRef(node) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments