|
| 1 | +/** |
| 2 | + * @author 2nofa11 |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | + |
| 9 | +/** |
| 10 | + * Check TypeScript type node for MaybeRef/MaybeRefOrGetter |
| 11 | + * @param {import('@typescript-eslint/types').TSESTree.TypeNode | undefined} typeNode |
| 12 | + * @returns {boolean} |
| 13 | + */ |
| 14 | +function isMaybeRefTypeNode(typeNode) { |
| 15 | + if (!typeNode) return false |
| 16 | + if ( |
| 17 | + typeNode.type === 'TSTypeReference' && |
| 18 | + typeNode.typeName && |
| 19 | + typeNode.typeName.type === 'Identifier' |
| 20 | + ) { |
| 21 | + return ( |
| 22 | + typeNode.typeName.name === 'MaybeRef' || |
| 23 | + typeNode.typeName.name === 'MaybeRefOrGetter' |
| 24 | + ) |
| 25 | + } |
| 26 | + if (typeNode.type === 'TSUnionType') { |
| 27 | + return typeNode.types.some((t) => isMaybeRefTypeNode(t)) |
| 28 | + } |
| 29 | + return false |
| 30 | +} |
| 31 | + |
| 32 | +module.exports = { |
| 33 | + meta: { |
| 34 | + type: 'problem', |
| 35 | + docs: { |
| 36 | + description: |
| 37 | + 'require `MaybeRef` values to be unwrapped with `unref()` before using in conditions', |
| 38 | + categories: undefined, |
| 39 | + url: 'https://eslint.vuejs.org/rules/require-mayberef-unwrap.html' |
| 40 | + }, |
| 41 | + fixable: 'code', |
| 42 | + schema: [], |
| 43 | + messages: { |
| 44 | + requireUnref: |
| 45 | + 'MaybeRef should be unwrapped with `unref()` before using in conditions. Use `unref({{name}})` instead.' |
| 46 | + } |
| 47 | + }, |
| 48 | + /** @param {RuleContext} context */ |
| 49 | + create(context) { |
| 50 | + const filename = context.getFilename() |
| 51 | + if (!utils.isVueFile(filename) && !utils.isTypeScriptFile(filename)) { |
| 52 | + return {} |
| 53 | + } |
| 54 | + |
| 55 | + /** @type {Map<string, Set<string>>} */ |
| 56 | + const maybeRefPropsMap = new Map() |
| 57 | + |
| 58 | + /** |
| 59 | + * Determine if identifier should be considered MaybeRef |
| 60 | + * @param {Identifier} node |
| 61 | + */ |
| 62 | + function isMaybeRef(node) { |
| 63 | + const variable = utils.findVariableByIdentifier(context, node) |
| 64 | + if (!variable) { |
| 65 | + return false |
| 66 | + } |
| 67 | + |
| 68 | + const definition = variable.defs[0] |
| 69 | + if (definition.type !== 'Variable') { |
| 70 | + return false |
| 71 | + } |
| 72 | + |
| 73 | + const id = definition.node?.id |
| 74 | + if (!id || id.type !== 'Identifier' || !id.typeAnnotation) { |
| 75 | + return false |
| 76 | + } |
| 77 | + |
| 78 | + return isMaybeRefTypeNode(id.typeAnnotation.typeAnnotation) |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Check if MemberExpression accesses a MaybeRef prop |
| 83 | + * @param {Identifier} objectNode |
| 84 | + * @param {string} propertyName |
| 85 | + */ |
| 86 | + function isMaybeRefPropsAccess(objectNode, propertyName) { |
| 87 | + if (!propertyName) { |
| 88 | + return false |
| 89 | + } |
| 90 | + |
| 91 | + const variable = utils.findVariableByIdentifier(context, objectNode) |
| 92 | + if (!variable) { |
| 93 | + return false |
| 94 | + } |
| 95 | + |
| 96 | + const maybeRefProps = maybeRefPropsMap.get(variable.name) |
| 97 | + return maybeRefProps ? maybeRefProps.has(propertyName) : false |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Reports if the identifier is a MaybeRef type |
| 102 | + * @param {Identifier} node |
| 103 | + * @param {string} [customName] Custom name for error message |
| 104 | + */ |
| 105 | + function reportIfMaybeRef(node, customName) { |
| 106 | + if (!isMaybeRef(node)) { |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + const sourceCode = context.getSourceCode() |
| 111 | + context.report({ |
| 112 | + node, |
| 113 | + messageId: 'requireUnref', |
| 114 | + data: { name: customName || node.name }, |
| 115 | + fix(fixer) { |
| 116 | + return fixer.replaceText(node, `unref(${sourceCode.getText(node)})`) |
| 117 | + } |
| 118 | + }) |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Reports if the MemberExpression accesses a MaybeRef prop |
| 123 | + * @param {MemberExpression} node |
| 124 | + */ |
| 125 | + function reportIfMaybeRefProps(node) { |
| 126 | + if (node.object.type !== 'Identifier') { |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + const propertyName = utils.getStaticPropertyName(node) |
| 131 | + if (!propertyName) { |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + if (!isMaybeRefPropsAccess(node.object, propertyName)) { |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + const sourceCode = context.getSourceCode() |
| 140 | + context.report({ |
| 141 | + node: node.property, |
| 142 | + messageId: 'requireUnref', |
| 143 | + data: { name: `${node.object.name}.${propertyName}` }, |
| 144 | + fix(fixer) { |
| 145 | + return fixer.replaceText(node, `unref(${sourceCode.getText(node)})`) |
| 146 | + } |
| 147 | + }) |
| 148 | + } |
| 149 | + |
| 150 | + return utils.compositingVisitors( |
| 151 | + { |
| 152 | + // if (maybeRef) |
| 153 | + /** @param {Identifier} node */ |
| 154 | + 'IfStatement>Identifier'(node) { |
| 155 | + reportIfMaybeRef(node) |
| 156 | + }, |
| 157 | + // maybeRef ? x : y |
| 158 | + /** @param {Identifier & {parent: ConditionalExpression}} node */ |
| 159 | + 'ConditionalExpression>Identifier'(node) { |
| 160 | + if (node.parent.test !== node) { |
| 161 | + return |
| 162 | + } |
| 163 | + reportIfMaybeRef(node) |
| 164 | + }, |
| 165 | + // !maybeRef, +maybeRef, -maybeRef, ~maybeRef, typeof maybeRef |
| 166 | + /** @param {Identifier} node */ |
| 167 | + 'UnaryExpression>Identifier'(node) { |
| 168 | + reportIfMaybeRef(node) |
| 169 | + }, |
| 170 | + // maybeRef || other, maybeRef && other, maybeRef ?? other |
| 171 | + /** @param {Identifier & {parent: LogicalExpression}} node */ |
| 172 | + 'LogicalExpression>Identifier'(node) { |
| 173 | + reportIfMaybeRef(node) |
| 174 | + }, |
| 175 | + // maybeRef == x, maybeRef != x, maybeRef === x, maybeRef !== x |
| 176 | + /** @param {Identifier} node */ |
| 177 | + 'BinaryExpression>Identifier'(node) { |
| 178 | + reportIfMaybeRef(node) |
| 179 | + }, |
| 180 | + // Boolean(maybeRef), String(maybeRef) |
| 181 | + /** @param {Identifier} node */ |
| 182 | + 'CallExpression>Identifier'(node) { |
| 183 | + const parent = node.parent |
| 184 | + if (parent?.type !== 'CallExpression') return |
| 185 | + |
| 186 | + const callee = parent.callee |
| 187 | + if (callee?.type !== 'Identifier') return |
| 188 | + |
| 189 | + if (!['Boolean', 'String'].includes(callee.name)) return |
| 190 | + |
| 191 | + if (parent.arguments[0] === node) { |
| 192 | + reportIfMaybeRef(node) |
| 193 | + } |
| 194 | + }, |
| 195 | + // props.maybeRefProp |
| 196 | + /** @param {MemberExpression} node */ |
| 197 | + MemberExpression(node) { |
| 198 | + reportIfMaybeRefProps(node) |
| 199 | + } |
| 200 | + }, |
| 201 | + utils.defineScriptSetupVisitor(context, { |
| 202 | + onDefinePropsEnter(node, props) { |
| 203 | + if ( |
| 204 | + !node.parent || |
| 205 | + node.parent.type !== 'VariableDeclarator' || |
| 206 | + node.parent.init !== node |
| 207 | + ) { |
| 208 | + return |
| 209 | + } |
| 210 | + |
| 211 | + const propsParam = node.parent.id |
| 212 | + if (propsParam.type !== 'Identifier') { |
| 213 | + return |
| 214 | + } |
| 215 | + |
| 216 | + const maybeRefProps = new Set() |
| 217 | + for (const prop of props) { |
| 218 | + if (prop.type !== 'type' || !prop.node) { |
| 219 | + continue |
| 220 | + } |
| 221 | + |
| 222 | + if ( |
| 223 | + prop.node.type !== 'TSPropertySignature' || |
| 224 | + !prop.node.typeAnnotation |
| 225 | + ) { |
| 226 | + continue |
| 227 | + } |
| 228 | + |
| 229 | + const typeAnnotation = prop.node.typeAnnotation.typeAnnotation |
| 230 | + if (isMaybeRefTypeNode(typeAnnotation)) { |
| 231 | + maybeRefProps.add(prop.propName) |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + if (maybeRefProps.size > 0) { |
| 236 | + maybeRefPropsMap.set(propsParam.name, maybeRefProps) |
| 237 | + } |
| 238 | + } |
| 239 | + }), |
| 240 | + utils.defineVueVisitor(context, { |
| 241 | + onSetupFunctionEnter(node) { |
| 242 | + const propsParam = utils.skipDefaultParamValue(node.params[0]) |
| 243 | + if (!propsParam || propsParam.type !== 'Identifier') { |
| 244 | + return |
| 245 | + } |
| 246 | + |
| 247 | + if (!propsParam.typeAnnotation) { |
| 248 | + return |
| 249 | + } |
| 250 | + |
| 251 | + const typeAnnotation = propsParam.typeAnnotation.typeAnnotation |
| 252 | + const maybeRefProps = new Set() |
| 253 | + |
| 254 | + if (typeAnnotation.type === 'TSTypeLiteral') { |
| 255 | + for (const member of typeAnnotation.members) { |
| 256 | + if ( |
| 257 | + member.type === 'TSPropertySignature' && |
| 258 | + member.key && |
| 259 | + member.key.type === 'Identifier' && |
| 260 | + member.typeAnnotation && |
| 261 | + isMaybeRefTypeNode(member.typeAnnotation.typeAnnotation) |
| 262 | + ) { |
| 263 | + maybeRefProps.add(member.key.name) |
| 264 | + } |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + if (maybeRefProps.size > 0) { |
| 269 | + maybeRefPropsMap.set(propsParam.name, maybeRefProps) |
| 270 | + } |
| 271 | + }, |
| 272 | + onVueObjectExit() { |
| 273 | + maybeRefPropsMap.clear() |
| 274 | + } |
| 275 | + }) |
| 276 | + ) |
| 277 | + } |
| 278 | +} |
0 commit comments