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 1344711 - Add an eslint rule to report an error when a get*Pref c…
…all is the only instruction in a try block, r=jaws. --HG-- extra : rebase_source : b98fc9c75089c3eeb2f1317623b08ee9cd4d1541
- Loading branch information
Showing
8 changed files
with
97 additions
and
18 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
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
47 changes: 47 additions & 0 deletions
47
tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-default-preference-values.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,47 @@ | ||
/** | ||
* @fileoverview Require providing a second parameter to get*Pref | ||
* methods instead of using a try/catch block. | ||
* | ||
* 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 { | ||
"TryStatement": function(node) { | ||
let types = ["Bool", "Char", "Float", "Int"]; | ||
let methods = types.map(type => "get" + type + "Pref"); | ||
if (node.block.type != "BlockStatement" || | ||
node.block.body.length != 1) { | ||
return; | ||
} | ||
|
||
let firstStm = node.block.body[0]; | ||
if (firstStm.type != "ExpressionStatement" || | ||
firstStm.expression.type != "AssignmentExpression" || | ||
firstStm.expression.right.type != "CallExpression" || | ||
firstStm.expression.right.callee.type != "MemberExpression" || | ||
firstStm.expression.right.callee.property.type != "Identifier" || | ||
!methods.includes(firstStm.expression.right.callee.property.name)) { | ||
return; | ||
} | ||
|
||
let msg = "provide a default value instead of using a try/catch block"; | ||
context.report(node, msg); | ||
} | ||
}; | ||
}; |
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
38 changes: 38 additions & 0 deletions
38
tools/lint/eslint/eslint-plugin-mozilla/tests/use-default-preference-values.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,38 @@ | ||
/* Any copyright is dedicated to the Public Domain. | ||
* http://creativecommons.org/publicdomain/zero/1.0/ */ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var rule = require("../lib/rules/use-default-preference-values"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
function invalidCode(code) { | ||
let message = "provide a default value instead of using a try/catch block"; | ||
return {code: code, errors: [{message: message, type: "TryStatement"}]}; | ||
} | ||
|
||
let types = ["Bool", "Char", "Float", "Int"]; | ||
let methods = types.map(type => "get" + type + "Pref"); | ||
|
||
exports.runTest = function(ruleTester) { | ||
ruleTester.run("use-ownerGlobal", rule, { | ||
valid: [].concat( | ||
methods.map(m => "blah = branch." + m + "('blah', true);"), | ||
methods.map(m => "blah = branch." + m + "('blah');"), | ||
methods.map(m => "try { canThrow();" + | ||
" blah = branch." + m + "('blah'); } catch(e) {}") | ||
), | ||
|
||
invalid: [].concat( | ||
methods.map(m => | ||
invalidCode("try { blah = branch." + m + "('blah'); } catch(e) {}")) | ||
) | ||
}); | ||
}; |