Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: support globalThis in no-implied-eval (fixes #12670) (#13105)
  • Loading branch information
yeonjuan committed Mar 27, 2020
1 parent 185982d commit 081e240
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rules/no-implied-eval.js
Expand Up @@ -36,7 +36,7 @@ module.exports = {

create(context) {
const EVAL_LIKE_FUNCS = Object.freeze(["setTimeout", "execScript", "setInterval"]);
const GLOBAL_CANDIDATES = Object.freeze(["global", "window"]);
const GLOBAL_CANDIDATES = Object.freeze(["global", "window", "globalThis"]);

/**
* Checks whether a node is evaluated as a string or not.
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/no-implied-eval.js
Expand Up @@ -33,6 +33,7 @@ ruleTester.run("no-implied-eval", rule, {
{ code: "global.setTimeout = foo;", env: { node: true } },
{ code: "global['setTimeout'];", env: { node: true } },
{ code: "global['setTimeout'] = foo;", env: { node: true } },
{ code: "globalThis['setTimeout'] = foo;", env: { es2020: true } },

"window.setTimeout('foo')",
"window.setInterval('foo')",
Expand All @@ -47,11 +48,14 @@ ruleTester.run("no-implied-eval", rule, {
{ code: "global.setInterval('foo')", env: { browser: true } },
{ code: "global['setTimeout']('foo')", env: { browser: true } },
{ code: "global['setInterval']('foo')", env: { browser: true } },
{ code: "globalThis.setTimeout('foo')", env: { es6: true } },
{ code: "globalThis['setInterval']('foo')", env: { es2017: true } },

{ code: "window[`SetTimeOut`]('foo', 100);", parserOptions: { ecmaVersion: 6 }, env: { browser: true } },
{ code: "global[`SetTimeOut`]('foo', 100);", parserOptions: { ecmaVersion: 6 }, env: { node: true } },
{ code: "global[`setTimeout${foo}`]('foo', 100);", parserOptions: { ecmaVersion: 6 }, env: { browser: true } },
{ code: "global[`setTimeout${foo}`]('foo', 100);", parserOptions: { ecmaVersion: 6 }, env: { node: true } },
{ code: "globalThis[`setTimeout${foo}`]('foo', 100);", parserOptions: { ecmaVersion: 6 }, env: { es2020: true } },

// normal usage
"setTimeout(function() { x = 1; }, 100);",
Expand All @@ -69,6 +73,7 @@ ruleTester.run("no-implied-eval", rule, {
{ code: "global.setTimeout(foo, 100);", env: { node: true } },
{ code: "global.setInterval(foo, 100);", env: { node: true } },
{ code: "global.execScript(foo, 100);", env: { node: true } },
{ code: "globalThis.setTimeout(foo, 100);", env: { es2020: true } },

// only checks on top-level statements or window.*
"foo.setTimeout('hi')",
Expand Down Expand Up @@ -136,6 +141,8 @@ ruleTester.run("no-implied-eval", rule, {
{ code: "global['setInterval']('foo')", env: { node: true }, errors: [expectedError] },
{ code: "global[`setInterval`]('foo')", parserOptions: { ecmaVersion: 6 }, env: { node: true }, errors: [expectedError] },
{ code: "global.global['setInterval']('foo')", env: { node: true }, errors: [expectedError] },
{ code: "globalThis.setTimeout('foo')", env: { es2020: true }, errors: [expectedError] },
{ code: "globalThis.setInterval('foo')", env: { es2020: true }, errors: [expectedError] },

// template literals
{ code: "setTimeout(`foo${bar}`)", parserOptions: { ecmaVersion: 6 }, errors: [expectedError] },
Expand All @@ -158,6 +165,7 @@ ruleTester.run("no-implied-eval", rule, {
{ code: "global.setTimeout(`foo` + bar)", parserOptions: { ecmaVersion: 6 }, env: { node: true }, errors: [expectedError] },
{ code: "global.setTimeout(1 + ';' + 1)", env: { node: true }, errors: [expectedError] },
{ code: "global.global.setTimeout(1 + ';' + 1)", env: { node: true }, errors: [expectedError] },
{ code: "globalThis.setTimeout('foo' + bar)", env: { es2020: true }, errors: [expectedError] },

// gives the correct node when dealing with nesting
{
Expand Down

0 comments on commit 081e240

Please sign in to comment.