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 1771097 - Add ESLint rule for ChromeUtils.defineESModuleGetters l…
…azy object name. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D147400
- Loading branch information
Showing
6 changed files
with
123 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
25 changes: 25 additions & 0 deletions
25
docs/code-quality/lint/linters/eslint-plugin-mozilla/lazy-getter-object-name.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,25 @@ | ||
lazy-getter-object-name | ||
============================= | ||
|
||
Enforce the standard object variable name ``lazy`` for | ||
``ChromeUtils.defineESModuleGetters`` | ||
|
||
Examples of incorrect code for this rule: | ||
----------------------------------------- | ||
|
||
.. code-block:: js | ||
const obj = {}; | ||
ChromeUtils.defineESModuleGetters(obj, { | ||
Services: “resource://gre/modules/Services.sys.mjs”, | ||
}); | ||
Examples of correct code for this rule: | ||
--------------------------------------- | ||
|
||
.. code-block:: js | ||
const lazy = {}; | ||
ChromeUtils.defineESModuleGetters(lazy, { | ||
Services: “resource://gre/modules/Services.sys.mjs”, | ||
}); |
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
45 changes: 45 additions & 0 deletions
45
tools/lint/eslint/eslint-plugin-mozilla/lib/rules/lazy-getter-object-name.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,45 @@ | ||
/** | ||
* @fileoverview Enforce the standard object name for | ||
* ChromeUtils.defineESModuleGetters | ||
* | ||
* 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"; | ||
|
||
function isIdentifier(node, id) { | ||
return node.type === "Identifier" && node.name === id; | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: | ||
"https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/lazy-getter-object-name.html", | ||
}, | ||
type: "problem", | ||
}, | ||
|
||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
let { callee } = node; | ||
if ( | ||
callee.type === "MemberExpression" && | ||
isIdentifier(callee.object, "ChromeUtils") && | ||
isIdentifier(callee.property, "defineESModuleGetters") && | ||
node.arguments.length >= 1 && | ||
!isIdentifier(node.arguments[0], "lazy") | ||
) { | ||
context.report({ | ||
node, | ||
message: | ||
"The variable name of the object passed to ChromeUtils.defineESModuleGetters must be `lazy`", | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
50 changes: 50 additions & 0 deletions
50
tools/lint/eslint/eslint-plugin-mozilla/tests/lazy-getter-object-name.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,50 @@ | ||
/* Any copyright is dedicated to the Public Domain. | ||
* http://creativecommons.org/publicdomain/zero/1.0/ */ | ||
|
||
"use strict"; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require("../lib/rules/lazy-getter-object-name"); | ||
var RuleTester = require("eslint").RuleTester; | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
function invalidCode(code) { | ||
let message = | ||
"The variable name of the object passed to ChromeUtils.defineESModuleGetters must be `lazy`"; | ||
return { code, errors: [{ message, type: "CallExpression" }] }; | ||
} | ||
|
||
ruleTester.run("lazy-getter-object-name", rule, { | ||
valid: [ | ||
` | ||
ChromeUtils.defineESModuleGetters(lazy, { | ||
Services: "resource://gre/modules/Services.sys.mjs", | ||
}); | ||
`, | ||
], | ||
invalid: [ | ||
invalidCode(` | ||
ChromeUtils.defineESModuleGetters(obj, { | ||
Services: "resource://gre/modules/Services.sys.mjs", | ||
}); | ||
`), | ||
invalidCode(` | ||
ChromeUtils.defineESModuleGetters(this, { | ||
Services: "resource://gre/modules/Services.sys.mjs", | ||
}); | ||
`), | ||
invalidCode(` | ||
ChromeUtils.defineESModuleGetters(window, { | ||
Services: "resource://gre/modules/Services.sys.mjs", | ||
}); | ||
`), | ||
], | ||
}); |