From 0c32702e56b4c60ee8d166e66a5fa4dbe5a9ee7f Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Thu, 13 Oct 2022 22:28:51 +0900 Subject: [PATCH] fix: add more invalid tests for global variable leaks --- tests/lib/rules/no-implicit-globals.js | 67 ++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tests/lib/rules/no-implicit-globals.js b/tests/lib/rules/no-implicit-globals.js index ded2cad2ecf..412ba766134 100644 --- a/tests/lib/rules/no-implicit-globals.js +++ b/tests/lib/rules/no-implicit-globals.js @@ -1551,6 +1551,73 @@ ruleTester.run("no-implicit-globals", rule, { type: "VariableDeclarator" } ] + }, + + // Global variable leaks + { + code: "/* exported foo */ foo = 1", + errors: [ + { + message: leakMessage, + type: "AssignmentExpression" + } + ] + }, + { + code: "/* exported foo */ foo = function() {};", + errors: [ + { + message: leakMessage, + type: "AssignmentExpression" + } + ] + }, + { + code: "/* exported foo */ foo = function*() {};", + parserOptions: { ecmaVersion: 2015 }, + errors: [ + { + message: leakMessage, + type: "AssignmentExpression" + } + ] + }, + { + code: "/* exported foo */ window.foo = function() { bar = 1; }", + errors: [ + { + message: leakMessage, + type: "AssignmentExpression" + } + ] + }, + { + code: "/* exported foo */ (function() {}(foo = 1));", + errors: [ + { + message: leakMessage, + type: "AssignmentExpression" + } + ] + }, + { + code: "/* exported foo */ for (foo in {});", + errors: [ + { + message: leakMessage, + type: "ForInStatement" + } + ] + }, + { + code: "/* exported foo */ for (foo of []);", + parserOptions: { ecmaVersion: 2015 }, + errors: [ + { + message: leakMessage, + type: "ForOfStatement" + } + ] } ] });