eslint-plugin-complete
Version:
An ESLint plugin that contains useful rules.
52 lines (51 loc) • 1.97 kB
JavaScript
import { AST_NODE_TYPES, ESLintUtils } from "@typescript-eslint/utils";
import { getTypeName } from "../typeUtils.js";
import { createRule } from "../utils.js";
const METHOD_NAME_TO_MESSAGE_ID = new Map([
["keys", "noKeys"],
["entries", "noEntries"],
]);
export const noConfusingSetMethods = createRule({
name: "no-confusing-set-methods",
meta: {
type: "problem",
docs: {
description: "Disallows confusing methods for sets",
recommended: true,
requiresTypeChecking: true,
},
schema: [],
messages: {
noKeys: "Using the `Set.keys` method is confusing, since sets do not have keys. Use the `Set.values` method instead.",
noEntries: "Using the `Set.entries` method is confusing, since sets only have values. Use the `Set.values` method instead.",
},
},
defaultOptions: [],
create(context) {
const parserServices = ESLintUtils.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
return {
MemberExpression(node) {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node.object);
const type = checker.getTypeAtLocation(tsNode);
const typeName = getTypeName(type);
if (typeName !== "Set" && typeName !== "ReadonlySet") {
return;
}
const { property } = node;
if (property.type !== AST_NODE_TYPES.Identifier) {
return;
}
const methodName = property.name;
const messageId = METHOD_NAME_TO_MESSAGE_ID.get(methodName);
if (messageId === undefined) {
return;
}
context.report({
loc: node.loc,
messageId,
});
},
};
},
});