@boundaries/elements
v1.1.2
Published
Element descriptors and matchers for @boundaries tools
Maintainers
Readme
@boundaries/elements
Element descriptors and matchers for
@boundariestools, such as@boundaries/eslint-plugin.
Table of Contents
Introduction
@boundaries/elements provides a powerful and flexible system for defining and enforcing architectural boundaries in your JavaScript and TypeScript projects. It allows you to:
- Define element types based on file path patterns (e.g., components, services, helpers)
- Match elements against specific criteria
- Validate dependencies between different parts of your codebase
- Enforce architectural rules by checking if dependencies between elements are allowed
This package is part of the @boundaries ecosystem and uses Micromatch patterns for flexible and powerful pattern matching.
[!NOTE] This package does not read or analyze your codebase directly. It provides the core logic for defining and matching elements and dependencies, which can be integrated into other tools such as linters or build systems.
Features
- ✨ Flexible pattern matching using Micromatch syntax
- 🎯 Element type and category identification based on file paths
- 📝 Template variables for dynamic selector matching
- ⚡ Built-in caching for optimal performance
- 🔄 Support for multiple file matching modes (file, folder, full path)
- 🎨 Capture path fragments for advanced matching scenarios
Installation
Install the package via npm:
npm install @boundaries/elementsQuick Start
Here's a quick example to get you started:
import { Elements } from '@boundaries/elements';
// Create an Elements instance
const elements = new Elements();
// Define element descriptors
const matcher = elements.getMatcher([
{
type: "component",
category: "react",
pattern: "src/components/*.tsx",
mode: "file",
capture: ["fileName"],
},
{
type: "service",
category: "data",
pattern: "src/services/*.ts",
mode: "file",
capture: ["fileName"],
},
]);
// Match an element
const isComponent = matcher.isMatch("src/components/Button.tsx", {
type: "component"
}); // true
// Match a dependency
const isValidDependency = matcher.isMatch(
{
from: "src/components/Button.tsx",
to: "src/services/Api.ts",
source: "../services/Api",
kind: "value",
nodeKind: "ImportDeclaration",
},
{
from: { category: "react" },
to: { type: "service" },
}
); // trueUsage
Configuration Options
When creating an Elements instance, you can provide configuration options that will be used as defaults for all matchers:
const elements = new Elements({
ignorePaths: ["**/dist/**", "**/build/**", "**/node_modules/**"],
includePaths: ["src/**/*"],
});Available options:
ignorePaths: Micromatch pattern(s) to exclude certain paths from element matching (default: none)includePaths: Micromatch pattern(s) to include only specific paths (default: all paths)legacyTemplates: Whether to enable legacy template syntax support (default:true, but it will befalsein future releases). This allows using${variable}syntax in templates for backward compatibility.cache: Whether to enable internal caching to improve performance (default:true)
Creating a Matcher
Use the getMatcher method to create a matcher with element descriptors:
const matcher = elements.getMatcher([
{
type: "component",
pattern: "src/components/*",
mode: "folder",
},
{
type: "helper",
pattern: "src/helpers/*.js",
mode: "file",
}
]);💡 Tip: Matchers with identical descriptors and options share the same cache instance for improved performance.
You can override the default options when creating a matcher:
const matcher = elements.getMatcher(
[/* descriptors */],
{
ignorePaths: ["**/*.test.ts"],
}
);Element Descriptors
Element descriptors define how files are identified and categorized. Each descriptor is an object with the following properties:
pattern(string | string[]): Micromatch pattern(s) to match file pathstype(string): The element type to assign to matching filescategory(string): Additional categorization for the element, providing another layer of classificationmode("file" | "folder" | "full"): Matching mode (default:"folder")"folder": Matches the first folder matching the pattern. The library will add**/*to the given pattern for matching files, because it needs to know exactly which folder has to be considered the element. So, you have to provide patterns matching the folder being the element, not the files directly."file": Matches files directly, but still matches progressively from the right. The provided pattern will not be modified."full": Matches the complete path.
basePattern(string): Additional pattern that must match from the project root. Use it when usingfileorfoldermodes and you want to capture fragments from the rest of the path.capture(string[]): Array of keys to capture path fragmentsbaseCapture(string[]): Array of keys to capture fragments frombasePattern. If the same key is defined in bothcaptureandbaseCapture, the value fromcapturetakes precedence.
Selectors
Selectors are used to match elements and dependencies against specific criteria. They are objects where each property represents a matching condition.
Element Properties
All element selectors support the following properties:
type(string | string[]): Micromatch pattern(s) for the element type/scategory(string | string[]): Micromatch pattern(s) for the element category/categoriescaptured(object): Object with keys matching captured values. Each key can be a string or an array of strings representing micromatch patterns.origin("local" | "external" | "core"): Element originlocal: Files within the projectexternal: External dependencies (e.g.,node_modules)core: Core modules (e.g., Node.js built-ins)
path(string | string[]): Micromatch pattern(s) for the file pathelementPath(string | string[]): Pattern(s) for the element pathinternalPath(string | string[]): Pattern(s) for the path within the element. For file elements, it's the same aselementPath; for folder elements, it's relative to the folder.isIgnored(boolean): Whether the element is ignoredisUnknown(boolean): Whether the element type is unknown (i.e., doesn't match any descriptor)
Dependency Properties
When matching dependencies, the to selector can additionally use:
kind(string | string[]): Micromatch pattern(s) for the dependency kindrelationship(string | string[]): Element relationship. Micromatch pattern(s) for the relationship between source and target elements:internal: Both files belong to the same elementchild: Target is a child of sourceparent: Target is a parent of sourcesibling: Elements share the same parentuncle: Target is a sibling of a source ancestornephew: Target is a child of a source siblingdescendant: Target is a descendant of sourceancestor: Target is an ancestor of source
specifiers(string | string[]): Pattern(s) for import/export specifiers (e.g., named imports)nodeKind(string | string[]): Pattern(s) for the AST node type causing the dependency (e.g.,"ImportDeclaration")source(string | string[]): Pattern(s) to match the source of the dependency. (e.g., the import path).baseSource(string | string[]): Pattern(s) for the base module name for external imports.
⚠️ Important: All properties in a selector must match for the selector to be considered a match (AND logic). Use multiple selectors for OR logic.
Note: You can also use the legacy selector syntax, but it’s deprecated and will be removed in a future release. See the Legacy Selectors section for more details.
Template Variables
Selectors support template variables using Handlebars syntax ({{ variableName }}). Templates are resolved at match time using:
- Element properties (
type,category,captured, etc.) - Dependency properties (
from,to)
Available Template Data
When matching, the following data is automatically available:
For element matching:
- Properties of the element under match are available in the
elementobject (type, category, captured, origin, path, etc.)
For dependency matching:
from: Properties of the dependency source elementto: Properties of the dependency target element, and properties of the dependency itself (source, kind, nodeKind, specifiers, etc.)
Template Examples
// Using captured values in templates
const matcher = elements.getMatcher([
{
type: "component",
pattern: "src/modules/(*)/**/*.component.tsx",
capture: ["module", "elementName", "fileName"],
mode: "file"
}
]);
// Match components from specific module using template
const isAuthComponent = matcher.isMatch(
"src/modules/auth/LoginForm.component.tsx",
{
type: "component",
captured: { module: "{{ element.captured.module }}" } // This will always match
},
);
// Using templates in dependency selectors
const isDependencyMatch = matcher.isMatch(
{
from: "src/components/Button.tsx",
to: "src/services/Api.ts",
source: "../services/Api",
kind: "type",
nodeKind: "ImportDeclaration",
specifiers: ["calculateSum", "calculateAvg"],
},
{
from: { type: "{{ from.type }}", captured: { fileName: "{{ from.captured.fileName }}" } },
to: { path: "{{ to.path }}", specifiers: "{{ lookup to.specifiers 0 }}", kind: "{{ to.kind }}" },
}
);Using Extra Template Data
You can provide additional template data using the extraTemplateData option in MatcherOptions:
// Using templates in selectors
const isMatch = matcher.isMatch(
"src/components/UserProfile.tsx",
{ type: "{{ componentType }}" },
{
extraTemplateData: { componentType: "component" }
}
);Using Matchers
You can use element selectors with a created matcher to check if a given path corresponds to an element with specific properties, or if a dependency between two paths matches certain criteria.
Element Matching
To match an element, use the isMatch method of the matcher, providing the file path and an element selector.
const isElementMatch = matcher.isMatch("src/components/Button.tsx", { type: "component" });[!TIP] You can also provide an array of selectors to the
isMatchmethod. In this case, the method will returntrueif the element matches any of the provided selectors (OR logic).
Dependency Matching
To match a dependency, use the isMatch method of the matcher, providing the properties of the dependency and a dependency selector.
Dependency object properties:
from(string): Source file pathto(string): Target file pathsource(string): Import/export source as written in codekind(string): Import kind ("type","value","typeof")nodeKind(string): AST node kindspecifiers(string[]): Imported/exported names
Dependency selector:
from: Element selector(s) for the source fileto: Dependency selector(s) for the target file
const isDependencyMatch = matcher.isMatch(
{ // Dependency properties
from: "src/components/Button.tsx",
to: "src/services/Api.ts",
source: "../services/Api",
kind: "type",
nodeKind: "ImportDeclaration",
},
{
from: { category: "react" }, // Dependency source selector/s
to: { type: "service", nodeKind: "Import*" }, // Dependency target selector/s
}
);[!TIP] You can also provide an array of selectors both to the
fromandtoproperties of the dependency selector. In this case, the method will returntrueif the dependency matches any combination of the provided selectors (OR logic).
API Reference
Class: Elements
Constructor
Creates a new Elements instance with optional default configuration.
new Elements(options?: ConfigurationOptions);Methods
getMatcher
Creates a new matcher instance.
- Parameters:
descriptors:array<ElementDescriptor>Array of element descriptors to be used by the matcher.options:ElementsOptionsOptional. Configuration options for the matcher. These options will override the default options set in theElementsinstance.
- Returns:
MatcherA new matcher instance.
const matcher = elements.getMatcher([
{
type: "component",
pattern: "src/components/*",
mode: "folder",
},
{
type: "helper",
pattern: "src/helpers/*.js",
mode: "file",
}
]);clearCache
Clears all cached matcher instances and shared caches.
elements.clearCache();serializeCache
Serializes all cached matcher instances and shared caches to a plain object.
const cache = elements.serializeCache();setCacheFromSerialized
Sets the cached matcher instances and shared caches from a serialized object.
const cache = elements.serializeCache();
elements.setCacheFromSerialized(cache);Matcher Instance Methods
isMatch
Checks if a given path matches the specified element or dependency selector.
const isElementMatch = matcher.isMatch("src/components/Button.tsx", [{ type: "component" }]);
const isDependencyMatch = matcher.isMatch(
{
from: "src/components/Button.tsx",
to: "src/services/Api.ts",
source: "../services/Api",
kind: "type",
nodeKind: "ImportDeclaration",
},
{
from: [{ category: "react" }],
to: { type: "service", nodeKind: "Import*" },
}
);- Parameters:
path:stringThe path to check when using an element selector.DependencyPropertiesThe properties of the dependency to check when using a dependency selector.
selector:ElementSelector | DependencySelectorThe selector to match against. It can be either an element selector (for path matching) or a dependency selector (for dependency matching).- If
pathis a string,selectorshould be anElementSelectoror an array ofElementSelector. - If
pathare dependency properties,selectorshould be aDependencySelectoror an array ofDependencySelector.
- If
options:MatcherOptionsOptional. Additional options for matching:extraTemplateData:objectOptional. Extra data to pass to selector templates. When using template variables in selectors, this data will be available for rendering.
getSelectorMatching
Returns the first matching selector or null.
const matchingSelector = matcher.getSelectorMatching("src/components/Button.tsx", [{ type: "component" }]);Parameters are the same as isMatch, but instead of returning a boolean, it returns the first matching selector or null if none match.
describeElement
Returns a detailed description of an element.
const elementDescription = matcher.describeElement("src/components/Button.tsx");- Parameters:
path:stringThe path of the element to describe.
describeDependency
Returns a detailed description of a dependency.
const dependencyDescription = matcher.describeDependency({
from: "src/components/Button.tsx",
to: "src/services/Api.ts",
source: "../services/Api",
kind: "type",
nodeKind: "ImportDeclaration",
});- Parameters:
dependency: The properties of the dependency to describe.
getSelectorMatchingDescription
Matches a description against selectors. As first argument, it should receive the result of describeElement or describeDependency.
const elementDescription = matcher.describeElement("src/components/Button.tsx");
const matchingSelector = matcher.getSelectorMatchingDescription(elementDescription, [{ type: "component" }]);clearCache
Clears the matcher's internal cache.
matcher.clearCache();[!WARNING] This only clears the internal cache for this matcher instance. Shared cache for micromatch results, regex and captures is not affected. You can clear all caches using
Elements.clearCache().
serializeCache
Serializes the matcher's cache.
const cache = matcher.serializeCache();setCacheFromSerialized
Restores the matcher's cache from a serialized object.
// Serialize cache to a serializable object
const cache = matcher.serializeCache();
// Clear current cache
matcher.clearCache();
// Restore cache from serialized object
matcher.setCacheFromSerialized(cache);Legacy Selectors
Legacy selectors are defined using a different syntax and are provided for backward compatibility. However, this syntax is deprecated and will be removed in a future release. It is recommended to migrate to the new selector syntax.
Selectors can be defined as either a string or an array of strings representing the element type(s):
// Legacy selector using a string
const isElementMatch = matcher.isMatch("src/components/Button.tsx", "component");
// Legacy selector using an array of strings
const isElementMatch = matcher.isMatch("src/components/Button.tsx", ["component", "service"]);They can also be defined as an array where the first element is the type and the second element is an object containing captured values:
// Legacy selector with captured values
const isElementMatch = matcher.isMatch(
"src/modules/auth/LoginForm.component.tsx",
["component", { module: "auth" }]
);⚠️ Warning: Avoid mixing legacy selectors with the new selector syntax in the same project, as this can lead to ambiguity. In particular, if you define a top-level array selector with two elements and the second one is an object containing a
typeorcategorykey, it will be interpreted as legacy options rather than two separate selectors.
Contributing
Contributors are welcome. Please read the contributing guidelines and code of conduct.
License
MIT, see LICENSE for details.
