2024-08-09 12:04:48 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Rule to disallow use of the new operator with the `Symbol` object
|
|
|
|
* @author Alberto Rodríguez
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// 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 `new` operators with the `Symbol` object",
|
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-new-symbol"
|
2024-08-09 12:04:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
schema: [],
|
|
|
|
|
|
|
|
messages: {
|
|
|
|
noNewSymbol: "`Symbol` cannot be called as a constructor."
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
create(context) {
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
const sourceCode = context.sourceCode;
|
|
|
|
|
2024-08-09 12:04:48 +00:00
|
|
|
return {
|
2024-08-21 06:34:30 +00:00
|
|
|
"Program:exit"(node) {
|
|
|
|
const globalScope = sourceCode.getScope(node);
|
2024-08-09 12:04:48 +00:00
|
|
|
const variable = globalScope.set.get("Symbol");
|
|
|
|
|
|
|
|
if (variable && variable.defs.length === 0) {
|
|
|
|
variable.references.forEach(ref => {
|
2024-08-21 06:34:30 +00:00
|
|
|
const idNode = ref.identifier;
|
|
|
|
const parent = idNode.parent;
|
2024-08-09 12:04:48 +00:00
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
if (parent && parent.type === "NewExpression" && parent.callee === idNode) {
|
2024-08-09 12:04:48 +00:00
|
|
|
context.report({
|
2024-08-21 06:34:30 +00:00
|
|
|
node: idNode,
|
2024-08-09 12:04:48 +00:00
|
|
|
messageId: "noNewSymbol"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|