forked from eslint/eslint
/
no-extend-native.js
141 lines (133 loc) · 4.26 KB
/
no-extend-native.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* @fileoverview Tests for no-extend-native rule.
* @author David Nelson
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-extend-native"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("no-extend-native", rule, {
valid: [
"x.prototype.p = 0",
"x.prototype['p'] = 0",
"Object.p = 0",
"Object.toString.bind = 0",
"Object['toString'].bind = 0",
"Object.defineProperty(x, 'p', {value: 0})",
"Object.defineProperties(x, {p: {value: 0}})",
"global.Object.prototype.toString = 0",
"this.Object.prototype.toString = 0",
"with(Object) { prototype.p = 0; }",
"o = Object; o.prototype.toString = 0",
"eval('Object.prototype.toString = 0')",
"parseFloat.prototype.x = 1",
{
code: "Object.prototype.g = 0",
options: [{ exceptions: ["Object"] }]
},
// https://github.com/eslint/eslint/issues/4438
"Object.defineProperty()",
"Object.defineProperties()",
// https://github.com/eslint/eslint/issues/8461
"function foo() { var Object = function() {}; Object.prototype.p = 0 }",
{
code: "{ let Object = function() {}; Object.prototype.p = 0 }",
parserOptions: { ecmaVersion: 6 }
}
],
invalid: [{
code: "Object.prototype.p = 0",
errors: [{
messageId: "unexpected",
data: { builtin: "Object" },
type: "AssignmentExpression"
}]
}, {
code: "BigInt.prototype.p = 0",
env: { es2020: true },
errors: [{
messageId: "unexpected",
data: { builtin: "BigInt" },
type: "AssignmentExpression"
}]
}, {
code: "Function.prototype['p'] = 0",
errors: [{
messageId: "unexpected",
data: { builtin: "Function" },
type: "AssignmentExpression"
}]
}, {
code: "String['prototype'].p = 0",
errors: [{
messageId: "unexpected",
data: { builtin: "String" },
type: "AssignmentExpression"
}]
}, {
code: "Number['prototype']['p'] = 0",
errors: [{
messageId: "unexpected",
data: { builtin: "Number" },
type: "AssignmentExpression"
}]
}, {
code: "Object.defineProperty(Array.prototype, 'p', {value: 0})",
errors: [{
messageId: "unexpected",
data: { builtin: "Array" },
type: "CallExpression"
}]
}, {
code: "Object.defineProperties(Array.prototype, {p: {value: 0}})",
errors: [{
messageId: "unexpected",
data: { builtin: "Array" },
type: "CallExpression"
}]
}, {
code: "Object.defineProperties(Array.prototype, {p: {value: 0}, q: {value: 0}})",
errors: [{
messageId: "unexpected",
data: { builtin: "Array" },
type: "CallExpression"
}]
},
{
code: "Number['prototype']['p'] = 0",
options: [{ exceptions: ["Object"] }],
errors: [{
messageId: "unexpected",
data: { builtin: "Number" },
type: "AssignmentExpression"
}]
},
{
code: "Object.prototype.p = 0; Object.prototype.q = 0",
errors: [{
messageId: "unexpected",
data: { builtin: "Object" },
type: "AssignmentExpression",
column: 1
}, {
messageId: "unexpected",
data: { builtin: "Object" },
type: "AssignmentExpression",
column: 25
}]
},
{
code: "function foo() { Object.prototype.p = 0 }",
errors: [{
messageId: "unexpected",
data: { builtin: "Object" },
type: "AssignmentExpression"
}]
}]
});