123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- "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",
- defaultOptions: [{
- enforceForClassMembers: true,
- getWithoutSet: false,
- setWithoutGet: true
- }],
- 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"
- },
- setWithoutGet: {
- type: "boolean"
- },
- enforceForClassMembers: {
- type: "boolean"
- }
- },
- 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 [{
- getWithoutSet: checkGetWithoutSet,
- setWithoutGet: checkSetWithoutGet,
- enforceForClassMembers
- }] = context.options;
- 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;
- }
- };
|