]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for no-new-func rule. | |
3 | * @author Ilya Volodin | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/no-new-func"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | const ruleTester = new RuleTester(); | |
20 | ||
21 | ruleTester.run("no-new-func", rule, { | |
22 | valid: [ | |
23 | "var a = new _function(\"b\", \"c\", \"return b+c\");", | |
ebb53d86 TL |
24 | "var a = _function(\"b\", \"c\", \"return b+c\");", |
25 | { | |
26 | code: "class Function {}; new Function()", | |
27 | parserOptions: { | |
28 | ecmaVersion: 2015 | |
29 | } | |
30 | }, | |
31 | { | |
32 | code: "const fn = () => { class Function {}; new Function() }", | |
33 | parserOptions: { | |
34 | ecmaVersion: 2015 | |
35 | } | |
36 | }, | |
37 | "function Function() {}; Function()", | |
38 | "var fn = function () { function Function() {}; Function() }", | |
39 | "var x = function Function() { Function(); }", | |
40 | "call(Function)", | |
609c276f TL |
41 | "new Class(Function)", |
42 | "foo[Function]()", | |
43 | "foo(Function.bind)", | |
44 | "Function.toString()", | |
45 | "Function[call]()" | |
eb39fafa DC |
46 | ], |
47 | invalid: [ | |
48 | { | |
49 | code: "var a = new Function(\"b\", \"c\", \"return b+c\");", | |
50 | errors: [{ | |
51 | messageId: "noFunctionConstructor", | |
52 | type: "NewExpression" | |
53 | }] | |
54 | }, | |
55 | { | |
56 | code: "var a = Function(\"b\", \"c\", \"return b+c\");", | |
57 | errors: [{ | |
58 | messageId: "noFunctionConstructor", | |
59 | type: "CallExpression" | |
60 | }] | |
ebb53d86 | 61 | }, |
609c276f TL |
62 | { |
63 | code: "var a = Function.call(null, \"b\", \"c\", \"return b+c\");", | |
64 | errors: [{ | |
65 | messageId: "noFunctionConstructor", | |
66 | type: "CallExpression" | |
67 | }] | |
68 | }, | |
69 | { | |
70 | code: "var a = Function.apply(null, [\"b\", \"c\", \"return b+c\"]);", | |
71 | errors: [{ | |
72 | messageId: "noFunctionConstructor", | |
73 | type: "CallExpression" | |
74 | }] | |
75 | }, | |
76 | { | |
77 | code: "var a = Function.bind(null, \"b\", \"c\", \"return b+c\")();", | |
78 | errors: [{ | |
79 | messageId: "noFunctionConstructor", | |
80 | type: "CallExpression" | |
81 | }] | |
82 | }, | |
83 | { | |
84 | code: "var a = Function.bind(null, \"b\", \"c\", \"return b+c\");", | |
85 | errors: [{ | |
86 | messageId: "noFunctionConstructor", | |
87 | type: "CallExpression" | |
88 | }] | |
89 | }, | |
90 | { | |
91 | code: "var a = Function[\"call\"](null, \"b\", \"c\", \"return b+c\");", | |
92 | errors: [{ | |
93 | messageId: "noFunctionConstructor", | |
94 | type: "CallExpression" | |
95 | }] | |
96 | }, | |
97 | { | |
98 | code: "var a = (Function?.call)(null, \"b\", \"c\", \"return b+c\");", | |
99 | parserOptions: { ecmaVersion: 2021 }, | |
100 | errors: [{ | |
101 | messageId: "noFunctionConstructor", | |
102 | type: "CallExpression" | |
103 | }] | |
104 | }, | |
ebb53d86 TL |
105 | { |
106 | code: "const fn = () => { class Function {} }; new Function('', '')", | |
107 | parserOptions: { | |
108 | ecmaVersion: 2015 | |
109 | }, | |
110 | errors: [{ | |
111 | messageId: "noFunctionConstructor", | |
112 | type: "NewExpression" | |
113 | }] | |
114 | }, | |
115 | { | |
116 | code: "var fn = function () { function Function() {} }; Function('', '')", | |
117 | errors: [{ | |
118 | messageId: "noFunctionConstructor", | |
119 | type: "CallExpression" | |
120 | }] | |
eb39fafa DC |
121 | } |
122 | ] | |
123 | }); |