UNPKG

eslint-plugin-vue

Version:

Official ESLint plugin for Vue.js

115 lines (114 loc) • 4.91 kB
const require_ref_object_references = require("../utils/ref-object-references.js"); const require_index = require("../utils/index.js"); let _eslint_community_eslint_utils = require("@eslint-community/eslint-utils"); // lib/rules/require-typed-ref.ts /** * @author Ivan Demchuk <https://github.com/Demivan> * See LICENSE file in root directory for full license. */ /** `ref() as Ref<T>`, `ref() satisfies Ref<T>` and `<Ref<T>>ref()` */ const TYPE_ASSERTIONS = /* @__PURE__ */ new Set([ "TSAsExpression", "TSSatisfiesExpression", "TSTypeAssertion" ]); function isNullOrUndefined(node) { return node.type === "Literal" && node.value === null || node.type === "Identifier" && node.name === "undefined"; } function hasTypeAnnotation(node) { return node.type === "AssignmentPattern" ? hasTypeAnnotation(node.left) : "typeAnnotation" in node && node.typeAnnotation != null; } /** * Resolves the single definition of the given identifier reference. */ function findDefinition(context, node) { const variable = (0, _eslint_community_eslint_utils.findVariable)(require_index.default.getScope(context, node), node); return variable?.defs.length === 1 ? variable.defs[0] : null; } /** * Checks whether the given identifier refers to a variable or a parameter * that was declared with an explicit type annotation. */ function isTypedVariable(context, node) { const def = findDefinition(context, node); return (def?.type === "Variable" || def?.type === "Parameter") && hasTypeAnnotation(def.name); } /** * Resolves the identifier to the function it refers to, if that function is * declared in the same file. */ function findFunction(context, node) { const def = findDefinition(context, node); if (def?.type === "FunctionName") return def.node; const init = def?.type === "Variable" ? def.node.init : null; return init != null && (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression") ? init : null; } /** * Checks whether the parameter receiving the argument at the given index has * an explicit type annotation. */ function hasTypedParameter(fn, index) { const param = fn.params[index]; if (param != null) return hasTypeAnnotation(param); const lastParam = fn.params.at(-1); return lastParam?.type === "RestElement" && hasTypeAnnotation(lastParam); } /** * Checks whether an explicit type annotation already applies to the given * expression, which makes the type of the created ref known. */ function isInTypedPosition(context, node) { const parent = node.parent; if (TYPE_ASSERTIONS.has(parent.type)) return true; switch (parent.type) { case "VariableDeclarator": return parent.init === node && hasTypeAnnotation(parent.id); case "AssignmentPattern": return parent.right === node && hasTypeAnnotation(parent.left); case "AssignmentExpression": return parent.operator === "=" && parent.left.type === "Identifier" && isTypedVariable(context, parent.left); case "CallExpression": { const index = parent.arguments.indexOf(node); if (index === -1 || parent.callee.type !== "Identifier") return false; const fn = findFunction(context, parent.callee); return fn != null && hasTypedParameter(fn, index); } default: return false; } } var require_typed_ref_default = { meta: { type: "suggestion", docs: { description: "require `ref` and `shallowRef` functions to be strongly typed", categories: void 0, url: "https://eslint.vuejs.org/rules/require-typed-ref.html" }, fixable: null, schema: [], messages: { noType: "Specify type parameter for `{{name}}` function, otherwise created variable will not be typechecked." } }, create(context) { const filename = context.filename; if (!require_index.default.isVueFile(filename) && !require_index.default.isTypeScriptFile(filename)) return {}; if (require_index.default.isVueFile(filename)) { const sourceCode = context.sourceCode; const documentFragment = sourceCode.parserServices.getDocumentFragment && sourceCode.parserServices.getDocumentFragment(); if (!documentFragment) return {}; if (documentFragment.children.filter((element) => require_index.default.isVElement(element) && element.name === "script").every((script) => !require_index.default.hasAttribute(script, "lang", "ts"))) return {}; } const defines = require_ref_object_references.iterateDefineRefs(context.sourceCode.scopeManager.globalScope); function report(name, node) { context.report({ node, messageId: "noType", data: { name } }); } return { Program() { for (const ref of defines) { if (ref.name !== "ref" && ref.name !== "shallowRef" || ref.node.arguments.length > 0 && !isNullOrUndefined(ref.node.arguments[0])) continue; if (("typeArguments" in ref.node ? ref.node.typeArguments : ref.node.typeParameters) != null) continue; if (!isInTypedPosition(context, ref.node)) report(ref.name, ref.node); } } }; } }; exports.default = require_typed_ref_default;