UNPKG

eslint-plugin-boundaries

Version:

Eslint plugin checking architecture boundaries between elements

187 lines (186 loc) 7.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ELEMENT_DESCRIPTOR_MODES_MAP = exports.SETTINGS_KEYS_MAP = exports.SETTINGS = exports.DEPENDENCY_NODE_KEYS_MAP = exports.DEPENDENCY_NODE_EXPORT = exports.DEPENDENCY_NODE_DYNAMIC_IMPORT = exports.DEPENDENCY_NODE_IMPORT = exports.DEPENDENCY_NODE_REQUIRE = exports.IMPORT_KINDS_MAP = exports.IMPORT_KIND_VALUE = exports.IMPORT_KIND_TYPE = void 0; exports.isString = isString; exports.isImportKind = isImportKind; exports.isDependencyNodeKey = isDependencyNodeKey; exports.isSettingsKey = isSettingsKey; exports.isElementDescriptorMode = isElementDescriptorMode; const plugin_1 = require("./plugin"); const rules_1 = require("./rules"); function isString(object) { return typeof object === "string"; } exports.IMPORT_KIND_TYPE = "type"; exports.IMPORT_KIND_VALUE = "value"; /** * Map of the kinds of import, either a type import or a value import. */ exports.IMPORT_KINDS_MAP = { /** * Type import, e.g., `import type { X } from 'module'` */ TYPE: exports.IMPORT_KIND_TYPE, /** * Value import, e.g., `import x from 'module'` or `const x = require('module')` */ VALUE: exports.IMPORT_KIND_VALUE, }; exports.DEPENDENCY_NODE_REQUIRE = "require"; exports.DEPENDENCY_NODE_IMPORT = "import"; exports.DEPENDENCY_NODE_DYNAMIC_IMPORT = "dynamic-import"; exports.DEPENDENCY_NODE_EXPORT = "export"; /** * Type guard to check if a value is a valid ImportKind. * @param value The value to check. * @returns True if the value is a valid ImportKind, false otherwise. */ function isImportKind(value) { return (isString(value) && Object.values(exports.IMPORT_KINDS_MAP).includes(value)); } /** * Different types of dependency nodes supported by the plugin by default. * Each type corresponds to a common way of importing or requiring modules in JavaScript/TypeScript. */ exports.DEPENDENCY_NODE_KEYS_MAP = { /** * CommonJS require statements, e.g., `const module = require('module')`. */ REQUIRE: exports.DEPENDENCY_NODE_REQUIRE, /** * ES6 import statements, e.g., `import module from 'module'`. */ IMPORT: exports.DEPENDENCY_NODE_IMPORT, /** * Dynamic import statements, e.g., `import('module')`. */ DYNAMIC_IMPORT: exports.DEPENDENCY_NODE_DYNAMIC_IMPORT, /** * Export statements, e.g., `export { module } from 'source'`. */ EXPORT: exports.DEPENDENCY_NODE_EXPORT, }; /** * Type guard to check if a value is a valid DependencyNodeKey. * @param value The value to check. * @returns True if the value is a valid DependencyNodeKey, false otherwise. */ function isDependencyNodeKey(value) { return (isString(value) && Object.values(exports.DEPENDENCY_NODE_KEYS_MAP).includes(value)); } exports.SETTINGS = { // settings ELEMENTS: `${plugin_1.PLUGIN_NAME}/elements`, IGNORE: `${plugin_1.PLUGIN_NAME}/ignore`, INCLUDE: `${plugin_1.PLUGIN_NAME}/include`, ROOT_PATH: `${plugin_1.PLUGIN_NAME}/root-path`, DEPENDENCY_NODES: `${plugin_1.PLUGIN_NAME}/dependency-nodes`, ADDITIONAL_DEPENDENCY_NODES: `${plugin_1.PLUGIN_NAME}/additional-dependency-nodes`, // env vars DEBUG: `${plugin_1.PLUGIN_ENV_VARS_PREFIX}_DEBUG`, ENV_ROOT_PATH: `${plugin_1.PLUGIN_ENV_VARS_PREFIX}_ROOT_PATH`, // rules RULE_ELEMENT_TYPES: `${plugin_1.PLUGIN_NAME}/${rules_1.ELEMENT_TYPES}`, RULE_ENTRY_POINT: `${plugin_1.PLUGIN_NAME}/${rules_1.ENTRY_POINT}`, RULE_EXTERNAL: `${plugin_1.PLUGIN_NAME}/${rules_1.EXTERNAL}`, RULE_NO_IGNORED: `${plugin_1.PLUGIN_NAME}/${rules_1.NO_IGNORED}`, RULE_NO_PRIVATE: `${plugin_1.PLUGIN_NAME}/${rules_1.NO_PRIVATE}`, RULE_NO_UNKNOWN_FILES: `${plugin_1.PLUGIN_NAME}/${rules_1.NO_UNKNOWN_FILES}`, RULE_NO_UNKNOWN: `${plugin_1.PLUGIN_NAME}/${rules_1.NO_UNKNOWN}`, // deprecated settings TYPES: `${plugin_1.PLUGIN_NAME}/types`, ALIAS: `${plugin_1.PLUGIN_NAME}/alias`, // elements settings properties, VALID_MODES: ["folder", "file", "full"], VALID_DEPENDENCY_NODE_KINDS: ["value", "type"], DEFAULT_DEPENDENCY_NODES: { [exports.DEPENDENCY_NODE_KEYS_MAP.REQUIRE]: [ // Note: detects "require('source')" { selector: "CallExpression[callee.name=require] > Literal", kind: "value", }, ], [exports.DEPENDENCY_NODE_KEYS_MAP.IMPORT]: [ // Note: detects "import x from 'source'" { selector: "ImportDeclaration:not([importKind=type]) > Literal", kind: "value", }, // Note: detects "import type x from 'source'" { selector: "ImportDeclaration[importKind=type] > Literal", kind: "type", }, ], [exports.DEPENDENCY_NODE_KEYS_MAP.DYNAMIC_IMPORT]: [ // Note: detects "import('source')" { selector: "ImportExpression > Literal", kind: "value" }, ], [exports.DEPENDENCY_NODE_KEYS_MAP.EXPORT]: [ // Note: detects "export * from 'source'"; { selector: "ExportAllDeclaration:not([exportKind=type]) > Literal", kind: "value", }, // Note: detects "export type * from 'source'"; { selector: "ExportAllDeclaration[exportKind=type] > Literal", kind: "type", }, // Note: detects "export { x } from 'source'"; { selector: "ExportNamedDeclaration:not([exportKind=type]) > Literal", kind: "value", }, // Note: detects "export type { x } from 'source'"; { selector: "ExportNamedDeclaration[exportKind=type] > Literal", kind: "type", }, ], }, }; /** * Map of the valid keys for the plugin settings. */ exports.SETTINGS_KEYS_MAP = { ELEMENTS: exports.SETTINGS.ELEMENTS, IGNORE: exports.SETTINGS.IGNORE, INCLUDE: exports.SETTINGS.INCLUDE, ROOT_PATH: exports.SETTINGS.ROOT_PATH, DEPENDENCY_NODES: exports.SETTINGS.DEPENDENCY_NODES, ADDITIONAL_DEPENDENCY_NODES: exports.SETTINGS.ADDITIONAL_DEPENDENCY_NODES, /** @deprecated Use 'ELEMENTS' instead */ TYPES: exports.SETTINGS.TYPES, /** @deprecated Use import/resolver settings instead */ ALIAS: exports.SETTINGS.ALIAS, }; /** * Type guard to check if a value is a valid key for the plugin settings. * @param value - The value to check. * @returns True if the value is a valid settings key, false otherwise. */ function isSettingsKey(value) { return (isString(value) && Object.values(exports.SETTINGS_KEYS_MAP).includes(value)); } /** * Map of the modes to interpret the pattern in an ElementDescriptor. */ exports.ELEMENT_DESCRIPTOR_MODES_MAP = { FOLDER: "folder", FILE: "file", FULL: "full", }; /** * Type guard to check if a value is a valid ElementDescriptorMode. * @param value The value to check. * @returns True if the value is a valid ElementDescriptorMode, false otherwise. */ function isElementDescriptorMode(value) { return (isString(value) && Object.values(exports.ELEMENT_DESCRIPTOR_MODES_MAP).includes(value)); }