2 * @fileoverview Rule to flag adding properties to native object's prototypes.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
13 const globals = require("globals");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
24 description: "disallow extending native types",
25 category: "Best Practices",
27 url: "https://eslint.org/docs/rules/no-extend-native"
42 additionalProperties: false
47 unexpected: "{{builtin}} prototype is read only, properties should not be added."
53 const config = context.options[0] || {};
54 const exceptions = new Set(config.exceptions || []);
55 const modifiedBuiltins = new Set(
56 Object.keys(globals.builtin)
57 .filter(builtin => builtin[0].toUpperCase() === builtin[0])
58 .filter(builtin => !exceptions.has(builtin))
62 * Reports a lint error for the given node.
63 * @param {ASTNode} node The node to report.
64 * @param {string} builtin The name of the native builtin being extended.
67 function reportNode(node, builtin) {
70 messageId: "unexpected",
78 * Check to see if the `prototype` property of the given object
79 * identifier node is being accessed.
80 * @param {ASTNode} identifierNode The Identifier representing the object
82 * @returns {boolean} True if the identifier is the object of a
83 * MemberExpression and its `prototype` property is being accessed,
86 function isPrototypePropertyAccessed(identifierNode) {
89 identifierNode.parent &&
90 identifierNode.parent.type === "MemberExpression" &&
91 identifierNode.parent.object === identifierNode &&
92 astUtils.getStaticPropertyName(identifierNode.parent) === "prototype"
97 * Check if it's an assignment to the property of the given node.
98 * Example: `*.prop = 0` // the `*` is the given node.
99 * @param {ASTNode} node The node to check.
100 * @returns {boolean} True if an assignment to the property of the node.
102 function isAssigningToPropertyOf(node) {
104 node.parent.type === "MemberExpression" &&
105 node.parent.object === node &&
106 node.parent.parent.type === "AssignmentExpression" &&
107 node.parent.parent.left === node.parent
112 * Checks if the given node is at the first argument of the method call of `Object.defineProperty()` or `Object.defineProperties()`.
113 * @param {ASTNode} node The node to check.
114 * @returns {boolean} True if the node is at the first argument of the method call of `Object.defineProperty()` or `Object.defineProperties()`.
116 function isInDefinePropertyCall(node) {
118 node.parent.type === "CallExpression" &&
119 node.parent.arguments[0] === node &&
120 astUtils.isSpecificMemberAccess(node.parent.callee, "Object", /^definePropert(?:y|ies)$/u)
125 * Check to see if object prototype access is part of a prototype
126 * extension. There are three ways a prototype can be extended:
127 * 1. Assignment to prototype property (Object.prototype.foo = 1)
128 * 2. Object.defineProperty()/Object.defineProperties() on a prototype
129 * If prototype extension is detected, report the AssignmentExpression
130 * or CallExpression node.
131 * @param {ASTNode} identifierNode The Identifier representing the object
132 * which prototype is being accessed and possibly extended.
135 function checkAndReportPrototypeExtension(identifierNode) {
136 if (!isPrototypePropertyAccessed(identifierNode)) {
137 return; // This is not `*.prototype` access.
141 * `identifierNode.parent` is a MemberExpression `*.prototype`.
142 * If it's an optional member access, it may be wrapped by a `ChainExpression` node.
144 const prototypeNode =
145 identifierNode.parent.parent.type === "ChainExpression"
146 ? identifierNode.parent.parent
147 : identifierNode.parent;
149 if (isAssigningToPropertyOf(prototypeNode)) {
151 // `*.prototype` -> MemberExpression -> AssignmentExpression
152 reportNode(prototypeNode.parent.parent, identifierNode.name);
153 } else if (isInDefinePropertyCall(prototypeNode)) {
155 // `*.prototype` -> CallExpression
156 reportNode(prototypeNode.parent, identifierNode.name);
163 const globalScope = context.getScope();
165 modifiedBuiltins.forEach(builtin => {
166 const builtinVar = globalScope.set.get(builtin);
168 if (builtinVar && builtinVar.references) {
169 builtinVar.references
170 .map(ref => ref.identifier)
171 .forEach(checkAndReportPrototypeExtension);