eslint-plugin-vue
Version:
Official ESLint plugin for Vue.js
359 lines (358 loc) • 12.3 kB
JavaScript
const require_runtime = require("../_virtual/_rolldown/runtime.js");
const require_index = require("../utils/index.js");
let _eslint_community_eslint_utils = require("@eslint-community/eslint-utils");
_eslint_community_eslint_utils = require_runtime.__toESM(_eslint_community_eslint_utils);
// lib/rules/no-use-computed-property-like-method.ts
/**
* @author tyankatsu <https://github.com/tyankatsu0105>
* See LICENSE file in root directory for full license.
*/
const GROUPS = /* @__PURE__ */ new Set([
"data",
"props",
"computed",
"methods"
]);
const NATIVE_NOT_FUNCTION_TYPES = /* @__PURE__ */ new Set([
"String",
"Number",
"BigInt",
"Boolean",
"Object",
"Array",
"Symbol"
]);
function resolvedExpressions(context, node) {
const resolvedMap = /* @__PURE__ */ new Map();
return resolvedExpressionsInternal(node);
function resolvedExpressionsInternal(node) {
let resolvedSet = resolvedMap.get(node);
if (!resolvedSet) {
resolvedSet = /* @__PURE__ */ new Set();
resolvedMap.set(node, resolvedSet);
for (const e of extractResolvedExpressions(node)) resolvedSet.add(e);
}
if (resolvedSet.size === 0) resolvedSet.add(node);
return resolvedSet;
}
function* extractResolvedExpressions(node) {
switch (node.type) {
case "Identifier": {
const variable = require_index.default.findVariableByIdentifier(context, node);
if (variable) for (const ref of variable.references) {
const id = ref.identifier;
if (id.parent.type === "VariableDeclarator") {
if (id.parent.id === id && id.parent.init) yield* resolvedExpressionsInternal(id.parent.init);
} else if (id.parent.type === "AssignmentExpression" && id.parent.left === id) yield* resolvedExpressionsInternal(id.parent.right);
}
break;
}
case "ConditionalExpression":
yield* resolvedExpressionsInternal(node.consequent);
yield* resolvedExpressionsInternal(node.alternate);
break;
case "LogicalExpression":
yield* resolvedExpressionsInternal(node.left);
yield* resolvedExpressionsInternal(node.right);
}
}
}
/**
* Get type of props item.
* Can't consider array props like: props: {propsA: [String, Number, Function]}
*
* @example
* props: {
* propA: String, // => String
* propB: {
* type: Number // => Number
* },
* }
*/
function getComponentPropsTypes(context, prop) {
const result = [];
for (const expr of resolvedExpressions(context, prop.property.value)) {
const types = getComponentPropsTypesFromExpression(expr);
if (types == null) return null;
result.push(...types);
}
return result;
function getComponentPropsTypesFromExpression(expr) {
let typeExprs;
/**
* Check object props `props: { objectProps: {...} }`
*/
if (expr.type === "ObjectExpression") {
const type = require_index.default.findProperty(expr, "type");
if (type == null) return null;
typeExprs = resolvedExpressions(context, type.value);
} else typeExprs = [expr];
const result = [];
for (const typeExpr of typeExprs) {
const types = getComponentPropsTypesFromTypeExpression(typeExpr);
if (types == null) return null;
result.push(...types);
}
return result;
}
function getComponentPropsTypesFromTypeExpression(typeExpr) {
if (typeExpr.type === "Identifier") return [typeExpr.name];
if (typeExpr.type === "ArrayExpression") {
const types = [];
for (const element of typeExpr.elements) {
if (!element) continue;
if (element.type === "SpreadElement") return null;
for (const elementExpr of resolvedExpressions(context, element)) {
if (elementExpr.type !== "Identifier") return null;
types.push(elementExpr.name);
}
}
return types;
}
return null;
}
}
/** Expression types that can never resolve to a function. */
const NON_FUNCTION_EXPRESSION_TYPES = /* @__PURE__ */ new Set([
"ObjectExpression",
"ArrayExpression",
"Literal",
"TemplateLiteral",
"BinaryExpression",
"UnaryExpression",
"UpdateExpression"
]);
/**
* Check whether given expression may be a function.
*/
function canBeFunction(context, node) {
for (const expr of resolvedExpressions(context, node)) {
if (NON_FUNCTION_EXPRESSION_TYPES.has(expr.type) || expr.type === "ConditionalExpression" && !canBeFunction(context, expr.consequent) && !canBeFunction(context, expr.alternate)) continue;
const evaluated = _eslint_community_eslint_utils.default.getStaticValue(expr, require_index.default.getScope(context, expr));
if (!evaluated || typeof evaluated.value === "function") return true;
}
return false;
}
var FunctionData = class {
context;
name;
kind;
node;
returnValues;
cacheMaybeReturnFunction;
constructor(name, kind, node, context) {
this.context = context;
this.name = name;
this.kind = kind;
this.node = node;
this.returnValues = [];
this.cacheMaybeReturnFunction = null;
}
addReturnValue(node) {
this.returnValues.push(node);
}
maybeReturnFunction(component) {
if (this.cacheMaybeReturnFunction != null) return this.cacheMaybeReturnFunction;
this.cacheMaybeReturnFunction = true;
return this.cacheMaybeReturnFunction = this.returnValues.some((returnValue) => returnValue && component.maybeFunctionExpression(returnValue));
}
};
/** Component information class. */
var ComponentStack = class {
node;
context;
/** Upper scope component */
upper;
maybeFunctions;
functions;
callMembers;
cacheMaybeFunctionExpressions;
constructor(node, context, upper) {
this.node = node;
this.context = context;
this.upper = upper;
const maybeFunctions = /* @__PURE__ */ new Map();
const functions = [];
function collectProperty(property) {
switch (property.groupName) {
case "data":
maybeFunctions.set(property.name, canBeFunction(context, property.property.value));
break;
case "props": {
const types = getComponentPropsTypes(context, property);
maybeFunctions.set(property.name, !types || types.some((type) => !NATIVE_NOT_FUNCTION_TYPES.has(type)));
break;
}
case "computed": {
let value = property.property.value;
if (value.type === "ObjectExpression") {
const getProp = require_index.default.findProperty(value, "get");
if (getProp) value = getProp.value;
}
processFunction(property.name, value, "computed");
break;
}
case "methods": {
const value = property.property.value;
processFunction(property.name, value, "methods");
maybeFunctions.set(property.name, true);
break;
}
}
}
for (const property of require_index.default.iterateProperties(node, GROUPS)) if (property.type !== "array") collectProperty(property);
this.maybeFunctions = maybeFunctions;
this.functions = functions;
this.callMembers = [];
this.cacheMaybeFunctionExpressions = /* @__PURE__ */ new Map();
function processFunction(name, value, kind) {
if (value.type === "FunctionExpression") functions.push(new FunctionData(name, kind, value, context));
else if (value.type === "ArrowFunctionExpression") {
const data = new FunctionData(name, kind, value, context);
if (value.expression) data.addReturnValue(value.body);
functions.push(data);
}
}
}
/**
* Adds the given return statement to the return value of the function.
*/
addReturnStatement(scopeFunction, returnNode) {
for (const data of this.functions) if (data.node === scopeFunction) {
data.addReturnValue(returnNode.argument);
break;
}
}
verifyComponent() {
for (const call of this.callMembers) this.verifyCallMember(call);
}
verifyCallMember(call) {
const fnData = this.functions.find((data) => data.name === call.name && data.kind === "computed");
if (!fnData || fnData.maybeReturnFunction(this)) return;
const prefix = call.node.callee.type === "MemberExpression" ? "this." : "";
this.context.report({
node: call.node,
messageId: "unexpected",
data: {
likeProperty: `${prefix}${call.name}`,
likeMethod: `${prefix}${call.name}()`
}
});
}
/**
* Check whether given expression may be a function.
*/
maybeFunctionExpression(node) {
const cache = this.cacheMaybeFunctionExpressions.get(node);
if (cache != null) return cache;
this.cacheMaybeFunctionExpressions.set(node, true);
const result = maybeFunctionExpressionWithoutCache.call(this);
this.cacheMaybeFunctionExpressions.set(node, result);
return result;
function maybeFunctionExpressionWithoutCache() {
for (const expr of resolvedExpressions(this.context, node)) {
if (!canBeFunction(this.context, expr)) continue;
if (this.expressionCouldBeFunction(expr)) return true;
}
return false;
}
}
/**
* Check whether the given expression could be a function.
*/
expressionCouldBeFunction(expr) {
switch (expr.type) {
case "MemberExpression":
if (require_index.default.isThis(expr.object, this.context)) {
const name = require_index.default.getStaticPropertyName(expr);
if (name && !this.maybeFunctionProperty(name)) return false;
}
return true;
case "CallExpression":
if (expr.callee.type === "MemberExpression" && require_index.default.isThis(expr.callee.object, this.context)) {
const name = require_index.default.getStaticPropertyName(expr.callee);
const fnData = this.functions.find((data) => data.name === name);
if (fnData && fnData.kind === "methods" && !fnData.maybeReturnFunction(this)) return false;
}
return true;
case "ConditionalExpression": return this.maybeFunctionExpression(expr.consequent) || this.maybeFunctionExpression(expr.alternate);
default: return true;
}
}
/**
* Check whether given property name may be a function.
*/
maybeFunctionProperty(name) {
const cache = this.maybeFunctions.get(name);
if (cache != null) return cache;
this.maybeFunctions.set(name, true);
const result = maybeFunctionPropertyWithoutCache.call(this);
this.maybeFunctions.set(name, result);
return result;
function maybeFunctionPropertyWithoutCache() {
const fnData = this.functions.find((data) => data.name === name);
return fnData && fnData.kind === "computed" ? fnData.maybeReturnFunction(this) : true;
}
}
};
var no_use_computed_property_like_method_default = {
meta: {
type: "problem",
docs: {
description: "disallow use computed property like method",
categories: ["vue3-essential", "vue2-essential"],
url: "https://eslint.vuejs.org/rules/no-use-computed-property-like-method.html"
},
fixable: null,
schema: [],
messages: { unexpected: "Use {{ likeProperty }} instead of {{ likeMethod }}." }
},
create(context) {
let scopeStack = null;
let componentStack = null;
let templateComponent = null;
return require_index.default.compositingVisitors({}, require_index.default.defineVueVisitor(context, {
onVueObjectEnter(node) {
componentStack = new ComponentStack(node, context, componentStack);
if (!templateComponent && require_index.default.isInExportDefault(node)) templateComponent = componentStack;
},
onVueObjectExit() {
if (!componentStack) return;
componentStack.verifyComponent();
componentStack = componentStack.upper;
},
":function"(node) {
scopeStack = {
upper: scopeStack,
scopeNode: node
};
},
ReturnStatement(node) {
if (scopeStack && componentStack) componentStack.addReturnStatement(scopeStack.scopeNode, node);
},
":function:exit"() {
scopeStack &&= scopeStack.upper;
},
"ThisExpression, Identifier"(node) {
if (!componentStack || node.parent.type !== "MemberExpression" || node.parent.object !== node || node.parent.parent.type !== "CallExpression" || node.parent.parent.callee !== node.parent || !require_index.default.isThis(node, context)) return;
const name = require_index.default.getStaticPropertyName(node.parent);
if (name) componentStack.callMembers.push({
name,
node: node.parent.parent
});
}
}), require_index.default.defineTemplateBodyVisitor(context, { VExpressionContainer(node) {
if (!templateComponent) return;
for (const ref of node.references) {
if (ref.variable != null) continue;
const id = ref.id;
if (id.parent.type !== "CallExpression" || id.parent.callee !== id) continue;
templateComponent.verifyCallMember({
name: id.name,
node: id.parent
});
}
} }));
}
};
exports.default = no_use_computed_property_like_method_default;