Bug 1225289 - Make eslint plugin code conform to .eslintrc r=pbrosset
Added use strict statements now that we are no longer set up for ES6 modules.
--- a/testing/eslint-plugin-mozilla/lib/helpers.js
+++ b/testing/eslint-plugin-mozilla/lib/helpers.js
@@ -161,37 +161,37 @@ module.exports = {
*/
getPermissiveConfig: function() {
return {
range: true,
loc: true,
tolerant: true,
ecmaFeatures: {
arrowFunctions: true,
+ binaryLiterals: true,
blockBindings: true,
+ classes: true,
+ defaultParams: true,
destructuring: true,
- regexYFlag: true,
- regexUFlag: true,
- templateStrings: true,
- binaryLiterals: true,
- octalLiterals: true,
- unicodeCodePointEscapes: true,
- defaultParams: true,
- restParams: true,
forOf: true,
+ generators: true,
+ globalReturn: true,
+ modules: true,
objectLiteralComputedProperties: true,
+ objectLiteralDuplicateProperties: true,
objectLiteralShorthandMethods: true,
objectLiteralShorthandProperties: true,
- objectLiteralDuplicateProperties: true,
- generators: true,
+ octalLiterals: true,
+ regexUFlag: true,
+ regexYFlag: true,
+ restParams: true,
spread: true,
superInFunctions: true,
- classes: true,
- modules: true,
- globalReturn: true
+ templateStrings: true,
+ unicodeCodePointEscapes: true,
}
};
},
/**
* Check whether the context is the global scope.
*
* @param {ASTContext} context
--- a/testing/eslint-plugin-mozilla/lib/index.js
+++ b/testing/eslint-plugin-mozilla/lib/index.js
@@ -1,15 +1,16 @@
/**
* @fileoverview A collection of rules that help enforce JavaScript coding
* standard and avoid common errors in the Mozilla project.
* 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";
//------------------------------------------------------------------------------
// Plugin Definition
//------------------------------------------------------------------------------
module.exports = {
rules: {
--- a/testing/eslint-plugin-mozilla/lib/rules/balanced-listeners.js
+++ b/testing/eslint-plugin-mozilla/lib/rules/balanced-listeners.js
@@ -4,26 +4,27 @@
* Note that for now, this rule is rather simple in that it only checks that
* for each event name there is both an add and remove listener. It doesn't
* check that these are called on the right objects or with the same callback.
*
* 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
-//------------------------------------------------------------------------------
+// -----------------------------------------------------------------------------
module.exports = function(context) {
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
// Helpers
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
var DICTIONARY = {
"addEventListener": "removeEventListener",
"on": "off"
};
// Invert this dictionary to make it easy later.
var INVERTED_DICTIONARY = {};
for (var i in DICTIONARY) {
@@ -49,42 +50,42 @@ module.exports = function(context) {
type: node.arguments[0].value,
useCapture: node.arguments[2] ? node.arguments[2].value : null
});
}
function getUnbalancedListeners() {
var unbalanced = [];
- for (var i = 0; i < addedListeners.length; i ++) {
- if (!hasRemovedListener(addedListeners[i])) {
- unbalanced.push(addedListeners[i]);
+ for (var j = 0; j < addedListeners.length; j++) {
+ if (!hasRemovedListener(addedListeners[j])) {
+ unbalanced.push(addedListeners[j]);
}
}
addedListeners = removedListeners = [];
return unbalanced;
}
function hasRemovedListener(addedListener) {
- for (var i = 0; i < removedListeners.length; i ++) {
- var listener = removedListeners[i];
+ for (var k = 0; k < removedListeners.length; k++) {
+ var listener = removedListeners[k];
if (DICTIONARY[addedListener.functionName] === listener.functionName &&
addedListener.type === listener.type &&
addedListener.useCapture === listener.useCapture) {
return true;
}
}
return false;
}
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
// Public
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
return {
CallExpression: function(node) {
if (node.callee.type === "MemberExpression") {
var listenerMethodName = node.callee.property.name;
if (DICTIONARY.hasOwnProperty(listenerMethodName)) {
addAddedListener(node);
@@ -92,16 +93,17 @@ module.exports = function(context) {
addRemovedListener(node);
}
}
},
"Program:exit": function() {
getUnbalancedListeners().forEach(function(listener) {
context.report(listener.node,
- "No corresponding '{{functionName}}({{type}})' was found.", {
- functionName: DICTIONARY[listener.functionName],
- type: listener.type
- });
+ "No corresponding '{{functionName}}({{type}})' was found.",
+ {
+ functionName: DICTIONARY[listener.functionName],
+ type: listener.type
+ });
});
}
};
};
--- a/testing/eslint-plugin-mozilla/lib/rules/components-imports.js
+++ b/testing/eslint-plugin-mozilla/lib/rules/components-imports.js
@@ -1,29 +1,29 @@
/**
* @fileoverview Adds the filename of imported files e.g.
* Cu.import("some/path/Blah.jsm") adds Blah to the current scope.
*
* 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 path = require("path");
var helpers = require("../helpers");
module.exports = function(context) {
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
// Public
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
return {
ExpressionStatement: function(node) {
var source = helpers.getSource(node, context);
var name = helpers.getVarNameFromImportSource(source);
if (name) {
helpers.addVarToScope(name, context);
--- a/testing/eslint-plugin-mozilla/lib/rules/import-headjs-globals.js
+++ b/testing/eslint-plugin-mozilla/lib/rules/import-headjs-globals.js
@@ -1,90 +1,94 @@
/**
* @fileoverview Import globals from head.js and from any files that were
* imported by head.js (as far as we can correctly resolve the path).
*
* 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 fs = require("fs");
var path = require("path");
var helpers = require("../helpers");
+var importRx = /^(?:Services\.scriptloader\.|loader)?loadSubScript\((.+)[",]/;
module.exports = function(context) {
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
// Helpers
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
function checkFile(fileArray) {
var filePath = fileArray.pop();
while (filePath) {
var headText;
try {
headText = fs.readFileSync(filePath, "utf8");
- } catch(e) {
+ } catch (e) {
// Couldn't find file, continue.
filePath = fileArray.pop();
continue;
}
var ast = helpers.getAST(headText);
var globalVars = helpers.getGlobals(ast);
for (var i = 0; i < globalVars.length; i++) {
- var name = globalVars[i];
- helpers.addVarToScope(name, context);
+ var varName = globalVars[i];
+ helpers.addVarToScope(varName, context);
}
for (var index in ast.body) {
var node = ast.body[index];
var source = helpers.getTextForNode(node, headText);
var name = helpers.getVarNameFromImportSource(source);
if (name) {
helpers.addVarToScope(name, context);
continue;
}
// Scripts loaded using loadSubScript or loadHelperScript
- var matches =
- source.match(/^(?:Services\.scriptloader\.|loader)?loadSubScript\((.+)[",]/);
+ var matches = source.match(importRx);
+
if (!matches) {
matches = source.match(/^loadHelperScript\((.+)[",]/);
}
+
if (matches) {
var cwd = process.cwd();
filePath = matches[1];
- filePath = filePath.replace("chrome://mochitests/content/browser", cwd + "/../../../..");
+ filePath = filePath.replace("chrome://mochitests/content/browser",
+ cwd + "/../../../..");
filePath = filePath.replace(/testdir\s*\+\s*["']/gi, cwd + "/");
filePath = filePath.replace(/test_dir\s*\+\s*["']/gi, cwd);
filePath = filePath.replace(/["']/gi, "");
filePath = path.normalize(filePath);
fileArray.push(filePath);
}
}
filePath = fileArray.pop();
}
}
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
// Public
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
return {
Program: function() {
if (!helpers.getIsBrowserMochitest(this)) {
return;
}
var testPath = this.getFilename();
--- a/testing/eslint-plugin-mozilla/lib/rules/mark-test-function-used.js
+++ b/testing/eslint-plugin-mozilla/lib/rules/mark-test-function-used.js
@@ -1,31 +1,32 @@
/**
* @fileoverview Simply marks test (the test method) as used. This avoids ESLint
* telling us that the function is never called..
*
* 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 {
- Program: function(node) {
+ Program: function() {
if (!helpers.getIsBrowserMochitest(this)) {
return;
}
context.markVariableAsUsed("test");
}
};
};
--- a/testing/eslint-plugin-mozilla/lib/rules/no-aArgs.js
+++ b/testing/eslint-plugin-mozilla/lib/rules/no-aArgs.js
@@ -1,50 +1,55 @@
/**
* @fileoverview warns against using hungarian notation in function arguments
* (i.e. aArg).
*
* 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
-//------------------------------------------------------------------------------
+// -----------------------------------------------------------------------------
module.exports = function(context) {
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
// Helpers
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
function isPrefixed(name) {
return name.length >= 2 && /^a[A-Z]/.test(name);
}
function deHungarianize(name) {
return name.substring(1, 2).toLowerCase() +
name.substring(2, name.length);
}
function checkFunction(node) {
- for (var i = 0; i < node.params.length; i ++) {
+ for (var i = 0; i < node.params.length; i++) {
var param = node.params[i];
if (param.name && isPrefixed(param.name)) {
- context.report(param, "Parameter '{{name}}' uses Hungarian Notation, consider using '{{suggestion}}' instead.", {
+ var errorObj = {
name: param.name,
suggestion: deHungarianize(param.name)
- });
+ };
+ context.report(param,
+ "Parameter '{{name}}' uses Hungarian Notation, " +
+ "consider using '{{suggestion}}' instead.",
+ errorObj);
}
}
}
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
// Public
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
return {
"FunctionDeclaration": checkFunction,
"ArrowFunctionExpression": checkFunction,
"FunctionExpression": checkFunction
};
};
--- a/testing/eslint-plugin-mozilla/lib/rules/no-cpows-in-tests.js
+++ b/testing/eslint-plugin-mozilla/lib/rules/no-cpows-in-tests.js
@@ -1,83 +1,84 @@
/**
* @fileoverview Prevent access to CPOWs in browser mochitests.
*
* 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");
var cpows = [
/^gBrowser\.contentWindow/,
/^gBrowser\.contentDocument/,
/^gBrowser\.selectedBrowser.contentWindow/,
/^browser\.contentDocument/,
/^window\.content/
];
module.exports = function(context) {
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
// Helpers
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
- function showError(context, node, identifier) {
+ function showError(node, identifier) {
context.report({
node: node,
- message: identifier + " is a possible Cross Process Object Wrapper (CPOW)."
+ message: identifier +
+ " is a possible Cross Process Object Wrapper (CPOW)."
});
}
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
// Public
- //--------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
return {
MemberExpression: function(node) {
if (!helpers.getIsBrowserMochitest(this)) {
return;
}
var expression = context.getSource(node);
cpows.some(function(cpow) {
if (cpow.test(expression)) {
- showError(context, node, expression);
+ showError(node, expression);
return true;
}
return false;
});
if (helpers.getIsGlobalScope(context)) {
if (/^content\./.test(expression)) {
- showError(context, node, expression);
+ showError(node, expression);
return;
}
}
},
Identifier: function(node) {
if (!helpers.getIsBrowserMochitest(this)) {
return;
}
var expression = context.getSource(node);
if (expression == "content" || /^content\./.test(expression)) {
-
if (node.parent.type === "MemberExpression" &&
node.parent.object &&
node.parent.object.type === "Identifier" &&
node.parent.object.name != "content") {
return;
}
- showError(context, node, expression);
+ showError(node, expression);
return;
}
}
};
};
--- a/testing/eslint-plugin-mozilla/lib/rules/var-only-at-top-level.js
+++ b/testing/eslint-plugin-mozilla/lib/rules/var-only-at-top-level.js
@@ -1,35 +1,33 @@
/**
* @fileoverview Marks all var declarations that are not at the top level
* invalid.
*
* 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 {
"VariableDeclaration": function(node) {
if (node.kind === "var") {
- var ancestors = context.getAncestors();
- var parent = ancestors.pop();
-
if (helpers.getIsGlobalScope(context)) {
return;
}
context.report(node, "Unexpected var, use let or const instead.");
}
}
};