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