Bug 1264649 - add reject-some-requires eslint rule; r=pbro

MozReview-Commit-ID: FVxy2c5Wsgg

--HG--
extra : rebase_source : 901f8aee971f9ab48cef7eceffb4cfc8ad567822
This commit is contained in:
Tom Tromey 2016-07-21 08:39:29 -06:00
parent a3e240204c
commit 8cd914069c
8 changed files with 77 additions and 11 deletions

View File

@ -32,6 +32,9 @@ second argument (meaning they add the exported properties into global scope).
"Cu.importGlobalProperties". Use of this function is undesirable in
some parts of the tree.
``reject-some-requires`` rejects some calls to ``require``, according
to a regexp passed in as an option.
``this-top-level-scope`` treats top-level assignments like
``this.mumble = value`` as declaring a global.
@ -79,5 +82,6 @@ Example configuration::
no-aArgs
no-cpows-in-tests
reject-importGlobalProperties
reject-some-requires
this-top-level-scope
var-only-at-top-level

View File

@ -0,0 +1,12 @@
.. _reject-some-requires:
====================
reject-some-requires
====================
Rule Details
------------
This takes an option, a regular expression. Invocations of
``require`` with a string literal argument are matched against this
regexp; and if it matches, the ``require`` use is flagged.

View File

@ -26,6 +26,7 @@ module.exports = {
"no-cpows-in-tests": require("../lib/rules/no-cpows-in-tests"),
"no-single-arg-cu-import": require("../lib/rules/no-single-arg-cu-import"),
"reject-importGlobalProperties": require("../lib/rules/reject-importGlobalProperties"),
"reject-some-requires": require("../lib/rules/reject-some-requires"),
"var-only-at-top-level": require("../lib/rules/var-only-at-top-level")
},
rulesConfig: {
@ -38,6 +39,7 @@ module.exports = {
"no-cpows-in-tests": 0,
"no-single-arg-cu-import": 0,
"reject-importGlobalProperties": 0,
"reject-some-requires": 0,
"var-only-at-top-level": 0
}
};

View File

@ -0,0 +1,48 @@
/**
* @fileoverview Reject some uses of require.
*
* 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) {
// ---------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------
if (typeof(context.options[0]) !== "string") {
throw new Error("reject-some-requires expects a regexp");
}
const RX = new RegExp(context.options[0]);
const checkPath = function(node, path) {
if (RX.test(path)) {
context.report(node, `require(${path}) is not allowed`);
}
};
return {
"CallExpression": function(node) {
if (node.callee.type == "Identifier" &&
node.callee.name == "require" &&
node.arguments.length == 1 &&
node.arguments[0].type == "Literal") {
checkPath(node, node.arguments[0].value);
} else if (node.callee.type == "MemberExpression" &&
node.callee.property.type == "Identifier" &&
node.callee.property.name == "lazyRequireGetter" &&
node.arguments.length >= 3 &&
node.arguments[2].type == "Literal") {
checkPath(node, node.arguments[2].value);
}
}
};
};

View File

@ -1,6 +1,6 @@
{
"name": "eslint-plugin-mozilla",
"version": "0.1.1",
"version": "0.2.0",
"description": "A collection of rules that help enforce JavaScript coding standard in the Mozilla project.",
"keywords": [
"eslint",

View File

@ -1,8 +1,8 @@
[
{
"size": 2348527,
"size": 2349680,
"visibility": "public",
"digest": "2e6c1f35b178e2ee1055c89f020f6b3b88f310a4b63f2fbb2023016c3890f672f86f8e35f716745135740c59fdccd3ad46d48c7995e7d281aa19d74637caa405",
"digest": "2b02ae6dd4bc735990660f97a831f05e604c28120977e4120cf59619fb02be22cbd42be26ec2bd176f172f4566f3dfb445082e8d9651346662b8fb8fde407b8c",
"algorithm": "sha512",
"filename": "eslint.tar.gz"
}

View File

@ -56,9 +56,9 @@
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.4.1.tgz"
},
"brace-expansion": {
"version": "1.1.5",
"version": "1.1.6",
"from": "brace-expansion@>=1.0.0 <2.0.0",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.5.tgz"
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.6.tgz"
},
"caller-path": {
"version": "0.1.0",
@ -220,7 +220,7 @@
"resolved": "https://registry.npmjs.org/eslint-plugin-html/-/eslint-plugin-html-1.4.0.tgz"
},
"eslint-plugin-mozilla": {
"version": "0.1.1",
"version": "0.2.0",
"from": "eslint-plugin-mozilla",
"resolved": "file:eslint-plugin-mozilla",
"dependencies": {
@ -279,9 +279,9 @@
"resolved": "https://registry.npmjs.org/exit-hook/-/exit-hook-1.1.1.tgz"
},
"fast-levenshtein": {
"version": "1.1.3",
"version": "1.1.4",
"from": "fast-levenshtein@>=1.1.0 <2.0.0",
"resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-1.1.3.tgz"
"resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-1.1.4.tgz"
},
"figures": {
"version": "1.7.0",
@ -569,9 +569,9 @@
"resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-1.0.1.tgz"
},
"rimraf": {
"version": "2.5.3",
"version": "2.5.4",
"from": "rimraf@>=2.2.8 <3.0.0",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.5.3.tgz"
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.5.4.tgz"
},
"run-async": {
"version": "0.1.0",

View File

@ -32,7 +32,7 @@ here = os.path.abspath(os.path.dirname(__file__))
ESLINT_PACKAGES = [
"eslint@2.9.0",
"eslint-plugin-html@1.4.0",
"eslint-plugin-mozilla@0.1.1",
"eslint-plugin-mozilla@0.2.0",
"eslint-plugin-react@4.2.3"
]