node/tools/eslint/lib/rules/no-restricted-globals.js
Rich Trott dd5a4e1d75
tools: update ESLint to current version
We have been stalled on ESLint 3.8.0 for some time. Current ESLint is
3.13.0. We have been unable to upgrade because of more aggressive
reporting on some rules, including indentation.

ESLint configuration options and bugfixes are now such that we can
reasonably upgrade.

PR-URL: https://github.com/nodejs/node/pull/10561
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
2017-01-30 12:08:27 -05:00

79 lines
2.3 KiB
JavaScript

/**
* @fileoverview Restrict usage of specified globals.
* @author Benoît Zugmeyer
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: "disallow specified global variables",
category: "Variables",
recommended: false
},
schema: {
type: "array",
items: {
type: "string"
},
uniqueItems: true
}
},
create(context) {
const restrictedGlobals = context.options;
// if no globals are restricted we don't need to check
if (restrictedGlobals.length === 0) {
return {};
}
/**
* Report a variable to be used as a restricted global.
* @param {Reference} reference the variable reference
* @returns {void}
* @private
*/
function reportReference(reference) {
context.report({ node: reference.identifier, message: "Unexpected use of '{{name}}'.", data: {
name: reference.identifier.name
} });
}
/**
* Check if the given name is a restricted global name.
* @param {string} name name of a variable
* @returns {boolean} whether the variable is a restricted global or not
* @private
*/
function isRestricted(name) {
return restrictedGlobals.indexOf(name) >= 0;
}
return {
Program() {
const scope = context.getScope();
// Report variables declared elsewhere (ex: variables defined as "global" by eslint)
scope.variables.forEach(variable => {
if (!variable.defs.length && isRestricted(variable.name)) {
variable.references.forEach(reportReference);
}
});
// Report variables not declared at all
scope.through.forEach(reference => {
if (isRestricted(reference.identifier.name)) {
reportReference(reference);
}
});
}
};
}
};