Skip to content

Commit

Permalink
Bug 1846356 - Convert remaining ESLint rules to use messageIds and da…
Browse files Browse the repository at this point in the history
…ta values rather than message strings. r=cmkm

Differential Revision: https://phabricator.services.mozilla.com/D184981
  • Loading branch information
Standard8 committed Aug 2, 2023
1 parent eb0444a commit 7653914
Show file tree
Hide file tree
Showing 36 changed files with 195 additions and 112 deletions.
1 change: 0 additions & 1 deletion tools/lint/eslint/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ module.exports = {
"no-undef-init": "error",
"one-var": ["error", "never"],
strict: ["error", "global"],
"eslint-plugin/prefer-message-ids": "off",
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ module.exports = {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-addtask-only.html",
},
hasSuggestions: true,
messages: {
addTaskNotAllowed:
"add_task(...).only() not allowed - add an exception if this is intentional",
addTaskNotAllowedSuggestion: "Remove only() call from task",
},
schema: [],
type: "suggestion",
},
Expand All @@ -29,10 +34,10 @@ module.exports = {
) {
context.report({
node,
message: `add_task(...).only() not allowed - add an exception if this is intentional`,
messageId: "addTaskNotAllowed",
suggest: [
{
desc: "Remove only() call from task",
messageId: "addTaskNotAllowedSuggestion",
fix: fixer =>
fixer.replaceTextRange(
[node.callee.object.range[1], node.range[1]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ module.exports = {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-chromeutils-import-params.html",
},
hasSuggestions: true,
messages: {
importOnlyOneArg: "ChromeUtils.import only takes one argument.",
importOnlyOneArgSuggestion: "Remove the unnecessary parameters.",
},
schema: [],
type: "problem",
},
Expand All @@ -43,10 +47,10 @@ module.exports = {
) {
context.report({
node,
message: "ChromeUtils.import only takes one argument.",
messageId: "importOnlyOneArg",
suggest: [
{
desc: "Remove the unnecessary parameters.",
messageId: "importOnlyOneArgSuggestion",
fix: fixer => {
return fixer.removeRange(
getRangeAfterArgToEnd(context, 0, node.arguments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module.exports = {
docs: {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-global-this.html",
},
messages: {
avoidGlobalThis: "JSM should not use the global this",
},
schema: [],
type: "problem",
},
Expand All @@ -32,7 +35,7 @@ module.exports = {

context.report({
node,
message: `JSM should not use the global this`,
messageId: "avoidGlobalThis",
});
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ module.exports = {
docs: {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-globalThis-modification.html",
},
messages: {
rejectModifyGlobalThis:
"`globalThis` shouldn't be modified. `globalThis` is the shared global inside the system module, and properties defined on it is visible from all modules.",
rejectPassingGlobalThis:
"`globalThis` shouldn't be passed to function that can modify it. `globalThis` is the shared global inside the system module, and properties defined on it is visible from all modules.",
},
schema: [],
type: "problem",
},
Expand All @@ -44,8 +50,7 @@ module.exports = {
if (isIdentifier(target, "globalThis")) {
context.report({
node,
message:
"`globalThis` shouldn't be modified. `globalThis` is the shared global inside the system module, and properties defined on it is visible from all modules.",
messageId: "rejectModifyGlobalThis",
});
}
},
Expand All @@ -59,8 +64,7 @@ module.exports = {
if (isIdentifier(arg, "globalThis")) {
context.report({
node,
message:
"`globalThis` shouldn't be passed to function that can modify it. `globalThis` is the shared global inside the system module, and properties defined on it is visible from all modules.",
messageId: "rejectPassingGlobalThis",
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module.exports = {
docs: {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-relative-requires.html",
},
messages: {
rejectRelativeRequires: "relative paths are not allowed with require()",
},
schema: [],
type: "problem",
},
Expand All @@ -30,7 +33,7 @@ module.exports = {
if (path && isRelativePath(path)) {
context.report({
node,
message: "relative paths are not allowed with require()",
messageId: "rejectRelativeRequires",
});
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ module.exports = {
docs: {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-scriptableunicodeconverter.html",
},
messages: {
rejectScriptableUnicodeConverter:
"Ci.nsIScriptableUnicodeConverter is deprecated. You should use TextEncoder or TextDecoder instead.",
},
schema: [],
type: "problem",
},
Expand All @@ -31,8 +35,7 @@ module.exports = {
) {
context.report({
node,
message:
"Ci.nsIScriptableUnicodeConverter is deprecated. You should use TextEncoder or TextDecoder instead.",
messageId: "rejectScriptableUnicodeConverter",
});
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ module.exports = {
docs: {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-some-requires.html",
},
messages: {
rejectRequire: `require({{path}}) is not allowed`,
},
schema: [
{
type: "string",
Expand All @@ -33,7 +36,7 @@ module.exports = {
CallExpression(node) {
const path = helpers.getDevToolsRequirePath(node);
if (path && RX.test(path)) {
context.report({ node, message: `require(${path}) is not allowed` });
context.report({ node, messageId: "rejectRequire", data: { path } });
}
},
};
Expand Down
11 changes: 8 additions & 3 deletions tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-cc-etc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ module.exports = {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/use-cc-etc.html",
},
fixable: "code",
messages: {
useCcEtc: "Use {{ shortName }} rather than Components.{{ oldName }}",
},
schema: [],
type: "suggestion",
},
Expand All @@ -36,9 +39,11 @@ module.exports = {
) {
context.report({
node,
message: `Use ${
componentsMap[node.property.name]
} rather than Components.${node.property.name}`,
messageId: "useCcEtc",
data: {
shortName: componentsMap[node.property.name],
oldName: node.property.name,
},
fix: fixer =>
fixer.replaceTextRange(
[node.range[0], node.range[1]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ function isMemberExpression(node, object, member) {
);
}

const MSG_NO_JS_QUERY_INTERFACE =
"Please use ChromeUtils.generateQI rather than manually creating " +
"JavaScript QueryInterface functions";

const MSG_NO_XPCOMUTILS_GENERATEQI =
"Please use ChromeUtils.generateQI instead of XPCOMUtils.generateQI";

function funcToGenerateQI(context, node) {
const sourceCode = context.getSourceCode();
const text = sourceCode.getText(node);
Expand All @@ -53,6 +46,13 @@ module.exports = {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/use-chromeutils-generateqi.html",
},
fixable: "code",
messages: {
noJSQueryInterface:
"Please use ChromeUtils.generateQI rather than" +
"manually creating JavaScript QueryInterface functions",
noXpcomUtilsGenerateQI:
"Please use ChromeUtils.generateQI instead of XPCOMUtils.generateQI",
},
schema: [],
type: "suggestion",
},
Expand All @@ -64,7 +64,7 @@ module.exports = {
if (isMemberExpression(callee, "XPCOMUtils", "generateQI")) {
context.report({
node,
message: MSG_NO_XPCOMUTILS_GENERATEQI,
messageId: "noXpcomUtilsGenerateQI",
fix(fixer) {
return fixer.replaceText(callee, "ChromeUtils.generateQI");
},
Expand All @@ -78,7 +78,7 @@ module.exports = {
if (right.type === "FunctionExpression") {
context.report({
node: node.parent,
message: MSG_NO_JS_QUERY_INTERFACE,
messageId: "noJSQueryInterface",
fix(fixer) {
return fixer.replaceText(
right,
Expand All @@ -93,7 +93,7 @@ module.exports = {
function (node) {
context.report({
node,
message: MSG_NO_JS_QUERY_INTERFACE,
messageId: "noJSQueryInterface",
fix(fixer) {
let generateQI = funcToGenerateQI(context, node.value);
return fixer.replaceText(node, `QueryInterface: ${generateQI}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ module.exports = {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/use-chromeutils-import.html",
},
fixable: "code",
messages: {
useChromeUtilsImport:
"Please use ChromeUtils.import instead of Cu.import",
useDefineModuleGetter:
"Please use ChromeUtils.defineModuleGetter instead of " +
"XPCOMUtils.defineLazyModuleGetter",
},
schema: [],
type: "suggestion",
},
Expand All @@ -48,7 +55,7 @@ module.exports = {
) {
context.report({
node,
message: "Please use ChromeUtils.import instead of Cu.import",
messageId: "useChromeUtilsImport",
fix(fixer) {
return fixer.replaceText(callee, "ChromeUtils.import");
},
Expand All @@ -61,9 +68,7 @@ module.exports = {
) {
context.report({
node,
message:
"Please use ChromeUtils.defineModuleGetter instead of " +
"XPCOMUtils.defineLazyModuleGetter",
messageId: "useDefineModuleGetter",
fix(fixer) {
return fixer.replaceText(
callee,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ module.exports = {
docs: {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/use-default-preference-values.html",
},
messages: {
provideDefaultValue:
"provide a default value instead of using a try/catch block",
},
schema: [],
type: "suggestion",
},
Expand Down Expand Up @@ -44,7 +48,7 @@ module.exports = {

context.report({
node,
message: "provide a default value instead of using a try/catch block",
messageId: "provideDefaultValue",
});
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module.exports = {
docs: {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/use-includes-instead-of-indexOf.html",
},
messages: {
useIncludes: "use .includes instead of .indexOf",
},
schema: [],
type: "suggestion",
},
Expand Down Expand Up @@ -41,7 +44,7 @@ module.exports = {
) {
context.report({
node,
message: "use .includes instead of .indexOf",
messageId: "useIncludes",
});
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ module.exports = {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/use-isInstance.html",
},
fixable: "code",
messages: {
preferIsInstance:
"Please prefer .isInstance() in chrome scripts over the standard instanceof operator for DOM interfaces, " +
"since the latter will return false when the object is created from a different context.",
},
schema: [],
type: "problem",
},
Expand All @@ -134,9 +139,7 @@ module.exports = {
) {
context.report({
node,
message:
"Please prefer .isInstance() in chrome scripts over the standard instanceof operator for DOM interfaces, " +
"since the latter will return false when the object is created from a different context.",
messageId: "preferIsInstance",
fix(fixer) {
const sourceCode = context.getSourceCode();
return fixer.replaceText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module.exports = {
docs: {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/use-ownerGlobal.html",
},
messages: {
useOwnerGlobal: "use .ownerGlobal instead of .ownerDocument.defaultView",
},
schema: [],
type: "suggestion",
},
Expand All @@ -32,7 +35,7 @@ module.exports = {

context.report({
node,
message: "use .ownerGlobal instead of .ownerDocument.defaultView",
messageId: "useOwnerGlobal",
});
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module.exports = {
docs: {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/use-returnValue.html",
},
messages: {
useReturnValue:
"{Array/String}.{{ property }} doesn't modify the instance in-place",
},
schema: [],
type: "problem",
},
Expand All @@ -33,7 +37,10 @@ module.exports = {

context.report({
node,
message: `{Array/String}.${node.expression.callee.property.name} doesn't modify the instance in-place`,
messageId: "useReturnValue",
data: {
property: node.expression.callee.property.name,
},
});
},
};
Expand Down
Loading

0 comments on commit 7653914

Please sign in to comment.