From d4f24237568cde1f39d2141816df1c24ee963afc Mon Sep 17 00:00:00 2001 From: Bob Ippolito Date: Wed, 1 May 2024 16:09:56 -0700 Subject: [PATCH] Add tests, use prettier, and add a workaround for comments-need-a-statement constraint --- scripts/www/rewriteImports.js | 56 +---------------------------------- 1 file changed, 1 insertion(+), 55 deletions(-) diff --git a/scripts/www/rewriteImports.js b/scripts/www/rewriteImports.js index 4d3361452d1..6635300f5ad 100644 --- a/scripts/www/rewriteImports.js +++ b/scripts/www/rewriteImports.js @@ -12,61 +12,7 @@ const fs = require('fs-extra'); const glob = require('glob'); const path = require('node:path'); const {packagesManager} = require('../shared/packagesManager'); -const npmToWwwName = require('./npmToWwwName'); -const {t, transform} = require('hermes-transform'); - -const wwwMappings = Object.fromEntries( - packagesManager - .getPublicPackages() - .flatMap((pkg) => - pkg.getExportedNpmModuleNames().map((npm) => [npm, npmToWwwName(npm)]), - ), -); - -/** - * It would be nice to use jscodeshift for this but the flow sources are using - * ast features that are not supported in ast-types (as of 2024-04-11) so it's - * not possible to traverse the tree and replace the imports & comments. - * - * It might be possible going straight to flow-parser, but it was a slew of - * hardcoded regexps before and now it's at least automated based on the - * exports. - * - * @param {string} source - * @returns {Promise} transformed source - */ -async function transformFlowFileContents(source) { - return await transform( - source, - (context) => ({ - ImportDeclaration(node) { - const value = wwwMappings[node.source.value]; - if (value) { - context.replaceNode(node.source, t.StringLiteral({value})); - } - }, - Program(node) { - if ( - node.docblock && - node.docblock.comment && - node.docblock.comment.value.includes('@flow strict') - ) { - // This is mutated in-place because I couldn't find a mutation that - // did not fail for replacing the Program node. - node.docblock.comment.value = node.docblock.comment.value.replace( - / \* @flow strict/g, - ' * @flow strict\n * @generated\n * @oncall lexical_web_text_editor', - ); - // We need the mutations array to be non-empty, so remove something - // that is not there. The AST traversals use object identity in a - // Set so we don't have to worry about some other line changing. - context.removeComments(t.LineComment({value: ''})); - } - }, - }), - {}, - ); -} +const transformFlowFileContents = require('./transformFlowFileContents'); // This script attempts to find all Flow definition modules, and makes // them compatible with www. Specifically, it finds any imports that