]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/no-extend-native.js
import eslint 7.28.0
[pve-eslint.git] / eslint / tests / lib / rules / no-extend-native.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Tests for no-extend-native rule.
3 * @author David Nelson
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/no-extend-native"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
19const ruleTester = new RuleTester();
20
21ruleTester.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 },
6f036462 40 "obj[Object.prototype] = 0",
eb39fafa
DC
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 invalid: [{
54 code: "Object.prototype.p = 0",
55 errors: [{
56 messageId: "unexpected",
57 data: { builtin: "Object" },
58 type: "AssignmentExpression"
59 }]
60 }, {
61 code: "BigInt.prototype.p = 0",
62 env: { es2020: true },
63 errors: [{
64 messageId: "unexpected",
65 data: { builtin: "BigInt" },
66 type: "AssignmentExpression"
67 }]
5422a9cc
TL
68 }, {
69 code: "WeakRef.prototype.p = 0",
70 env: { es2021: true },
71 errors: [{
72 messageId: "unexpected",
73 data: { builtin: "WeakRef" },
74 type: "AssignmentExpression"
75 }]
76 }, {
77 code: "FinalizationRegistry.prototype.p = 0",
78 env: { es2021: true },
79 errors: [{
80 messageId: "unexpected",
81 data: { builtin: "FinalizationRegistry" },
82 type: "AssignmentExpression"
83 }]
84 }, {
85 code: "AggregateError.prototype.p = 0",
86 env: { es2021: true },
87 errors: [{
88 messageId: "unexpected",
89 data: { builtin: "AggregateError" },
90 type: "AssignmentExpression"
91 }]
eb39fafa
DC
92 }, {
93 code: "Function.prototype['p'] = 0",
94 errors: [{
95 messageId: "unexpected",
96 data: { builtin: "Function" },
97 type: "AssignmentExpression"
98 }]
99 }, {
100 code: "String['prototype'].p = 0",
101 errors: [{
102 messageId: "unexpected",
103 data: { builtin: "String" },
104 type: "AssignmentExpression"
105 }]
106 }, {
107 code: "Number['prototype']['p'] = 0",
108 errors: [{
109 messageId: "unexpected",
110 data: { builtin: "Number" },
111 type: "AssignmentExpression"
112 }]
113 }, {
114 code: "Object.defineProperty(Array.prototype, 'p', {value: 0})",
115 errors: [{
116 messageId: "unexpected",
117 data: { builtin: "Array" },
118 type: "CallExpression"
119 }]
120 }, {
121 code: "Object.defineProperties(Array.prototype, {p: {value: 0}})",
122 errors: [{
123 messageId: "unexpected",
124 data: { builtin: "Array" },
125 type: "CallExpression"
126 }]
127 }, {
128 code: "Object.defineProperties(Array.prototype, {p: {value: 0}, q: {value: 0}})",
129 errors: [{
130 messageId: "unexpected",
131 data: { builtin: "Array" },
132 type: "CallExpression"
133 }]
134 },
135 {
136 code: "Number['prototype']['p'] = 0",
137 options: [{ exceptions: ["Object"] }],
138 errors: [{
139 messageId: "unexpected",
140 data: { builtin: "Number" },
141 type: "AssignmentExpression"
142 }]
143 },
144 {
145 code: "Object.prototype.p = 0; Object.prototype.q = 0",
146 errors: [{
147 messageId: "unexpected",
148 data: { builtin: "Object" },
149 type: "AssignmentExpression",
150 column: 1
151 }, {
152 messageId: "unexpected",
153 data: { builtin: "Object" },
154 type: "AssignmentExpression",
155 column: 25
156 }]
157 },
158 {
159 code: "function foo() { Object.prototype.p = 0 }",
160 errors: [{
161 messageId: "unexpected",
162 data: { builtin: "Object" },
163 type: "AssignmentExpression"
164 }]
6f036462
TL
165 },
166
167 // Optional chaining
168 {
169 code: "(Object?.prototype).p = 0",
170 parserOptions: { ecmaVersion: 2020 },
171 errors: [{ messageId: "unexpected", data: { builtin: "Object" } }]
172 },
173 {
174 code: "Object.defineProperty(Object?.prototype, 'p', { value: 0 })",
175 parserOptions: { ecmaVersion: 2020 },
176 errors: [{ messageId: "unexpected", data: { builtin: "Object" } }]
177 },
178 {
179 code: "Object?.defineProperty(Object.prototype, 'p', { value: 0 })",
180 parserOptions: { ecmaVersion: 2020 },
181 errors: [{ messageId: "unexpected", data: { builtin: "Object" } }]
182 },
183 {
184 code: "(Object?.defineProperty)(Object.prototype, 'p', { value: 0 })",
185 parserOptions: { ecmaVersion: 2020 },
186 errors: [{ messageId: "unexpected", data: { builtin: "Object" } }]
187 },
188
189 // Logical assignments
190 {
191 code: "Array.prototype.p &&= 0",
192 parserOptions: { ecmaVersion: 2021 },
193 errors: [{ messageId: "unexpected", data: { builtin: "Array" } }]
194 },
195 {
196 code: "Array.prototype.p ||= 0",
197 parserOptions: { ecmaVersion: 2021 },
198 errors: [{ messageId: "unexpected", data: { builtin: "Array" } }]
199 },
200 {
201 code: "Array.prototype.p ??= 0",
202 parserOptions: { ecmaVersion: 2021 },
203 errors: [{ messageId: "unexpected", data: { builtin: "Array" } }]
204 }
205
206 ]
eb39fafa 207});