github-pages-deploy-action/node_modules/eslint/lib/rules/no-implicit-globals.js

141 lines
5.2 KiB
JavaScript
Raw Permalink Normal View History

2020-03-07 11:45:40 +08:00
/**
2020-03-31 20:40:00 +08:00
* @fileoverview Rule to check for implicit global variables, functions and classes.
2020-03-07 11:45:40 +08:00
* @author Joshua Peek
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "suggestion",
docs: {
2020-03-31 20:40:00 +08:00
description: "disallow declarations in the global scope",
2020-03-07 11:45:40 +08:00
category: "Best Practices",
recommended: false,
url: "https://eslint.org/docs/rules/no-implicit-globals"
},
2020-03-31 20:40:00 +08:00
schema: [{
type: "object",
properties: {
lexicalBindings: {
type: "boolean",
default: false
}
},
additionalProperties: false
}],
messages: {
globalNonLexicalBinding: "Unexpected {{kind}} declaration in the global scope, wrap in an IIFE for a local variable, assign as global property for a global variable.",
globalLexicalBinding: "Unexpected {{kind}} declaration in the global scope, wrap in a block or in an IIFE.",
globalVariableLeak: "Global variable leak, declare the variable if it is intended to be local.",
assignmentToReadonlyGlobal: "Unexpected assignment to read-only global variable.",
redeclarationOfReadonlyGlobal: "Unexpected redeclaration of read-only global variable."
}
2020-03-07 11:45:40 +08:00
},
create(context) {
2020-03-31 20:40:00 +08:00
const checkLexicalBindings = context.options[0] && context.options[0].lexicalBindings === true;
/**
* Reports the node.
* @param {ASTNode} node Node to report.
* @param {string} messageId Id of the message to report.
* @param {string|undefined} kind Declaration kind, can be 'var', 'const', 'let', function or class.
* @returns {void}
*/
function report(node, messageId, kind) {
context.report({
node,
messageId,
data: {
kind
}
});
}
2020-03-07 11:45:40 +08:00
return {
Program() {
const scope = context.getScope();
scope.variables.forEach(variable => {
2020-03-31 20:40:00 +08:00
// Only ESLint global variables have the `writable` key.
const isReadonlyEslintGlobalVariable = variable.writeable === false;
const isWritableEslintGlobalVariable = variable.writeable === true;
if (isWritableEslintGlobalVariable) {
// Everything is allowed with writable ESLint global variables.
2020-03-07 11:45:40 +08:00
return;
}
variable.defs.forEach(def => {
2020-03-31 20:40:00 +08:00
const defNode = def.node;
2020-03-07 11:45:40 +08:00
if (def.type === "FunctionName" || (def.type === "Variable" && def.parent.kind === "var")) {
2020-03-31 20:40:00 +08:00
if (isReadonlyEslintGlobalVariable) {
report(defNode, "redeclarationOfReadonlyGlobal");
} else {
report(
defNode,
"globalNonLexicalBinding",
def.type === "FunctionName" ? "function" : `'${def.parent.kind}'`
);
}
}
if (checkLexicalBindings) {
if (def.type === "ClassName" ||
(def.type === "Variable" && (def.parent.kind === "let" || def.parent.kind === "const"))) {
if (isReadonlyEslintGlobalVariable) {
report(defNode, "redeclarationOfReadonlyGlobal");
} else {
report(
defNode,
"globalLexicalBinding",
def.type === "ClassName" ? "class" : `'${def.parent.kind}'`
);
}
}
2020-03-07 11:45:40 +08:00
}
});
});
2020-03-31 20:40:00 +08:00
// Undeclared assigned variables.
2020-03-07 11:45:40 +08:00
scope.implicit.variables.forEach(variable => {
const scopeVariable = scope.set.get(variable.name);
2020-03-31 20:40:00 +08:00
let messageId;
2020-03-07 11:45:40 +08:00
2020-03-31 20:40:00 +08:00
if (scopeVariable) {
// ESLint global variable
if (scopeVariable.writeable) {
return;
}
messageId = "assignmentToReadonlyGlobal";
} else {
// Reference to an unknown variable, possible global leak.
messageId = "globalVariableLeak";
2020-03-07 11:45:40 +08:00
}
2020-03-31 20:40:00 +08:00
// def.node is an AssignmentExpression, ForInStatement or ForOfStatement.
2020-03-07 11:45:40 +08:00
variable.defs.forEach(def => {
2020-03-31 20:40:00 +08:00
report(def.node, messageId);
2020-03-07 11:45:40 +08:00
});
});
}
};
}
};