google_forms/node_modules/eslint/lib/rules/no-new-wrappers.js

61 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-08-09 12:04:48 +00:00
/**
* @fileoverview Rule to flag when using constructor for wrapper objects
* @author Ilya Volodin
*/
"use strict";
2024-08-21 06:34:30 +00:00
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const { getVariableByName } = require("./utils/ast-utils");
2024-08-09 12:04:48 +00:00
//------------------------------------------------------------------------------
// 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: "suggestion",
docs: {
2024-08-21 06:34:30 +00:00
description: "Disallow `new` operators with the `String`, `Number`, and `Boolean` objects",
2024-08-09 12:04:48 +00:00
recommended: false,
2024-08-21 06:34:30 +00:00
url: "https://eslint.org/docs/latest/rules/no-new-wrappers"
2024-08-09 12:04:48 +00:00
},
schema: [],
messages: {
noConstructor: "Do not use {{fn}} as a constructor."
}
},
create(context) {
2024-08-21 06:34:30 +00:00
const { sourceCode } = context;
2024-08-09 12:04:48 +00:00
return {
NewExpression(node) {
const wrapperObjects = ["String", "Number", "Boolean"];
2024-08-21 06:34:30 +00:00
const { name } = node.callee;
if (wrapperObjects.includes(name)) {
const variable = getVariableByName(sourceCode.getScope(node), name);
if (variable && variable.identifiers.length === 0) {
context.report({
node,
messageId: "noConstructor",
data: { fn: name }
});
}
2024-08-09 12:04:48 +00:00
}
}
};
}
};