@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
|
@@ -679,6 +679,7 @@ function traceSymbolsToExports(_ref10) {
|
|
|
679
679
|
_step13;
|
|
680
680
|
try {
|
|
681
681
|
for (_iterator13.s(); !(_step13 = _iterator13.n()).done;) {
|
|
682
|
+
var _findExportForSourceF, _findExportForSourceF2;
|
|
682
683
|
var symbolName = _step13.value;
|
|
683
684
|
var exportInfo = exportMap.get(symbolName);
|
|
684
685
|
if (!exportInfo) {
|
|
@@ -703,10 +704,10 @@ function traceSymbolsToExports(_ref10) {
|
|
|
703
704
|
}
|
|
704
705
|
|
|
705
706
|
// First try to find an export that directly exposes the source file
|
|
706
|
-
var targetExportPath = (0, _packageResolution.findExportForSourceFile)({
|
|
707
|
+
var targetExportPath = (_findExportForSourceF = (_findExportForSourceF2 = (0, _packageResolution.findExportForSourceFile)({
|
|
707
708
|
sourceFilePath: exportInfo.path,
|
|
708
709
|
exportsMap: exportsMap
|
|
709
|
-
});
|
|
710
|
+
})) === null || _findExportForSourceF2 === void 0 ? void 0 : _findExportForSourceF2.exportPath) !== null && _findExportForSourceF !== void 0 ? _findExportForSourceF : null;
|
|
710
711
|
|
|
711
712
|
// If no direct match, check which export can provide this symbol
|
|
712
713
|
// (handles nested barrels where the symbol is re-exported through intermediate files)
|
|
@@ -766,16 +767,80 @@ function escapeRegExp(str) {
|
|
|
766
767
|
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
767
768
|
}
|
|
768
769
|
|
|
770
|
+
/** Mock object keys that are module interop metadata, not package exports. */
|
|
771
|
+
var ESM_INTEROP_MOCK_KEYS = new Set(['__esModule']);
|
|
772
|
+
|
|
773
|
+
/**
|
|
774
|
+
* If a preamble line is `const|let actual = jest.requireActual('<barrel>')`, rewrite the specifier to
|
|
775
|
+
* `targetImportPathForThisMock` when every `binding.<prop>` read in this split mock's implementation
|
|
776
|
+
* resolves to that same path via `symbolToNewImportPath`. Otherwise leave unchanged (e.g. mixed paths
|
|
777
|
+
* still need the barrel module).
|
|
778
|
+
*/
|
|
779
|
+
function rewritePreambleLineBarrelRequireActual(_ref12) {
|
|
780
|
+
var lineText = _ref12.lineText,
|
|
781
|
+
oldBarrelPath = _ref12.oldBarrelPath,
|
|
782
|
+
targetImportPathForThisMock = _ref12.targetImportPathForThisMock,
|
|
783
|
+
mockImplementationTextForGroup = _ref12.mockImplementationTextForGroup,
|
|
784
|
+
symbolToNewImportPath = _ref12.symbolToNewImportPath;
|
|
785
|
+
var requireActualRe = /jest\.requireActual(?:<[^>]*>)?\((['"])([^'"]+)\1\)/;
|
|
786
|
+
var requireMatch = requireActualRe.exec(lineText);
|
|
787
|
+
if (!requireMatch || requireMatch[2] !== oldBarrelPath) {
|
|
788
|
+
return lineText;
|
|
789
|
+
}
|
|
790
|
+
var quote = requireMatch[1];
|
|
791
|
+
var bindingMatch = /^\s*(?:const|let)\s+(\w+)\s*=/m.exec(lineText);
|
|
792
|
+
if (!bindingMatch) {
|
|
793
|
+
return lineText;
|
|
794
|
+
}
|
|
795
|
+
var binding = bindingMatch[1];
|
|
796
|
+
var propAccessRe = new RegExp("\\b".concat(escapeRegExp(binding), "\\.(\\w+)"), 'g');
|
|
797
|
+
var accessedProps = new Set();
|
|
798
|
+
var propMatch;
|
|
799
|
+
while ((propMatch = propAccessRe.exec(mockImplementationTextForGroup)) !== null) {
|
|
800
|
+
accessedProps.add(propMatch[1]);
|
|
801
|
+
}
|
|
802
|
+
if (accessedProps.size === 0) {
|
|
803
|
+
return lineText;
|
|
804
|
+
}
|
|
805
|
+
var resolvedPaths = [];
|
|
806
|
+
var _iterator14 = _createForOfIteratorHelper(accessedProps),
|
|
807
|
+
_step14;
|
|
808
|
+
try {
|
|
809
|
+
for (_iterator14.s(); !(_step14 = _iterator14.n()).done;) {
|
|
810
|
+
var prop = _step14.value;
|
|
811
|
+
var mapped = symbolToNewImportPath.get(prop);
|
|
812
|
+
if (mapped) {
|
|
813
|
+
resolvedPaths.push(mapped);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
} catch (err) {
|
|
817
|
+
_iterator14.e(err);
|
|
818
|
+
} finally {
|
|
819
|
+
_iterator14.f();
|
|
820
|
+
}
|
|
821
|
+
if (resolvedPaths.length === 0) {
|
|
822
|
+
return lineText;
|
|
823
|
+
}
|
|
824
|
+
var uniquePaths = new Set(resolvedPaths);
|
|
825
|
+
if (uniquePaths.size !== 1 || !uniquePaths.has(targetImportPathForThisMock)) {
|
|
826
|
+
return lineText;
|
|
827
|
+
}
|
|
828
|
+
return lineText.replace("jest.requireActual(".concat(quote).concat(oldBarrelPath).concat(quote, ")"), "jest.requireActual(".concat(quote).concat(targetImportPathForThisMock).concat(quote, ")"));
|
|
829
|
+
}
|
|
830
|
+
|
|
769
831
|
/**
|
|
770
832
|
* Generate fix text for multiple jest.mock calls
|
|
771
833
|
*/
|
|
772
|
-
function generateMockFixes(
|
|
773
|
-
var groups =
|
|
774
|
-
crossPackageGroups =
|
|
775
|
-
packageName =
|
|
776
|
-
mockProperties =
|
|
777
|
-
quote =
|
|
778
|
-
preambleStatements =
|
|
834
|
+
function generateMockFixes(_ref13) {
|
|
835
|
+
var groups = _ref13.groups,
|
|
836
|
+
crossPackageGroups = _ref13.crossPackageGroups,
|
|
837
|
+
packageName = _ref13.packageName,
|
|
838
|
+
mockProperties = _ref13.mockProperties,
|
|
839
|
+
quote = _ref13.quote,
|
|
840
|
+
preambleStatements = _ref13.preambleStatements,
|
|
841
|
+
propagateEsModuleFromOriginalMock = _ref13.propagateEsModuleFromOriginalMock,
|
|
842
|
+
oldBarrelImportPath = _ref13.oldBarrelImportPath,
|
|
843
|
+
symbolToNewImportPath = _ref13.symbolToNewImportPath;
|
|
779
844
|
var mockCalls = [];
|
|
780
845
|
|
|
781
846
|
// Helper to generate a single mock call
|
|
@@ -783,15 +848,15 @@ function generateMockFixes(_ref12) {
|
|
|
783
848
|
var propTexts = [];
|
|
784
849
|
propTexts.push("...jest.requireActual(".concat(quote).concat(fullImportPath).concat(quote, ")"));
|
|
785
850
|
|
|
786
|
-
// Add __esModule: true when mocking default exports
|
|
787
|
-
if (group.hasDefaultExport) {
|
|
851
|
+
// Add __esModule: true when mocking default exports, or when the original mock already used __esModule (apply to every split).
|
|
852
|
+
if (group.hasDefaultExport || propagateEsModuleFromOriginalMock) {
|
|
788
853
|
propTexts.push('__esModule: true');
|
|
789
854
|
}
|
|
790
|
-
var
|
|
791
|
-
|
|
855
|
+
var _iterator15 = _createForOfIteratorHelper(group.propertyNames),
|
|
856
|
+
_step15;
|
|
792
857
|
try {
|
|
793
|
-
for (
|
|
794
|
-
var propName =
|
|
858
|
+
for (_iterator15.s(); !(_step15 = _iterator15.n()).done;) {
|
|
859
|
+
var propName = _step15.value;
|
|
795
860
|
// First try to get from group's propertyTexts (used for merged mocks)
|
|
796
861
|
var groupPropText = group.propertyTexts.get(propName);
|
|
797
862
|
if (groupPropText) {
|
|
@@ -825,21 +890,34 @@ function generateMockFixes(_ref12) {
|
|
|
825
890
|
}
|
|
826
891
|
}
|
|
827
892
|
}
|
|
828
|
-
|
|
829
|
-
// Determine if we need preamble for this group
|
|
830
893
|
} catch (err) {
|
|
831
|
-
|
|
894
|
+
_iterator15.e(err);
|
|
832
895
|
} finally {
|
|
833
|
-
|
|
896
|
+
_iterator15.f();
|
|
834
897
|
}
|
|
898
|
+
var combinedGroupImplText = group.propertyNames.map(function (name) {
|
|
899
|
+
var _ref14, _group$propertyTexts$, _mockProperties$get;
|
|
900
|
+
return (_ref14 = (_group$propertyTexts$ = group.propertyTexts.get(name)) !== null && _group$propertyTexts$ !== void 0 ? _group$propertyTexts$ : (_mockProperties$get = mockProperties.get(name)) === null || _mockProperties$get === void 0 ? void 0 : _mockProperties$get.text) !== null && _ref14 !== void 0 ? _ref14 : '';
|
|
901
|
+
}).join('\n');
|
|
902
|
+
|
|
903
|
+
// Determine if we need preamble for this group
|
|
835
904
|
var neededPreamble = getNeededPreamble({
|
|
836
905
|
propertyTexts: propTexts,
|
|
837
906
|
allPreamble: preambleStatements
|
|
838
907
|
});
|
|
908
|
+
var rewrittenPreamble = neededPreamble.map(function (p) {
|
|
909
|
+
return rewritePreambleLineBarrelRequireActual({
|
|
910
|
+
lineText: p.text,
|
|
911
|
+
oldBarrelPath: oldBarrelImportPath,
|
|
912
|
+
targetImportPathForThisMock: fullImportPath,
|
|
913
|
+
mockImplementationTextForGroup: combinedGroupImplText,
|
|
914
|
+
symbolToNewImportPath: symbolToNewImportPath
|
|
915
|
+
});
|
|
916
|
+
});
|
|
839
917
|
if (neededPreamble.length > 0) {
|
|
840
918
|
// Generate block body arrow function with preamble
|
|
841
|
-
var preambleLines =
|
|
842
|
-
return "\t".concat(
|
|
919
|
+
var preambleLines = rewrittenPreamble.map(function (text) {
|
|
920
|
+
return "\t".concat(text);
|
|
843
921
|
}).join('\n');
|
|
844
922
|
var formattedProps = propTexts.map(function (p) {
|
|
845
923
|
return "\t\t".concat(p, ",");
|
|
@@ -855,34 +933,34 @@ function generateMockFixes(_ref12) {
|
|
|
855
933
|
};
|
|
856
934
|
|
|
857
935
|
// Generate mocks for cross-package groups first
|
|
858
|
-
var
|
|
859
|
-
|
|
936
|
+
var _iterator16 = _createForOfIteratorHelper(crossPackageGroups),
|
|
937
|
+
_step16;
|
|
860
938
|
try {
|
|
861
|
-
for (
|
|
862
|
-
var group =
|
|
939
|
+
for (_iterator16.s(); !(_step16 = _iterator16.n()).done;) {
|
|
940
|
+
var group = _step16.value;
|
|
863
941
|
mockCalls.push(generateMockCall(group, group.importPath));
|
|
864
942
|
}
|
|
865
943
|
|
|
866
944
|
// Generate mocks for same-package groups
|
|
867
945
|
} catch (err) {
|
|
868
|
-
|
|
946
|
+
_iterator16.e(err);
|
|
869
947
|
} finally {
|
|
870
|
-
|
|
948
|
+
_iterator16.f();
|
|
871
949
|
}
|
|
872
|
-
var
|
|
873
|
-
|
|
950
|
+
var _iterator17 = _createForOfIteratorHelper(groups),
|
|
951
|
+
_step17;
|
|
874
952
|
try {
|
|
875
|
-
for (
|
|
876
|
-
var _group =
|
|
953
|
+
for (_iterator17.s(); !(_step17 = _iterator17.n()).done;) {
|
|
954
|
+
var _group = _step17.value;
|
|
877
955
|
var fullImportPath = "".concat(packageName).concat(_group.exportPath.slice(1));
|
|
878
956
|
mockCalls.push(generateMockCall(_group, fullImportPath));
|
|
879
957
|
}
|
|
880
958
|
|
|
881
959
|
// Join with semicolons but don't add trailing semicolon
|
|
882
960
|
} catch (err) {
|
|
883
|
-
|
|
961
|
+
_iterator17.e(err);
|
|
884
962
|
} finally {
|
|
885
|
-
|
|
963
|
+
_iterator17.f();
|
|
886
964
|
}
|
|
887
965
|
return mockCalls.join(';\n');
|
|
888
966
|
}
|
|
@@ -895,11 +973,11 @@ function generateMockFixes(_ref12) {
|
|
|
895
973
|
* Resolves jest.mock context for barrel file analysis.
|
|
896
974
|
* Returns null if the mock should not be processed.
|
|
897
975
|
*/
|
|
898
|
-
function resolveJestMockContext(
|
|
899
|
-
var importPath =
|
|
900
|
-
workspaceRoot =
|
|
901
|
-
fs =
|
|
902
|
-
applyToImportsFrom =
|
|
976
|
+
function resolveJestMockContext(_ref15) {
|
|
977
|
+
var importPath = _ref15.importPath,
|
|
978
|
+
workspaceRoot = _ref15.workspaceRoot,
|
|
979
|
+
fs = _ref15.fs,
|
|
980
|
+
applyToImportsFrom = _ref15.applyToImportsFrom;
|
|
903
981
|
if ((0, _fileSystem.isRelativeImport)(importPath)) {
|
|
904
982
|
return null;
|
|
905
983
|
}
|
|
@@ -962,9 +1040,9 @@ function resolveJestMockContext(_ref13) {
|
|
|
962
1040
|
/**
|
|
963
1041
|
* Check if the entry file is a barrel file (re-exports from other files)
|
|
964
1042
|
*/
|
|
965
|
-
function isBarrelFile(
|
|
966
|
-
var exportMap =
|
|
967
|
-
entryFilePath =
|
|
1043
|
+
function isBarrelFile(_ref16) {
|
|
1044
|
+
var exportMap = _ref16.exportMap,
|
|
1045
|
+
entryFilePath = _ref16.entryFilePath;
|
|
968
1046
|
return (0, _barrelParsing.hasReExportsFromOtherFiles)({
|
|
969
1047
|
exportMap: exportMap,
|
|
970
1048
|
sourceFilePath: entryFilePath
|
|
@@ -996,7 +1074,8 @@ var ruleMeta = {
|
|
|
996
1074
|
additionalProperties: false
|
|
997
1075
|
}],
|
|
998
1076
|
messages: {
|
|
999
|
-
barrelEntryMock: "jest.mock('{{path}}') is mocking a barrel file entry point. Split into separate mocks for each source file using package.json exports."
|
|
1077
|
+
barrelEntryMock: "jest.mock('{{path}}') is mocking a barrel file entry point. Split into separate mocks for each source file using package.json exports.",
|
|
1078
|
+
barrelEntryRequireActual: "jest.requireActual('{{path}}') references a barrel file entry point. Use a specific package.json export path instead."
|
|
1000
1079
|
}
|
|
1001
1080
|
};
|
|
1002
1081
|
|
|
@@ -1019,6 +1098,134 @@ function createRule(fs) {
|
|
|
1019
1098
|
return {
|
|
1020
1099
|
CallExpression: function CallExpression(rawNode) {
|
|
1021
1100
|
var node = rawNode;
|
|
1101
|
+
|
|
1102
|
+
// Handle standalone jest.requireActual() calls that reference barrel entries.
|
|
1103
|
+
// e.g. jest.requireActual('@atlaskit/pkg').Foo or const { Foo } = jest.requireActual('@atlaskit/pkg')
|
|
1104
|
+
if ((0, _jestUtils.isJestRequireActual)(node)) {
|
|
1105
|
+
var raImportPath = (0, _jestUtils.extractImportPath)(node);
|
|
1106
|
+
if (!raImportPath) {
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
var raContext = resolveJestMockContext({
|
|
1110
|
+
importPath: raImportPath,
|
|
1111
|
+
workspaceRoot: workspaceRoot,
|
|
1112
|
+
fs: fs,
|
|
1113
|
+
applyToImportsFrom: applyToImportsFrom
|
|
1114
|
+
});
|
|
1115
|
+
if (!raContext) {
|
|
1116
|
+
return;
|
|
1117
|
+
}
|
|
1118
|
+
if (!isBarrelFile({
|
|
1119
|
+
exportMap: raContext.exportMap,
|
|
1120
|
+
entryFilePath: raContext.entryFilePath
|
|
1121
|
+
})) {
|
|
1122
|
+
return;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
// `jest.requireActual('<barrel>')` inside `jest.mock('<barrel>')` is handled by the mock
|
|
1126
|
+
// rule's fix (preamble retargeting + `jest.requireActual('barrel').x` rewriting), including
|
|
1127
|
+
// `const actual = jest.requireActual('<barrel>')` with no member access on the call.
|
|
1128
|
+
// Skip standalone handling here to avoid duplicate diagnostics.
|
|
1129
|
+
var ancestor = node.parent;
|
|
1130
|
+
while (ancestor) {
|
|
1131
|
+
if (ancestor.type === 'CallExpression' && (0, _jestUtils.isJestMockCall)(ancestor)) {
|
|
1132
|
+
var ancestorPath = (0, _jestUtils.extractImportPath)(ancestor);
|
|
1133
|
+
if (ancestorPath) {
|
|
1134
|
+
var ancestorCtx = resolveJestMockContext({
|
|
1135
|
+
importPath: ancestorPath,
|
|
1136
|
+
workspaceRoot: workspaceRoot,
|
|
1137
|
+
fs: fs,
|
|
1138
|
+
applyToImportsFrom: applyToImportsFrom
|
|
1139
|
+
});
|
|
1140
|
+
if (ancestorCtx && isBarrelFile({
|
|
1141
|
+
exportMap: ancestorCtx.exportMap,
|
|
1142
|
+
entryFilePath: ancestorCtx.entryFilePath
|
|
1143
|
+
})) {
|
|
1144
|
+
return;
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
ancestor = ancestor.parent;
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
// Determine which symbols are accessed from the barrel
|
|
1152
|
+
var parent = node.parent;
|
|
1153
|
+
var accessedSymbols = [];
|
|
1154
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'MemberExpression' && parent.property.type === 'Identifier') {
|
|
1155
|
+
accessedSymbols.push(parent.property.name);
|
|
1156
|
+
} else if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'VariableDeclarator' && parent.id.type === 'ObjectPattern') {
|
|
1157
|
+
var _iterator18 = _createForOfIteratorHelper(parent.id.properties),
|
|
1158
|
+
_step18;
|
|
1159
|
+
try {
|
|
1160
|
+
for (_iterator18.s(); !(_step18 = _iterator18.n()).done;) {
|
|
1161
|
+
var prop = _step18.value;
|
|
1162
|
+
if (prop.type === 'Property' && prop.key.type === 'Identifier') {
|
|
1163
|
+
accessedSymbols.push(prop.key.name);
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
} catch (err) {
|
|
1167
|
+
_iterator18.e(err);
|
|
1168
|
+
} finally {
|
|
1169
|
+
_iterator18.f();
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
if (accessedSymbols.length === 0) {
|
|
1173
|
+
context.report({
|
|
1174
|
+
node: node,
|
|
1175
|
+
messageId: 'barrelEntryRequireActual',
|
|
1176
|
+
data: {
|
|
1177
|
+
path: raImportPath
|
|
1178
|
+
}
|
|
1179
|
+
});
|
|
1180
|
+
return;
|
|
1181
|
+
}
|
|
1182
|
+
var _traceSymbolsToExport = traceSymbolsToExports({
|
|
1183
|
+
symbolNames: accessedSymbols,
|
|
1184
|
+
exportMap: raContext.exportMap,
|
|
1185
|
+
exportsMap: raContext.exportsMap,
|
|
1186
|
+
currentExportPath: raContext.currentExportPath,
|
|
1187
|
+
fs: fs
|
|
1188
|
+
}),
|
|
1189
|
+
_groupedByExport = _traceSymbolsToExport.groupedByExport,
|
|
1190
|
+
_crossPackageGroups = _traceSymbolsToExport.crossPackageGroups;
|
|
1191
|
+
var newPath = null;
|
|
1192
|
+
if (_groupedByExport.size === 1 && _crossPackageGroups.size === 0) {
|
|
1193
|
+
var _groupedByExport$keys = _groupedByExport.keys(),
|
|
1194
|
+
_groupedByExport$keys2 = (0, _slicedToArray2.default)(_groupedByExport$keys, 1),
|
|
1195
|
+
exportPath = _groupedByExport$keys2[0];
|
|
1196
|
+
newPath = "".concat(raContext.packageName).concat(exportPath.slice(1));
|
|
1197
|
+
} else if (_crossPackageGroups.size === 1 && _groupedByExport.size === 0) {
|
|
1198
|
+
var _crossPackageGroups$k = _crossPackageGroups.keys(),
|
|
1199
|
+
_crossPackageGroups$k2 = (0, _slicedToArray2.default)(_crossPackageGroups$k, 1),
|
|
1200
|
+
cpImportPath = _crossPackageGroups$k2[0];
|
|
1201
|
+
newPath = cpImportPath;
|
|
1202
|
+
}
|
|
1203
|
+
var _sourceCode = context.getSourceCode();
|
|
1204
|
+
if (newPath) {
|
|
1205
|
+
var resolvedNewPath = newPath;
|
|
1206
|
+
context.report({
|
|
1207
|
+
node: node,
|
|
1208
|
+
messageId: 'barrelEntryRequireActual',
|
|
1209
|
+
data: {
|
|
1210
|
+
path: raImportPath
|
|
1211
|
+
},
|
|
1212
|
+
fix: function fix(fixer) {
|
|
1213
|
+
var firstArg = node.arguments[0];
|
|
1214
|
+
var quote = _sourceCode.getText(firstArg)[0];
|
|
1215
|
+
return fixer.replaceText(firstArg, "".concat(quote).concat(resolvedNewPath).concat(quote));
|
|
1216
|
+
}
|
|
1217
|
+
});
|
|
1218
|
+
} else {
|
|
1219
|
+
context.report({
|
|
1220
|
+
node: node,
|
|
1221
|
+
messageId: 'barrelEntryRequireActual',
|
|
1222
|
+
data: {
|
|
1223
|
+
path: raImportPath
|
|
1224
|
+
}
|
|
1225
|
+
});
|
|
1226
|
+
}
|
|
1227
|
+
return;
|
|
1228
|
+
}
|
|
1022
1229
|
if (!(0, _jestUtils.isJestMockCall)(node)) {
|
|
1023
1230
|
return;
|
|
1024
1231
|
}
|
|
@@ -1065,17 +1272,20 @@ function createRule(fs) {
|
|
|
1065
1272
|
if (mockProperties.size === 0) {
|
|
1066
1273
|
return;
|
|
1067
1274
|
}
|
|
1068
|
-
var
|
|
1069
|
-
var
|
|
1275
|
+
var originalMockHadEsModule = mockProperties.has('__esModule');
|
|
1276
|
+
var symbolNames = Array.from(mockProperties.keys()).filter(function (name) {
|
|
1277
|
+
return !ESM_INTEROP_MOCK_KEYS.has(name);
|
|
1278
|
+
});
|
|
1279
|
+
var _traceSymbolsToExport2 = traceSymbolsToExports({
|
|
1070
1280
|
symbolNames: symbolNames,
|
|
1071
1281
|
exportMap: mockContext.exportMap,
|
|
1072
1282
|
exportsMap: mockContext.exportsMap,
|
|
1073
1283
|
currentExportPath: mockContext.currentExportPath,
|
|
1074
1284
|
fs: fs
|
|
1075
1285
|
}),
|
|
1076
|
-
groupedByExport =
|
|
1077
|
-
crossPackageGroups =
|
|
1078
|
-
unmappedSymbols =
|
|
1286
|
+
groupedByExport = _traceSymbolsToExport2.groupedByExport,
|
|
1287
|
+
crossPackageGroups = _traceSymbolsToExport2.crossPackageGroups,
|
|
1288
|
+
unmappedSymbols = _traceSymbolsToExport2.unmappedSymbols;
|
|
1079
1289
|
|
|
1080
1290
|
// If no symbols can be mapped to specific exports or cross-package sources,
|
|
1081
1291
|
// there's nothing to fix so don't report an error
|
|
@@ -1083,20 +1293,20 @@ function createRule(fs) {
|
|
|
1083
1293
|
return;
|
|
1084
1294
|
}
|
|
1085
1295
|
var groups = [];
|
|
1086
|
-
var
|
|
1087
|
-
|
|
1296
|
+
var _iterator19 = _createForOfIteratorHelper(groupedByExport),
|
|
1297
|
+
_step19;
|
|
1088
1298
|
try {
|
|
1089
|
-
for (
|
|
1090
|
-
var
|
|
1091
|
-
|
|
1092
|
-
symbols =
|
|
1299
|
+
for (_iterator19.s(); !(_step19 = _iterator19.n()).done;) {
|
|
1300
|
+
var _step19$value = (0, _slicedToArray2.default)(_step19.value, 2),
|
|
1301
|
+
_exportPath = _step19$value[0],
|
|
1302
|
+
symbols = _step19$value[1];
|
|
1093
1303
|
// Build name mapping for aliased exports
|
|
1094
1304
|
var nameMapping = new Map();
|
|
1095
|
-
var
|
|
1096
|
-
|
|
1305
|
+
var _iterator25 = _createForOfIteratorHelper(symbols),
|
|
1306
|
+
_step25;
|
|
1097
1307
|
try {
|
|
1098
|
-
for (
|
|
1099
|
-
var s =
|
|
1308
|
+
for (_iterator25.s(); !(_step25 = _iterator25.n()).done;) {
|
|
1309
|
+
var s = _step25.value;
|
|
1100
1310
|
if (s.originalName) {
|
|
1101
1311
|
nameMapping.set(s.symbolName, s.originalName);
|
|
1102
1312
|
}
|
|
@@ -1104,16 +1314,16 @@ function createRule(fs) {
|
|
|
1104
1314
|
|
|
1105
1315
|
// Check if any symbol in this group is a default export
|
|
1106
1316
|
} catch (err) {
|
|
1107
|
-
|
|
1317
|
+
_iterator25.e(err);
|
|
1108
1318
|
} finally {
|
|
1109
|
-
|
|
1319
|
+
_iterator25.f();
|
|
1110
1320
|
}
|
|
1111
1321
|
var hasDefaultExport = symbols.some(function (s) {
|
|
1112
1322
|
return s.originalName === 'default';
|
|
1113
1323
|
});
|
|
1114
1324
|
groups.push({
|
|
1115
|
-
exportPath:
|
|
1116
|
-
importPath: "".concat(mockContext.packageName).concat(
|
|
1325
|
+
exportPath: _exportPath,
|
|
1326
|
+
importPath: "".concat(mockContext.packageName).concat(_exportPath.slice(1)),
|
|
1117
1327
|
propertyNames: symbols.map(function (s) {
|
|
1118
1328
|
return s.symbolName;
|
|
1119
1329
|
}),
|
|
@@ -1127,25 +1337,25 @@ function createRule(fs) {
|
|
|
1127
1337
|
|
|
1128
1338
|
// Build cross-package groups
|
|
1129
1339
|
} catch (err) {
|
|
1130
|
-
|
|
1340
|
+
_iterator19.e(err);
|
|
1131
1341
|
} finally {
|
|
1132
|
-
|
|
1342
|
+
_iterator19.f();
|
|
1133
1343
|
}
|
|
1134
1344
|
var crossPackageMockGroups = [];
|
|
1135
|
-
var
|
|
1136
|
-
|
|
1345
|
+
var _iterator20 = _createForOfIteratorHelper(crossPackageGroups),
|
|
1346
|
+
_step20;
|
|
1137
1347
|
try {
|
|
1138
|
-
for (
|
|
1139
|
-
var
|
|
1140
|
-
_importPath =
|
|
1141
|
-
_symbols =
|
|
1348
|
+
for (_iterator20.s(); !(_step20 = _iterator20.n()).done;) {
|
|
1349
|
+
var _step20$value = (0, _slicedToArray2.default)(_step20.value, 2),
|
|
1350
|
+
_importPath = _step20$value[0],
|
|
1351
|
+
_symbols = _step20$value[1];
|
|
1142
1352
|
// Build name mapping for aliased exports
|
|
1143
1353
|
var _nameMapping = new Map();
|
|
1144
|
-
var
|
|
1145
|
-
|
|
1354
|
+
var _iterator26 = _createForOfIteratorHelper(_symbols),
|
|
1355
|
+
_step26;
|
|
1146
1356
|
try {
|
|
1147
|
-
for (
|
|
1148
|
-
var _s =
|
|
1357
|
+
for (_iterator26.s(); !(_step26 = _iterator26.n()).done;) {
|
|
1358
|
+
var _s = _step26.value;
|
|
1149
1359
|
if (_s.originalName) {
|
|
1150
1360
|
_nameMapping.set(_s.symbolName, _s.originalName);
|
|
1151
1361
|
}
|
|
@@ -1153,9 +1363,9 @@ function createRule(fs) {
|
|
|
1153
1363
|
|
|
1154
1364
|
// Check if any symbol in this group is a default export
|
|
1155
1365
|
} catch (err) {
|
|
1156
|
-
|
|
1366
|
+
_iterator26.e(err);
|
|
1157
1367
|
} finally {
|
|
1158
|
-
|
|
1368
|
+
_iterator26.f();
|
|
1159
1369
|
}
|
|
1160
1370
|
var _hasDefaultExport = _symbols.some(function (s) {
|
|
1161
1371
|
return s.originalName === 'default';
|
|
@@ -1177,9 +1387,9 @@ function createRule(fs) {
|
|
|
1177
1387
|
});
|
|
1178
1388
|
}
|
|
1179
1389
|
} catch (err) {
|
|
1180
|
-
|
|
1390
|
+
_iterator20.e(err);
|
|
1181
1391
|
} finally {
|
|
1182
|
-
|
|
1392
|
+
_iterator20.f();
|
|
1183
1393
|
}
|
|
1184
1394
|
if (unmappedSymbols.length > 0) {
|
|
1185
1395
|
groups.push({
|
|
@@ -1221,11 +1431,11 @@ function createRule(fs) {
|
|
|
1221
1431
|
if (existingMock && existingMock.node !== node) {
|
|
1222
1432
|
// Merge properties from existing mock with new properties
|
|
1223
1433
|
var newPropertiesMap = new Map();
|
|
1224
|
-
var
|
|
1225
|
-
|
|
1434
|
+
var _iterator21 = _createForOfIteratorHelper(group.propertyNames),
|
|
1435
|
+
_step21;
|
|
1226
1436
|
try {
|
|
1227
|
-
for (
|
|
1228
|
-
var propName =
|
|
1437
|
+
for (_iterator21.s(); !(_step21 = _iterator21.n()).done;) {
|
|
1438
|
+
var propName = _step21.value;
|
|
1229
1439
|
var propInfo = mockProperties.get(propName);
|
|
1230
1440
|
if (propInfo) {
|
|
1231
1441
|
// Check if this property needs to be renamed (aliased export)
|
|
@@ -1246,9 +1456,9 @@ function createRule(fs) {
|
|
|
1246
1456
|
}
|
|
1247
1457
|
}
|
|
1248
1458
|
} catch (err) {
|
|
1249
|
-
|
|
1459
|
+
_iterator21.e(err);
|
|
1250
1460
|
} finally {
|
|
1251
|
-
|
|
1461
|
+
_iterator21.f();
|
|
1252
1462
|
}
|
|
1253
1463
|
var mergedProperties = mergeMockProperties({
|
|
1254
1464
|
existingProperties: existingMock.properties,
|
|
@@ -1260,10 +1470,10 @@ function createRule(fs) {
|
|
|
1260
1470
|
exportPath: group.exportPath,
|
|
1261
1471
|
importPath: group.importPath,
|
|
1262
1472
|
propertyNames: Array.from(mergedProperties.keys()),
|
|
1263
|
-
propertyTexts: new Map(Array.from(mergedProperties.entries()).map(function (
|
|
1264
|
-
var
|
|
1265
|
-
k =
|
|
1266
|
-
v =
|
|
1473
|
+
propertyTexts: new Map(Array.from(mergedProperties.entries()).map(function (_ref17) {
|
|
1474
|
+
var _ref18 = (0, _slicedToArray2.default)(_ref17, 2),
|
|
1475
|
+
k = _ref18[0],
|
|
1476
|
+
v = _ref18[1];
|
|
1267
1477
|
return [k, v.text];
|
|
1268
1478
|
})),
|
|
1269
1479
|
nameMapping: new Map(),
|
|
@@ -1278,32 +1488,46 @@ function createRule(fs) {
|
|
|
1278
1488
|
mergedGroups.push(group);
|
|
1279
1489
|
}
|
|
1280
1490
|
}
|
|
1281
|
-
var fixText = generateMockFixes({
|
|
1282
|
-
groups: mergedGroups,
|
|
1283
|
-
crossPackageGroups: crossPackageMockGroups,
|
|
1284
|
-
packageName: mockContext.packageName,
|
|
1285
|
-
mockProperties: mockProperties,
|
|
1286
|
-
quote: quote,
|
|
1287
|
-
preambleStatements: preambleStatements
|
|
1288
|
-
});
|
|
1289
|
-
|
|
1290
|
-
// Build a map of symbol name -> new import path for jest.requireMock() rewriting
|
|
1291
1491
|
var symbolToNewImportPath = new Map();
|
|
1292
1492
|
for (var _i4 = 0, _arr = [].concat(mergedGroups, crossPackageMockGroups); _i4 < _arr.length; _i4++) {
|
|
1293
1493
|
var _group2 = _arr[_i4];
|
|
1294
|
-
var
|
|
1295
|
-
|
|
1494
|
+
var _iterator22 = _createForOfIteratorHelper(_group2.propertyNames),
|
|
1495
|
+
_step22;
|
|
1296
1496
|
try {
|
|
1297
|
-
for (
|
|
1298
|
-
var _propName =
|
|
1497
|
+
for (_iterator22.s(); !(_step22 = _iterator22.n()).done;) {
|
|
1498
|
+
var _propName = _step22.value;
|
|
1299
1499
|
symbolToNewImportPath.set(_propName, _group2.importPath);
|
|
1300
1500
|
}
|
|
1301
1501
|
} catch (err) {
|
|
1302
|
-
|
|
1502
|
+
_iterator22.e(err);
|
|
1303
1503
|
} finally {
|
|
1304
|
-
|
|
1504
|
+
_iterator22.f();
|
|
1305
1505
|
}
|
|
1306
1506
|
}
|
|
1507
|
+
var fixText = generateMockFixes({
|
|
1508
|
+
groups: mergedGroups,
|
|
1509
|
+
crossPackageGroups: crossPackageMockGroups,
|
|
1510
|
+
packageName: mockContext.packageName,
|
|
1511
|
+
mockProperties: mockProperties,
|
|
1512
|
+
quote: quote,
|
|
1513
|
+
preambleStatements: preambleStatements,
|
|
1514
|
+
propagateEsModuleFromOriginalMock: originalMockHadEsModule,
|
|
1515
|
+
oldBarrelImportPath: oldImportPath,
|
|
1516
|
+
symbolToNewImportPath: symbolToNewImportPath
|
|
1517
|
+
});
|
|
1518
|
+
|
|
1519
|
+
// Post-process fixText to update jest.requireActual('barrel').Symbol
|
|
1520
|
+
// references embedded in property texts (e.g. inside jest.fn callbacks)
|
|
1521
|
+
fixText = fixText.replace(/jest\.requireActual(?:<[^>]*>)?\((['"])([^'"]+)\1\)\.(\w+)/g, function (match, _q, path, symbol) {
|
|
1522
|
+
if (path !== oldImportPath) {
|
|
1523
|
+
return match;
|
|
1524
|
+
}
|
|
1525
|
+
var newPath = symbolToNewImportPath.get(symbol);
|
|
1526
|
+
if (newPath && newPath !== path) {
|
|
1527
|
+
return match.replace(path, newPath);
|
|
1528
|
+
}
|
|
1529
|
+
return match;
|
|
1530
|
+
});
|
|
1307
1531
|
|
|
1308
1532
|
// Sort nodes by position
|
|
1309
1533
|
var sortedNodesToRemove = nodesToRemove.sort(function (a, b) {
|
|
@@ -1350,27 +1574,78 @@ function createRule(fs) {
|
|
|
1350
1574
|
return candidatePath === oldImportPath;
|
|
1351
1575
|
}
|
|
1352
1576
|
});
|
|
1353
|
-
var
|
|
1354
|
-
|
|
1577
|
+
var _iterator23 = _createForOfIteratorHelper(requireMockCalls),
|
|
1578
|
+
_step23;
|
|
1355
1579
|
try {
|
|
1356
|
-
for (
|
|
1357
|
-
var requireMockNode =
|
|
1580
|
+
for (_iterator23.s(); !(_step23 = _iterator23.n()).done;) {
|
|
1581
|
+
var requireMockNode = _step23.value;
|
|
1358
1582
|
var requireMockArg = requireMockNode.arguments[0];
|
|
1359
1583
|
if (!requireMockArg) {
|
|
1360
1584
|
continue;
|
|
1361
1585
|
}
|
|
1362
|
-
var
|
|
1586
|
+
var _newPath = (0, _jestUtils.resolveNewPathForRequireMock)({
|
|
1363
1587
|
requireMockNode: requireMockNode,
|
|
1364
1588
|
symbolToNewPath: symbolToNewImportPath
|
|
1365
1589
|
});
|
|
1366
|
-
if (
|
|
1367
|
-
fixes.push(fixer.replaceText(requireMockArg, "".concat(quote).concat(
|
|
1590
|
+
if (_newPath) {
|
|
1591
|
+
fixes.push(fixer.replaceText(requireMockArg, "".concat(quote).concat(_newPath).concat(quote)));
|
|
1368
1592
|
}
|
|
1369
1593
|
}
|
|
1594
|
+
|
|
1595
|
+
// Fix jest.requireActual() calls that reference the old barrel path.
|
|
1596
|
+
// Only fix calls OUTSIDE the replaced jest.mock node range
|
|
1597
|
+
// (calls inside it are handled via fixText string replacement below).
|
|
1598
|
+
} catch (err) {
|
|
1599
|
+
_iterator23.e(err);
|
|
1600
|
+
} finally {
|
|
1601
|
+
_iterator23.f();
|
|
1602
|
+
}
|
|
1603
|
+
var replacedRanges = sortedNodesToRemove.map(function (n) {
|
|
1604
|
+
return n.range;
|
|
1605
|
+
});
|
|
1606
|
+
var requireActualCalls = (0, _jestUtils.findJestRequireActualCalls)({
|
|
1607
|
+
ast: ast,
|
|
1608
|
+
matchPath: function matchPath(candidatePath) {
|
|
1609
|
+
return candidatePath === oldImportPath;
|
|
1610
|
+
}
|
|
1611
|
+
});
|
|
1612
|
+
var _iterator24 = _createForOfIteratorHelper(requireActualCalls),
|
|
1613
|
+
_step24;
|
|
1614
|
+
try {
|
|
1615
|
+
var _loop = function _loop() {
|
|
1616
|
+
var raNode = _step24.value;
|
|
1617
|
+
var raArg = raNode.arguments[0];
|
|
1618
|
+
if (!raArg || !raNode.range) {
|
|
1619
|
+
return 0; // continue
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
// Skip calls inside any node being replaced (ranges overlap)
|
|
1623
|
+
var insideReplacedNode = replacedRanges.some(function (_ref19) {
|
|
1624
|
+
var _ref20 = (0, _slicedToArray2.default)(_ref19, 2),
|
|
1625
|
+
start = _ref20[0],
|
|
1626
|
+
end = _ref20[1];
|
|
1627
|
+
return raNode.range[0] >= start && raNode.range[1] <= end;
|
|
1628
|
+
});
|
|
1629
|
+
if (insideReplacedNode) {
|
|
1630
|
+
return 0; // continue
|
|
1631
|
+
}
|
|
1632
|
+
var newPath = (0, _jestUtils.resolveNewPathForRequireMock)({
|
|
1633
|
+
requireMockNode: raNode,
|
|
1634
|
+
symbolToNewPath: symbolToNewImportPath
|
|
1635
|
+
});
|
|
1636
|
+
if (newPath) {
|
|
1637
|
+
fixes.push(fixer.replaceText(raArg, "".concat(quote).concat(newPath).concat(quote)));
|
|
1638
|
+
}
|
|
1639
|
+
},
|
|
1640
|
+
_ret;
|
|
1641
|
+
for (_iterator24.s(); !(_step24 = _iterator24.n()).done;) {
|
|
1642
|
+
_ret = _loop();
|
|
1643
|
+
if (_ret === 0) continue;
|
|
1644
|
+
}
|
|
1370
1645
|
} catch (err) {
|
|
1371
|
-
|
|
1646
|
+
_iterator24.e(err);
|
|
1372
1647
|
} finally {
|
|
1373
|
-
|
|
1648
|
+
_iterator24.f();
|
|
1374
1649
|
}
|
|
1375
1650
|
return fixes;
|
|
1376
1651
|
}
|