|
|
|
|
|
|
|
|
|
|
|
"use strict"; |
|
|
|
|
|
|
|
|
|
|
|
const astUtils = require("./utils/ast-utils"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function areEqualTokenLists(left, right) { |
|
if (left.length !== right.length) { |
|
return false; |
|
} |
|
|
|
for (let i = 0; i < left.length; i++) { |
|
const leftToken = left[i], |
|
rightToken = right[i]; |
|
|
|
if (leftToken.type !== rightToken.type || leftToken.value !== rightToken.value) { |
|
return false; |
|
} |
|
} |
|
|
|
return true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function areEqualKeys(left, right) { |
|
if (typeof left === "string" && typeof right === "string") { |
|
|
|
|
|
return left === right; |
|
} |
|
if (Array.isArray(left) && Array.isArray(right)) { |
|
|
|
|
|
return areEqualTokenLists(left, right); |
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function isAccessorKind(node) { |
|
return node.kind === "get" || node.kind === "set"; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isArgumentOfMethodCall(node, index, object, property) { |
|
const parent = node.parent; |
|
|
|
return ( |
|
parent.type === "CallExpression" && |
|
astUtils.isSpecificMemberAccess(parent.callee, object, property) && |
|
parent.arguments[index] === node |
|
); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function isPropertyDescriptor(node) { |
|
|
|
|
|
if (isArgumentOfMethodCall(node, 2, "Object", "defineProperty") || |
|
isArgumentOfMethodCall(node, 2, "Reflect", "defineProperty") |
|
) { |
|
return true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
const grandparent = node.parent.parent; |
|
|
|
return grandparent.type === "ObjectExpression" && ( |
|
isArgumentOfMethodCall(grandparent, 1, "Object", "create") || |
|
isArgumentOfMethodCall(grandparent, 1, "Object", "defineProperties") |
|
); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = { |
|
meta: { |
|
type: "suggestion", |
|
|
|
docs: { |
|
description: "Enforce getter and setter pairs in objects and classes", |
|
recommended: false, |
|
url: "https://eslint.org/docs/latest/rules/accessor-pairs" |
|
}, |
|
|
|
schema: [{ |
|
type: "object", |
|
properties: { |
|
getWithoutSet: { |
|
type: "boolean", |
|
default: false |
|
}, |
|
setWithoutGet: { |
|
type: "boolean", |
|
default: true |
|
}, |
|
enforceForClassMembers: { |
|
type: "boolean", |
|
default: true |
|
} |
|
}, |
|
additionalProperties: false |
|
}], |
|
|
|
messages: { |
|
missingGetterInPropertyDescriptor: "Getter is not present in property descriptor.", |
|
missingSetterInPropertyDescriptor: "Setter is not present in property descriptor.", |
|
missingGetterInObjectLiteral: "Getter is not present for {{ name }}.", |
|
missingSetterInObjectLiteral: "Setter is not present for {{ name }}.", |
|
missingGetterInClass: "Getter is not present for class {{ name }}.", |
|
missingSetterInClass: "Setter is not present for class {{ name }}." |
|
} |
|
}, |
|
create(context) { |
|
const config = context.options[0] || {}; |
|
const checkGetWithoutSet = config.getWithoutSet === true; |
|
const checkSetWithoutGet = config.setWithoutGet !== false; |
|
const enforceForClassMembers = config.enforceForClassMembers !== false; |
|
const sourceCode = context.sourceCode; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function report(node, messageKind) { |
|
if (node.type === "Property") { |
|
context.report({ |
|
node, |
|
messageId: `${messageKind}InObjectLiteral`, |
|
loc: astUtils.getFunctionHeadLoc(node.value, sourceCode), |
|
data: { name: astUtils.getFunctionNameWithKind(node.value) } |
|
}); |
|
} else if (node.type === "MethodDefinition") { |
|
context.report({ |
|
node, |
|
messageId: `${messageKind}InClass`, |
|
loc: astUtils.getFunctionHeadLoc(node.value, sourceCode), |
|
data: { name: astUtils.getFunctionNameWithKind(node.value) } |
|
}); |
|
} else { |
|
context.report({ |
|
node, |
|
messageId: `${messageKind}InPropertyDescriptor` |
|
}); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function reportList(nodes, messageKind) { |
|
for (const node of nodes) { |
|
report(node, messageKind); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function checkList(nodes) { |
|
const accessors = []; |
|
let found = false; |
|
|
|
for (let i = 0; i < nodes.length; i++) { |
|
const node = nodes[i]; |
|
|
|
if (isAccessorKind(node)) { |
|
|
|
|
|
const name = astUtils.getStaticPropertyName(node); |
|
const key = (name !== null) ? name : sourceCode.getTokens(node.key); |
|
|
|
|
|
for (let j = 0; j < accessors.length; j++) { |
|
const accessor = accessors[j]; |
|
|
|
if (areEqualKeys(accessor.key, key)) { |
|
accessor.getters.push(...node.kind === "get" ? [node] : []); |
|
accessor.setters.push(...node.kind === "set" ? [node] : []); |
|
found = true; |
|
break; |
|
} |
|
} |
|
if (!found) { |
|
accessors.push({ |
|
key, |
|
getters: node.kind === "get" ? [node] : [], |
|
setters: node.kind === "set" ? [node] : [] |
|
}); |
|
} |
|
found = false; |
|
} |
|
} |
|
|
|
for (const { getters, setters } of accessors) { |
|
if (checkSetWithoutGet && setters.length && !getters.length) { |
|
reportList(setters, "missingGetter"); |
|
} |
|
if (checkGetWithoutSet && getters.length && !setters.length) { |
|
reportList(getters, "missingSetter"); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function checkObjectLiteral(node) { |
|
checkList(node.properties.filter(p => p.type === "Property")); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function checkPropertyDescriptor(node) { |
|
const namesToCheck = new Set(node.properties |
|
.filter(p => p.type === "Property" && p.kind === "init" && !p.computed) |
|
.map(({ key }) => key.name)); |
|
|
|
const hasGetter = namesToCheck.has("get"); |
|
const hasSetter = namesToCheck.has("set"); |
|
|
|
if (checkSetWithoutGet && hasSetter && !hasGetter) { |
|
report(node, "missingGetter"); |
|
} |
|
if (checkGetWithoutSet && hasGetter && !hasSetter) { |
|
report(node, "missingSetter"); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function checkObjectExpression(node) { |
|
checkObjectLiteral(node); |
|
if (isPropertyDescriptor(node)) { |
|
checkPropertyDescriptor(node); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function checkClassBody(node) { |
|
const methodDefinitions = node.body.filter(m => m.type === "MethodDefinition"); |
|
|
|
checkList(methodDefinitions.filter(m => m.static)); |
|
checkList(methodDefinitions.filter(m => !m.static)); |
|
} |
|
|
|
const listeners = {}; |
|
|
|
if (checkSetWithoutGet || checkGetWithoutSet) { |
|
listeners.ObjectExpression = checkObjectExpression; |
|
if (enforceForClassMembers) { |
|
listeners.ClassBody = checkClassBody; |
|
} |
|
} |
|
|
|
return listeners; |
|
} |
|
}; |
|
|