@atlaskit/eslint-plugin-platform 2.8.0 → 2.9.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/CHANGELOG.md +9 -0
- package/dist/cjs/index.js +6 -1
- package/dist/cjs/rules/ensure-use-sync-external-store-server-snapshot/index.js +41 -0
- package/dist/cjs/rules/import/no-barrel-entry-imports/index.js +475 -67
- package/dist/cjs/rules/import/no-barrel-entry-jest-mock/index.js +387 -112
- package/dist/cjs/rules/import/no-jest-mock-barrel-files/index.js +3 -2
- package/dist/cjs/rules/import/no-relative-barrel-file-imports/index.js +7 -3
- package/dist/cjs/rules/import/shared/jest-utils.js +62 -9
- package/dist/cjs/rules/import/shared/package-resolution.js +156 -23
- package/dist/cjs/rules/visit-example-type-import-required/index.js +409 -0
- package/dist/es2019/index.js +6 -1
- package/dist/es2019/rules/ensure-use-sync-external-store-server-snapshot/index.js +43 -0
- package/dist/es2019/rules/import/no-barrel-entry-imports/index.js +372 -15
- package/dist/es2019/rules/import/no-barrel-entry-jest-mock/index.js +245 -17
- package/dist/es2019/rules/import/no-jest-mock-barrel-files/index.js +3 -2
- package/dist/es2019/rules/import/no-relative-barrel-file-imports/index.js +7 -3
- package/dist/es2019/rules/import/shared/jest-utils.js +44 -0
- package/dist/es2019/rules/import/shared/package-resolution.js +97 -5
- package/dist/es2019/rules/visit-example-type-import-required/index.js +375 -0
- package/dist/esm/index.js +6 -1
- package/dist/esm/rules/ensure-use-sync-external-store-server-snapshot/index.js +35 -0
- package/dist/esm/rules/import/no-barrel-entry-imports/index.js +475 -67
- package/dist/esm/rules/import/no-barrel-entry-jest-mock/index.js +388 -113
- package/dist/esm/rules/import/no-jest-mock-barrel-files/index.js +3 -2
- package/dist/esm/rules/import/no-relative-barrel-file-imports/index.js +7 -3
- package/dist/esm/rules/import/shared/jest-utils.js +61 -9
- package/dist/esm/rules/import/shared/package-resolution.js +156 -25
- package/dist/esm/rules/visit-example-type-import-required/index.js +402 -0
- package/dist/types/index.d.ts +12 -0
- package/dist/types/rules/ensure-use-sync-external-store-server-snapshot/index.d.ts +3 -0
- package/dist/types/rules/import/shared/jest-utils.d.ts +8 -0
- package/dist/types/rules/import/shared/package-resolution.d.ts +22 -2
- package/dist/types/rules/visit-example-type-import-required/index.d.ts +4 -0
- package/dist/types-ts4.5/index.d.ts +12 -0
- package/dist/types-ts4.5/rules/ensure-use-sync-external-store-server-snapshot/index.d.ts +3 -0
- package/dist/types-ts4.5/rules/import/shared/jest-utils.d.ts +8 -0
- package/dist/types-ts4.5/rules/import/shared/package-resolution.d.ts +22 -2
- package/dist/types-ts4.5/rules/visit-example-type-import-required/index.d.ts +4 -0
- package/package.json +3 -1
|
@@ -130,19 +130,14 @@ function buildImportStatement(_ref) {
|
|
|
130
130
|
*/
|
|
131
131
|
|
|
132
132
|
/**
|
|
133
|
-
* Resolves import context for barrel file analysis.
|
|
133
|
+
* Resolves import context for barrel file analysis from a module specifier string.
|
|
134
134
|
* Returns null if the import should not be processed (relative import, not in target folder, etc.)
|
|
135
135
|
*/
|
|
136
|
-
function
|
|
137
|
-
var
|
|
136
|
+
function resolveImportContextFromModulePath(_ref2) {
|
|
137
|
+
var importPath = _ref2.importPath,
|
|
138
138
|
workspaceRoot = _ref2.workspaceRoot,
|
|
139
139
|
fs = _ref2.fs,
|
|
140
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
141
|
// Skip relative imports - this rule is for cross-package imports
|
|
147
142
|
if (isRelativeImport(importPath)) {
|
|
148
143
|
return null;
|
|
@@ -216,16 +211,36 @@ function resolveImportContext(_ref2) {
|
|
|
216
211
|
};
|
|
217
212
|
}
|
|
218
213
|
|
|
214
|
+
/**
|
|
215
|
+
* Resolves import context for barrel file analysis.
|
|
216
|
+
* Returns null if the import should not be processed (relative import, not in target folder, etc.)
|
|
217
|
+
*/
|
|
218
|
+
function resolveImportContext(_ref3) {
|
|
219
|
+
var node = _ref3.node,
|
|
220
|
+
workspaceRoot = _ref3.workspaceRoot,
|
|
221
|
+
fs = _ref3.fs,
|
|
222
|
+
applyToImportsFrom = _ref3.applyToImportsFrom;
|
|
223
|
+
if (!node.source || typeof node.source.value !== 'string') {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
return resolveImportContextFromModulePath({
|
|
227
|
+
importPath: node.source.value,
|
|
228
|
+
workspaceRoot: workspaceRoot,
|
|
229
|
+
fs: fs,
|
|
230
|
+
applyToImportsFrom: applyToImportsFrom
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
219
234
|
/**
|
|
220
235
|
* Classifies import specifiers by their target export paths.
|
|
221
236
|
* Groups specifiers that can be remapped to more specific exports.
|
|
222
237
|
* For cross-package re-exports, suggests importing from the source package's most specific subpath.
|
|
223
238
|
*/
|
|
224
|
-
function classifySpecifiers(
|
|
225
|
-
var node =
|
|
226
|
-
importContext =
|
|
227
|
-
workspaceRoot =
|
|
228
|
-
fs =
|
|
239
|
+
function classifySpecifiers(_ref4) {
|
|
240
|
+
var node = _ref4.node,
|
|
241
|
+
importContext = _ref4.importContext,
|
|
242
|
+
workspaceRoot = _ref4.workspaceRoot,
|
|
243
|
+
fs = _ref4.fs;
|
|
229
244
|
var currentExportPath = importContext.currentExportPath,
|
|
230
245
|
exportsMap = importContext.exportsMap,
|
|
231
246
|
exportMap = importContext.exportMap;
|
|
@@ -258,7 +273,7 @@ function classifySpecifiers(_ref3) {
|
|
|
258
273
|
}
|
|
259
274
|
var exportInfo = exportMap.get(nameInSource);
|
|
260
275
|
if (exportInfo) {
|
|
261
|
-
var _exportInfo$crossPack;
|
|
276
|
+
var _exportInfo$crossPack, _exportInfo$originalN2, _matchResult$exportPa2;
|
|
262
277
|
var effectiveKind = kind === 'type' || exportInfo.isTypeOnly ? 'type' : 'value';
|
|
263
278
|
|
|
264
279
|
// Check if this is a cross-package re-export
|
|
@@ -284,11 +299,20 @@ function classifySpecifiers(_ref3) {
|
|
|
284
299
|
|
|
285
300
|
// Find the best export path in the source package
|
|
286
301
|
var _targetExportPath = null;
|
|
302
|
+
var resolvedOriginalName = exportInfo.originalName;
|
|
287
303
|
if (sourcePackageExportsMap) {
|
|
288
|
-
|
|
304
|
+
var _exportInfo$originalN, _matchResult$exportPa;
|
|
305
|
+
var _sourceExportName = (_exportInfo$originalN = exportInfo.originalName) !== null && _exportInfo$originalN !== void 0 ? _exportInfo$originalN : nameInSource;
|
|
306
|
+
var _matchResult = findExportForSourceFile({
|
|
289
307
|
sourceFilePath: exportInfo.path,
|
|
290
|
-
exportsMap: sourcePackageExportsMap
|
|
308
|
+
exportsMap: sourcePackageExportsMap,
|
|
309
|
+
fs: fs,
|
|
310
|
+
sourceExportName: _sourceExportName
|
|
291
311
|
});
|
|
312
|
+
_targetExportPath = (_matchResult$exportPa = _matchResult === null || _matchResult === void 0 ? void 0 : _matchResult.exportPath) !== null && _matchResult$exportPa !== void 0 ? _matchResult$exportPa : null;
|
|
313
|
+
if ((_matchResult === null || _matchResult === void 0 ? void 0 : _matchResult.entryPointExportName) !== undefined) {
|
|
314
|
+
resolvedOriginalName = _matchResult.entryPointExportName === nameInSource ? undefined : _matchResult.entryPointExportName;
|
|
315
|
+
}
|
|
292
316
|
}
|
|
293
317
|
|
|
294
318
|
// Build the full import path: @package/subpath or just @package if no subpath found
|
|
@@ -301,7 +325,7 @@ function classifySpecifiers(_ref3) {
|
|
|
301
325
|
spec: _objectSpread(_objectSpread({}, spec), {}, {
|
|
302
326
|
importKind: effectiveKind
|
|
303
327
|
}),
|
|
304
|
-
originalName:
|
|
328
|
+
originalName: resolvedOriginalName,
|
|
305
329
|
targetExportPath: targetKey,
|
|
306
330
|
kind: effectiveKind,
|
|
307
331
|
sourcePackageName: sourcePackageName
|
|
@@ -310,10 +334,18 @@ function classifySpecifiers(_ref3) {
|
|
|
310
334
|
}
|
|
311
335
|
|
|
312
336
|
// Find if there's a package.json export that points to this source file
|
|
313
|
-
var
|
|
337
|
+
var sourceExportName = (_exportInfo$originalN2 = exportInfo.originalName) !== null && _exportInfo$originalN2 !== void 0 ? _exportInfo$originalN2 : nameInSource;
|
|
338
|
+
var matchResult = findExportForSourceFile({
|
|
314
339
|
sourceFilePath: exportInfo.path,
|
|
315
|
-
exportsMap: exportsMap
|
|
340
|
+
exportsMap: exportsMap,
|
|
341
|
+
fs: fs,
|
|
342
|
+
sourceExportName: sourceExportName
|
|
316
343
|
});
|
|
344
|
+
var targetExportPath = (_matchResult$exportPa2 = matchResult === null || matchResult === void 0 ? void 0 : matchResult.exportPath) !== null && _matchResult$exportPa2 !== void 0 ? _matchResult$exportPa2 : null;
|
|
345
|
+
var resolvedOriginalName2 = exportInfo.originalName;
|
|
346
|
+
if ((matchResult === null || matchResult === void 0 ? void 0 : matchResult.entryPointExportName) !== undefined) {
|
|
347
|
+
resolvedOriginalName2 = matchResult.entryPointExportName === nameInSource ? undefined : matchResult.entryPointExportName;
|
|
348
|
+
}
|
|
317
349
|
|
|
318
350
|
// Get the file that the current export path resolves to
|
|
319
351
|
var currentExportResolvedFile = exportsMap.get(currentExportPath);
|
|
@@ -332,7 +364,7 @@ function classifySpecifiers(_ref3) {
|
|
|
332
364
|
spec: _objectSpread(_objectSpread({}, spec), {}, {
|
|
333
365
|
importKind: effectiveKind
|
|
334
366
|
}),
|
|
335
|
-
originalName:
|
|
367
|
+
originalName: resolvedOriginalName2,
|
|
336
368
|
targetExportPath: targetExportPath,
|
|
337
369
|
kind: effectiveKind
|
|
338
370
|
});
|
|
@@ -368,10 +400,10 @@ function classifySpecifiers(_ref3) {
|
|
|
368
400
|
* Transforms a specifier to use the original export name (handling aliasing).
|
|
369
401
|
* Converts named imports of default exports to ImportDefaultSpecifier.
|
|
370
402
|
*/
|
|
371
|
-
function transformSpecifierForExport(
|
|
372
|
-
var spec =
|
|
373
|
-
originalName =
|
|
374
|
-
kind =
|
|
403
|
+
function transformSpecifierForExport(_ref5) {
|
|
404
|
+
var spec = _ref5.spec,
|
|
405
|
+
originalName = _ref5.originalName,
|
|
406
|
+
kind = _ref5.kind;
|
|
375
407
|
if (!originalName) {
|
|
376
408
|
return spec;
|
|
377
409
|
}
|
|
@@ -420,12 +452,12 @@ function transformSpecifierForExport(_ref4) {
|
|
|
420
452
|
* Merges new specifiers with an existing import declaration.
|
|
421
453
|
* Returns the new import statement string.
|
|
422
454
|
*/
|
|
423
|
-
function buildMergedImportStatement(
|
|
424
|
-
var existingImport =
|
|
425
|
-
newSpecs =
|
|
426
|
-
newImportPath =
|
|
427
|
-
nodeImportKind =
|
|
428
|
-
quoteChar =
|
|
455
|
+
function buildMergedImportStatement(_ref6) {
|
|
456
|
+
var existingImport = _ref6.existingImport,
|
|
457
|
+
newSpecs = _ref6.newSpecs,
|
|
458
|
+
newImportPath = _ref6.newImportPath,
|
|
459
|
+
nodeImportKind = _ref6.nodeImportKind,
|
|
460
|
+
quoteChar = _ref6.quoteChar;
|
|
429
461
|
var existingSpecs = existingImport.specifiers.map(function (s) {
|
|
430
462
|
if (existingImport.importKind === 'type') {
|
|
431
463
|
return _objectSpread(_objectSpread({}, s), {}, {
|
|
@@ -498,9 +530,9 @@ function getJestAutomock(node) {
|
|
|
498
530
|
/**
|
|
499
531
|
* Find all Jest automocks in the AST that match the given import path.
|
|
500
532
|
*/
|
|
501
|
-
function findMatchingAutomocks(
|
|
502
|
-
var sourceCode =
|
|
503
|
-
importPath =
|
|
533
|
+
function findMatchingAutomocks(_ref7) {
|
|
534
|
+
var sourceCode = _ref7.sourceCode,
|
|
535
|
+
importPath = _ref7.importPath;
|
|
504
536
|
var automocks = [];
|
|
505
537
|
var ast = sourceCode.ast;
|
|
506
538
|
var _iterator2 = _createForOfIteratorHelper(ast.body),
|
|
@@ -524,9 +556,9 @@ function findMatchingAutomocks(_ref6) {
|
|
|
524
556
|
/**
|
|
525
557
|
* Build a jest.mock() statement string
|
|
526
558
|
*/
|
|
527
|
-
function buildAutomockStatement(
|
|
528
|
-
var path =
|
|
529
|
-
quoteChar =
|
|
559
|
+
function buildAutomockStatement(_ref8) {
|
|
560
|
+
var path = _ref8.path,
|
|
561
|
+
quoteChar = _ref8.quoteChar;
|
|
530
562
|
return "jest.mock(".concat(quoteChar).concat(path).concat(quoteChar, ");");
|
|
531
563
|
}
|
|
532
564
|
|
|
@@ -534,10 +566,10 @@ function buildAutomockStatement(_ref7) {
|
|
|
534
566
|
* Creates a fix to remove a node with proper whitespace handling.
|
|
535
567
|
* Removes surrounding newlines to avoid leaving blank lines.
|
|
536
568
|
*/
|
|
537
|
-
function createNodeRemovalFix(
|
|
538
|
-
var fixer =
|
|
539
|
-
node =
|
|
540
|
-
sourceCode =
|
|
569
|
+
function createNodeRemovalFix(_ref9) {
|
|
570
|
+
var fixer = _ref9.fixer,
|
|
571
|
+
node = _ref9.node,
|
|
572
|
+
sourceCode = _ref9.sourceCode;
|
|
541
573
|
var nodeStart = node.range[0];
|
|
542
574
|
var nodeEnd = node.range[1];
|
|
543
575
|
|
|
@@ -563,13 +595,13 @@ function createNodeRemovalFix(_ref8) {
|
|
|
563
595
|
* Generates new import statements and handles merging with existing imports.
|
|
564
596
|
* Also updates Jest automocks (jest.mock calls with only a path) when present.
|
|
565
597
|
*/
|
|
566
|
-
function createBarrelImportFix(
|
|
567
|
-
var fixer =
|
|
568
|
-
node =
|
|
569
|
-
context =
|
|
570
|
-
importContext =
|
|
571
|
-
specifiersByTarget =
|
|
572
|
-
unmappedSpecifiers =
|
|
598
|
+
function createBarrelImportFix(_ref0) {
|
|
599
|
+
var fixer = _ref0.fixer,
|
|
600
|
+
node = _ref0.node,
|
|
601
|
+
context = _ref0.context,
|
|
602
|
+
importContext = _ref0.importContext,
|
|
603
|
+
specifiersByTarget = _ref0.specifiersByTarget,
|
|
604
|
+
unmappedSpecifiers = _ref0.unmappedSpecifiers;
|
|
573
605
|
var importPath = importContext.importPath,
|
|
574
606
|
packageName = importContext.packageName;
|
|
575
607
|
var sourceCode = context.sourceCode;
|
|
@@ -611,10 +643,10 @@ function createBarrelImportFix(_ref9) {
|
|
|
611
643
|
: packageName + targetExportPath.slice(1); // Remove leading '.' for same-package imports
|
|
612
644
|
|
|
613
645
|
// Transform specifiers if needed (handle aliasing)
|
|
614
|
-
var specs = specsWithTarget.map(function (
|
|
615
|
-
var spec =
|
|
616
|
-
originalName =
|
|
617
|
-
kind =
|
|
646
|
+
var specs = specsWithTarget.map(function (_ref10) {
|
|
647
|
+
var spec = _ref10.spec,
|
|
648
|
+
originalName = _ref10.originalName,
|
|
649
|
+
kind = _ref10.kind;
|
|
618
650
|
return transformSpecifierForExport({
|
|
619
651
|
spec: spec,
|
|
620
652
|
originalName: originalName,
|
|
@@ -625,9 +657,9 @@ function createBarrelImportFix(_ref9) {
|
|
|
625
657
|
// Check if any specifier in this group is a value import (not type-only)
|
|
626
658
|
// Only add automock paths for value imports (types don't need mocking at runtime)
|
|
627
659
|
if (automocks.length > 0) {
|
|
628
|
-
var hasValueImport = specsWithTarget.some(function (
|
|
629
|
-
var kind =
|
|
630
|
-
spec =
|
|
660
|
+
var hasValueImport = specsWithTarget.some(function (_ref11) {
|
|
661
|
+
var kind = _ref11.kind,
|
|
662
|
+
spec = _ref11.spec;
|
|
631
663
|
return kind === 'value' && (spec.type !== 'ImportSpecifier' || spec.importKind !== 'type');
|
|
632
664
|
});
|
|
633
665
|
if (hasValueImport) {
|
|
@@ -698,9 +730,9 @@ function createBarrelImportFix(_ref9) {
|
|
|
698
730
|
|
|
699
731
|
// If there are unmapped value specifiers and automocks, keep the original automock path too
|
|
700
732
|
if (automocks.length > 0) {
|
|
701
|
-
var hasUnmappedValueImport = unmappedSpecifiers.some(function (
|
|
702
|
-
var kind =
|
|
703
|
-
spec =
|
|
733
|
+
var hasUnmappedValueImport = unmappedSpecifiers.some(function (_ref1) {
|
|
734
|
+
var kind = _ref1.kind,
|
|
735
|
+
spec = _ref1.spec;
|
|
704
736
|
return kind === 'value' && (spec.type !== 'ImportSpecifier' || spec.importKind !== 'type');
|
|
705
737
|
});
|
|
706
738
|
if (hasUnmappedValueImport) {
|
|
@@ -751,17 +783,375 @@ function createBarrelImportFix(_ref9) {
|
|
|
751
783
|
}
|
|
752
784
|
return fixes;
|
|
753
785
|
}
|
|
786
|
+
function isPlainRequireCall(node) {
|
|
787
|
+
if (node.callee.type !== 'Identifier' || node.callee.name !== 'require') {
|
|
788
|
+
return false;
|
|
789
|
+
}
|
|
790
|
+
if (node.arguments.length !== 1) {
|
|
791
|
+
return false;
|
|
792
|
+
}
|
|
793
|
+
var arg = node.arguments[0];
|
|
794
|
+
return arg.type === 'Literal' && typeof arg.value === 'string';
|
|
795
|
+
}
|
|
796
|
+
function unwrapToRequireCall(expr) {
|
|
797
|
+
var e = expr;
|
|
798
|
+
for (;;) {
|
|
799
|
+
var wrapped = e;
|
|
800
|
+
if (wrapped.type !== 'ParenthesizedExpression' || !wrapped.expression) {
|
|
801
|
+
break;
|
|
802
|
+
}
|
|
803
|
+
e = wrapped.expression;
|
|
804
|
+
}
|
|
805
|
+
if (e.type !== 'CallExpression' || !isPlainRequireCall(e)) {
|
|
806
|
+
return null;
|
|
807
|
+
}
|
|
808
|
+
return e;
|
|
809
|
+
}
|
|
810
|
+
function buildSyntheticImportFromRequireAccess(exportPropertyName, modulePath) {
|
|
811
|
+
var specifiers = exportPropertyName === 'default' ? [{
|
|
812
|
+
type: 'ImportDefaultSpecifier',
|
|
813
|
+
local: {
|
|
814
|
+
type: 'Identifier',
|
|
815
|
+
name: '_r'
|
|
816
|
+
}
|
|
817
|
+
}] : [{
|
|
818
|
+
type: 'ImportSpecifier',
|
|
819
|
+
imported: {
|
|
820
|
+
type: 'Identifier',
|
|
821
|
+
name: exportPropertyName
|
|
822
|
+
},
|
|
823
|
+
local: {
|
|
824
|
+
type: 'Identifier',
|
|
825
|
+
name: exportPropertyName
|
|
826
|
+
}
|
|
827
|
+
}];
|
|
828
|
+
return {
|
|
829
|
+
type: 'ImportDeclaration',
|
|
830
|
+
source: {
|
|
831
|
+
type: 'Literal',
|
|
832
|
+
value: modulePath,
|
|
833
|
+
raw: "'".concat(modulePath, "'")
|
|
834
|
+
},
|
|
835
|
+
specifiers: specifiers,
|
|
836
|
+
importKind: 'value'
|
|
837
|
+
};
|
|
838
|
+
}
|
|
839
|
+
function fullNewImportPathForTarget(targetKey, specsWithTarget, packageName) {
|
|
840
|
+
var isCrossPackage = specsWithTarget.some(function (s) {
|
|
841
|
+
return s.sourcePackageName;
|
|
842
|
+
});
|
|
843
|
+
return isCrossPackage ? targetKey : packageName + targetKey.slice(1);
|
|
844
|
+
}
|
|
845
|
+
function getRhsPropertyAfterTransform(spec) {
|
|
846
|
+
if (spec.type === 'ImportDefaultSpecifier') {
|
|
847
|
+
return 'default';
|
|
848
|
+
}
|
|
849
|
+
return getImportedName(spec);
|
|
850
|
+
}
|
|
851
|
+
function appendAutomockFixesForPathMigration(_ref12) {
|
|
852
|
+
var fixer = _ref12.fixer,
|
|
853
|
+
sourceCode = _ref12.sourceCode,
|
|
854
|
+
oldBarrelPath = _ref12.oldBarrelPath,
|
|
855
|
+
newPaths = _ref12.newPaths;
|
|
856
|
+
var automocks = findMatchingAutomocks({
|
|
857
|
+
sourceCode: sourceCode,
|
|
858
|
+
importPath: oldBarrelPath
|
|
859
|
+
});
|
|
860
|
+
if (automocks.length === 0 || newPaths.length === 0) {
|
|
861
|
+
return [];
|
|
862
|
+
}
|
|
863
|
+
var fixes = [];
|
|
864
|
+
var _iterator5 = _createForOfIteratorHelper(automocks),
|
|
865
|
+
_step5;
|
|
866
|
+
try {
|
|
867
|
+
var _loop3 = function _loop3() {
|
|
868
|
+
var automock = _step5.value;
|
|
869
|
+
var newAutomockStatements = newPaths.map(function (path) {
|
|
870
|
+
return buildAutomockStatement({
|
|
871
|
+
path: path,
|
|
872
|
+
quoteChar: automock.quoteChar
|
|
873
|
+
});
|
|
874
|
+
});
|
|
875
|
+
fixes.push(fixer.replaceTextRange(automock.statementNode.range, newAutomockStatements.join('\n')));
|
|
876
|
+
};
|
|
877
|
+
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
|
|
878
|
+
_loop3();
|
|
879
|
+
}
|
|
880
|
+
} catch (err) {
|
|
881
|
+
_iterator5.e(err);
|
|
882
|
+
} finally {
|
|
883
|
+
_iterator5.f();
|
|
884
|
+
}
|
|
885
|
+
return fixes;
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
/**
|
|
889
|
+
* `require('barrel').default` or `require('barrel').namedExport`
|
|
890
|
+
*/
|
|
891
|
+
function handleRequireMemberExpression(_ref13) {
|
|
892
|
+
var node = _ref13.node,
|
|
893
|
+
context = _ref13.context,
|
|
894
|
+
workspaceRoot = _ref13.workspaceRoot,
|
|
895
|
+
fs = _ref13.fs,
|
|
896
|
+
applyToImportsFrom = _ref13.applyToImportsFrom;
|
|
897
|
+
if (node.computed || node.property.type !== 'Identifier') {
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
var reqCall = unwrapToRequireCall(node.object);
|
|
901
|
+
if (!reqCall) {
|
|
902
|
+
return;
|
|
903
|
+
}
|
|
904
|
+
var modulePath = reqCall.arguments[0].value;
|
|
905
|
+
var importContext = resolveImportContextFromModulePath({
|
|
906
|
+
importPath: modulePath,
|
|
907
|
+
workspaceRoot: workspaceRoot,
|
|
908
|
+
fs: fs,
|
|
909
|
+
applyToImportsFrom: applyToImportsFrom
|
|
910
|
+
});
|
|
911
|
+
if (!importContext) {
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
var exportPropertyName = node.property.name;
|
|
915
|
+
var synthetic = buildSyntheticImportFromRequireAccess(exportPropertyName, modulePath);
|
|
916
|
+
var _classifySpecifiers = classifySpecifiers({
|
|
917
|
+
node: synthetic,
|
|
918
|
+
importContext: importContext,
|
|
919
|
+
workspaceRoot: workspaceRoot,
|
|
920
|
+
fs: fs
|
|
921
|
+
}),
|
|
922
|
+
specifiersByTarget = _classifySpecifiers.specifiersByTarget,
|
|
923
|
+
hasNamespaceImport = _classifySpecifiers.hasNamespaceImport;
|
|
924
|
+
if (hasNamespaceImport || specifiersByTarget.size === 0) {
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
var entries = _toConsumableArray(specifiersByTarget.entries());
|
|
928
|
+
if (entries.length !== 1) {
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
var _ref14 = entries[0],
|
|
932
|
+
_ref15 = _slicedToArray(_ref14, 2),
|
|
933
|
+
targetKey = _ref15[0],
|
|
934
|
+
specsWithTarget = _ref15[1];
|
|
935
|
+
if (specsWithTarget.length !== 1) {
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
var st = specsWithTarget[0];
|
|
939
|
+
var newImportPath = fullNewImportPathForTarget(targetKey, specsWithTarget, importContext.packageName);
|
|
940
|
+
var transformed = transformSpecifierForExport({
|
|
941
|
+
spec: st.spec,
|
|
942
|
+
originalName: st.originalName,
|
|
943
|
+
kind: st.kind
|
|
944
|
+
});
|
|
945
|
+
var newRhs = getRhsPropertyAfterTransform(transformed);
|
|
946
|
+
var sourceCode = context.getSourceCode();
|
|
947
|
+
var quote = sourceCode.getText(reqCall.arguments[0])[0];
|
|
948
|
+
context.report({
|
|
949
|
+
node: node,
|
|
950
|
+
messageId: 'barrelEntryImport',
|
|
951
|
+
data: {
|
|
952
|
+
path: importContext.importPath
|
|
953
|
+
},
|
|
954
|
+
fix: function fix(fixer) {
|
|
955
|
+
var fixes = [];
|
|
956
|
+
fixes.push(fixer.replaceText(node, "require(".concat(quote).concat(newImportPath).concat(quote, ").").concat(newRhs)));
|
|
957
|
+
if (st.kind === 'value') {
|
|
958
|
+
fixes.push.apply(fixes, _toConsumableArray(appendAutomockFixesForPathMigration({
|
|
959
|
+
fixer: fixer,
|
|
960
|
+
sourceCode: sourceCode,
|
|
961
|
+
oldBarrelPath: modulePath,
|
|
962
|
+
newPaths: [newImportPath]
|
|
963
|
+
})));
|
|
964
|
+
}
|
|
965
|
+
return fixes;
|
|
966
|
+
}
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
/**
|
|
971
|
+
* `const { a, b } = require('barrel')`
|
|
972
|
+
*/
|
|
973
|
+
function handleRequireDestructuringDeclarator(_ref16) {
|
|
974
|
+
var node = _ref16.node,
|
|
975
|
+
context = _ref16.context,
|
|
976
|
+
workspaceRoot = _ref16.workspaceRoot,
|
|
977
|
+
fs = _ref16.fs,
|
|
978
|
+
applyToImportsFrom = _ref16.applyToImportsFrom;
|
|
979
|
+
if (node.id.type !== 'ObjectPattern' || !node.init || node.init.type !== 'CallExpression') {
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
var initCall = node.init;
|
|
983
|
+
if (!isPlainRequireCall(initCall)) {
|
|
984
|
+
return;
|
|
985
|
+
}
|
|
986
|
+
var modulePath = initCall.arguments[0].value;
|
|
987
|
+
var importContext = resolveImportContextFromModulePath({
|
|
988
|
+
importPath: modulePath,
|
|
989
|
+
workspaceRoot: workspaceRoot,
|
|
990
|
+
fs: fs,
|
|
991
|
+
applyToImportsFrom: applyToImportsFrom
|
|
992
|
+
});
|
|
993
|
+
if (!importContext) {
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
var specifiers = [];
|
|
997
|
+
var _iterator6 = _createForOfIteratorHelper(node.id.properties),
|
|
998
|
+
_step6;
|
|
999
|
+
try {
|
|
1000
|
+
for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
|
|
1001
|
+
var prop = _step6.value;
|
|
1002
|
+
if (prop.type !== 'Property' || prop.computed) {
|
|
1003
|
+
continue;
|
|
1004
|
+
}
|
|
1005
|
+
if (prop.key.type !== 'Identifier' || prop.value.type !== 'Identifier') {
|
|
1006
|
+
continue;
|
|
1007
|
+
}
|
|
1008
|
+
var importedName = prop.key.name;
|
|
1009
|
+
var localName = prop.value.name;
|
|
1010
|
+
specifiers.push({
|
|
1011
|
+
type: 'ImportSpecifier',
|
|
1012
|
+
imported: {
|
|
1013
|
+
type: 'Identifier',
|
|
1014
|
+
name: importedName
|
|
1015
|
+
},
|
|
1016
|
+
local: {
|
|
1017
|
+
type: 'Identifier',
|
|
1018
|
+
name: localName
|
|
1019
|
+
}
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
} catch (err) {
|
|
1023
|
+
_iterator6.e(err);
|
|
1024
|
+
} finally {
|
|
1025
|
+
_iterator6.f();
|
|
1026
|
+
}
|
|
1027
|
+
if (specifiers.length === 0) {
|
|
1028
|
+
return;
|
|
1029
|
+
}
|
|
1030
|
+
var synthetic = {
|
|
1031
|
+
type: 'ImportDeclaration',
|
|
1032
|
+
source: {
|
|
1033
|
+
type: 'Literal',
|
|
1034
|
+
value: modulePath,
|
|
1035
|
+
raw: "'".concat(modulePath, "'")
|
|
1036
|
+
},
|
|
1037
|
+
specifiers: specifiers,
|
|
1038
|
+
importKind: 'value'
|
|
1039
|
+
};
|
|
1040
|
+
var _classifySpecifiers2 = classifySpecifiers({
|
|
1041
|
+
node: synthetic,
|
|
1042
|
+
importContext: importContext,
|
|
1043
|
+
workspaceRoot: workspaceRoot,
|
|
1044
|
+
fs: fs
|
|
1045
|
+
}),
|
|
1046
|
+
specifiersByTarget = _classifySpecifiers2.specifiersByTarget,
|
|
1047
|
+
unmappedSpecifiers = _classifySpecifiers2.unmappedSpecifiers,
|
|
1048
|
+
hasNamespaceImport = _classifySpecifiers2.hasNamespaceImport;
|
|
1049
|
+
if (hasNamespaceImport || specifiersByTarget.size === 0 || unmappedSpecifiers.length > 0) {
|
|
1050
|
+
return;
|
|
1051
|
+
}
|
|
1052
|
+
var parentDecl = node.parent;
|
|
1053
|
+
if (parentDecl.type !== 'VariableDeclaration') {
|
|
1054
|
+
return;
|
|
1055
|
+
}
|
|
1056
|
+
if (specifiersByTarget.size > 1 && parentDecl.declarations.length !== 1) {
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
var sourceCode = context.getSourceCode();
|
|
1060
|
+
var quote = sourceCode.getText(initCall.arguments[0])[0];
|
|
1061
|
+
var pkg = importContext.packageName;
|
|
1062
|
+
var buildFixes = function buildFixes(fixer) {
|
|
1063
|
+
var fixes = [];
|
|
1064
|
+
var hasValue = false;
|
|
1065
|
+
var automockPaths = [];
|
|
1066
|
+
if (specifiersByTarget.size === 1) {
|
|
1067
|
+
var _ref17 = _toConsumableArray(specifiersByTarget.entries())[0],
|
|
1068
|
+
_ref18 = _slicedToArray(_ref17, 2),
|
|
1069
|
+
targetKey = _ref18[0],
|
|
1070
|
+
specsWithTarget = _ref18[1];
|
|
1071
|
+
var newImportPath = fullNewImportPathForTarget(targetKey, specsWithTarget, pkg);
|
|
1072
|
+
if (specsWithTarget.some(function (s) {
|
|
1073
|
+
return s.kind === 'value';
|
|
1074
|
+
})) {
|
|
1075
|
+
hasValue = true;
|
|
1076
|
+
automockPaths.push(newImportPath);
|
|
1077
|
+
}
|
|
1078
|
+
fixes.push(fixer.replaceText(initCall.arguments[0], "".concat(quote).concat(newImportPath).concat(quote)));
|
|
1079
|
+
} else {
|
|
1080
|
+
var lines = [];
|
|
1081
|
+
var _iterator7 = _createForOfIteratorHelper(specifiersByTarget),
|
|
1082
|
+
_step7;
|
|
1083
|
+
try {
|
|
1084
|
+
for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
|
|
1085
|
+
var _step7$value = _slicedToArray(_step7.value, 2),
|
|
1086
|
+
_targetKey = _step7$value[0],
|
|
1087
|
+
_specsWithTarget = _step7$value[1];
|
|
1088
|
+
var _newImportPath = fullNewImportPathForTarget(_targetKey, _specsWithTarget, pkg);
|
|
1089
|
+
if (_specsWithTarget.some(function (s) {
|
|
1090
|
+
return s.kind === 'value';
|
|
1091
|
+
})) {
|
|
1092
|
+
hasValue = true;
|
|
1093
|
+
automockPaths.push(_newImportPath);
|
|
1094
|
+
}
|
|
1095
|
+
var _iterator8 = _createForOfIteratorHelper(_specsWithTarget),
|
|
1096
|
+
_step8;
|
|
1097
|
+
try {
|
|
1098
|
+
for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
|
|
1099
|
+
var st = _step8.value;
|
|
1100
|
+
var transformed = transformSpecifierForExport({
|
|
1101
|
+
spec: st.spec,
|
|
1102
|
+
originalName: st.originalName,
|
|
1103
|
+
kind: st.kind
|
|
1104
|
+
});
|
|
1105
|
+
var rhs = getRhsPropertyAfterTransform(transformed);
|
|
1106
|
+
var local = st.spec.local.name;
|
|
1107
|
+
lines.push("".concat(local, " = require(").concat(quote).concat(_newImportPath).concat(quote, ").").concat(rhs));
|
|
1108
|
+
}
|
|
1109
|
+
} catch (err) {
|
|
1110
|
+
_iterator8.e(err);
|
|
1111
|
+
} finally {
|
|
1112
|
+
_iterator8.f();
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
} catch (err) {
|
|
1116
|
+
_iterator7.e(err);
|
|
1117
|
+
} finally {
|
|
1118
|
+
_iterator7.f();
|
|
1119
|
+
}
|
|
1120
|
+
var declText = lines.map(function (l) {
|
|
1121
|
+
return "".concat(parentDecl.kind, " ").concat(l, ";");
|
|
1122
|
+
}).join('\n');
|
|
1123
|
+
fixes.push(fixer.replaceText(parentDecl, declText));
|
|
1124
|
+
}
|
|
1125
|
+
if (hasValue) {
|
|
1126
|
+
fixes.push.apply(fixes, _toConsumableArray(appendAutomockFixesForPathMigration({
|
|
1127
|
+
fixer: fixer,
|
|
1128
|
+
sourceCode: sourceCode,
|
|
1129
|
+
oldBarrelPath: modulePath,
|
|
1130
|
+
newPaths: _toConsumableArray(new Set(automockPaths))
|
|
1131
|
+
})));
|
|
1132
|
+
}
|
|
1133
|
+
return fixes;
|
|
1134
|
+
};
|
|
1135
|
+
context.report({
|
|
1136
|
+
node: initCall,
|
|
1137
|
+
messageId: 'barrelEntryImport',
|
|
1138
|
+
data: {
|
|
1139
|
+
path: importContext.importPath
|
|
1140
|
+
},
|
|
1141
|
+
fix: buildFixes
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
754
1144
|
|
|
755
1145
|
/**
|
|
756
1146
|
* Handles an ImportDeclaration node to check for barrel file imports.
|
|
757
1147
|
* Reports and auto-fixes imports that could use more specific export paths.
|
|
758
1148
|
*/
|
|
759
|
-
function handleImportDeclaration(
|
|
760
|
-
var node =
|
|
761
|
-
context =
|
|
762
|
-
workspaceRoot =
|
|
763
|
-
fs =
|
|
764
|
-
applyToImportsFrom =
|
|
1149
|
+
function handleImportDeclaration(_ref19) {
|
|
1150
|
+
var node = _ref19.node,
|
|
1151
|
+
context = _ref19.context,
|
|
1152
|
+
workspaceRoot = _ref19.workspaceRoot,
|
|
1153
|
+
fs = _ref19.fs,
|
|
1154
|
+
applyToImportsFrom = _ref19.applyToImportsFrom;
|
|
765
1155
|
// Resolve import context (validates and extracts package/export info)
|
|
766
1156
|
// applyToImportsFrom is used here to filter which packages the rule applies to
|
|
767
1157
|
var importContext = resolveImportContext({
|
|
@@ -780,15 +1170,15 @@ function handleImportDeclaration(_ref11) {
|
|
|
780
1170
|
}
|
|
781
1171
|
|
|
782
1172
|
// Classify specifiers by their target export paths
|
|
783
|
-
var
|
|
1173
|
+
var _classifySpecifiers3 = classifySpecifiers({
|
|
784
1174
|
node: node,
|
|
785
1175
|
importContext: importContext,
|
|
786
1176
|
workspaceRoot: workspaceRoot,
|
|
787
1177
|
fs: fs
|
|
788
1178
|
}),
|
|
789
|
-
specifiersByTarget =
|
|
790
|
-
unmappedSpecifiers =
|
|
791
|
-
hasNamespaceImport =
|
|
1179
|
+
specifiersByTarget = _classifySpecifiers3.specifiersByTarget,
|
|
1180
|
+
unmappedSpecifiers = _classifySpecifiers3.unmappedSpecifiers,
|
|
1181
|
+
hasNamespaceImport = _classifySpecifiers3.hasNamespaceImport;
|
|
792
1182
|
|
|
793
1183
|
// If namespace import, report without auto-fix if there are specific exports available
|
|
794
1184
|
if (hasNamespaceImport) {
|
|
@@ -855,6 +1245,24 @@ export function createRule(fs) {
|
|
|
855
1245
|
fs: fs,
|
|
856
1246
|
applyToImportsFrom: applyToImportsFrom
|
|
857
1247
|
});
|
|
1248
|
+
},
|
|
1249
|
+
MemberExpression: function MemberExpression(rawNode) {
|
|
1250
|
+
handleRequireMemberExpression({
|
|
1251
|
+
node: rawNode,
|
|
1252
|
+
context: context,
|
|
1253
|
+
workspaceRoot: workspaceRoot,
|
|
1254
|
+
fs: fs,
|
|
1255
|
+
applyToImportsFrom: applyToImportsFrom
|
|
1256
|
+
});
|
|
1257
|
+
},
|
|
1258
|
+
VariableDeclarator: function VariableDeclarator(rawNode) {
|
|
1259
|
+
handleRequireDestructuringDeclarator({
|
|
1260
|
+
node: rawNode,
|
|
1261
|
+
context: context,
|
|
1262
|
+
workspaceRoot: workspaceRoot,
|
|
1263
|
+
fs: fs,
|
|
1264
|
+
applyToImportsFrom: applyToImportsFrom
|
|
1265
|
+
});
|
|
858
1266
|
}
|
|
859
1267
|
};
|
|
860
1268
|
}
|