From 52c41e52c9b76eaf29181c98b806595513829684 Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Wed, 10 Feb 2021 02:17:05 -0600 Subject: [PATCH] Cross-fork lint: Support named export declaration Noticed this didn't work when I ran `replace-fork` and a named export declaration in ReactFiberReconciler was not properly fixed. --- .../no-cross-fork-imports-test.internal.js | 24 ++++++ scripts/eslint-rules/no-cross-fork-imports.js | 83 +++++++++++-------- 2 files changed, 71 insertions(+), 36 deletions(-) diff --git a/scripts/eslint-rules/__tests__/no-cross-fork-imports-test.internal.js b/scripts/eslint-rules/__tests__/no-cross-fork-imports-test.internal.js index 453df47bdd8bf..2adfc9a9f9cd6 100644 --- a/scripts/eslint-rules/__tests__/no-cross-fork-imports-test.internal.js +++ b/scripts/eslint-rules/__tests__/no-cross-fork-imports-test.internal.js @@ -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';", + }, ], }); diff --git a/scripts/eslint-rules/no-cross-fork-imports.js b/scripts/eslint-rules/no-cross-fork-imports.js index c2d27960d77d0..76df033c282a4 100644 --- a/scripts/eslint-rules/no-cross-fork-imports.js +++ b/scripts/eslint-rules/no-cross-fork-imports.js @@ -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, }; }