Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: support globalThis (refs #12670) #12774

Merged
merged 18 commits into from Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion conf/environments.js
Expand Up @@ -40,7 +40,8 @@ const newGlobals2017 = {
const newGlobals2020 = {
BigInt: false,
BigInt64Array: false,
BigUint64Array: false
BigUint64Array: false,
globalThis: false
};

//------------------------------------------------------------------------------
Expand Down
8 changes: 5 additions & 3 deletions lib/rules/no-alert.js
Expand Up @@ -8,7 +8,10 @@
// Requirements
//------------------------------------------------------------------------------

const getPropertyName = require("./utils/ast-utils").getStaticPropertyName;
const {
getStaticPropertyName: getPropertyName,
getVariableByName
} = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -61,7 +64,7 @@ function isGlobalThisReferenceOrGlobalWindow(scope, node) {
if (scope.type === "global" && node.type === "ThisExpression") {
return true;
}
if (node.name === "window") {
if (node.name === "window" || (node.name === "globalThis" && getVariableByName(scope, "globalThis"))) {
return !isShadowed(scope, node);
}

Expand Down Expand Up @@ -119,7 +122,6 @@ module.exports = {
});
}
}

}
};

Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-eval.js
Expand Up @@ -17,7 +17,8 @@ const astUtils = require("./utils/ast-utils");

const candidatesOfGlobalObject = Object.freeze([
"global",
"window"
"window",
"globalThis"
]);

/**
Expand Down
15 changes: 14 additions & 1 deletion lib/rules/no-obj-calls.js
Expand Up @@ -10,13 +10,26 @@
//------------------------------------------------------------------------------

const { CALL, ReferenceTracker } = require("eslint-utils");
const getPropertyName = require("./utils/ast-utils").getStaticPropertyName;

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

const nonCallableGlobals = ["Atomics", "JSON", "Math", "Reflect"];

/**
* Returns the name of the node to report
* @param {ASTNode} node A node to report
* @returns {string} name to report
*/
function getReportNodeName(node) {
if (node.callee.type === "MemberExpression") {
return getPropertyName(node.callee);
}
return node.callee.name;
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -54,7 +67,7 @@ module.exports = {
}

for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
context.report({ node, messageId: "unexpectedCall", data: { name: node.callee.name } });
context.report({ node, messageId: "unexpectedCall", data: { name: getReportNodeName(node) } });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this bug!

Current behavior (demo):

/* eslint no-obj-calls: "error"*/
/* eslint-env browser*/

window.JSON(); // 'undefined' is not a function (no-obj-calls)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A question, maybe we could use path from iterateglobalreferences?

I guess it depends on whether we want to report "JSON is not a function" or "foo is not a function" in cases such as the following:

/* eslint no-obj-calls: "error"*/

var foo = bar ? baz : JSON;

foo();

Copy link
Member Author

@yeonjuan yeonjuan Jan 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll try it! :) But what would be a better report?
In my personal opinion, current behavior looks better. But I respect what eslint members think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, let's wait for more opinions.

Current behavior reports "foo is not a function".

I'd vote for "JSON is not a function" as it might be a bit more helpful for the user to find out what's actually happening and why is the location of "foo()" reported as a global object call.

Copy link
Member Author

@yeonjuan yeonjuan Jan 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, let's wait for more opinions.

Ok. And just to note.
The reason for my preference was that foo is not a function looks like a more intuitive explanation for the code.

I'd vote for "JSON is not a function" as it might be a bit more helpful for the user to find out what's actually happening and why is the location of "foo()" reported as a global object call.

But I agree that JSON is not a function is more helpful to find out what's actually happening. it seems more reasonable 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do both? "foo is a reference to JSON, which is not a function"? That seems like it gives the right amount of information without being too confusing or requiring you to figure out what the code is doing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do both? "foo is a reference to JSON, which is not a function"?

That would be ideal!

I'd be also fine with "foo is not a function" in this PR (it's already much better than "undefined is not a function") and improving the message further in another PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do both? "foo is a reference to JSON, which is not a function"?

Agree! it would make the message more clear. maybe it needs new messageId like -unexpectedRefCall. I will do it. thanks :)

}
}
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -53,7 +53,7 @@
"debug": "^4.0.1",
"doctrine": "^3.0.0",
"eslint-scope": "^5.0.0",
"eslint-utils": "^1.4.3",
"eslint-utils": "^2.0.0",
"eslint-visitor-keys": "^1.1.0",
"espree": "^6.1.2",
"esquery": "^1.0.1",
Expand Down
22 changes: 21 additions & 1 deletion tests/lib/rules/no-alert.js
Expand Up @@ -34,7 +34,12 @@ ruleTester.run("no-alert", rule, {
"function prompt() {} prompt();",
"window[alert]();",
"function foo() { this.alert(); }",
"function foo() { var window = bar; window.alert(); }"
"function foo() { var window = bar; window.alert(); }",
"globalThis.alert();",
{ code: "globalThis['alert']();", env: { es6: true } },
{ code: "globalThis.alert();", env: { es2017: true } },
{ code: "var globalThis = foo; globalThis.alert();", env: { es2020: true } },
{ code: "function foo() { var globalThis = foo; globalThis.alert(); }", env: { es2020: true } }
],
invalid: [
{
Expand Down Expand Up @@ -104,6 +109,21 @@ ruleTester.run("no-alert", rule, {
{
code: "function foo() { var window = bar; window.alert(); }\nwindow.alert();",
errors: [{ messageId: "unexpected", data: { name: "alert" }, type: "CallExpression", line: 2, column: 1 }]
},
{
code: "globalThis['alert'](foo)",
env: { es2020: true },
errors: [{ messageId: "unexpected", data: { name: "alert" }, type: "CallExpression", line: 1, column: 1 }]
},
{
code: "globalThis.alert();",
env: { es2020: true },
errors: [{ messageId: "unexpected", data: { name: "alert" }, type: "CallExpression", line: 1, column: 1 }]
},
{
code: "function foo() { var globalThis = bar; globalThis.alert(); }\nglobalThis.alert();",
env: { es2020: true },
errors: [{ messageId: "unexpected", data: { name: "alert" }, type: "CallExpression", line: 2, column: 1 }]
}
]
});
20 changes: 18 additions & 2 deletions tests/lib/rules/no-eval.js
Expand Up @@ -35,6 +35,11 @@ ruleTester.run("no-eval", rule, {
{ code: "global.eval('foo')", env: { browser: true } },
{ code: "global.noeval('foo')", env: { node: true } },
{ code: "function foo() { var eval = 'foo'; global[eval]('foo') }", env: { node: true } },
"globalThis.eval('foo')",
{ code: "globalThis.eval('foo')", env: { es2017: true } },
{ code: "globalThis.eval('foo')", env: { browser: true } },
{ code: "globalThis.noneval('foo')", env: { es2020: true } },
{ code: "function foo() { var eval = 'foo'; globalThis[eval]('foo') }", env: { es2020: true } },
"this.noeval('foo');",
"function foo() { 'use strict'; this.eval('foo'); }",
{ code: "function foo() { this.eval('foo'); }", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
Expand All @@ -57,7 +62,12 @@ ruleTester.run("no-eval", rule, {
{ code: "global.eval('foo')", options: [{ allowIndirect: true }], env: { node: true } },
{ code: "global.global.eval('foo')", options: [{ allowIndirect: true }], env: { node: true } },
{ code: "this.eval('foo')", options: [{ allowIndirect: true }] },
{ code: "function foo() { this.eval('foo') }", options: [{ allowIndirect: true }] }
{ code: "function foo() { this.eval('foo') }", options: [{ allowIndirect: true }] },
{ code: "(0, globalThis.eval)('foo')", options: [{ allowIndirect: true }], env: { es2020: true } },
{ code: "(0, globalThis['eval'])('foo')", options: [{ allowIndirect: true }], env: { es2020: true } },
{ code: "var EVAL = globalThis.eval; EVAL('foo')", options: [{ allowIndirect: true }] },
{ code: "function foo() { globalThis.eval('foo') }", options: [{ allowIndirect: true }], env: { es2020: true } },
{ code: "globalThis.globalThis.eval('foo');", options: [{ allowIndirect: true }], env: { es2020: true } }
],

invalid: [
Expand All @@ -84,6 +94,12 @@ ruleTester.run("no-eval", rule, {
{ code: "global.global.eval('foo')", env: { node: true }, errors: [{ messageId: "unexpected", type: "CallExpression" }] },
{ code: "global.global[`eval`]('foo')", parserOptions: { ecmaVersion: 6 }, env: { node: true }, errors: [{ messageId: "unexpected", type: "CallExpression" }] },
{ code: "this.eval('foo')", errors: [{ messageId: "unexpected", type: "CallExpression" }] },
{ code: "function foo() { this.eval('foo') }", errors: [{ messageId: "unexpected", type: "CallExpression" }] }
{ code: "function foo() { this.eval('foo') }", errors: [{ messageId: "unexpected", type: "CallExpression" }] },
{ code: "var EVAL = globalThis.eval; EVAL('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "MemberExpression" }] },
{ code: "globalThis.eval('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "CallExpression" }] },
{ code: "globalThis.globalThis.eval('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "CallExpression" }] },
{ code: "globalThis.globalThis['eval']('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "CallExpression" }] },
{ code: "(0, globalThis.eval)('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "MemberExpression" }] },
{ code: "(0, globalThis['eval'])('foo')", env: { es2020: true }, errors: [{ messageId: "unexpected", type: "MemberExpression" }] }
]
});
24 changes: 23 additions & 1 deletion tests/lib/rules/no-misleading-character-class.js
Expand Up @@ -67,7 +67,9 @@ ruleTester.run("no-misleading-character-class", rule, {

// don't report and don't crash on invalid regex
"var r = new RegExp('[Á] [ ');",
"var r = RegExp('{ [Á]', 'u');"
"var r = RegExp('{ [Á]', 'u');",
{ code: "var r = new globalThis.RegExp('[Á] [ ');", env: { es2020: true } },
{ code: "var r = globalThis.RegExp('{ [Á]', 'u');", env: { es2020: true } }
],
invalid: [

Expand Down Expand Up @@ -271,6 +273,26 @@ ruleTester.run("no-misleading-character-class", rule, {
{
code: String.raw`var r = new RegExp("[\\u{1F468}\\u{200D}\\u{1F469}\\u{200D}\\u{1F466}]", "u")`,
errors: [{ messageId: "zwj" }]
},
{
code: String.raw`var r = new globalThis.RegExp("[❇️]", "")`,
env: { es2020: true },
errors: [{ messageId: "combiningClass" }]
},
{
code: String.raw`var r = new globalThis.RegExp("[👶🏻]", "u")`,
env: { es2020: true },
errors: [{ messageId: "emojiModifier" }]
},
{
code: String.raw`var r = new globalThis.RegExp("[🇯🇵]", "")`,
env: { es2020: true },
errors: [{ messageId: "surrogatePairWithoutUFlag" }]
},
{
code: String.raw`var r = new globalThis.RegExp("[\\u{1F468}\\u{200D}\\u{1F469}\\u{200D}\\u{1F466}]", "u")`,
env: { es2020: true },
errors: [{ messageId: "zwj" }]
}
]
});
61 changes: 61 additions & 0 deletions tests/lib/rules/no-obj-calls.js
Expand Up @@ -28,6 +28,19 @@ ruleTester.run("no-obj-calls", rule, {
"Reflect.get(foo, 'x')",
"Atomics.load(foo, 0)",

{ code: "globalThis.Math();", env: { es6: true } },
{ code: "var x = globalThis.Math();", env: { es6: true } },
{ code: "f(globalThis.Math());", env: { es6: true } },
{ code: "globalThis.Math().foo;", env: { es6: true } },
{ code: "var x = globalThis.JSON();", env: { es6: true } },
{ code: "x = globalThis.JSON(str);", env: { es6: true } },
{ code: "globalThis.Math( globalThis.JSON() );", env: { es6: true } },
{ code: "var x = globalThis.Reflect();", env: { es6: true } },
{ code: "var x = globalThis.Reflect();", env: { es2017: true } },
{ code: "/*globals Reflect: true*/ globalThis.Reflect();", env: { es2017: true } },
{ code: "var x = globalThis.Atomics();", env: { es2017: true } },
{ code: "var x = globalThis.Atomics();", globals: { Atomics: false }, env: { es2017: true } },

// non-existing variables
"/*globals Math: off*/ Math();",
{
Expand Down Expand Up @@ -130,6 +143,54 @@ ruleTester.run("no-obj-calls", rule, {
code: "var x = Atomics();",
globals: { Atomics: false },
errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "CallExpression" }]
},
{
code: "var x = globalThis.Math();",
env: { es2020: true },
errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression" }]
},
{
code: "f(globalThis.Math());",
env: { es2020: true },
errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 3, endColumn: 20 }]
},
{
code: "globalThis.Math().foo;",
env: { es2020: true },
errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 1, endColumn: 18 }]
},
{
code: "var x = globalThis.JSON();",
env: { es2020: true },
errors: [{ messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression" }]
},
{
code: "x = globalThis.JSON(str);",
env: { es2020: true },
errors: [{ messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression" }]
},
{
code: "globalThis.Math( globalThis.JSON() );",
env: { es2020: true },
errors: [
{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 1, endColumn: 37 },
{ messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression", column: 18, endColumn: 35 }
]
},
{
code: "var x = globalThis.Reflect();",
env: { es2020: true },
errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
},
{
code: "/*globals Reflect: true*/ Reflect();",
env: { es2020: true },
errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
},
{
code: "var x = globalThis.Atomics();",
env: { es2020: true },
errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "CallExpression" }]
}
]
});
18 changes: 18 additions & 0 deletions tests/lib/rules/no-redeclare.js
Expand Up @@ -41,6 +41,8 @@ ruleTester.run("no-redeclare", rule, {
options: [{ builtinGlobals: true }],
env: { browser: false }
},
{ code: "var glovalThis = foo", options: [{ builtinGlobals: true }], env: { es6: true } },
{ code: "var glovalThis = foo", options: [{ builtinGlobals: true }], env: { es2017: true } },

// Comments and built-ins.
{
Expand Down Expand Up @@ -124,6 +126,22 @@ ruleTester.run("no-redeclare", rule, {
{ message: "'a' is already defined.", type: "Identifier" }
]
},
{
code: "var globalThis = 0;",
options: [{ builtinGlobals: true }],
env: { es2020: true },
errors: [{ message: "'globalThis' is already defined as a built-in global variable.", type: "Identifier" }]
},
{
code: "var a; var {a = 0, b: globalThis = 0} = {};",
options: [{ builtinGlobals: true }],
parserOptions: { ecmaVersion: 6 },
env: { es2020: true },
errors: [
{ message: "'a' is already defined.", type: "Identifier" },
{ message: "'globalThis' is already defined as a built-in global variable.", type: "Identifier" }
]
},
{
code: "/*global b:false*/ var b = 1;",
options: [{ builtinGlobals: true }],
Expand Down
45 changes: 44 additions & 1 deletion tests/lib/rules/prefer-exponentiation-operator.js
Expand Up @@ -58,14 +58,27 @@ ruleTester.run("prefer-exponentiation-operator", rule, {
"foo.Math.pow(a, b)",
"new Math.pow(a, b)",
"Math[pow](a, b)",
{ code: "globalThis.Object.pow(a, b)", env: { es2020: true } },
{ code: "globalThis.Math.max(a, b)", env: { es2020: true } },

// not the global Math
"/* globals Math:off*/ Math.pow(a, b)",
"let Math; Math.pow(a, b);",
"if (foo) { const Math = 1; Math.pow(a, b); }",
"var x = function Math() { Math.pow(a, b); }",
"function foo(Math) { Math.pow(a, b); }",
"function foo() { Math.pow(a, b); var Math; }"
"function foo() { Math.pow(a, b); var Math; }",

"globalThis.Math.pow(a, b)",
{ code: "globalThis.Math.pow(a, b)", env: { es6: true } },
{ code: "globalThis.Math.pow(a, b)", env: { es2017: true } },
{
code: `
var globalThis = bar;
globalThis.Math.pow(a, b)
`,
env: { es2020: true }
}
],

invalid: [
Expand All @@ -75,6 +88,36 @@ ruleTester.run("prefer-exponentiation-operator", rule, {
invalid("Math['pow'](a, b)", "a**b"),
invalid("(Math)['pow'](a, b)", "a**b"),
invalid("var x=Math\n. pow( a, \n b )", "var x=a**b"),
{
code: "globalThis.Math.pow(a, b)",
output: "a**b",
env: { es2020: true },
errors: [
{
messageId: "useExponentiation",
type: "CallExpression",
line: 1,
column: 1,
endLine: 1,
endColumn: 26
}
]
},
{
code: "globalThis.Math['pow'](a, b)",
output: "a**b",
env: { es2020: true },
errors: [
{
messageId: "useExponentiation",
type: "CallExpression",
line: 1,
column: 1,
endLine: 1,
endColumn: 29
}
]
},

// able to catch some workarounds
invalid("Math[`pow`](a, b)", "a**b"),
Expand Down