forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1607331 - Part 3: Reject global this usage in JSM. r=Standard8
Differential Revision: https://phabricator.services.mozilla.com/D148551
- Loading branch information
Showing
15 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
docs/code-quality/lint/linters/eslint-plugin-mozilla/reject-global-this.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
reject-global-this | ||
====================== | ||
|
||
Rejects global ``this`` usage in JSM files. The global ``this`` is not | ||
available in ESM, and this is a preparation for the migration. | ||
|
||
Examples of incorrect code for this rule: | ||
----------------------------------------- | ||
|
||
.. code-block:: js | ||
this.EXPORTED_SYMBOLS = ["foo"]; | ||
XPCOMUtils.defineLazyModuleGetters(this, { | ||
AddonManager: "resource://gre/modules/AddonManager.jsm", | ||
}); | ||
Examples of correct code for this rule: | ||
--------------------------------------- | ||
|
||
.. code-block:: js | ||
const EXPORTED_SYMBOLS = ["foo"]; | ||
const lazy = {}; | ||
XPCOMUtils.defineLazyModuleGetters(lazy, { | ||
AddonManager: "resource://gre/modules/AddonManager.jsm", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-global-this.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @fileoverview Reject attempts to use the global object in jsms. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
"use strict"; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Rule Definition | ||
// ----------------------------------------------------------------------------- | ||
|
||
function isGlobalThis(context, node) { | ||
for (let ancestor of context.getAncestors()) { | ||
if ( | ||
ancestor.type == "FunctionDeclaration" || | ||
ancestor.type == "FunctionExpression" || | ||
ancestor.type == "ClassProperty" || | ||
ancestor.type == "ClassPrivateProperty" || | ||
ancestor.type == "PropertyDefinition" || | ||
ancestor.type == "StaticBlock" | ||
) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: | ||
"https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-global-this.html", | ||
}, | ||
type: "problem", | ||
}, | ||
|
||
create(context) { | ||
return { | ||
ThisExpression(node) { | ||
if (!isGlobalThis(context, node)) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
message: `JSM should not use the global this`, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
49 changes: 49 additions & 0 deletions
49
tools/lint/eslint/eslint-plugin-mozilla/tests/reject-global-this.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* Any copyright is dedicated to the Public Domain. | ||
* http://creativecommons.org/publicdomain/zero/1.0/ */ | ||
|
||
"use strict"; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require("../lib/rules/reject-global-this"); | ||
var RuleTester = require("eslint").RuleTester; | ||
|
||
// class static block is available from ES2022 = 13. | ||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 13 } }); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
function invalidCode(code) { | ||
let message = "JSM should not use the global this"; | ||
return { code, errors: [{ message, type: "ThisExpression" }] }; | ||
} | ||
|
||
ruleTester.run("reject-top-level-await", rule, { | ||
valid: [ | ||
"function f() { this; }", | ||
"(function f() { this; });", | ||
"({ foo() { this; } });", | ||
"({ get foo() { this; } })", | ||
"({ set foo(x) { this; } })", | ||
"class X { foo() { this; } }", | ||
"class X { get foo() { this; } }", | ||
"class X { set foo(x) { this; } }", | ||
"class X { static foo() { this; } }", | ||
"class X { static get foo() { this; } }", | ||
"class X { static set foo(x) { this; } }", | ||
"class X { P = this; }", | ||
"class X { #P = this; }", | ||
"class X { static { this; } }", | ||
], | ||
invalid: [ | ||
invalidCode("this;"), | ||
invalidCode("() => this;"), | ||
|
||
invalidCode("this.foo = 10;"), | ||
invalidCode("ChromeUtils.defineModuleGetter(this, {});"), | ||
], | ||
}); |