/
no-new-wrappers.js
105 lines (99 loc) · 2.66 KB
/
no-new-wrappers.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
/**
* @fileoverview Tests for no-new-wrappers rule.
* @author Ilya Volodin
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-new-wrappers"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("no-new-wrappers", rule, {
valid: [
"var a = new Object();",
"var a = String('test'), b = String.fromCharCode(32);",
`
function test(Number) {
return new Number;
}
`,
{
code: `
import String from "./string";
const str = new String(42);
`,
parserOptions: { ecmaVersion: 6, sourceType: "module" }
},
`
if (foo) {
result = new Boolean(bar);
} else {
var Boolean = CustomBoolean;
}
`,
{
code: "new String()",
globals: {
String: "off"
}
},
`
/* global Boolean:off */
assert(new Boolean);
`
],
invalid: [
{
code: "var a = new String('hello');",
errors: [{
messageId: "noConstructor",
data: {
fn: "String"
},
type: "NewExpression"
}]
},
{
code: "var a = new Number(10);",
errors: [{
messageId: "noConstructor",
data: {
fn: "Number"
},
type: "NewExpression"
}]
},
{
code: "var a = new Boolean(false);",
errors: [{
messageId: "noConstructor",
data: {
fn: "Boolean"
},
type: "NewExpression"
}]
},
{
code: `
const a = new String('bar');
{
const String = CustomString;
const b = new String('foo');
}
`,
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "noConstructor",
data: {
fn: "String"
},
type: "NewExpression",
line: 2
}]
}
]
});