@atlaskit/eslint-plugin-platform 2.7.1 → 2.8.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.
Files changed (125) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cjs/index.js +17 -9
  3. package/dist/cjs/rules/constants.js +1 -1
  4. package/dist/cjs/rules/ensure-critical-dependency-resolutions/index.js +5 -5
  5. package/dist/cjs/rules/ensure-no-private-dependencies/index.js +48 -66
  6. package/dist/cjs/rules/feature-gating/inline-usage/index.js +14 -3
  7. package/dist/cjs/rules/feature-gating/no-alias/index.js +1 -1
  8. package/dist/cjs/rules/feature-gating/no-module-level-eval/index.js +1 -1
  9. package/dist/cjs/rules/feature-gating/no-module-level-eval-nav4/index.js +1 -1
  10. package/dist/cjs/rules/feature-gating/no-preconditioning/index.js +4 -1
  11. package/dist/cjs/rules/feature-gating/prefer-fg/index.js +1 -1
  12. package/dist/cjs/rules/feature-gating/static-feature-flags/index.js +2 -2
  13. package/dist/cjs/rules/feature-gating/use-recommended-utils/index.js +1 -1
  14. package/dist/cjs/rules/feature-gating/valid-gate-name/index.js +60 -0
  15. package/dist/cjs/rules/import/no-barrel-entry-imports/index.js +871 -0
  16. package/dist/cjs/rules/import/no-barrel-entry-jest-mock/index.js +1384 -0
  17. package/dist/cjs/rules/import/no-conversation-assistant-barrel-imports/index.js +43 -0
  18. package/dist/cjs/rules/import/no-jest-mock-barrel-files/index.js +1401 -0
  19. package/dist/cjs/rules/import/no-relative-barrel-file-imports/index.js +777 -0
  20. package/dist/cjs/rules/import/shared/barrel-parsing.js +511 -0
  21. package/dist/cjs/rules/import/shared/file-system.js +186 -0
  22. package/dist/cjs/rules/import/shared/jest-utils.js +191 -0
  23. package/dist/cjs/rules/import/shared/package-registry.js +263 -0
  24. package/dist/cjs/rules/import/shared/package-resolution.js +185 -0
  25. package/dist/cjs/rules/import/shared/perf.js +89 -0
  26. package/dist/cjs/rules/import/shared/types.js +67 -0
  27. package/dist/cjs/rules/no-invalid-storybook-decorator-usage/index.js +1 -1
  28. package/dist/cjs/rules/no-sparse-checkout/index.js +1 -1
  29. package/dist/cjs/rules/prefer-crypto-random-uuid/index.js +87 -0
  30. package/dist/cjs/rules/use-entrypoints-in-examples/index.js +1 -1
  31. package/dist/es2019/index.js +17 -9
  32. package/dist/es2019/rules/constants.js +1 -1
  33. package/dist/es2019/rules/ensure-critical-dependency-resolutions/index.js +5 -5
  34. package/dist/es2019/rules/ensure-no-private-dependencies/index.js +10 -9
  35. package/dist/es2019/rules/feature-gating/inline-usage/index.js +14 -3
  36. package/dist/es2019/rules/feature-gating/no-alias/index.js +1 -1
  37. package/dist/es2019/rules/feature-gating/no-module-level-eval/index.js +1 -1
  38. package/dist/es2019/rules/feature-gating/no-module-level-eval-nav4/index.js +1 -1
  39. package/dist/es2019/rules/feature-gating/no-preconditioning/index.js +4 -1
  40. package/dist/es2019/rules/feature-gating/prefer-fg/index.js +1 -1
  41. package/dist/es2019/rules/feature-gating/static-feature-flags/index.js +2 -2
  42. package/dist/es2019/rules/feature-gating/use-recommended-utils/index.js +1 -1
  43. package/dist/es2019/rules/feature-gating/valid-gate-name/index.js +52 -0
  44. package/dist/es2019/rules/import/no-barrel-entry-imports/index.js +801 -0
  45. package/dist/es2019/rules/import/no-barrel-entry-jest-mock/index.js +1113 -0
  46. package/dist/es2019/rules/import/no-conversation-assistant-barrel-imports/index.js +37 -0
  47. package/dist/es2019/rules/import/no-jest-mock-barrel-files/index.js +1179 -0
  48. package/dist/es2019/rules/import/no-relative-barrel-file-imports/index.js +738 -0
  49. package/dist/es2019/rules/import/shared/barrel-parsing.js +433 -0
  50. package/dist/es2019/rules/import/shared/file-system.js +174 -0
  51. package/dist/es2019/rules/import/shared/jest-utils.js +159 -0
  52. package/dist/es2019/rules/import/shared/package-registry.js +240 -0
  53. package/dist/es2019/rules/import/shared/package-resolution.js +161 -0
  54. package/dist/es2019/rules/import/shared/perf.js +83 -0
  55. package/dist/es2019/rules/import/shared/types.js +57 -0
  56. package/dist/es2019/rules/no-invalid-storybook-decorator-usage/index.js +1 -1
  57. package/dist/es2019/rules/no-sparse-checkout/index.js +1 -1
  58. package/dist/es2019/rules/prefer-crypto-random-uuid/index.js +81 -0
  59. package/dist/es2019/rules/use-entrypoints-in-examples/index.js +1 -1
  60. package/dist/esm/index.js +17 -9
  61. package/dist/esm/rules/constants.js +1 -1
  62. package/dist/esm/rules/ensure-critical-dependency-resolutions/index.js +5 -5
  63. package/dist/esm/rules/ensure-no-private-dependencies/index.js +48 -65
  64. package/dist/esm/rules/feature-gating/inline-usage/index.js +14 -3
  65. package/dist/esm/rules/feature-gating/no-alias/index.js +1 -1
  66. package/dist/esm/rules/feature-gating/no-module-level-eval/index.js +1 -1
  67. package/dist/esm/rules/feature-gating/no-module-level-eval-nav4/index.js +1 -1
  68. package/dist/esm/rules/feature-gating/no-preconditioning/index.js +4 -1
  69. package/dist/esm/rules/feature-gating/prefer-fg/index.js +1 -1
  70. package/dist/esm/rules/feature-gating/static-feature-flags/index.js +2 -2
  71. package/dist/esm/rules/feature-gating/use-recommended-utils/index.js +1 -1
  72. package/dist/esm/rules/feature-gating/valid-gate-name/index.js +53 -0
  73. package/dist/esm/rules/import/no-barrel-entry-imports/index.js +864 -0
  74. package/dist/esm/rules/import/no-barrel-entry-jest-mock/index.js +1375 -0
  75. package/dist/esm/rules/import/no-conversation-assistant-barrel-imports/index.js +37 -0
  76. package/dist/esm/rules/import/no-jest-mock-barrel-files/index.js +1391 -0
  77. package/dist/esm/rules/import/no-relative-barrel-file-imports/index.js +770 -0
  78. package/dist/esm/rules/import/shared/barrel-parsing.js +500 -0
  79. package/dist/esm/rules/import/shared/file-system.js +176 -0
  80. package/dist/esm/rules/import/shared/jest-utils.js +179 -0
  81. package/dist/esm/rules/import/shared/package-registry.js +256 -0
  82. package/dist/esm/rules/import/shared/package-resolution.js +175 -0
  83. package/dist/esm/rules/import/shared/perf.js +80 -0
  84. package/dist/esm/rules/import/shared/types.js +61 -0
  85. package/dist/esm/rules/no-invalid-storybook-decorator-usage/index.js +1 -1
  86. package/dist/esm/rules/no-sparse-checkout/index.js +1 -1
  87. package/dist/esm/rules/prefer-crypto-random-uuid/index.js +81 -0
  88. package/dist/esm/rules/use-entrypoints-in-examples/index.js +1 -1
  89. package/dist/types/index.d.ts +18 -16
  90. package/dist/types/rules/import/no-barrel-entry-imports/index.d.ts +9 -0
  91. package/dist/types/rules/import/no-barrel-entry-jest-mock/index.d.ts +9 -0
  92. package/dist/types/rules/import/no-conversation-assistant-barrel-imports/index.d.ts +3 -0
  93. package/dist/types/rules/import/no-jest-mock-barrel-files/index.d.ts +22 -0
  94. package/dist/types/rules/import/no-relative-barrel-file-imports/index.d.ts +5 -0
  95. package/dist/types/rules/import/shared/barrel-parsing.d.ts +30 -0
  96. package/dist/types/rules/import/shared/file-system.d.ts +38 -0
  97. package/dist/types/rules/import/shared/jest-utils.d.ts +47 -0
  98. package/dist/types/rules/import/shared/package-registry.d.ts +26 -0
  99. package/dist/types/rules/import/shared/package-resolution.d.ts +38 -0
  100. package/dist/types/rules/import/shared/perf.d.ts +13 -0
  101. package/dist/types/rules/import/shared/types.d.ts +131 -0
  102. package/dist/types/rules/prefer-crypto-random-uuid/index.d.ts +3 -0
  103. package/dist/types-ts4.5/index.d.ts +18 -28
  104. package/dist/types-ts4.5/rules/import/no-barrel-entry-imports/index.d.ts +9 -0
  105. package/dist/types-ts4.5/rules/import/no-barrel-entry-jest-mock/index.d.ts +9 -0
  106. package/dist/types-ts4.5/rules/import/no-jest-mock-barrel-files/index.d.ts +22 -0
  107. package/dist/types-ts4.5/rules/import/no-relative-barrel-file-imports/index.d.ts +5 -0
  108. package/dist/types-ts4.5/rules/import/shared/barrel-parsing.d.ts +30 -0
  109. package/dist/types-ts4.5/rules/import/shared/file-system.d.ts +38 -0
  110. package/dist/types-ts4.5/rules/import/shared/jest-utils.d.ts +47 -0
  111. package/dist/types-ts4.5/rules/import/shared/package-registry.d.ts +26 -0
  112. package/dist/types-ts4.5/rules/import/shared/package-resolution.d.ts +38 -0
  113. package/dist/types-ts4.5/rules/import/shared/perf.d.ts +13 -0
  114. package/dist/types-ts4.5/rules/import/shared/types.d.ts +131 -0
  115. package/package.json +4 -5
  116. package/dist/cjs/rules/ensure-feature-flag-prefix/index.js +0 -75
  117. package/dist/cjs/rules/ensure-native-and-af-exports-synced/index.js +0 -158
  118. package/dist/es2019/rules/ensure-feature-flag-prefix/index.js +0 -65
  119. package/dist/es2019/rules/ensure-native-and-af-exports-synced/index.js +0 -146
  120. package/dist/esm/rules/ensure-feature-flag-prefix/index.js +0 -69
  121. package/dist/esm/rules/ensure-native-and-af-exports-synced/index.js +0 -151
  122. /package/dist/types/rules/{ensure-native-and-af-exports-synced → feature-gating/valid-gate-name}/index.d.ts +0 -0
  123. /package/dist/types-ts4.5/rules/{ensure-feature-flag-prefix → feature-gating/valid-gate-name}/index.d.ts +0 -0
  124. /package/dist/types-ts4.5/rules/{ensure-native-and-af-exports-synced → import/no-conversation-assistant-barrel-imports}/index.d.ts +0 -0
  125. /package/dist/{types/rules/ensure-feature-flag-prefix → types-ts4.5/rules/prefer-crypto-random-uuid}/index.d.ts +0 -0
@@ -0,0 +1,864 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
3
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
5
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
6
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
7
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
8
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
9
+ import { dirname } from 'path';
10
+ import { parseBarrelExports } from '../shared/barrel-parsing';
11
+ import { DEFAULT_TARGET_FOLDERS, findWorkspaceRoot, isRelativeImport } from '../shared/file-system';
12
+ import { findPackageInRegistry, isPackageInApplyToImportsFrom } from '../shared/package-registry';
13
+ import { findExportForSourceFile, parsePackageExports } from '../shared/package-resolution';
14
+ import { realFileSystem } from '../shared/types';
15
+
16
+ /**
17
+ * Options for the no-barrel-entry-imports rule.
18
+ */
19
+
20
+ /**
21
+ * Represents a Jest automock call: jest.mock('path') with no additional arguments
22
+ */
23
+
24
+ /**
25
+ * Metadata for the ESLint rule
26
+ */
27
+ var ruleMeta = {
28
+ type: 'problem',
29
+ docs: {
30
+ description: 'Disallow importing from barrel files in entry points.',
31
+ category: 'Best Practices',
32
+ recommended: false
33
+ },
34
+ fixable: 'code',
35
+ schema: [{
36
+ type: 'object',
37
+ properties: {
38
+ applyToImportsFrom: {
39
+ type: 'array',
40
+ items: {
41
+ type: 'string'
42
+ },
43
+ description: 'The folder paths (relative to workspace root) containing packages whose imports will be checked and autofixed.'
44
+ }
45
+ },
46
+ additionalProperties: false
47
+ }],
48
+ messages: {
49
+ barrelEntryImport: "Importing from barrel file '{{path}}' is not allowed. Import directly from the source file using a more specific package.json export instead."
50
+ }
51
+ };
52
+
53
+ /**
54
+ * Get the imported name from an ImportSpecifier, handling both Identifier and Literal
55
+ */
56
+ function getImportedName(spec) {
57
+ var imported = spec.imported;
58
+ return imported.type === 'Identifier' ? imported.name : String(imported.value);
59
+ }
60
+
61
+ /**
62
+ * Build an import statement for a set of specifiers
63
+ */
64
+ function buildImportStatement(_ref) {
65
+ var specs = _ref.specs,
66
+ path = _ref.path,
67
+ quoteChar = _ref.quoteChar,
68
+ _ref$isTypeImport = _ref.isTypeImport,
69
+ isTypeImport = _ref$isTypeImport === void 0 ? false : _ref$isTypeImport;
70
+ var importNames = specs.map(function (spec) {
71
+ if (spec.type === 'ImportDefaultSpecifier') {
72
+ return spec.local.name;
73
+ } else if (spec.type === 'ImportSpecifier') {
74
+ var imported = getImportedName(spec);
75
+ var local = spec.local.name;
76
+ var isInlineType = spec.importKind === 'type' && !isTypeImport;
77
+ var prefix = isInlineType ? 'type ' : '';
78
+ return imported === local ? "".concat(prefix).concat(imported) : "".concat(prefix).concat(imported, " as ").concat(local);
79
+ }
80
+ return '';
81
+ }).filter(function (name) {
82
+ return name.length > 0;
83
+ });
84
+ if (importNames.length === 0) {
85
+ return '';
86
+ }
87
+ var typeKeyword = isTypeImport ? 'type ' : '';
88
+ var hasDefault = specs.some(function (spec) {
89
+ return spec.type === 'ImportDefaultSpecifier';
90
+ });
91
+ var hasNamed = specs.some(function (spec) {
92
+ return spec.type === 'ImportSpecifier';
93
+ });
94
+ if (hasDefault && hasNamed) {
95
+ var _specs$find;
96
+ var defaultName = (_specs$find = specs.find(function (spec) {
97
+ return spec.type === 'ImportDefaultSpecifier';
98
+ })) === null || _specs$find === void 0 ? void 0 : _specs$find.local.name;
99
+ var namedImports = specs.filter(function (spec) {
100
+ return spec.type === 'ImportSpecifier';
101
+ }).map(function (spec) {
102
+ var imported = getImportedName(spec);
103
+ var local = spec.local.name;
104
+ var isInlineType = spec.importKind === 'type' && !isTypeImport;
105
+ var prefix = isInlineType ? 'type ' : '';
106
+ return imported === local ? "".concat(prefix).concat(imported) : "".concat(prefix).concat(imported, " as ").concat(local);
107
+ }).join(', ');
108
+ return "import ".concat(typeKeyword).concat(defaultName, ", { ").concat(namedImports, " } from ").concat(quoteChar).concat(path).concat(quoteChar, ";");
109
+ } else if (hasDefault) {
110
+ var _specs$find2;
111
+ var _defaultName = (_specs$find2 = specs.find(function (spec) {
112
+ return spec.type === 'ImportDefaultSpecifier';
113
+ })) === null || _specs$find2 === void 0 ? void 0 : _specs$find2.local.name;
114
+ return "import ".concat(typeKeyword).concat(_defaultName, " from ").concat(quoteChar).concat(path).concat(quoteChar, ";");
115
+ } else {
116
+ return "import ".concat(typeKeyword, "{ ").concat(importNames.join(', '), " } from ").concat(quoteChar).concat(path).concat(quoteChar, ";");
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Represents a specifier with its resolved target export path information.
122
+ */
123
+
124
+ /**
125
+ * Context resolved for an import that may be a barrel import.
126
+ */
127
+
128
+ /**
129
+ * Result of classifying specifiers by their target export paths.
130
+ */
131
+
132
+ /**
133
+ * Resolves import context for barrel file analysis.
134
+ * Returns null if the import should not be processed (relative import, not in target folder, etc.)
135
+ */
136
+ function resolveImportContext(_ref2) {
137
+ var node = _ref2.node,
138
+ workspaceRoot = _ref2.workspaceRoot,
139
+ fs = _ref2.fs,
140
+ applyToImportsFrom = _ref2.applyToImportsFrom;
141
+ if (!node.source || typeof node.source.value !== 'string') {
142
+ return null;
143
+ }
144
+ var importPath = node.source.value;
145
+
146
+ // Skip relative imports - this rule is for cross-package imports
147
+ if (isRelativeImport(importPath)) {
148
+ return null;
149
+ }
150
+
151
+ // Extract the base package name (without subpath)
152
+ // e.g., "@atlassian/conversation-assistant-instrumentation" from
153
+ // "@atlassian/conversation-assistant-instrumentation" or
154
+ // "@atlassian/conversation-assistant-instrumentation/controllers/analytics"
155
+ var packageNameMatch = importPath.match(/^(@[^/]+\/[^/]+)/);
156
+ if (!packageNameMatch) {
157
+ return null;
158
+ }
159
+ var packageName = packageNameMatch[1];
160
+ var subPath = importPath.slice(packageName.length); // e.g., "" or "/controllers/analytics"
161
+
162
+ // Find the package (resolution is not constrained by applyToImportsFrom)
163
+ var packageDir = findPackageInRegistry({
164
+ packageName: packageName,
165
+ workspaceRoot: workspaceRoot,
166
+ fs: fs
167
+ });
168
+ if (!packageDir) {
169
+ return null;
170
+ }
171
+
172
+ // Only check imports from packages in our applyToImportsFrom folders
173
+ if (!isPackageInApplyToImportsFrom({
174
+ packageDir: packageDir,
175
+ workspaceRoot: workspaceRoot,
176
+ applyToImportsFrom: applyToImportsFrom
177
+ })) {
178
+ return null;
179
+ }
180
+
181
+ // Get the exports map for this package
182
+ var exportsMap = parsePackageExports({
183
+ packageDir: packageDir,
184
+ fs: fs
185
+ });
186
+ if (exportsMap.size === 0) {
187
+ return null;
188
+ }
189
+
190
+ // Determine which export path we're importing from
191
+ // For bare package imports, it's ".", for subpath imports it's "./" + subPath
192
+ var currentExportPath = subPath ? '.' + subPath : '.';
193
+
194
+ // Get the resolved path for the current export (the entry point file for this import)
195
+ var entryFilePath = exportsMap.get(currentExportPath);
196
+ if (!entryFilePath) {
197
+ return null;
198
+ }
199
+
200
+ // Parse the entry file to find where each export originates
201
+ // Pass workspaceRoot to enable cross-package re-export resolution
202
+ var exportMap = parseBarrelExports({
203
+ barrelFilePath: entryFilePath,
204
+ fs: fs,
205
+ workspaceRoot: workspaceRoot
206
+ });
207
+ if (exportMap.size === 0) {
208
+ return null;
209
+ }
210
+ return {
211
+ importPath: importPath,
212
+ packageName: packageName,
213
+ currentExportPath: currentExportPath,
214
+ exportsMap: exportsMap,
215
+ exportMap: exportMap
216
+ };
217
+ }
218
+
219
+ /**
220
+ * Classifies import specifiers by their target export paths.
221
+ * Groups specifiers that can be remapped to more specific exports.
222
+ * For cross-package re-exports, suggests importing from the source package's most specific subpath.
223
+ */
224
+ function classifySpecifiers(_ref3) {
225
+ var node = _ref3.node,
226
+ importContext = _ref3.importContext,
227
+ workspaceRoot = _ref3.workspaceRoot,
228
+ fs = _ref3.fs;
229
+ var currentExportPath = importContext.currentExportPath,
230
+ exportsMap = importContext.exportsMap,
231
+ exportMap = importContext.exportMap;
232
+ var specifiers = node.specifiers;
233
+ var specifiersByTarget = new Map();
234
+ var unmappedSpecifiers = [];
235
+ var hasNamespaceImport = false;
236
+
237
+ // Cache for source package exports maps to avoid redundant parsing
238
+ var sourcePackageExportsMaps = new Map();
239
+ var _iterator = _createForOfIteratorHelper(specifiers),
240
+ _step;
241
+ try {
242
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
243
+ var spec = _step.value;
244
+ if (spec.type === 'ImportNamespaceSpecifier') {
245
+ hasNamespaceImport = true;
246
+ continue;
247
+ }
248
+ var nameInSource = void 0;
249
+ var kind = 'value';
250
+ if (spec.type === 'ImportDefaultSpecifier') {
251
+ nameInSource = 'default';
252
+ } else if (spec.type === 'ImportSpecifier') {
253
+ nameInSource = getImportedName(spec);
254
+ var parentImportKind = node.importKind;
255
+ kind = parentImportKind === 'type' || spec.importKind === 'type' ? 'type' : 'value';
256
+ } else {
257
+ continue;
258
+ }
259
+ var exportInfo = exportMap.get(nameInSource);
260
+ if (exportInfo) {
261
+ var _exportInfo$crossPack;
262
+ var effectiveKind = kind === 'type' || exportInfo.isTypeOnly ? 'type' : 'value';
263
+
264
+ // Check if this is a cross-package re-export
265
+ var sourcePackageName = (_exportInfo$crossPack = exportInfo.crossPackageSource) === null || _exportInfo$crossPack === void 0 ? void 0 : _exportInfo$crossPack.packageName;
266
+ if (sourcePackageName) {
267
+ // For cross-package re-exports, find the most specific subpath in the source package
268
+ // Note: Package resolution is not constrained by applyToImportsFrom - any package can be resolved
269
+ var sourcePackageExportsMap = sourcePackageExportsMaps.get(sourcePackageName);
270
+ if (!sourcePackageExportsMap) {
271
+ var sourcePackageDir = findPackageInRegistry({
272
+ packageName: sourcePackageName,
273
+ workspaceRoot: workspaceRoot,
274
+ fs: fs
275
+ });
276
+ if (sourcePackageDir) {
277
+ sourcePackageExportsMap = parsePackageExports({
278
+ packageDir: sourcePackageDir,
279
+ fs: fs
280
+ });
281
+ sourcePackageExportsMaps.set(sourcePackageName, sourcePackageExportsMap);
282
+ }
283
+ }
284
+
285
+ // Find the best export path in the source package
286
+ var _targetExportPath = null;
287
+ if (sourcePackageExportsMap) {
288
+ _targetExportPath = findExportForSourceFile({
289
+ sourceFilePath: exportInfo.path,
290
+ exportsMap: sourcePackageExportsMap
291
+ });
292
+ }
293
+
294
+ // Build the full import path: @package/subpath or just @package if no subpath found
295
+ var targetKey = _targetExportPath ? sourcePackageName + _targetExportPath.slice(1) // Remove leading '.' from subpath
296
+ : sourcePackageName;
297
+ if (!specifiersByTarget.has(targetKey)) {
298
+ specifiersByTarget.set(targetKey, []);
299
+ }
300
+ specifiersByTarget.get(targetKey).push({
301
+ spec: _objectSpread(_objectSpread({}, spec), {}, {
302
+ importKind: effectiveKind
303
+ }),
304
+ originalName: exportInfo.originalName,
305
+ targetExportPath: targetKey,
306
+ kind: effectiveKind,
307
+ sourcePackageName: sourcePackageName
308
+ });
309
+ continue;
310
+ }
311
+
312
+ // Find if there's a package.json export that points to this source file
313
+ var targetExportPath = findExportForSourceFile({
314
+ sourceFilePath: exportInfo.path,
315
+ exportsMap: exportsMap
316
+ });
317
+
318
+ // Get the file that the current export path resolves to
319
+ var currentExportResolvedFile = exportsMap.get(currentExportPath);
320
+
321
+ // Skip if:
322
+ // 1. No target export path found
323
+ // 2. Target is same as current (no change needed)
324
+ // 3. Current export path already resolves to the same file as the source
325
+ // (handles multiple exports pointing to same file - avoid no-op changes)
326
+ var currentExportAlreadyPointsToSourceFile = currentExportResolvedFile !== undefined && currentExportResolvedFile === exportInfo.path;
327
+ if (targetExportPath && targetExportPath !== currentExportPath && !currentExportAlreadyPointsToSourceFile) {
328
+ if (!specifiersByTarget.has(targetExportPath)) {
329
+ specifiersByTarget.set(targetExportPath, []);
330
+ }
331
+ specifiersByTarget.get(targetExportPath).push({
332
+ spec: _objectSpread(_objectSpread({}, spec), {}, {
333
+ importKind: effectiveKind
334
+ }),
335
+ originalName: exportInfo.originalName,
336
+ targetExportPath: targetExportPath,
337
+ kind: effectiveKind
338
+ });
339
+ } else {
340
+ // No more specific export available
341
+ unmappedSpecifiers.push({
342
+ spec: spec,
343
+ targetExportPath: null,
344
+ kind: kind
345
+ });
346
+ }
347
+ } else {
348
+ unmappedSpecifiers.push({
349
+ spec: spec,
350
+ targetExportPath: null,
351
+ kind: kind
352
+ });
353
+ }
354
+ }
355
+ } catch (err) {
356
+ _iterator.e(err);
357
+ } finally {
358
+ _iterator.f();
359
+ }
360
+ return {
361
+ specifiersByTarget: specifiersByTarget,
362
+ unmappedSpecifiers: unmappedSpecifiers,
363
+ hasNamespaceImport: hasNamespaceImport
364
+ };
365
+ }
366
+
367
+ /**
368
+ * Transforms a specifier to use the original export name (handling aliasing).
369
+ * Converts named imports of default exports to ImportDefaultSpecifier.
370
+ */
371
+ function transformSpecifierForExport(_ref4) {
372
+ var spec = _ref4.spec,
373
+ originalName = _ref4.originalName,
374
+ kind = _ref4.kind;
375
+ if (!originalName) {
376
+ return spec;
377
+ }
378
+ if (originalName === 'default') {
379
+ // Should be ImportDefaultSpecifier
380
+ if (spec.type === 'ImportDefaultSpecifier') {
381
+ return spec;
382
+ }
383
+ // Convert ImportSpecifier to ImportDefaultSpecifier
384
+ return {
385
+ type: 'ImportDefaultSpecifier',
386
+ local: spec.local,
387
+ range: spec.range,
388
+ loc: spec.loc,
389
+ parent: spec.parent
390
+ };
391
+ } else {
392
+ // Create synthetic ImportSpecifier with correct importKind
393
+ return {
394
+ type: 'ImportSpecifier',
395
+ local: spec.local,
396
+ imported: {
397
+ type: 'Identifier',
398
+ name: originalName,
399
+ range: [0, 0],
400
+ loc: {
401
+ start: {
402
+ line: 0,
403
+ column: 0
404
+ },
405
+ end: {
406
+ line: 0,
407
+ column: 0
408
+ }
409
+ }
410
+ },
411
+ importKind: kind,
412
+ range: spec.range,
413
+ loc: spec.loc,
414
+ parent: spec.parent
415
+ };
416
+ }
417
+ }
418
+
419
+ /**
420
+ * Merges new specifiers with an existing import declaration.
421
+ * Returns the new import statement string.
422
+ */
423
+ function buildMergedImportStatement(_ref5) {
424
+ var existingImport = _ref5.existingImport,
425
+ newSpecs = _ref5.newSpecs,
426
+ newImportPath = _ref5.newImportPath,
427
+ nodeImportKind = _ref5.nodeImportKind,
428
+ quoteChar = _ref5.quoteChar;
429
+ var existingSpecs = existingImport.specifiers.map(function (s) {
430
+ if (existingImport.importKind === 'type') {
431
+ return _objectSpread(_objectSpread({}, s), {}, {
432
+ importKind: 'type'
433
+ });
434
+ }
435
+ return s;
436
+ });
437
+ var augmentedNewSpecs = newSpecs.map(function (s) {
438
+ if (nodeImportKind === 'type') {
439
+ return _objectSpread(_objectSpread({}, s), {}, {
440
+ importKind: 'type'
441
+ });
442
+ }
443
+ return s;
444
+ });
445
+ var mergedSpecs = [].concat(_toConsumableArray(existingSpecs), _toConsumableArray(augmentedNewSpecs));
446
+
447
+ // Determine if we should use 'import type'
448
+ var allType = mergedSpecs.every(function (s) {
449
+ return s.importKind === 'type';
450
+ });
451
+ return buildImportStatement({
452
+ specs: mergedSpecs,
453
+ path: newImportPath,
454
+ quoteChar: quoteChar,
455
+ isTypeImport: allType
456
+ });
457
+ }
458
+
459
+ /**
460
+ * Check if an ExpressionStatement is a Jest automock: jest.mock('path') with exactly one string argument.
461
+ * Returns the JestAutomock info if it is, null otherwise.
462
+ */
463
+ function getJestAutomock(node) {
464
+ if (node.type !== 'ExpressionStatement') {
465
+ return null;
466
+ }
467
+ var statement = node;
468
+ var expr = statement.expression;
469
+ if (expr.type !== 'CallExpression') {
470
+ return null;
471
+ }
472
+
473
+ // Check for jest.mock(...)
474
+ var callee = expr.callee;
475
+ if (callee.type !== 'MemberExpression' || callee.object.type !== 'Identifier' || callee.object.name !== 'jest' || callee.property.type !== 'Identifier' || callee.property.name !== 'mock') {
476
+ return null;
477
+ }
478
+
479
+ // Must have exactly one argument (automock = no factory function)
480
+ if (expr.arguments.length !== 1) {
481
+ return null;
482
+ }
483
+ var arg = expr.arguments[0];
484
+ if (arg.type !== 'Literal' || typeof arg.value !== 'string') {
485
+ return null;
486
+ }
487
+
488
+ // Get the quote character from the raw value
489
+ var raw = arg.raw || "'".concat(arg.value, "'");
490
+ var quoteChar = raw[0];
491
+ return {
492
+ statementNode: statement,
493
+ path: arg.value,
494
+ quoteChar: quoteChar
495
+ };
496
+ }
497
+
498
+ /**
499
+ * Find all Jest automocks in the AST that match the given import path.
500
+ */
501
+ function findMatchingAutomocks(_ref6) {
502
+ var sourceCode = _ref6.sourceCode,
503
+ importPath = _ref6.importPath;
504
+ var automocks = [];
505
+ var ast = sourceCode.ast;
506
+ var _iterator2 = _createForOfIteratorHelper(ast.body),
507
+ _step2;
508
+ try {
509
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
510
+ var statement = _step2.value;
511
+ var automock = getJestAutomock(statement);
512
+ if (automock && automock.path === importPath) {
513
+ automocks.push(automock);
514
+ }
515
+ }
516
+ } catch (err) {
517
+ _iterator2.e(err);
518
+ } finally {
519
+ _iterator2.f();
520
+ }
521
+ return automocks;
522
+ }
523
+
524
+ /**
525
+ * Build a jest.mock() statement string
526
+ */
527
+ function buildAutomockStatement(_ref7) {
528
+ var path = _ref7.path,
529
+ quoteChar = _ref7.quoteChar;
530
+ return "jest.mock(".concat(quoteChar).concat(path).concat(quoteChar, ");");
531
+ }
532
+
533
+ /**
534
+ * Creates a fix to remove a node with proper whitespace handling.
535
+ * Removes surrounding newlines to avoid leaving blank lines.
536
+ */
537
+ function createNodeRemovalFix(_ref8) {
538
+ var fixer = _ref8.fixer,
539
+ node = _ref8.node,
540
+ sourceCode = _ref8.sourceCode;
541
+ var nodeStart = node.range[0];
542
+ var nodeEnd = node.range[1];
543
+
544
+ // Check for leading newline (prefer removing the line separator before the node)
545
+ var textBeforeNode = sourceCode.text.slice(0, nodeStart);
546
+ var leadingNewlineMatch = textBeforeNode.match(/(\r?\n)$/);
547
+ if (leadingNewlineMatch) {
548
+ // Remove the leading newline plus the node
549
+ return fixer.removeRange([nodeStart - leadingNewlineMatch[1].length, nodeEnd]);
550
+ }
551
+
552
+ // No leading newline - check for trailing newline
553
+ var textAfterNode = sourceCode.text.slice(nodeEnd);
554
+ var trailingNewlineMatch = textAfterNode.match(/^(\r?\n)/);
555
+ if (trailingNewlineMatch) {
556
+ return fixer.removeRange([nodeStart, nodeEnd + trailingNewlineMatch[1].length]);
557
+ }
558
+ return fixer.remove(node);
559
+ }
560
+
561
+ /**
562
+ * Creates the auto-fix for barrel import violations.
563
+ * Generates new import statements and handles merging with existing imports.
564
+ * Also updates Jest automocks (jest.mock calls with only a path) when present.
565
+ */
566
+ function createBarrelImportFix(_ref9) {
567
+ var fixer = _ref9.fixer,
568
+ node = _ref9.node,
569
+ context = _ref9.context,
570
+ importContext = _ref9.importContext,
571
+ specifiersByTarget = _ref9.specifiersByTarget,
572
+ unmappedSpecifiers = _ref9.unmappedSpecifiers;
573
+ var importPath = importContext.importPath,
574
+ packageName = importContext.packageName;
575
+ var sourceCode = context.sourceCode;
576
+ var quote = sourceCode.getText(node.source)[0]; // Get quote character
577
+
578
+ var fixes = [];
579
+ var newStatements = [];
580
+
581
+ // Find any Jest automocks that match this import path
582
+ var automocks = findMatchingAutomocks({
583
+ sourceCode: sourceCode,
584
+ importPath: importPath
585
+ });
586
+
587
+ // Track which new import paths need automocks (only value imports, not type-only)
588
+ var automockPaths = [];
589
+
590
+ // Track if we have any value imports at all (to determine if automocks should be updated)
591
+ var hasAnyValueImports = false;
592
+
593
+ // Get all existing imports to check for merging
594
+ var allImports = sourceCode.ast.body.filter(function (n) {
595
+ return n.type === 'ImportDeclaration' && n !== node;
596
+ });
597
+
598
+ // Generate new import statements for each target export path
599
+ var _iterator3 = _createForOfIteratorHelper(specifiersByTarget),
600
+ _step3;
601
+ try {
602
+ var _loop2 = function _loop2() {
603
+ var _step3$value = _slicedToArray(_step3.value, 2),
604
+ targetExportPath = _step3$value[0],
605
+ specsWithTarget = _step3$value[1];
606
+ // Check if this is a cross-package re-export (sourcePackageName is set)
607
+ var isCrossPackage = specsWithTarget.some(function (s) {
608
+ return s.sourcePackageName;
609
+ });
610
+ var newImportPath = isCrossPackage ? targetExportPath // For cross-package, targetExportPath is already the full import path (e.g., @package/subpath)
611
+ : packageName + targetExportPath.slice(1); // Remove leading '.' for same-package imports
612
+
613
+ // Transform specifiers if needed (handle aliasing)
614
+ var specs = specsWithTarget.map(function (_ref1) {
615
+ var spec = _ref1.spec,
616
+ originalName = _ref1.originalName,
617
+ kind = _ref1.kind;
618
+ return transformSpecifierForExport({
619
+ spec: spec,
620
+ originalName: originalName,
621
+ kind: kind
622
+ });
623
+ });
624
+
625
+ // Check if any specifier in this group is a value import (not type-only)
626
+ // Only add automock paths for value imports (types don't need mocking at runtime)
627
+ if (automocks.length > 0) {
628
+ var hasValueImport = specsWithTarget.some(function (_ref10) {
629
+ var kind = _ref10.kind,
630
+ spec = _ref10.spec;
631
+ return kind === 'value' && (spec.type !== 'ImportSpecifier' || spec.importKind !== 'type');
632
+ });
633
+ if (hasValueImport) {
634
+ hasAnyValueImports = true;
635
+ automockPaths.push(newImportPath);
636
+ }
637
+ }
638
+
639
+ // Check for existing import from the same path
640
+ var existingImport = allImports.find(function (n) {
641
+ return n.source.value === newImportPath;
642
+ });
643
+
644
+ // Skip merging if existing is namespace import
645
+ var isNamespace = existingImport === null || existingImport === void 0 ? void 0 : existingImport.specifiers.some(function (s) {
646
+ return s.type === 'ImportNamespaceSpecifier';
647
+ });
648
+ if (existingImport && !isNamespace) {
649
+ // Merge with existing import
650
+ var newImportStatement = buildMergedImportStatement({
651
+ existingImport: existingImport,
652
+ newSpecs: specs,
653
+ newImportPath: newImportPath,
654
+ nodeImportKind: node.importKind,
655
+ quoteChar: quote
656
+ });
657
+ if (newImportStatement.length > 0) {
658
+ fixes.push(fixer.replaceText(existingImport, newImportStatement));
659
+ }
660
+ } else {
661
+ // Create new import
662
+ var _isTypeImport = node.importKind === 'type';
663
+ var importStatement = buildImportStatement({
664
+ specs: specs,
665
+ path: newImportPath,
666
+ quoteChar: quote,
667
+ isTypeImport: _isTypeImport
668
+ });
669
+ if (importStatement.length > 0) {
670
+ newStatements.push(importStatement);
671
+ }
672
+ }
673
+ };
674
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
675
+ _loop2();
676
+ }
677
+
678
+ // Handle unmapped specifiers - they stay in the original import
679
+ } catch (err) {
680
+ _iterator3.e(err);
681
+ } finally {
682
+ _iterator3.f();
683
+ }
684
+ if (unmappedSpecifiers.length > 0) {
685
+ var unmappedSpecs = unmappedSpecifiers.map(function (u) {
686
+ return u.spec;
687
+ });
688
+ var isTypeImport = node.importKind === 'type';
689
+ var remainingImport = buildImportStatement({
690
+ specs: unmappedSpecs,
691
+ path: importPath,
692
+ quoteChar: quote,
693
+ isTypeImport: isTypeImport
694
+ });
695
+ if (remainingImport.length > 0) {
696
+ newStatements.push(remainingImport);
697
+ }
698
+
699
+ // If there are unmapped value specifiers and automocks, keep the original automock path too
700
+ if (automocks.length > 0) {
701
+ var hasUnmappedValueImport = unmappedSpecifiers.some(function (_ref0) {
702
+ var kind = _ref0.kind,
703
+ spec = _ref0.spec;
704
+ return kind === 'value' && (spec.type !== 'ImportSpecifier' || spec.importKind !== 'type');
705
+ });
706
+ if (hasUnmappedValueImport) {
707
+ hasAnyValueImports = true;
708
+ automockPaths.push(importPath);
709
+ }
710
+ }
711
+ }
712
+ if (newStatements.length > 0) {
713
+ fixes.push(fixer.replaceText(node, newStatements.join('\n')));
714
+ } else {
715
+ // If all were merged, remove the node including surrounding whitespace/newlines
716
+ fixes.push(createNodeRemovalFix({
717
+ fixer: fixer,
718
+ node: node,
719
+ sourceCode: sourceCode
720
+ }));
721
+ }
722
+
723
+ // Handle automock updates
724
+ // Only modify automocks if there are value imports being fixed
725
+ // Type-only imports don't need runtime mocking, so we preserve existing automocks
726
+ if (automocks.length > 0 && hasAnyValueImports && automockPaths.length > 0) {
727
+ var _iterator4 = _createForOfIteratorHelper(automocks),
728
+ _step4;
729
+ try {
730
+ var _loop = function _loop() {
731
+ var automock = _step4.value;
732
+ // Build new automock statements for all new paths
733
+ var newAutomockStatements = automockPaths.map(function (path) {
734
+ return buildAutomockStatement({
735
+ path: path,
736
+ quoteChar: automock.quoteChar
737
+ });
738
+ });
739
+
740
+ // Replace the original automock statement with the new automock(s)
741
+ fixes.push(fixer.replaceTextRange(automock.statementNode.range, newAutomockStatements.join('\n')));
742
+ };
743
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
744
+ _loop();
745
+ }
746
+ } catch (err) {
747
+ _iterator4.e(err);
748
+ } finally {
749
+ _iterator4.f();
750
+ }
751
+ }
752
+ return fixes;
753
+ }
754
+
755
+ /**
756
+ * Handles an ImportDeclaration node to check for barrel file imports.
757
+ * Reports and auto-fixes imports that could use more specific export paths.
758
+ */
759
+ function handleImportDeclaration(_ref11) {
760
+ var node = _ref11.node,
761
+ context = _ref11.context,
762
+ workspaceRoot = _ref11.workspaceRoot,
763
+ fs = _ref11.fs,
764
+ applyToImportsFrom = _ref11.applyToImportsFrom;
765
+ // Resolve import context (validates and extracts package/export info)
766
+ // applyToImportsFrom is used here to filter which packages the rule applies to
767
+ var importContext = resolveImportContext({
768
+ node: node,
769
+ workspaceRoot: workspaceRoot,
770
+ fs: fs,
771
+ applyToImportsFrom: applyToImportsFrom
772
+ });
773
+ if (!importContext) {
774
+ return;
775
+ }
776
+
777
+ // Check each imported specifier to see if we can find a more specific export
778
+ if (node.specifiers.length === 0) {
779
+ return;
780
+ }
781
+
782
+ // Classify specifiers by their target export paths
783
+ var _classifySpecifiers = classifySpecifiers({
784
+ node: node,
785
+ importContext: importContext,
786
+ workspaceRoot: workspaceRoot,
787
+ fs: fs
788
+ }),
789
+ specifiersByTarget = _classifySpecifiers.specifiersByTarget,
790
+ unmappedSpecifiers = _classifySpecifiers.unmappedSpecifiers,
791
+ hasNamespaceImport = _classifySpecifiers.hasNamespaceImport;
792
+
793
+ // If namespace import, report without auto-fix if there are specific exports available
794
+ if (hasNamespaceImport) {
795
+ if (specifiersByTarget.size > 0) {
796
+ context.report({
797
+ node: node,
798
+ messageId: 'barrelEntryImport',
799
+ data: {
800
+ path: importContext.importPath
801
+ }
802
+ });
803
+ }
804
+ return;
805
+ }
806
+
807
+ // If no specifiers can be remapped to more specific imports, don't report
808
+ if (specifiersByTarget.size === 0) {
809
+ return;
810
+ }
811
+
812
+ // Report with auto-fix
813
+ context.report({
814
+ node: node,
815
+ messageId: 'barrelEntryImport',
816
+ data: {
817
+ path: importContext.importPath
818
+ },
819
+ fix: function fix(fixer) {
820
+ return createBarrelImportFix({
821
+ fixer: fixer,
822
+ node: node,
823
+ context: context,
824
+ importContext: importContext,
825
+ specifiersByTarget: specifiersByTarget,
826
+ unmappedSpecifiers: unmappedSpecifiers
827
+ });
828
+ }
829
+ });
830
+ }
831
+
832
+ /**
833
+ * Factory function to create the ESLint rule with a given file system.
834
+ * This enables testing with mock file systems.
835
+ */
836
+ export function createRule(fs) {
837
+ return {
838
+ meta: ruleMeta,
839
+ create: function create(context) {
840
+ var _options$applyToImpor;
841
+ var options = context.options[0] || {};
842
+ var applyToImportsFrom = (_options$applyToImpor = options.applyToImportsFrom) !== null && _options$applyToImpor !== void 0 ? _options$applyToImpor : DEFAULT_TARGET_FOLDERS;
843
+ var workspaceRoot = findWorkspaceRoot({
844
+ startPath: dirname(context.filename),
845
+ fs: fs,
846
+ applyToImportsFrom: applyToImportsFrom
847
+ });
848
+ return {
849
+ ImportDeclaration: function ImportDeclaration(rawNode) {
850
+ var node = rawNode;
851
+ handleImportDeclaration({
852
+ node: node,
853
+ context: context,
854
+ workspaceRoot: workspaceRoot,
855
+ fs: fs,
856
+ applyToImportsFrom: applyToImportsFrom
857
+ });
858
+ }
859
+ };
860
+ }
861
+ };
862
+ }
863
+ var rule = createRule(realFileSystem);
864
+ export default rule;