@boundaries/elements 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -5,6 +5,46 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
5
5
  throw Error('Dynamic require of "' + x + '" is not supported');
6
6
  });
7
7
 
8
+ // src/Support/TypeGuards.ts
9
+ function isString(value) {
10
+ return typeof value === "string";
11
+ }
12
+ function isNullish(value) {
13
+ return value === null || value === void 0;
14
+ }
15
+ function isNull(value) {
16
+ return value === null;
17
+ }
18
+ function isBoolean(value) {
19
+ return typeof value === "boolean";
20
+ }
21
+ function isObject(value) {
22
+ return !isNullish(value) && !isBoolean(value) && !isArray(value) && typeof value === "object";
23
+ }
24
+ function isEmptyObject(obj) {
25
+ return isObject(obj) && Object.keys(obj).length === 0;
26
+ }
27
+ function isArray(value) {
28
+ return Array.isArray(value);
29
+ }
30
+ function isEmptyArray(arr) {
31
+ return arr.length === 0;
32
+ }
33
+ function isStringArray(value) {
34
+ return isArray(value) && value.every(isString);
35
+ }
36
+ function isObjectWithProperty(value, key) {
37
+ return isObject(value) && Object.hasOwn(value, key);
38
+ }
39
+ function isObjectWithAnyOfProperties(value, keys) {
40
+ return isObject(value) && keys.some((key) => key in value);
41
+ }
42
+
43
+ // src/Support/Paths.ts
44
+ function normalizePath(filePath) {
45
+ return filePath.replaceAll("\\", "/");
46
+ }
47
+
8
48
  // src/Config/Config.ts
9
49
  var Config = class {
10
50
  /** The ignore paths */
@@ -15,6 +55,10 @@ var Config = class {
15
55
  _legacyTemplates;
16
56
  /** Whether the cache is enabled */
17
57
  _cache;
58
+ /** Configuration for categorizing dependencies as external or local */
59
+ _flagAsExternal;
60
+ /** Root path of the project */
61
+ _rootPath;
18
62
  /**
19
63
  * Creates a new Config instance
20
64
  * @param options Configuration options
@@ -24,6 +68,18 @@ var Config = class {
24
68
  this._includePaths = options?.includePaths;
25
69
  this._legacyTemplates = options?.legacyTemplates ?? true;
26
70
  this._cache = options?.cache ?? true;
71
+ this._flagAsExternal = {
72
+ unresolvableAlias: options?.flagAsExternal?.unresolvableAlias ?? true,
73
+ inNodeModules: options?.flagAsExternal?.inNodeModules ?? true,
74
+ outsideRootPath: options?.flagAsExternal?.outsideRootPath ?? false,
75
+ customSourcePatterns: options?.flagAsExternal?.customSourcePatterns ?? []
76
+ };
77
+ if (options?.rootPath) {
78
+ const normalizedRoot = normalizePath(options.rootPath);
79
+ this._rootPath = normalizedRoot.endsWith("/") ? normalizedRoot : `${normalizedRoot}/`;
80
+ } else {
81
+ this._rootPath = void 0;
82
+ }
27
83
  }
28
84
  /**
29
85
  * The normalized configuration options
@@ -33,7 +89,9 @@ var Config = class {
33
89
  ignorePaths: this._ignorePaths,
34
90
  includePaths: this._includePaths,
35
91
  legacyTemplates: this._legacyTemplates,
36
- cache: this._cache
92
+ cache: this._cache,
93
+ flagAsExternal: this._flagAsExternal,
94
+ rootPath: this._rootPath
37
95
  };
38
96
  }
39
97
  /**
@@ -43,7 +101,9 @@ var Config = class {
43
101
  return {
44
102
  ignorePaths: this._ignorePaths,
45
103
  includePaths: this._includePaths,
46
- cache: this._cache
104
+ cache: this._cache,
105
+ flagAsExternal: this._flagAsExternal,
106
+ rootPath: this._rootPath
47
107
  };
48
108
  }
49
109
  /**
@@ -65,41 +125,6 @@ var Config = class {
65
125
  // src/Matcher/BaseElementsMatcher.ts
66
126
  import Handlebars from "handlebars";
67
127
 
68
- // src/Support/TypeGuards.ts
69
- function isString(value) {
70
- return typeof value === "string";
71
- }
72
- function isNullish(value) {
73
- return value === null || value === void 0;
74
- }
75
- function isNull(value) {
76
- return value === null;
77
- }
78
- function isBoolean(value) {
79
- return typeof value === "boolean";
80
- }
81
- function isObject(value) {
82
- return !isNullish(value) && !isBoolean(value) && !isArray(value) && typeof value === "object";
83
- }
84
- function isEmptyObject(obj) {
85
- return isObject(obj) && Object.keys(obj).length === 0;
86
- }
87
- function isArray(value) {
88
- return Array.isArray(value);
89
- }
90
- function isEmptyArray(arr) {
91
- return arr.length === 0;
92
- }
93
- function isStringArray(value) {
94
- return isArray(value) && value.every(isString);
95
- }
96
- function isObjectWithProperty(value, key) {
97
- return isObject(value) && Object.hasOwn(value, key);
98
- }
99
- function isObjectWithAnyOfProperties(value, keys) {
100
- return isObject(value) && keys.some((key) => key in value);
101
- }
102
-
103
128
  // src/Matcher/MatcherHelpers.ts
104
129
  function isCapturedValuesSelector(value) {
105
130
  if (!isObject(value) || isArray(value)) {
@@ -1530,17 +1555,72 @@ var ElementsDescriptor = class {
1530
1555
  return pkg;
1531
1556
  }
1532
1557
  /**
1533
- * Determines if an element is external based on its file path and dependency source.
1534
- * Files inside "node_modules" are considered external.
1535
- * If the dependency source is not provided, only the file path is considered.
1536
- * If the dependency source is provided, it must not be a local path (i.e, it should start by "./", "../", or "/").
1537
- * @param filePath
1538
- * @param dependencySource
1539
- * @returns
1558
+ * Determines if a file path is outside the configured root path.
1559
+ * @param filePath The file path to check.
1560
+ * @returns True if the file path is outside the root path, false otherwise.
1561
+ */
1562
+ _isOutsideRootPath(filePath) {
1563
+ if (!this._config.rootPath) {
1564
+ return false;
1565
+ }
1566
+ return !filePath.startsWith(this._config.rootPath);
1567
+ }
1568
+ /**
1569
+ * Converts an absolute file path to a relative path if rootPath is configured.
1570
+ * If rootPath is not configured, returns the path as-is (maintains backward compatibility).
1571
+ * @param filePath The file path to convert (can be absolute or relative)
1572
+ * @returns The relative path if rootPath is configured and path is absolute, otherwise the original path
1573
+ */
1574
+ _toRelativePath(filePath) {
1575
+ if (!this._config.rootPath || this._isOutsideRootPath(filePath)) {
1576
+ return filePath;
1577
+ }
1578
+ return filePath.replace(this._config.rootPath, "");
1579
+ }
1580
+ /**
1581
+ * Checks if a source string matches any of the provided patterns using micromatch.
1582
+ * @param patterns - Array of micromatch patterns
1583
+ * @param source - The source string to match against patterns
1584
+ * @returns True if the source matches any pattern, false otherwise
1540
1585
  */
1541
- _isExternalDependency(filePath, dependencySource) {
1542
- return (!filePath || filePath.includes("node_modules")) && // Not having a source, and being in node_modules only could happen if user is analyzing a file directly from there, not as a dependency. Should this be considered external then?
1543
- (!dependencySource || this._dependencySourceIsExternalOrScoped(dependencySource));
1586
+ _matchesAnyPattern(patterns, source) {
1587
+ if (!source || patterns.length === 0) {
1588
+ return false;
1589
+ }
1590
+ return this._micromatch.isMatch(source, patterns);
1591
+ }
1592
+ /**
1593
+ * Determines if an element is external based on its file path and dependency source.
1594
+ * Uses the flagAsExternal configuration to evaluate multiple conditions with OR logic:
1595
+ * - unresolvableAlias: Files whose path cannot be resolved (filePath is null)
1596
+ * - inNodeModules: Non-relative paths that include "node_modules"
1597
+ * - outsideRootPath: Resolved path is outside the configured root path (only if rootPath is configured)
1598
+ * - customSourcePatterns: Source matches any of the configured patterns
1599
+ * @param filePath The resolved file path (null if unresolved). Can be absolute if rootPath is configured, or relative if rootPath is not configured.
1600
+ * @param isOutsideRootPath Whether the file path is outside the configured root path.
1601
+ * @param dependencySource The import/export source string
1602
+ * @returns True if any of the configured conditions is met, false otherwise
1603
+ */
1604
+ _isExternalDependency(filePath, isOutsideRootPath, dependencySource) {
1605
+ const {
1606
+ unresolvableAlias,
1607
+ inNodeModules,
1608
+ outsideRootPath,
1609
+ customSourcePatterns
1610
+ } = this._config.flagAsExternal;
1611
+ if (outsideRootPath && isOutsideRootPath) {
1612
+ return true;
1613
+ }
1614
+ if (inNodeModules && filePath?.includes("node_modules")) {
1615
+ return true;
1616
+ }
1617
+ if (unresolvableAlias && !filePath && dependencySource && this._dependencySourceIsExternalOrScoped(dependencySource)) {
1618
+ return true;
1619
+ }
1620
+ if (this._matchesAnyPattern(customSourcePatterns, dependencySource)) {
1621
+ return true;
1622
+ }
1623
+ return false;
1544
1624
  }
1545
1625
  /**
1546
1626
  * Determines if a given path is included based on the configuration.
@@ -1608,6 +1688,16 @@ var ElementsDescriptor = class {
1608
1688
  }
1609
1689
  return `${[...allPathSegments].reverse().join("/").split(result)[0]}${result}`;
1610
1690
  }
1691
+ /**
1692
+ * Determines if an element descriptor matches the given parameters in the provided path.
1693
+ * @param options The options for matching the descriptor.
1694
+ * @param options.elementDescriptor The element descriptor to match.
1695
+ * @param options.filePath The file path to match against the descriptor.
1696
+ * @param options.currentPathSegments The current path segments leading to the element.
1697
+ * @param options.lastPathSegmentMatching The last path segment that was matched.
1698
+ * @param options.alreadyMatched Whether the element matched previously.
1699
+ * @returns The result of the match, including whether it matched.
1700
+ */
1611
1701
  _fileDescriptorMatch({
1612
1702
  elementDescriptor,
1613
1703
  filePath,
@@ -1762,10 +1852,11 @@ var ElementsDescriptor = class {
1762
1852
  /**
1763
1853
  * Returns an external or core dependency element given its dependency source and file path.
1764
1854
  * @param dependencySource The source of the dependency.
1765
- * @param filePath The resolved file path of the dependency, if known.
1855
+ * @param isOutsideRootPath Whether the file path is outside the configured root path.
1856
+ * @param filePath The resolved file path of the dependency, if known. Can be absolute if rootPath is configured.
1766
1857
  * @returns The external or core dependency element, or null if it is a local dependency.
1767
1858
  */
1768
- _getExternalOrCoreDependencyElement(dependencySource, filePath) {
1859
+ _getExternalOrCoreDependencyElement(dependencySource, isOutsideRootPath, filePath) {
1769
1860
  const baseDependencySource = this._getExternalOrCoreModuleBaseSource(dependencySource);
1770
1861
  const isCore = this._dependencySourceIsCoreModule(
1771
1862
  dependencySource,
@@ -1782,6 +1873,7 @@ var ElementsDescriptor = class {
1782
1873
  }
1783
1874
  const isExternal = this._isExternalDependency(
1784
1875
  filePath || null,
1876
+ isOutsideRootPath,
1785
1877
  dependencySource
1786
1878
  );
1787
1879
  if (isExternal) {
@@ -1802,12 +1894,19 @@ var ElementsDescriptor = class {
1802
1894
  if (this._descriptionsCache.has(cacheKey)) {
1803
1895
  return this._descriptionsCache.get(cacheKey);
1804
1896
  }
1805
- const externalOrCoreDependencyElement = dependencySource ? this._getExternalOrCoreDependencyElement(dependencySource, filePath) : null;
1897
+ const normalizedFilePath = filePath ? normalizePath(filePath) : filePath;
1898
+ const isOutsideRootPath = normalizedFilePath ? this._isOutsideRootPath(normalizedFilePath) : false;
1899
+ const relativePath = normalizedFilePath && this._config.rootPath ? this._toRelativePath(normalizedFilePath) : normalizedFilePath;
1900
+ const externalOrCoreDependencyElement = dependencySource ? this._getExternalOrCoreDependencyElement(
1901
+ dependencySource,
1902
+ isOutsideRootPath,
1903
+ relativePath
1904
+ ) : null;
1806
1905
  if (externalOrCoreDependencyElement) {
1807
1906
  this._descriptionsCache.set(cacheKey, externalOrCoreDependencyElement);
1808
1907
  return externalOrCoreDependencyElement;
1809
1908
  }
1810
- const fileDescription = this._describeFile(filePath);
1909
+ const fileDescription = this._describeFile(relativePath);
1811
1910
  const elementResult = dependencySource ? {
1812
1911
  ...fileDescription,
1813
1912
  source: dependencySource
@@ -1817,7 +1916,7 @@ var ElementsDescriptor = class {
1817
1916
  }
1818
1917
  /**
1819
1918
  * Describes an element given its file path.
1820
- * @param filePath The path of the file to describe.
1919
+ * @param filePath The path of the file to describe. Can be absolute if rootPath is configured, or relative if not.
1821
1920
  * @returns The description of the element.
1822
1921
  */
1823
1922
  describeElement(filePath) {
@@ -1826,7 +1925,7 @@ var ElementsDescriptor = class {
1826
1925
  /**
1827
1926
  * Describes a dependency element given its dependency source and file path.
1828
1927
  * @param dependencySource The source of the dependency.
1829
- * @param filePath The path of the file being the dependency, if known.
1928
+ * @param filePath The path of the file being the dependency, if known. Can be absolute if rootPath is configured, or relative if not.
1830
1929
  * @returns The description of the dependency element.
1831
1930
  */
1832
1931
  describeDependencyElement(dependencySource, filePath) {
@@ -2214,7 +2313,7 @@ var MatchersCache = class extends CacheManager {
2214
2313
  config,
2215
2314
  elementDescriptors
2216
2315
  }) {
2217
- const configHash = `${config.legacyTemplates}|${config.includePaths}|${config.ignorePaths}|${config.cache}`;
2316
+ const configHash = `${config.legacyTemplates}|${config.includePaths}|${config.ignorePaths}|${config.cache}|${config.rootPath}|${config.flagAsExternal.inNodeModules}|${config.flagAsExternal.unresolvableAlias}|${config.flagAsExternal.outsideRootPath}|${config.flagAsExternal.customSourcePatterns.join(",")}`;
2218
2317
  const elementDescriptorsHash = elementDescriptors.map(
2219
2318
  (descriptor) => `${descriptor.type}|${descriptor.category}|${descriptor.pattern}|${descriptor.basePattern}|${descriptor.mode}|${descriptor.capture}|${descriptor.baseCapture}`
2220
2319
  ).join(",");