github-pages-deploy-action/node_modules/eslint/lib/rules/no-restricted-modules.js

215 lines
6.9 KiB
JavaScript
Raw Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview Restrict usage of specified node modules.
* @author Christian Schulz
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const ignore = require("ignore");
const arrayOfStrings = {
type: "array",
items: { type: "string" },
uniqueItems: true
};
const arrayOfStringsOrObjects = {
type: "array",
items: {
anyOf: [
{ type: "string" },
{
type: "object",
properties: {
name: { type: "string" },
message: {
type: "string",
minLength: 1
}
},
additionalProperties: false,
required: ["name"]
}
]
},
uniqueItems: true
};
module.exports = {
meta: {
2020-05-15 05:33:08 +08:00
deprecated: true,
2020-06-06 22:11:37 +08:00
replacedBy: [],
2020-05-15 05:33:08 +08:00
2020-03-07 11:45:40 +08:00
type: "suggestion",
docs: {
description: "disallow specified modules when loaded by `require`",
category: "Node.js and CommonJS",
recommended: false,
url: "https://eslint.org/docs/rules/no-restricted-modules"
},
schema: {
anyOf: [
arrayOfStringsOrObjects,
{
type: "array",
items: {
type: "object",
properties: {
paths: arrayOfStringsOrObjects,
patterns: arrayOfStrings
},
additionalProperties: false
},
additionalItems: false
}
]
2020-05-15 05:33:08 +08:00
},
messages: {
defaultMessage: "'{{name}}' module is restricted from being used.",
// eslint-disable-next-line eslint-plugin/report-message-format
customMessage: "'{{name}}' module is restricted from being used. {{customMessage}}",
patternMessage: "'{{name}}' module is restricted from being used by a pattern."
2020-03-07 11:45:40 +08:00
}
},
create(context) {
const options = Array.isArray(context.options) ? context.options : [];
const isPathAndPatternsObject =
typeof options[0] === "object" &&
(Object.prototype.hasOwnProperty.call(options[0], "paths") || Object.prototype.hasOwnProperty.call(options[0], "patterns"));
const restrictedPaths = (isPathAndPatternsObject ? options[0].paths : context.options) || [];
const restrictedPatterns = (isPathAndPatternsObject ? options[0].patterns : []) || [];
const restrictedPathMessages = restrictedPaths.reduce((memo, importName) => {
if (typeof importName === "string") {
memo[importName] = null;
} else {
memo[importName.name] = importName.message;
}
return memo;
}, {});
// if no imports are restricted we don"t need to check
if (Object.keys(restrictedPaths).length === 0 && restrictedPatterns.length === 0) {
return {};
}
const ig = ignore().add(restrictedPatterns);
/**
* Function to check if a node is a string literal.
* @param {ASTNode} node The node to check.
* @returns {boolean} If the node is a string literal.
*/
2020-05-15 05:33:08 +08:00
function isStringLiteral(node) {
2020-03-07 11:45:40 +08:00
return node && node.type === "Literal" && typeof node.value === "string";
}
2020-05-15 05:33:08 +08:00
/**
* Function to check if a node is a static string template literal.
* @param {ASTNode} node The node to check.
* @returns {boolean} If the node is a string template literal.
*/
function isStaticTemplateLiteral(node) {
return node && node.type === "TemplateLiteral" && node.expressions.length === 0;
}
2020-03-07 11:45:40 +08:00
/**
* Function to check if a node is a require call.
* @param {ASTNode} node The node to check.
* @returns {boolean} If the node is a require call.
*/
function isRequireCall(node) {
return node.callee.type === "Identifier" && node.callee.name === "require";
}
2020-05-15 05:33:08 +08:00
/**
* Extract string from Literal or TemplateLiteral node
* @param {ASTNode} node The node to extract from
* @returns {string|null} Extracted string or null if node doesn't represent a string
*/
function getFirstArgumentString(node) {
if (isStringLiteral(node)) {
return node.value.trim();
}
if (isStaticTemplateLiteral(node)) {
return node.quasis[0].value.cooked.trim();
}
return null;
}
2020-03-07 11:45:40 +08:00
/**
* Report a restricted path.
* @param {node} node representing the restricted path reference
2020-05-15 05:33:08 +08:00
* @param {string} name restricted path
2020-03-07 11:45:40 +08:00
* @returns {void}
* @private
*/
2020-05-15 05:33:08 +08:00
function reportPath(node, name) {
const customMessage = restrictedPathMessages[name];
const messageId = customMessage
? "customMessage"
: "defaultMessage";
2020-03-07 11:45:40 +08:00
context.report({
node,
2020-05-15 05:33:08 +08:00
messageId,
2020-03-07 11:45:40 +08:00
data: {
2020-05-15 05:33:08 +08:00
name,
2020-03-07 11:45:40 +08:00
customMessage
}
});
}
/**
* Check if the given name is a restricted path name
* @param {string} name name of a variable
* @returns {boolean} whether the variable is a restricted path or not
* @private
*/
function isRestrictedPath(name) {
return Object.prototype.hasOwnProperty.call(restrictedPathMessages, name);
}
return {
CallExpression(node) {
if (isRequireCall(node)) {
2020-05-15 05:33:08 +08:00
// node has arguments
if (node.arguments.length) {
const name = getFirstArgumentString(node.arguments[0]);
// if first argument is a string literal or a static string template literal
if (name) {
// check if argument value is in restricted modules array
if (isRestrictedPath(name)) {
reportPath(node, name);
}
if (restrictedPatterns.length > 0 && ig.ignores(name)) {
context.report({
node,
messageId: "patternMessage",
data: { name }
});
}
2020-03-07 11:45:40 +08:00
}
}
}
}
};
}
};