]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-eval.js
79ba4a1eb11bfb26c8c53d54a11f04847919220e
[pve-eslint.git] / eslint / tests / lib / rules / no-eval.js
1 /**
2 * @fileoverview Tests for no-eval rule.
3 * @author Nicholas C. Zakas
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-eval"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("no-eval", rule, {
22 valid: [
23 "Eval(foo)",
24 "setTimeout('foo')",
25 "setInterval('foo')",
26 "window.setTimeout('foo')",
27 "window.setInterval('foo')",
28
29 // User-defined eval methods.
30 "window.eval('foo')",
31 { code: "window.eval('foo')", env: { node: true } },
32 { code: "window.noeval('foo')", env: { browser: true } },
33 { code: "function foo() { var eval = 'foo'; window[eval]('foo') }", env: { browser: true } },
34 "global.eval('foo')",
35 { code: "global.eval('foo')", env: { browser: true } },
36 { code: "global.noeval('foo')", env: { node: true } },
37 { code: "function foo() { var eval = 'foo'; global[eval]('foo') }", env: { node: true } },
38 "globalThis.eval('foo')",
39 { code: "globalThis.eval('foo')", env: { es2017: true } },
40 { code: "globalThis.eval('foo')", env: { browser: true } },
41 { code: "globalThis.noneval('foo')", env: { es2020: true } },
42 { code: "function foo() { var eval = 'foo'; globalThis[eval]('foo') }", env: { es2020: true } },
43 "this.noeval('foo');",
44 "function foo() { 'use strict'; this.eval('foo'); }",
45 { code: "function foo() { this.eval('foo'); }", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
46 { code: "function foo() { this.eval('foo'); }", parserOptions: { ecmaFeatures: { impliedStrict: true } } },
47 "var obj = {foo: function() { this.eval('foo'); }}",
48 "var obj = {}; obj.foo = function() { this.eval('foo'); }",
49 { code: "class A { foo() { this.eval(); } }", parserOptions: { ecmaVersion: 6 } },
50 { code: "class A { static foo() { this.eval(); } }", parserOptions: { ecmaVersion: 6 } },
51 { code: "class A { field = this.eval(); }", parserOptions: { ecmaVersion: 2022 } },
52 { code: "class A { field = () => this.eval(); }", parserOptions: { ecmaVersion: 2022 } },
53 { code: "class A { static { this.eval(); } }", parserOptions: { ecmaVersion: 2022 } },
54
55 // Allows indirect eval
56 { code: "(0, eval)('foo')", options: [{ allowIndirect: true }] },
57 { code: "(0, window.eval)('foo')", options: [{ allowIndirect: true }], env: { browser: true } },
58 { code: "(0, window['eval'])('foo')", options: [{ allowIndirect: true }], env: { browser: true } },
59 { code: "var EVAL = eval; EVAL('foo')", options: [{ allowIndirect: true }] },
60 { code: "var EVAL = this.eval; EVAL('foo')", options: [{ allowIndirect: true }] },
61 { code: "(function(exe){ exe('foo') })(eval);", options: [{ allowIndirect: true }] },
62 { code: "window.eval('foo')", options: [{ allowIndirect: true }], env: { browser: true } },
63 { code: "window.window.eval('foo')", options: [{ allowIndirect: true }], env: { browser: true } },
64 { code: "window.window['eval']('foo')", options: [{ allowIndirect: true }], env: { browser: true } },
65 { code: "global.eval('foo')", options: [{ allowIndirect: true }], env: { node: true } },
66 { code: "global.global.eval('foo')", options: [{ allowIndirect: true }], env: { node: true } },
67 { code: "this.eval('foo')", options: [{ allowIndirect: true }] },
68 { code: "function foo() { this.eval('foo') }", options: [{ allowIndirect: true }] },
69 { code: "(0, globalThis.eval)('foo')", options: [{ allowIndirect: true }], env: { es2020: true } },
70 { code: "(0, globalThis['eval'])('foo')", options: [{ allowIndirect: true }], env: { es2020: true } },
71 { code: "var EVAL = globalThis.eval; EVAL('foo')", options: [{ allowIndirect: true }] },
72 { code: "function foo() { globalThis.eval('foo') }", options: [{ allowIndirect: true }], env: { es2020: true } },
73 { code: "globalThis.globalThis.eval('foo');", options: [{ allowIndirect: true }], env: { es2020: true } },
74 { code: "eval?.('foo')", options: [{ allowIndirect: true }], parserOptions: { ecmaVersion: 2020 } },
75 { code: "window?.eval('foo')", options: [{ allowIndirect: true }], parserOptions: { ecmaVersion: 2020 }, env: { browser: true } },
76 { code: "(window?.eval)('foo')", options: [{ allowIndirect: true }], parserOptions: { ecmaVersion: 2020 }, env: { browser: true } }
77 ],
78
79 invalid: [
80
81 // Direct eval
82 { code: "eval(foo)", errors: [{ messageId: "unexpected", type: "CallExpression", column: 1, endColumn: 5 }] },
83 { code: "eval('foo')", errors: [{ messageId: "unexpected", type: "CallExpression", column: 1, endColumn: 5 }] },
84 { code: "function foo(eval) { eval('foo') }", errors: [{ messageId: "unexpected", type: "CallExpression", column: 22, endColumn: 26 }] },
85 { code: "eval(foo)", options: [{ allowIndirect: true }], errors: [{ messageId: "unexpected", type: "CallExpression", column: 1, endColumn: 5 }] },
86 { code: "eval('foo')", options: [{ allowIndirect: true }], errors: [{ messageId: "unexpected", type: "CallExpression", column: 1, endColumn: 5 }] },
87 { code: "function foo(eval) { eval('foo') }", options: [{ allowIndirect: true }], errors: [{ messageId: "unexpected", type: "CallExpression", column: 22, endColumn: 26 }] },
88
89 // Indirect eval
90 { code: "(0, eval)('foo')", errors: [{ messageId: "unexpected", type: "Identifier", column: 5, endColumn: 9 }] },
91 { code: "(0, window.eval)('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 12, endColumn: 16 }] },
92 { code: "(0, window['eval'])('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 12, endColumn: 18 }] },
93 { code: "var EVAL = eval; EVAL('foo')", errors: [{ messageId: "unexpected", type: "Identifier", column: 12, endColumn: 16 }] },
94 { code: "var EVAL = this.eval; EVAL('foo')", errors: [{ messageId: "unexpected", type: "MemberExpression", column: 17, endColumn: 21 }] },
95 { code: "(function(exe){ exe('foo') })(eval);", errors: [{ messageId: "unexpected", type: "Identifier", column: 31, endColumn: 35 }] },
96 { code: "window.eval('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 8, endColumn: 12 }] },
97 { code: "window.window.eval('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 15, endColumn: 19 }] },
98 { code: "window.window['eval']('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 15, endColumn: 21 }] },
99 { code: "global.eval('foo')", env: { node: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 8, endColumn: 12 }] },
100 { code: "global.global.eval('foo')", env: { node: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 15, endColumn: 19 }] },
101 { code: "global.global[`eval`]('foo')", parserOptions: { ecmaVersion: 6 }, env: { node: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 15, endColumn: 21 }] },
102 { code: "this.eval('foo')", errors: [{ messageId: "unexpected", type: "CallExpression", column: 6, endColumn: 10 }] },
103 { code: "function foo() { this.eval('foo') }", errors: [{ messageId: "unexpected", type: "CallExpression", column: 23, endColumn: 27 }] },
104 { code: "var EVAL = globalThis.eval; EVAL('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 23, endColumn: 27 }] },
105 { code: "globalThis.eval('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 12, endColumn: 16 }] },
106 { code: "globalThis.globalThis.eval('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 23, endColumn: 27 }] },
107 { code: "globalThis.globalThis['eval']('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 23, endColumn: 29 }] },
108 { code: "(0, globalThis.eval)('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 16, endColumn: 20 }] },
109 { code: "(0, globalThis['eval'])('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 16, endColumn: 22 }] },
110
111 // Optional chaining
112 {
113 code: "window?.eval('foo')",
114 parserOptions: { ecmaVersion: 2020 },
115 globals: { window: "readonly" },
116 errors: [{ messageId: "unexpected" }]
117 },
118 {
119 code: "(window?.eval)('foo')",
120 parserOptions: { ecmaVersion: 2020 },
121 globals: { window: "readonly" },
122 errors: [{ messageId: "unexpected" }]
123 },
124 {
125 code: "(window?.window).eval('foo')",
126 parserOptions: { ecmaVersion: 2020 },
127 globals: { window: "readonly" },
128 errors: [{ messageId: "unexpected" }]
129 },
130
131 // Class fields
132 {
133 code: "class C { [this.eval('foo')] }",
134 parserOptions: { ecmaVersion: 2022 },
135 errors: [{ messageId: "unexpected" }]
136 },
137
138 {
139 code: "class A { static {} [this.eval()]; }",
140 parserOptions: { ecmaVersion: 2022 },
141 errors: [{ messageId: "unexpected" }]
142 }
143 ]
144 });