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 1330147 - add eslint rule to reject newURI(uri, null, null) and r…
…emoveObserver(notificationName, observer, false), r=jaws.
- Loading branch information
Showing
7 changed files
with
108 additions
and
2 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
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
50 changes: 50 additions & 0 deletions
50
tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-parameters.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 @@ | ||
/** | ||
* @fileoverview Reject common XPCOM methods called with useless optional | ||
* parameters, or non-existent parameters. | ||
* | ||
* 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 | ||
// ----------------------------------------------------------------------------- | ||
|
||
var helpers = require("../helpers"); | ||
|
||
module.exports = function(context) { | ||
|
||
// --------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
"CallExpression": function(node) { | ||
let callee = node.callee; | ||
if (callee.type !== "MemberExpression" || | ||
callee.property.type !== "Identifier") { | ||
return; | ||
} | ||
|
||
if (callee.property.name === "removeObserver" && | ||
node.arguments.length === 3) { | ||
let arg = node.arguments[2]; | ||
if (arg.type === "Literal" && (arg.value === false || | ||
arg.value === true)) { | ||
context.report(node, "removeObserver only takes 2 parameters."); | ||
} | ||
} | ||
|
||
if (callee.property.name === "newURI" && | ||
node.arguments.length === 3) { | ||
let arg = node.arguments[2]; | ||
if (arg.type === "Literal" && arg.value === null) { | ||
context.report(node, "newURI's optional parameters passed as null."); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
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
46 changes: 46 additions & 0 deletions
46
tools/lint/eslint/eslint-plugin-mozilla/tests/no-useless-parameters.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,46 @@ | ||
/* Any copyright is dedicated to the Public Domain. | ||
* http://creativecommons.org/publicdomain/zero/1.0/ */ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var rule = require("../lib/rules/no-useless-parameters"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
function callError(message) { | ||
return [{message: message, type: "CallExpression"}]; | ||
} | ||
|
||
exports.runTest = function(ruleTester) { | ||
ruleTester.run("no-useless-parameters", rule, { | ||
valid: [ | ||
"Services.removeObserver('notification name', {});", | ||
"Services.io.newURI('http://example.com');", | ||
"Services.io.newURI('http://example.com', 'utf8');", | ||
], | ||
invalid: [ | ||
{ | ||
code: "Services.removeObserver('notification name', {}, false);", | ||
errors: callError("removeObserver only takes 2 parameters.") | ||
}, | ||
{ | ||
code: "Services.removeObserver('notification name', {}, true);", | ||
errors: callError("removeObserver only takes 2 parameters.") | ||
}, | ||
{ | ||
code: "Services.io.newURI('http://example.com', null, null);", | ||
errors: callError("newURI's optional parameters passed as null.") | ||
}, | ||
{ | ||
code: "Services.io.newURI('http://example.com', 'utf8', null);", | ||
errors: callError("newURI's optional parameters passed as null.") | ||
} | ||
] | ||
}); | ||
}; |
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