]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-extend-native.js
7ab25ab4895476004c365a48468ea9328aea354a
[pve-eslint.git] / eslint / lib / rules / no-extend-native.js
1 /**
2 * @fileoverview Rule to flag adding properties to native object's prototypes.
3 * @author David Nelson
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const astUtils = require("./utils/ast-utils");
13 const globals = require("globals");
14
15 //------------------------------------------------------------------------------
16 // Helpers
17 //------------------------------------------------------------------------------
18
19 const propertyDefinitionMethods = new Set(["defineProperty", "defineProperties"]);
20
21 //------------------------------------------------------------------------------
22 // Rule Definition
23 //------------------------------------------------------------------------------
24
25 module.exports = {
26 meta: {
27 type: "suggestion",
28
29 docs: {
30 description: "disallow extending native types",
31 category: "Best Practices",
32 recommended: false,
33 url: "https://eslint.org/docs/rules/no-extend-native"
34 },
35
36 schema: [
37 {
38 type: "object",
39 properties: {
40 exceptions: {
41 type: "array",
42 items: {
43 type: "string"
44 },
45 uniqueItems: true
46 }
47 },
48 additionalProperties: false
49 }
50 ],
51
52 messages: {
53 unexpected: "{{builtin}} prototype is read only, properties should not be added."
54 }
55 },
56
57 create(context) {
58
59 const config = context.options[0] || {};
60 const exceptions = new Set(config.exceptions || []);
61 const modifiedBuiltins = new Set(
62 Object.keys(globals.builtin)
63 .filter(builtin => builtin[0].toUpperCase() === builtin[0])
64 .filter(builtin => !exceptions.has(builtin))
65 );
66
67 /**
68 * Reports a lint error for the given node.
69 * @param {ASTNode} node The node to report.
70 * @param {string} builtin The name of the native builtin being extended.
71 * @returns {void}
72 */
73 function reportNode(node, builtin) {
74 context.report({
75 node,
76 messageId: "unexpected",
77 data: {
78 builtin
79 }
80 });
81 }
82
83 /**
84 * Check to see if the `prototype` property of the given object
85 * identifier node is being accessed.
86 * @param {ASTNode} identifierNode The Identifier representing the object
87 * to check.
88 * @returns {boolean} True if the identifier is the object of a
89 * MemberExpression and its `prototype` property is being accessed,
90 * false otherwise.
91 */
92 function isPrototypePropertyAccessed(identifierNode) {
93 return Boolean(
94 identifierNode &&
95 identifierNode.parent &&
96 identifierNode.parent.type === "MemberExpression" &&
97 identifierNode.parent.object === identifierNode &&
98 astUtils.getStaticPropertyName(identifierNode.parent) === "prototype"
99 );
100 }
101
102 /**
103 * Checks that an identifier is an object of a prototype whose member
104 * is being assigned in an AssignmentExpression.
105 * Example: Object.prototype.foo = "bar"
106 * @param {ASTNode} identifierNode The identifier to check.
107 * @returns {boolean} True if the identifier's prototype is modified.
108 */
109 function isInPrototypePropertyAssignment(identifierNode) {
110 return Boolean(
111 isPrototypePropertyAccessed(identifierNode) &&
112 identifierNode.parent.parent.type === "MemberExpression" &&
113 identifierNode.parent.parent.parent.type === "AssignmentExpression" &&
114 identifierNode.parent.parent.parent.left === identifierNode.parent.parent
115 );
116 }
117
118 /**
119 * Checks that an identifier is an object of a prototype whose member
120 * is being extended via the Object.defineProperty() or
121 * Object.defineProperties() methods.
122 * Example: Object.defineProperty(Array.prototype, "foo", ...)
123 * Example: Object.defineProperties(Array.prototype, ...)
124 * @param {ASTNode} identifierNode The identifier to check.
125 * @returns {boolean} True if the identifier's prototype is modified.
126 */
127 function isInDefinePropertyCall(identifierNode) {
128 return Boolean(
129 isPrototypePropertyAccessed(identifierNode) &&
130 identifierNode.parent.parent.type === "CallExpression" &&
131 identifierNode.parent.parent.arguments[0] === identifierNode.parent &&
132 identifierNode.parent.parent.callee.type === "MemberExpression" &&
133 identifierNode.parent.parent.callee.object.type === "Identifier" &&
134 identifierNode.parent.parent.callee.object.name === "Object" &&
135 identifierNode.parent.parent.callee.property.type === "Identifier" &&
136 propertyDefinitionMethods.has(identifierNode.parent.parent.callee.property.name)
137 );
138 }
139
140 /**
141 * Check to see if object prototype access is part of a prototype
142 * extension. There are three ways a prototype can be extended:
143 * 1. Assignment to prototype property (Object.prototype.foo = 1)
144 * 2. Object.defineProperty()/Object.defineProperties() on a prototype
145 * If prototype extension is detected, report the AssignmentExpression
146 * or CallExpression node.
147 * @param {ASTNode} identifierNode The Identifier representing the object
148 * which prototype is being accessed and possibly extended.
149 * @returns {void}
150 */
151 function checkAndReportPrototypeExtension(identifierNode) {
152 if (isInPrototypePropertyAssignment(identifierNode)) {
153
154 // Identifier --> MemberExpression --> MemberExpression --> AssignmentExpression
155 reportNode(identifierNode.parent.parent.parent, identifierNode.name);
156 } else if (isInDefinePropertyCall(identifierNode)) {
157
158 // Identifier --> MemberExpression --> CallExpression
159 reportNode(identifierNode.parent.parent, identifierNode.name);
160 }
161 }
162
163 return {
164
165 "Program:exit"() {
166 const globalScope = context.getScope();
167
168 modifiedBuiltins.forEach(builtin => {
169 const builtinVar = globalScope.set.get(builtin);
170
171 if (builtinVar && builtinVar.references) {
172 builtinVar.references
173 .map(ref => ref.identifier)
174 .forEach(checkAndReportPrototypeExtension);
175 }
176 });
177 }
178 };
179
180 }
181 };