]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/no-eval.js
import 7.12.1 upstream release
[pve-eslint.git] / eslint / tests / lib / rules / no-eval.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Tests for no-eval rule.
3 * @author Nicholas C. Zakas
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/no-eval"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
19const ruleTester = new RuleTester();
20
21ruleTester.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 } },
6f036462
TL
70 { code: "globalThis.globalThis.eval('foo');", options: [{ allowIndirect: true }], env: { es2020: true } },
71 { code: "eval?.('foo')", options: [{ allowIndirect: true }], parserOptions: { ecmaVersion: 2020 } },
72 { code: "window?.eval('foo')", options: [{ allowIndirect: true }], parserOptions: { ecmaVersion: 2020 }, env: { browser: true } },
73 { code: "(window?.eval)('foo')", options: [{ allowIndirect: true }], parserOptions: { ecmaVersion: 2020 }, env: { browser: true } }
eb39fafa
DC
74 ],
75
76 invalid: [
77
78 // Direct eval
79 { code: "eval(foo)", errors: [{ messageId: "unexpected", type: "CallExpression", column: 1, endColumn: 5 }] },
80 { code: "eval('foo')", errors: [{ messageId: "unexpected", type: "CallExpression", column: 1, endColumn: 5 }] },
81 { code: "function foo(eval) { eval('foo') }", errors: [{ messageId: "unexpected", type: "CallExpression", column: 22, endColumn: 26 }] },
82 { code: "eval(foo)", options: [{ allowIndirect: true }], errors: [{ messageId: "unexpected", type: "CallExpression", column: 1, endColumn: 5 }] },
83 { code: "eval('foo')", options: [{ allowIndirect: true }], errors: [{ messageId: "unexpected", type: "CallExpression", column: 1, endColumn: 5 }] },
84 { code: "function foo(eval) { eval('foo') }", options: [{ allowIndirect: true }], errors: [{ messageId: "unexpected", type: "CallExpression", column: 22, endColumn: 26 }] },
85
86 // Indirect eval
87 { code: "(0, eval)('foo')", errors: [{ messageId: "unexpected", type: "Identifier", column: 5, endColumn: 9 }] },
88 { code: "(0, window.eval)('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 12, endColumn: 16 }] },
89 { code: "(0, window['eval'])('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 12, endColumn: 18 }] },
90 { code: "var EVAL = eval; EVAL('foo')", errors: [{ messageId: "unexpected", type: "Identifier", column: 12, endColumn: 16 }] },
91 { code: "var EVAL = this.eval; EVAL('foo')", errors: [{ messageId: "unexpected", type: "MemberExpression", column: 17, endColumn: 21 }] },
92 { code: "(function(exe){ exe('foo') })(eval);", errors: [{ messageId: "unexpected", type: "Identifier", column: 31, endColumn: 35 }] },
93 { code: "window.eval('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 8, endColumn: 12 }] },
94 { code: "window.window.eval('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 15, endColumn: 19 }] },
95 { code: "window.window['eval']('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 15, endColumn: 21 }] },
96 { code: "global.eval('foo')", env: { node: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 8, endColumn: 12 }] },
97 { code: "global.global.eval('foo')", env: { node: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 15, endColumn: 19 }] },
98 { code: "global.global[`eval`]('foo')", parserOptions: { ecmaVersion: 6 }, env: { node: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 15, endColumn: 21 }] },
99 { code: "this.eval('foo')", errors: [{ messageId: "unexpected", type: "CallExpression", column: 6, endColumn: 10 }] },
100 { code: "function foo() { this.eval('foo') }", errors: [{ messageId: "unexpected", type: "CallExpression", column: 23, endColumn: 27 }] },
101 { code: "var EVAL = globalThis.eval; EVAL('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 23, endColumn: 27 }] },
102 { code: "globalThis.eval('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 12, endColumn: 16 }] },
103 { code: "globalThis.globalThis.eval('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 23, endColumn: 27 }] },
104 { code: "globalThis.globalThis['eval']('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 23, endColumn: 29 }] },
105 { code: "(0, globalThis.eval)('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 16, endColumn: 20 }] },
6f036462
TL
106 { code: "(0, globalThis['eval'])('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "MemberExpression", column: 16, endColumn: 22 }] },
107
108 // Optional chaining
109 {
110 code: "window?.eval('foo')",
111 parserOptions: { ecmaVersion: 2020 },
112 globals: { window: "readonly" },
113 errors: [{ messageId: "unexpected" }]
114 },
115 {
116 code: "(window?.eval)('foo')",
117 parserOptions: { ecmaVersion: 2020 },
118 globals: { window: "readonly" },
119 errors: [{ messageId: "unexpected" }]
120 },
121 {
122 code: "(window?.window).eval('foo')",
123 parserOptions: { ecmaVersion: 2020 },
124 globals: { window: "readonly" },
125 errors: [{ messageId: "unexpected" }]
126 }
eb39fafa
DC
127 ]
128});