123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- "use strict";
- const astUtils = require("./utils/ast-utils");
- module.exports = {
- meta: {
- type: "suggestion",
- defaultOptions: [{ exceptions: [] }],
- docs: {
- description: "Disallow extending native types",
- recommended: false,
- url: "https://eslint.org/docs/latest/rules/no-extend-native"
- },
- schema: [
- {
- type: "object",
- properties: {
- exceptions: {
- type: "array",
- items: {
- type: "string"
- },
- uniqueItems: true
- }
- },
- additionalProperties: false
- }
- ],
- messages: {
- unexpected: "{{builtin}} prototype is read only, properties should not be added."
- }
- },
- create(context) {
- const sourceCode = context.sourceCode;
- const exceptions = new Set(context.options[0].exceptions);
- const modifiedBuiltins = new Set(
- Object.keys(astUtils.ECMASCRIPT_GLOBALS)
- .filter(builtin => builtin[0].toUpperCase() === builtin[0])
- .filter(builtin => !exceptions.has(builtin))
- );
-
- function reportNode(node, builtin) {
- context.report({
- node,
- messageId: "unexpected",
- data: {
- builtin
- }
- });
- }
-
- function isPrototypePropertyAccessed(identifierNode) {
- return Boolean(
- identifierNode &&
- identifierNode.parent &&
- identifierNode.parent.type === "MemberExpression" &&
- identifierNode.parent.object === identifierNode &&
- astUtils.getStaticPropertyName(identifierNode.parent) === "prototype"
- );
- }
-
- function isAssigningToPropertyOf(node) {
- return (
- node.parent.type === "MemberExpression" &&
- node.parent.object === node &&
- node.parent.parent.type === "AssignmentExpression" &&
- node.parent.parent.left === node.parent
- );
- }
-
- function isInDefinePropertyCall(node) {
- return (
- node.parent.type === "CallExpression" &&
- node.parent.arguments[0] === node &&
- astUtils.isSpecificMemberAccess(node.parent.callee, "Object", /^definePropert(?:y|ies)$/u)
- );
- }
-
- function checkAndReportPrototypeExtension(identifierNode) {
- if (!isPrototypePropertyAccessed(identifierNode)) {
- return;
- }
-
- const prototypeNode =
- identifierNode.parent.parent.type === "ChainExpression"
- ? identifierNode.parent.parent
- : identifierNode.parent;
- if (isAssigningToPropertyOf(prototypeNode)) {
-
- reportNode(prototypeNode.parent.parent, identifierNode.name);
- } else if (isInDefinePropertyCall(prototypeNode)) {
-
- reportNode(prototypeNode.parent, identifierNode.name);
- }
- }
- return {
- "Program:exit"(node) {
- const globalScope = sourceCode.getScope(node);
- modifiedBuiltins.forEach(builtin => {
- const builtinVar = globalScope.set.get(builtin);
- if (builtinVar && builtinVar.references) {
- builtinVar.references
- .map(ref => ref.identifier)
- .forEach(checkAndReportPrototypeExtension);
- }
- });
- }
- };
- }
- };
|