Bug 1808400 - Handle cases of .then(foo, Cu.reportError) in ESLint rule no-cu-reportError. r=mossop

Differential Revision: https://phabricator.services.mozilla.com/D167520
This commit is contained in:
Mark Banner
2023-01-23 18:09:04 +00:00
parent 24d67b71d0
commit 774d70b85f
10 changed files with 54 additions and 49 deletions

View File

@@ -1854,7 +1854,7 @@ var gSync = {
let navbar = document.getElementById(CustomizableUI.AREA_NAVBAR); let navbar = document.getElementById(CustomizableUI.AREA_NAVBAR);
navbar.overflowable.show().then(() => { navbar.overflowable.show().then(() => {
PanelUI.showSubView("PanelUI-remotetabs", anchor); PanelUI.showSubView("PanelUI-remotetabs", anchor);
}, Cu.reportError); }, console.error);
} else { } else {
// It is placed somewhere else - just try and show it. // It is placed somewhere else - just try and show it.
PanelUI.showSubView("PanelUI-remotetabs", anchor); PanelUI.showSubView("PanelUI-remotetabs", anchor);

View File

@@ -3923,7 +3923,7 @@ BrowserGlue.prototype = {
.catch(console.error) .catch(console.error)
.then(() => enableProfilerButton(wasAddonActive)) .then(() => enableProfilerButton(wasAddonActive))
.catch(console.error); .catch(console.error);
}, Cu.reportError); }, console.error);
} }
// Clear unused socks proxy backup values - see bug 1625773. // Clear unused socks proxy backup values - see bug 1625773.

View File

@@ -647,7 +647,7 @@ var gEditItemOverlay = {
if (this._paneInfo) { if (this._paneInfo) {
this._mayUpdateFirstEditField("tagsField"); this._mayUpdateFirstEditField("tagsField");
} }
}, Cu.reportError); }, console.error);
} }
}, },

View File

@@ -626,7 +626,7 @@ var gEditItemOverlay = {
if (anyChanges && this._paneInfo) { if (anyChanges && this._paneInfo) {
this._mayUpdateFirstEditField("tagsField"); this._mayUpdateFirstEditField("tagsField");
} }
}, Cu.reportError); }, console.error);
} }
}, },

View File

@@ -1160,7 +1160,7 @@ add_setup(function test_common_initialize() {
aResponse.finish(); aResponse.finish();
info("Aborting response with network reset."); info("Aborting response with network reset.");
}) })
.then(null, Cu.reportError); .then(null, console.error);
}); });
// During unit tests, most of the functions that require profile access or // During unit tests, most of the functions that require profile access or

View File

@@ -316,7 +316,7 @@ EnterprisePoliciesManager.prototype = {
break; break;
case "EnterprisePolicies:Restart": case "EnterprisePolicies:Restart":
this._restart().then(null, Cu.reportError); this._restart().then(null, console.error);
break; break;
} }
}, },

View File

@@ -564,7 +564,7 @@ function sendConsoleAPIMessage(aConsole, aLevel, aFrame, aArgs, aOptions = {}) {
consoleEvent.groupName = Array.prototype.join.call(aArgs, " "); consoleEvent.groupName = Array.prototype.join.call(aArgs, " ");
} catch (ex) { } catch (ex) {
console.error(ex); console.error(ex);
Cu.reportError(ex.stack); console.error(ex.stack);
return; return;
} }
break; break;

View File

@@ -102,7 +102,7 @@ class nsContentDispatchChooser {
aURI aURI
); );
} catch (error) { } catch (error) {
Cu.reportError(error.message); console.error(error.message);
} }
if (!shouldOpenHandler) { if (!shouldOpenHandler) {

View File

@@ -84,28 +84,22 @@ module.exports = {
create(context) { create(context) {
return { return {
CallExpression(node) { CallExpression(node) {
let checkNode; let checkNodes = [];
if ( if (isCuReportError(node.callee)) {
node.arguments.length >= 1 &&
node.arguments[0].type == "MemberExpression"
) {
// Handles cases of `.foo(Cu.reportError)`.
checkNode = node.arguments[0];
} else {
// Handles cases of `Cu.reportError()`. // Handles cases of `Cu.reportError()`.
checkNode = node.callee; if (node.arguments.length > 1) {
}
if (!isCuReportError(checkNode)) {
return;
}
if (checkNode == node.callee && node.arguments.length > 1) {
// TODO: Bug 1802347 For initial landing, we allow the two // TODO: Bug 1802347 For initial landing, we allow the two
// argument form of Cu.reportError as the second argument is a stack // argument form of Cu.reportError as the second argument is a stack
// argument which is more complicated to deal with. // argument which is more complicated to deal with.
return; return;
} }
checkNodes = [node.callee];
} else if (node.arguments.length >= 1) {
// Handles cases of `.foo(Cu.reportError)`.
checkNodes = node.arguments.filter(n => isCuReportError(n));
}
for (let checkNode of checkNodes) {
context.report({ context.report({
node, node,
fix: fixer => { fix: fixer => {
@@ -129,6 +123,7 @@ module.exports = {
}, },
messageId: "useConsoleError", messageId: "useConsoleError",
}); });
}
}, },
}; };
}, },

View File

@@ -39,11 +39,21 @@ ruleTester.run("no-cu-reportError", rule, {
output: "console.error(bar)", output: "console.error(bar)",
errors: callError(), errors: callError(),
}, },
{
code: "Cu.reportError(bar.stack)",
output: "console.error(bar.stack)",
errors: callError(),
},
{ {
code: "foo().catch(Cu.reportError)", code: "foo().catch(Cu.reportError)",
output: "foo().catch(console.error)", output: "foo().catch(console.error)",
errors: callError(), errors: callError(),
}, },
{
code: "foo().then(bar, Cu.reportError)",
output: "foo().then(bar, console.error)",
errors: callError(),
},
// When referencing identifiers/members, try to reference them rather // When referencing identifiers/members, try to reference them rather
// than stringifying: // than stringifying:
{ {