Skip to content

Commit

Permalink
Bug 1877304 - Allow exporting the lazy object. r=Standard8
Browse files Browse the repository at this point in the history
  • Loading branch information
arai-a committed Jan 31, 2024
1 parent d324aaa commit 215ba94
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
27 changes: 20 additions & 7 deletions tools/lint/eslint/eslint-plugin-mozilla/lib/rules/valid-lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module.exports = {
create(context) {
let lazyProperties = new Map();
let unknownProperties = [];
let isLazyExported = false;

function addProp(node, name) {
if (lazyProperties.has(name)) {
Expand Down Expand Up @@ -190,6 +191,16 @@ module.exports = {
}
},

ExportNamedDeclaration(node) {
for (const spec of node.specifiers) {
if (spec.local.name === "lazy") {
// If the lazy object is exported, do not check unused property.
isLazyExported = true;
break;
}
}
},

"Program:exit": function () {
for (let { name, node } of unknownProperties) {
let property = lazyProperties.get(name);
Expand All @@ -203,13 +214,15 @@ module.exports = {
property.used = true;
}
}
for (let [name, property] of lazyProperties.entries()) {
if (!property.used) {
context.report({
node: property.node,
messageId: "unusedProperty",
data: { name },
});
if (!isLazyExported) {
for (let [name, property] of lazyProperties.entries()) {
if (!property.used) {
context.report({
node: property.node,
messageId: "unusedProperty",
data: { name },
});
}
}
}
},
Expand Down
9 changes: 8 additions & 1 deletion tools/lint/eslint/eslint-plugin-mozilla/tests/valid-lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
var rule = require("../lib/rules/valid-lazy");
var RuleTester = require("eslint").RuleTester;

const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: "latest" } });
const ruleTester = new RuleTester({
parserOptions: { ecmaVersion: "latest", sourceType: "module" },
});

// ------------------------------------------------------------------------------
// Tests
Expand Down Expand Up @@ -95,6 +97,11 @@ ruleTester.run("valid-lazy", rule, {
var { x = lazy.foo.bar() } = {};
var [ y = lazy.foo.bar() ] = [];
`,
`
const lazy = {};
ChromeUtils.defineLazyGetter(lazy, "foo", () => {});
export { lazy as Foo };
`,
],
invalid: [
invalidCode("if (x) { lazy.bar; }", "bar", "unknownProperty"),
Expand Down

0 comments on commit 215ba94

Please sign in to comment.