UNPKG

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