Skip to content

Commit 290c594

Browse files
authored
feat: add self to no-implied-eval rule (#20468)
1 parent 1d29d22 commit 290c594

2 files changed

Lines changed: 206 additions & 0 deletions

File tree

‎lib/rules/no-implied-eval.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module.exports = {
4141
"global",
4242
"window",
4343
"globalThis",
44+
"self",
4445
]);
4546
const EVAL_LIKE_FUNC_PATTERN =
4647
/^(?:set(?:Interval|Timeout)|execScript)$/u;

‎tests/lib/rules/no-implied-eval.js‎

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,78 @@ ruleTester.run("no-implied-eval", rule, {
485485
globals: {}, // No window global defined
486486
},
487487
},
488+
{
489+
code: "self.setTimeout;",
490+
languageOptions: { globals: globals.browser },
491+
},
492+
{
493+
code: "self.setTimeout = foo;",
494+
languageOptions: { globals: globals.browser },
495+
},
496+
{
497+
code: "self['setTimeout'];",
498+
languageOptions: { globals: globals.browser },
499+
},
500+
{
501+
code: "self['setTimeout'] = foo;",
502+
languageOptions: { globals: globals.browser },
503+
},
504+
{
505+
code: "self[`SetTimeOut`]('foo', 100);",
506+
languageOptions: { ecmaVersion: 6, globals: globals.browser },
507+
},
508+
{
509+
code: "self[`setTimeout${foo}`]('foo', 100);",
510+
languageOptions: { ecmaVersion: 6, globals: globals.browser },
511+
},
512+
{
513+
code: "self.setTimeout(function() { x = 1; }, 100);",
514+
languageOptions: { globals: globals.browser },
515+
},
516+
{
517+
code: "self.setInterval(function() { x = 1; }, 100);",
518+
languageOptions: { globals: globals.browser },
519+
},
520+
{
521+
code: "self.execScript(function() { x = 1; }, 100);",
522+
languageOptions: { globals: globals.browser },
523+
},
524+
{
525+
code: "self.setTimeout(foo, 100);",
526+
languageOptions: { globals: globals.browser },
527+
},
528+
{
529+
code: "foo.self.setTimeout('foo', 100);",
530+
languageOptions: { globals: globals.browser },
531+
},
532+
{
533+
code: "var self; self.setTimeout('foo', 100);",
534+
languageOptions: { globals: globals.browser },
535+
},
536+
{
537+
code: "function foo(self) { self.setTimeout('foo', 100); }",
538+
languageOptions: { globals: globals.browser },
539+
},
540+
{
541+
code: "foo('', self.setTimeout);",
542+
languageOptions: { globals: globals.browser },
543+
},
544+
{
545+
code: `
546+
function outer() {
547+
function self() {
548+
console.log("Shadowed self");
549+
}
550+
self.setTimeout('code');
551+
}`,
552+
languageOptions: { globals: globals.browser },
553+
},
554+
{
555+
code: "self.setTimeout('code');",
556+
languageOptions: {
557+
globals: {}, // No globals defined
558+
},
559+
},
488560
],
489561

490562
invalid: [
@@ -879,5 +951,138 @@ ruleTester.run("no-implied-eval", rule, {
879951
},
880952
errors: [{ messageId: "execScript" }],
881953
},
954+
{
955+
code: "self.setTimeout('foo')",
956+
languageOptions: { globals: globals.browser },
957+
errors: [expectedError],
958+
},
959+
{
960+
code: "self.setInterval('foo')",
961+
languageOptions: { globals: globals.browser },
962+
errors: [expectedError],
963+
},
964+
{
965+
code: "self.execScript('foo')",
966+
languageOptions: { globals: globals.browser },
967+
errors: [
968+
{
969+
messageId: "execScript",
970+
},
971+
],
972+
},
973+
{
974+
code: "self['setTimeout']('foo')",
975+
languageOptions: { globals: globals.browser },
976+
errors: [expectedError],
977+
},
978+
{
979+
code: "self['setInterval']('foo')",
980+
languageOptions: { globals: globals.browser },
981+
errors: [expectedError],
982+
},
983+
{
984+
code: "self[`setInterval`]('foo')",
985+
languageOptions: { ecmaVersion: 6, globals: globals.browser },
986+
errors: [expectedError],
987+
},
988+
{
989+
code: "self['execScript']('foo')",
990+
languageOptions: { globals: globals.browser },
991+
errors: [
992+
{
993+
messageId: "execScript",
994+
},
995+
],
996+
},
997+
{
998+
code: "self[`execScript`]('foo')",
999+
languageOptions: { ecmaVersion: 6, globals: globals.browser },
1000+
errors: [
1001+
{
1002+
messageId: "execScript",
1003+
},
1004+
],
1005+
},
1006+
{
1007+
code: "self.self['setInterval']('foo')",
1008+
languageOptions: { globals: globals.browser },
1009+
errors: [expectedError],
1010+
},
1011+
{
1012+
code: "self.self['execScript']('foo')",
1013+
languageOptions: { globals: globals.browser },
1014+
errors: [
1015+
{
1016+
messageId: "execScript",
1017+
},
1018+
],
1019+
},
1020+
{
1021+
code: "self.setTimeout(`foo${bar}`)",
1022+
languageOptions: { ecmaVersion: 6, globals: globals.browser },
1023+
errors: [expectedError],
1024+
},
1025+
{
1026+
code: "self.self.setTimeout(`foo${bar}`)",
1027+
languageOptions: { ecmaVersion: 6, globals: globals.browser },
1028+
errors: [expectedError],
1029+
},
1030+
{
1031+
code: "self.setTimeout('foo' + bar)",
1032+
languageOptions: { globals: globals.browser },
1033+
errors: [expectedError],
1034+
},
1035+
{
1036+
code: "self.setTimeout(foo + 'bar')",
1037+
languageOptions: { globals: globals.browser },
1038+
errors: [expectedError],
1039+
},
1040+
{
1041+
code: "self.setTimeout(`foo` + bar)",
1042+
languageOptions: { ecmaVersion: 6, globals: globals.browser },
1043+
errors: [expectedError],
1044+
},
1045+
{
1046+
code: "self.setTimeout(1 + ';' + 1)",
1047+
languageOptions: { globals: globals.browser },
1048+
errors: [expectedError],
1049+
},
1050+
{
1051+
code: "self.self.setTimeout(1 + ';' + 1)",
1052+
languageOptions: { globals: globals.browser },
1053+
errors: [expectedError],
1054+
},
1055+
{
1056+
code: "self?.setTimeout('code', 0)",
1057+
languageOptions: {
1058+
ecmaVersion: 2020,
1059+
globals: { self: "readonly" },
1060+
},
1061+
errors: [{ messageId: "impliedEval" }],
1062+
},
1063+
{
1064+
code: "(self?.setTimeout)('code', 0)",
1065+
languageOptions: {
1066+
ecmaVersion: 2020,
1067+
globals: { self: "readonly" },
1068+
},
1069+
errors: [{ messageId: "impliedEval" }],
1070+
},
1071+
{
1072+
code: "self?.execScript('code')",
1073+
languageOptions: {
1074+
ecmaVersion: 2020,
1075+
globals: { self: "readonly" },
1076+
},
1077+
errors: [{ messageId: "execScript" }],
1078+
},
1079+
{
1080+
code: "(self?.execScript)('code')",
1081+
languageOptions: {
1082+
ecmaVersion: 2020,
1083+
globals: { self: "readonly" },
1084+
},
1085+
errors: [{ messageId: "execScript" }],
1086+
},
8821087
],
8831088
});

0 commit comments

Comments
 (0)