123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- "use strict";
- const astUtils = require("./utils/ast-utils");
- module.exports = {
- meta: {
- type: "problem",
- docs: {
- description: "Disallow duplicate class members",
- recommended: true,
- url: "https://eslint.org/docs/latest/rules/no-dupe-class-members"
- },
- schema: [],
- messages: {
- unexpected: "Duplicate name '{{name}}'."
- }
- },
- create(context) {
- let stack = [];
-
- function getState(name, isStatic) {
- const stateMap = stack.at(-1);
- const key = `$${name}`;
- if (!stateMap[key]) {
- stateMap[key] = {
- nonStatic: { init: false, get: false, set: false },
- static: { init: false, get: false, set: false }
- };
- }
- return stateMap[key][isStatic ? "static" : "nonStatic"];
- }
- return {
-
- Program() {
- stack = [];
- },
-
- ClassBody() {
- stack.push(Object.create(null));
- },
-
- "ClassBody:exit"() {
- stack.pop();
- },
-
- "MethodDefinition, PropertyDefinition"(node) {
- const name = astUtils.getStaticPropertyName(node);
- const kind = node.type === "MethodDefinition" ? node.kind : "field";
- if (name === null || kind === "constructor") {
- return;
- }
- const state = getState(name, node.static);
- let isDuplicate;
- if (kind === "get") {
- isDuplicate = (state.init || state.get);
- state.get = true;
- } else if (kind === "set") {
- isDuplicate = (state.init || state.set);
- state.set = true;
- } else {
- isDuplicate = (state.init || state.get || state.set);
- state.init = true;
- }
- if (isDuplicate) {
- context.report({ node, messageId: "unexpected", data: { name } });
- }
- }
- };
- }
- };
|