[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Restrict usage of specified node modules.
|
---|
| 3 | * @author Christian Schulz
|
---|
| 4 | * @deprecated in ESLint v7.0.0
|
---|
| 5 | */
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | //------------------------------------------------------------------------------
|
---|
| 9 | // Requirements
|
---|
| 10 | //------------------------------------------------------------------------------
|
---|
| 11 |
|
---|
| 12 | const astUtils = require("./utils/ast-utils");
|
---|
| 13 |
|
---|
| 14 | //------------------------------------------------------------------------------
|
---|
| 15 | // Rule Definition
|
---|
| 16 | //------------------------------------------------------------------------------
|
---|
| 17 |
|
---|
| 18 | const ignore = require("ignore");
|
---|
| 19 |
|
---|
| 20 | const arrayOfStrings = {
|
---|
| 21 | type: "array",
|
---|
| 22 | items: { type: "string" },
|
---|
| 23 | uniqueItems: true
|
---|
| 24 | };
|
---|
| 25 |
|
---|
| 26 | const arrayOfStringsOrObjects = {
|
---|
| 27 | type: "array",
|
---|
| 28 | items: {
|
---|
| 29 | anyOf: [
|
---|
| 30 | { type: "string" },
|
---|
| 31 | {
|
---|
| 32 | type: "object",
|
---|
| 33 | properties: {
|
---|
| 34 | name: { type: "string" },
|
---|
| 35 | message: {
|
---|
| 36 | type: "string",
|
---|
| 37 | minLength: 1
|
---|
| 38 | }
|
---|
| 39 | },
|
---|
| 40 | additionalProperties: false,
|
---|
| 41 | required: ["name"]
|
---|
| 42 | }
|
---|
| 43 | ]
|
---|
| 44 | },
|
---|
| 45 | uniqueItems: true
|
---|
| 46 | };
|
---|
| 47 |
|
---|
| 48 | /** @type {import('../shared/types').Rule} */
|
---|
| 49 | module.exports = {
|
---|
| 50 | meta: {
|
---|
| 51 | deprecated: true,
|
---|
| 52 |
|
---|
| 53 | replacedBy: [],
|
---|
| 54 |
|
---|
| 55 | type: "suggestion",
|
---|
| 56 |
|
---|
| 57 | docs: {
|
---|
| 58 | description: "Disallow specified modules when loaded by `require`",
|
---|
| 59 | recommended: false,
|
---|
| 60 | url: "https://eslint.org/docs/latest/rules/no-restricted-modules"
|
---|
| 61 | },
|
---|
| 62 |
|
---|
| 63 | schema: {
|
---|
| 64 | anyOf: [
|
---|
| 65 | arrayOfStringsOrObjects,
|
---|
| 66 | {
|
---|
| 67 | type: "array",
|
---|
| 68 | items: {
|
---|
| 69 | type: "object",
|
---|
| 70 | properties: {
|
---|
| 71 | paths: arrayOfStringsOrObjects,
|
---|
| 72 | patterns: arrayOfStrings
|
---|
| 73 | },
|
---|
| 74 | additionalProperties: false
|
---|
| 75 | },
|
---|
| 76 | additionalItems: false
|
---|
| 77 | }
|
---|
| 78 | ]
|
---|
| 79 | },
|
---|
| 80 |
|
---|
| 81 | messages: {
|
---|
| 82 | defaultMessage: "'{{name}}' module is restricted from being used.",
|
---|
| 83 | // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
|
---|
| 84 | customMessage: "'{{name}}' module is restricted from being used. {{customMessage}}",
|
---|
| 85 | patternMessage: "'{{name}}' module is restricted from being used by a pattern."
|
---|
| 86 | }
|
---|
| 87 | },
|
---|
| 88 |
|
---|
| 89 | create(context) {
|
---|
| 90 | const options = Array.isArray(context.options) ? context.options : [];
|
---|
| 91 | const isPathAndPatternsObject =
|
---|
| 92 | typeof options[0] === "object" &&
|
---|
| 93 | (Object.prototype.hasOwnProperty.call(options[0], "paths") || Object.prototype.hasOwnProperty.call(options[0], "patterns"));
|
---|
| 94 |
|
---|
| 95 | const restrictedPaths = (isPathAndPatternsObject ? options[0].paths : context.options) || [];
|
---|
| 96 | const restrictedPatterns = (isPathAndPatternsObject ? options[0].patterns : []) || [];
|
---|
| 97 |
|
---|
| 98 | const restrictedPathMessages = restrictedPaths.reduce((memo, importName) => {
|
---|
| 99 | if (typeof importName === "string") {
|
---|
| 100 | memo[importName] = null;
|
---|
| 101 | } else {
|
---|
| 102 | memo[importName.name] = importName.message;
|
---|
| 103 | }
|
---|
| 104 | return memo;
|
---|
| 105 | }, {});
|
---|
| 106 |
|
---|
| 107 | // if no imports are restricted we don't need to check
|
---|
| 108 | if (Object.keys(restrictedPaths).length === 0 && restrictedPatterns.length === 0) {
|
---|
| 109 | return {};
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | // relative paths are supported for this rule
|
---|
| 113 | const ig = ignore({ allowRelativePaths: true }).add(restrictedPatterns);
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 | /**
|
---|
| 117 | * Function to check if a node is a string literal.
|
---|
| 118 | * @param {ASTNode} node The node to check.
|
---|
| 119 | * @returns {boolean} If the node is a string literal.
|
---|
| 120 | */
|
---|
| 121 | function isStringLiteral(node) {
|
---|
| 122 | return node && node.type === "Literal" && typeof node.value === "string";
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * Function to check if a node is a require call.
|
---|
| 127 | * @param {ASTNode} node The node to check.
|
---|
| 128 | * @returns {boolean} If the node is a require call.
|
---|
| 129 | */
|
---|
| 130 | function isRequireCall(node) {
|
---|
| 131 | return node.callee.type === "Identifier" && node.callee.name === "require";
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * Extract string from Literal or TemplateLiteral node
|
---|
| 136 | * @param {ASTNode} node The node to extract from
|
---|
| 137 | * @returns {string|null} Extracted string or null if node doesn't represent a string
|
---|
| 138 | */
|
---|
| 139 | function getFirstArgumentString(node) {
|
---|
| 140 | if (isStringLiteral(node)) {
|
---|
| 141 | return node.value.trim();
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | if (astUtils.isStaticTemplateLiteral(node)) {
|
---|
| 145 | return node.quasis[0].value.cooked.trim();
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | return null;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /**
|
---|
| 152 | * Report a restricted path.
|
---|
| 153 | * @param {node} node representing the restricted path reference
|
---|
| 154 | * @param {string} name restricted path
|
---|
| 155 | * @returns {void}
|
---|
| 156 | * @private
|
---|
| 157 | */
|
---|
| 158 | function reportPath(node, name) {
|
---|
| 159 | const customMessage = restrictedPathMessages[name];
|
---|
| 160 | const messageId = customMessage
|
---|
| 161 | ? "customMessage"
|
---|
| 162 | : "defaultMessage";
|
---|
| 163 |
|
---|
| 164 | context.report({
|
---|
| 165 | node,
|
---|
| 166 | messageId,
|
---|
| 167 | data: {
|
---|
| 168 | name,
|
---|
| 169 | customMessage
|
---|
| 170 | }
|
---|
| 171 | });
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /**
|
---|
| 175 | * Check if the given name is a restricted path name
|
---|
| 176 | * @param {string} name name of a variable
|
---|
| 177 | * @returns {boolean} whether the variable is a restricted path or not
|
---|
| 178 | * @private
|
---|
| 179 | */
|
---|
| 180 | function isRestrictedPath(name) {
|
---|
| 181 | return Object.prototype.hasOwnProperty.call(restrictedPathMessages, name);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | return {
|
---|
| 185 | CallExpression(node) {
|
---|
| 186 | if (isRequireCall(node)) {
|
---|
| 187 |
|
---|
| 188 | // node has arguments
|
---|
| 189 | if (node.arguments.length) {
|
---|
| 190 | const name = getFirstArgumentString(node.arguments[0]);
|
---|
| 191 |
|
---|
| 192 | // if first argument is a string literal or a static string template literal
|
---|
| 193 | if (name) {
|
---|
| 194 |
|
---|
| 195 | // check if argument value is in restricted modules array
|
---|
| 196 | if (isRestrictedPath(name)) {
|
---|
| 197 | reportPath(node, name);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | if (restrictedPatterns.length > 0 && ig.ignores(name)) {
|
---|
| 201 | context.report({
|
---|
| 202 | node,
|
---|
| 203 | messageId: "patternMessage",
|
---|
| 204 | data: { name }
|
---|
| 205 | });
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | };
|
---|
| 212 | }
|
---|
| 213 | };
|
---|