]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-extend-native.js
import 7.12.1 upstream release
[pve-eslint.git] / eslint / tests / lib / rules / no-extend-native.js
1 /**
2 * @fileoverview Tests for no-extend-native rule.
3 * @author David Nelson
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-extend-native"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("no-extend-native", rule, {
22 valid: [
23 "x.prototype.p = 0",
24 "x.prototype['p'] = 0",
25 "Object.p = 0",
26 "Object.toString.bind = 0",
27 "Object['toString'].bind = 0",
28 "Object.defineProperty(x, 'p', {value: 0})",
29 "Object.defineProperties(x, {p: {value: 0}})",
30 "global.Object.prototype.toString = 0",
31 "this.Object.prototype.toString = 0",
32 "with(Object) { prototype.p = 0; }",
33 "o = Object; o.prototype.toString = 0",
34 "eval('Object.prototype.toString = 0')",
35 "parseFloat.prototype.x = 1",
36 {
37 code: "Object.prototype.g = 0",
38 options: [{ exceptions: ["Object"] }]
39 },
40 "obj[Object.prototype] = 0",
41
42 // https://github.com/eslint/eslint/issues/4438
43 "Object.defineProperty()",
44 "Object.defineProperties()",
45
46 // https://github.com/eslint/eslint/issues/8461
47 "function foo() { var Object = function() {}; Object.prototype.p = 0 }",
48 {
49 code: "{ let Object = function() {}; Object.prototype.p = 0 }",
50 parserOptions: { ecmaVersion: 6 }
51 },
52
53 // TODO(mdjermanovic): This test should become `invalid` in the next major version, when we upgrade the `globals` package.
54 {
55 code: "WeakRef.prototype.p = 0",
56 env: { es2021: true }
57 }
58 ],
59 invalid: [{
60 code: "Object.prototype.p = 0",
61 errors: [{
62 messageId: "unexpected",
63 data: { builtin: "Object" },
64 type: "AssignmentExpression"
65 }]
66 }, {
67 code: "BigInt.prototype.p = 0",
68 env: { es2020: true },
69 errors: [{
70 messageId: "unexpected",
71 data: { builtin: "BigInt" },
72 type: "AssignmentExpression"
73 }]
74 }, {
75 code: "Function.prototype['p'] = 0",
76 errors: [{
77 messageId: "unexpected",
78 data: { builtin: "Function" },
79 type: "AssignmentExpression"
80 }]
81 }, {
82 code: "String['prototype'].p = 0",
83 errors: [{
84 messageId: "unexpected",
85 data: { builtin: "String" },
86 type: "AssignmentExpression"
87 }]
88 }, {
89 code: "Number['prototype']['p'] = 0",
90 errors: [{
91 messageId: "unexpected",
92 data: { builtin: "Number" },
93 type: "AssignmentExpression"
94 }]
95 }, {
96 code: "Object.defineProperty(Array.prototype, 'p', {value: 0})",
97 errors: [{
98 messageId: "unexpected",
99 data: { builtin: "Array" },
100 type: "CallExpression"
101 }]
102 }, {
103 code: "Object.defineProperties(Array.prototype, {p: {value: 0}})",
104 errors: [{
105 messageId: "unexpected",
106 data: { builtin: "Array" },
107 type: "CallExpression"
108 }]
109 }, {
110 code: "Object.defineProperties(Array.prototype, {p: {value: 0}, q: {value: 0}})",
111 errors: [{
112 messageId: "unexpected",
113 data: { builtin: "Array" },
114 type: "CallExpression"
115 }]
116 },
117 {
118 code: "Number['prototype']['p'] = 0",
119 options: [{ exceptions: ["Object"] }],
120 errors: [{
121 messageId: "unexpected",
122 data: { builtin: "Number" },
123 type: "AssignmentExpression"
124 }]
125 },
126 {
127 code: "Object.prototype.p = 0; Object.prototype.q = 0",
128 errors: [{
129 messageId: "unexpected",
130 data: { builtin: "Object" },
131 type: "AssignmentExpression",
132 column: 1
133 }, {
134 messageId: "unexpected",
135 data: { builtin: "Object" },
136 type: "AssignmentExpression",
137 column: 25
138 }]
139 },
140 {
141 code: "function foo() { Object.prototype.p = 0 }",
142 errors: [{
143 messageId: "unexpected",
144 data: { builtin: "Object" },
145 type: "AssignmentExpression"
146 }]
147 },
148
149 // Optional chaining
150 {
151 code: "(Object?.prototype).p = 0",
152 parserOptions: { ecmaVersion: 2020 },
153 errors: [{ messageId: "unexpected", data: { builtin: "Object" } }]
154 },
155 {
156 code: "Object.defineProperty(Object?.prototype, 'p', { value: 0 })",
157 parserOptions: { ecmaVersion: 2020 },
158 errors: [{ messageId: "unexpected", data: { builtin: "Object" } }]
159 },
160 {
161 code: "Object?.defineProperty(Object.prototype, 'p', { value: 0 })",
162 parserOptions: { ecmaVersion: 2020 },
163 errors: [{ messageId: "unexpected", data: { builtin: "Object" } }]
164 },
165 {
166 code: "(Object?.defineProperty)(Object.prototype, 'p', { value: 0 })",
167 parserOptions: { ecmaVersion: 2020 },
168 errors: [{ messageId: "unexpected", data: { builtin: "Object" } }]
169 },
170
171 // Logical assignments
172 {
173 code: "Array.prototype.p &&= 0",
174 parserOptions: { ecmaVersion: 2021 },
175 errors: [{ messageId: "unexpected", data: { builtin: "Array" } }]
176 },
177 {
178 code: "Array.prototype.p ||= 0",
179 parserOptions: { ecmaVersion: 2021 },
180 errors: [{ messageId: "unexpected", data: { builtin: "Array" } }]
181 },
182 {
183 code: "Array.prototype.p ??= 0",
184 parserOptions: { ecmaVersion: 2021 },
185 errors: [{ messageId: "unexpected", data: { builtin: "Array" } }]
186 }
187
188 ]
189 });