Skip to content

Commit

Permalink
Cross-fork lint: Support named export declaration (#20784)
Browse files Browse the repository at this point in the history
Noticed this didn't work when I ran `replace-fork` and a named export
declaration in ReactFiberReconciler was not properly fixed.
  • Loading branch information
acdlite committed Feb 10, 2021
1 parent 3b870b1 commit eee874c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberReconciler.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export {
IdleLanePriority as IdleEventPriority,
} from './ReactFiberLane.old';

export {registerMutableSourceForHydration} from './ReactMutableSource.new';
export {registerMutableSourceForHydration} from './ReactMutableSource.old';
export {createPortal} from './ReactPortal';
export {
createComponentSelector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,29 @@ ruleTester.run('eslint-rules/no-cross-fork-imports', rule, {
],
output: "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.new';",
},
{
code: "export {DiscreteEventPriority} from './ReactFiberLane.old.js';",
filename: 'ReactFiberReconciler.new.js',
errors: [
{
message:
'A module that belongs to the new fork cannot import a module ' +
'from the old fork.',
},
],
output: "export {DiscreteEventPriority} from './ReactFiberLane.new';",
},
{
code: "export {DiscreteEventPriority} from './ReactFiberLane.new.js';",
filename: 'ReactFiberReconciler.old.js',
errors: [
{
message:
'A module that belongs to the old fork cannot import a module ' +
'from the new fork.',
},
],
output: "export {DiscreteEventPriority} from './ReactFiberLane.old';",
},
],
});
83 changes: 47 additions & 36 deletions scripts/eslint-rules/no-cross-fork-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,59 @@ module.exports = {
const sourceFilename = context.getFilename();

if (isOldFork(sourceFilename)) {
const visitor = node => {
const sourceNode = node.source;
if (sourceNode === null) {
return;
}
const filename = sourceNode.value;
if (isNewFork(filename)) {
context.report({
node: sourceNode,
message:
'A module that belongs to the old fork cannot import a module ' +
'from the new fork.',
fix(fixer) {
return fixer.replaceText(
sourceNode,
`'${filename.replace(/\.new(\.js)?$/, '.old')}'`
);
},
});
}
};
return {
ImportDeclaration(node) {
const importSourceNode = node.source;
const filename = importSourceNode.value;
if (isNewFork(filename)) {
context.report({
node: importSourceNode,
message:
'A module that belongs to the old fork cannot import a module ' +
'from the new fork.',
fix(fixer) {
return fixer.replaceText(
importSourceNode,
`'${filename.replace(/\.new(\.js)?$/, '.old')}'`
);
},
});
}
},
ImportDeclaration: visitor,
ExportNamedDeclaration: visitor,
};
}

if (isNewFork(sourceFilename)) {
const visitor = node => {
const sourceNode = node.source;
if (sourceNode === null) {
return;
}
const filename = sourceNode.value;
if (isOldFork(filename)) {
context.report({
node: sourceNode,
message:
'A module that belongs to the new fork cannot import a module ' +
'from the old fork.',
fix(fixer) {
return fixer.replaceText(
sourceNode,
`'${filename.replace(/\.old(\.js)?$/, '.new')}'`
);
},
});
}
};

return {
ImportDeclaration(node) {
const importSourceNode = node.source;
const filename = importSourceNode.value;
if (isOldFork(filename)) {
context.report({
node: importSourceNode,
message:
'A module that belongs to the new fork cannot import a module ' +
'from the old fork.',
fix(fixer) {
return fixer.replaceText(
importSourceNode,
`'${filename.replace(/\.old(\.js)?$/, '.new')}'`
);
},
});
}
},
ImportDeclaration: visitor,
ExportNamedDeclaration: visitor,
};
}

Expand Down

0 comments on commit eee874c

Please sign in to comment.