google_forms/node_modules/eslint/lib/rules/no-dupe-class-members.js

105 lines
3.3 KiB
JavaScript
Raw Normal View History

2024-08-09 12:04:48 +00:00
/**
* @fileoverview A rule to disallow duplicate name in class members.
* @author Toru Nagashima
*/
"use strict";
const astUtils = require("./utils/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
2024-08-21 06:34:30 +00:00
/** @type {import('../shared/types').Rule} */
2024-08-09 12:04:48 +00:00
module.exports = {
meta: {
type: "problem",
docs: {
2024-08-21 06:34:30 +00:00
description: "Disallow duplicate class members",
2024-08-09 12:04:48 +00:00
recommended: true,
2024-08-21 06:34:30 +00:00
url: "https://eslint.org/docs/latest/rules/no-dupe-class-members"
2024-08-09 12:04:48 +00:00
},
schema: [],
messages: {
unexpected: "Duplicate name '{{name}}'."
}
},
create(context) {
let stack = [];
/**
* Gets state of a given member name.
* @param {string} name A name of a member.
* @param {boolean} isStatic A flag which specifies that is a static member.
* @returns {Object} A state of a given member name.
* - retv.init {boolean} A flag which shows the name is declared as normal member.
* - retv.get {boolean} A flag which shows the name is declared as getter.
* - retv.set {boolean} A flag which shows the name is declared as setter.
*/
function getState(name, isStatic) {
const stateMap = stack[stack.length - 1];
const key = `$${name}`; // to avoid "__proto__".
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 {
// Initializes the stack of state of member declarations.
Program() {
stack = [];
},
// Initializes state of member declarations for the class.
ClassBody() {
stack.push(Object.create(null));
},
// Disposes the state for the class.
"ClassBody:exit"() {
stack.pop();
},
// Reports the node if its name has been declared already.
2024-08-21 06:34:30 +00:00
"MethodDefinition, PropertyDefinition"(node) {
2024-08-09 12:04:48 +00:00
const name = astUtils.getStaticPropertyName(node);
2024-08-21 06:34:30 +00:00
const kind = node.type === "MethodDefinition" ? node.kind : "field";
2024-08-09 12:04:48 +00:00
2024-08-21 06:34:30 +00:00
if (name === null || kind === "constructor") {
2024-08-09 12:04:48 +00:00
return;
}
const state = getState(name, node.static);
let isDuplicate = false;
2024-08-21 06:34:30 +00:00
if (kind === "get") {
2024-08-09 12:04:48 +00:00
isDuplicate = (state.init || state.get);
state.get = true;
2024-08-21 06:34:30 +00:00
} else if (kind === "set") {
2024-08-09 12:04:48 +00:00
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 } });
}
}
};
}
};