Skip to content

Commit eee874c

Browse files
authored
Cross-fork lint: Support named export declaration (#20784)
Noticed this didn't work when I ran `replace-fork` and a named export declaration in ReactFiberReconciler was not properly fixed.
1 parent 3b870b1 commit eee874c

File tree

3 files changed

+72
-37
lines changed

3 files changed

+72
-37
lines changed

packages/react-reconciler/src/ReactFiberReconciler.old.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export {
104104
IdleLanePriority as IdleEventPriority,
105105
} from './ReactFiberLane.old';
106106

107-
export {registerMutableSourceForHydration} from './ReactMutableSource.new';
107+
export {registerMutableSourceForHydration} from './ReactMutableSource.old';
108108
export {createPortal} from './ReactPortal';
109109
export {
110110
createComponentSelector,

scripts/eslint-rules/__tests__/no-cross-fork-imports-test.internal.js

+24
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,29 @@ ruleTester.run('eslint-rules/no-cross-fork-imports', rule, {
9494
],
9595
output: "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.new';",
9696
},
97+
{
98+
code: "export {DiscreteEventPriority} from './ReactFiberLane.old.js';",
99+
filename: 'ReactFiberReconciler.new.js',
100+
errors: [
101+
{
102+
message:
103+
'A module that belongs to the new fork cannot import a module ' +
104+
'from the old fork.',
105+
},
106+
],
107+
output: "export {DiscreteEventPriority} from './ReactFiberLane.new';",
108+
},
109+
{
110+
code: "export {DiscreteEventPriority} from './ReactFiberLane.new.js';",
111+
filename: 'ReactFiberReconciler.old.js',
112+
errors: [
113+
{
114+
message:
115+
'A module that belongs to the old fork cannot import a module ' +
116+
'from the new fork.',
117+
},
118+
],
119+
output: "export {DiscreteEventPriority} from './ReactFiberLane.old';",
120+
},
97121
],
98122
});

scripts/eslint-rules/no-cross-fork-imports.js

+47-36
Original file line numberDiff line numberDiff line change
@@ -26,48 +26,59 @@ module.exports = {
2626
const sourceFilename = context.getFilename();
2727

2828
if (isOldFork(sourceFilename)) {
29+
const visitor = node => {
30+
const sourceNode = node.source;
31+
if (sourceNode === null) {
32+
return;
33+
}
34+
const filename = sourceNode.value;
35+
if (isNewFork(filename)) {
36+
context.report({
37+
node: sourceNode,
38+
message:
39+
'A module that belongs to the old fork cannot import a module ' +
40+
'from the new fork.',
41+
fix(fixer) {
42+
return fixer.replaceText(
43+
sourceNode,
44+
`'${filename.replace(/\.new(\.js)?$/, '.old')}'`
45+
);
46+
},
47+
});
48+
}
49+
};
2950
return {
30-
ImportDeclaration(node) {
31-
const importSourceNode = node.source;
32-
const filename = importSourceNode.value;
33-
if (isNewFork(filename)) {
34-
context.report({
35-
node: importSourceNode,
36-
message:
37-
'A module that belongs to the old fork cannot import a module ' +
38-
'from the new fork.',
39-
fix(fixer) {
40-
return fixer.replaceText(
41-
importSourceNode,
42-
`'${filename.replace(/\.new(\.js)?$/, '.old')}'`
43-
);
44-
},
45-
});
46-
}
47-
},
51+
ImportDeclaration: visitor,
52+
ExportNamedDeclaration: visitor,
4853
};
4954
}
5055

5156
if (isNewFork(sourceFilename)) {
57+
const visitor = node => {
58+
const sourceNode = node.source;
59+
if (sourceNode === null) {
60+
return;
61+
}
62+
const filename = sourceNode.value;
63+
if (isOldFork(filename)) {
64+
context.report({
65+
node: sourceNode,
66+
message:
67+
'A module that belongs to the new fork cannot import a module ' +
68+
'from the old fork.',
69+
fix(fixer) {
70+
return fixer.replaceText(
71+
sourceNode,
72+
`'${filename.replace(/\.old(\.js)?$/, '.new')}'`
73+
);
74+
},
75+
});
76+
}
77+
};
78+
5279
return {
53-
ImportDeclaration(node) {
54-
const importSourceNode = node.source;
55-
const filename = importSourceNode.value;
56-
if (isOldFork(filename)) {
57-
context.report({
58-
node: importSourceNode,
59-
message:
60-
'A module that belongs to the new fork cannot import a module ' +
61-
'from the old fork.',
62-
fix(fixer) {
63-
return fixer.replaceText(
64-
importSourceNode,
65-
`'${filename.replace(/\.old(\.js)?$/, '.new')}'`
66-
);
67-
},
68-
});
69-
}
70-
},
80+
ImportDeclaration: visitor,
81+
ExportNamedDeclaration: visitor,
7182
};
7283
}
7384

0 commit comments

Comments
 (0)