]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-eval.js
first commit
[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
52 // Allows indirect eval
53 { code: "(0, eval)('foo')", options: [{ allowIndirect: true }] },
54 { code: "(0, window.eval)('foo')", options: [{ allowIndirect: true }], env: { browser: true } },
55 { code: "(0, window['eval'])('foo')", options: [{ allowIndirect: true }], env: { browser: true } },
56 { code: "var EVAL = eval; EVAL('foo')", options: [{ allowIndirect: true }] },
57 { code: "var EVAL = this.eval; EVAL('foo')", options: [{ allowIndirect: true }] },
58 { code: "(function(exe){ exe('foo') })(eval);", options: [{ allowIndirect: true }] },
59 { code: "window.eval('foo')", options: [{ allowIndirect: true }], env: { browser: true } },
60 { code: "window.window.eval('foo')", options: [{ allowIndirect: true }], env: { browser: true } },
61 { code: "window.window['eval']('foo')", options: [{ allowIndirect: true }], env: { browser: true } },
62 { code: "global.eval('foo')", options: [{ allowIndirect: true }], env: { node: true } },
63 { code: "global.global.eval('foo')", options: [{ allowIndirect: true }], env: { node: true } },
64 { code: "this.eval('foo')", options: [{ allowIndirect: true }] },
65 { code: "function foo() { this.eval('foo') }", options: [{ allowIndirect: true }] },
66 { code: "(0, globalThis.eval)('foo')", options: [{ allowIndirect: true }], env: { es2020: true } },
67 { code: "(0, globalThis['eval'])('foo')", options: [{ allowIndirect: true }], env: { es2020: true } },
68 { code: "var EVAL = globalThis.eval; EVAL('foo')", options: [{ allowIndirect: true }] },
69 { code: "function foo() { globalThis.eval('foo') }", options: [{ allowIndirect: true }], env: { es2020: true } },
70 { code: "globalThis.globalThis.eval('foo');", options: [{ allowIndirect: true }], env: { es2020: true } }
71 ],
72
73 invalid: [
74
75 // Direct eval
76 { code: "eval(foo)", errors: [{ messageId: "unexpected", type: "CallExpression", column: 1, endColumn: 5 }] },
77 { code: "eval('foo')", errors: [{ messageId: "unexpected", type: "CallExpression", column: 1, endColumn: 5 }] },
78 { code: "function foo(eval) { eval('foo') }", errors: [{ messageId: "unexpected", type: "CallExpression", column: 22, endColumn: 26 }] },
79 { code: "eval(foo)", options: [{ allowIndirect: true }], errors: [{ messageId: "unexpected", type: "CallExpression", column: 1, endColumn: 5 }] },
80 { code: "eval('foo')", options: [{ allowIndirect: true }], errors: [{ messageId: "unexpected", type: "CallExpression", column: 1, endColumn: 5 }] },
81 { code: "function foo(eval) { eval('foo') }", options: [{ allowIndirect: true }], errors: [{ messageId: "unexpected", type: "CallExpression", column: 22, endColumn: 26 }] },
82
83 // Indirect eval
84 { code: "(0, eval)('foo')", errors: [{ messageId: "unexpected", type: "Identifier", column: 5, endColumn: 9 }] },
85 { code: "(0, window.eval)('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 12, endColumn: 16 }] },
86 { code: "(0, window['eval'])('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 12, endColumn: 18 }] },
87 { code: "var EVAL = eval; EVAL('foo')", errors: [{ messageId: "unexpected", type: "Identifier", column: 12, endColumn: 16 }] },
88 { code: "var EVAL = this.eval; EVAL('foo')", errors: [{ messageId: "unexpected", type: "MemberExpression", column: 17, endColumn: 21 }] },
89 { code: "(function(exe){ exe('foo') })(eval);", errors: [{ messageId: "unexpected", type: "Identifier", column: 31, endColumn: 35 }] },
90 { code: "window.eval('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 8, endColumn: 12 }] },
91 { code: "window.window.eval('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 15, endColumn: 19 }] },
92 { code: "window.window['eval']('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 15, endColumn: 21 }] },
93 { code: "global.eval('foo')", env: { node: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 8, endColumn: 12 }] },
94 { code: "global.global.eval('foo')", env: { node: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 15, endColumn: 19 }] },
95 { code: "global.global[`eval`]('foo')", parserOptions: { ecmaVersion: 6 }, env: { node: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 15, endColumn: 21 }] },
96 { code: "this.eval('foo')", errors: [{ messageId: "unexpected", type: "CallExpression", column: 6, endColumn: 10 }] },
97 { code: "function foo() { this.eval('foo') }", errors: [{ messageId: "unexpected", type: "CallExpression", column: 23, endColumn: 27 }] },
98 { code: "var EVAL = globalThis.eval; EVAL('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 23, endColumn: 27 }] },
99 { code: "globalThis.eval('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 12, endColumn: 16 }] },
100 { code: "globalThis.globalThis.eval('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 23, endColumn: 27 }] },
101 { code: "globalThis.globalThis['eval']('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 23, endColumn: 29 }] },
102 { code: "(0, globalThis.eval)('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 16, endColumn: 20 }] },
103 { code: "(0, globalThis['eval'])('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 16, endColumn: 22 }] }
104 ]
105 });