Skip to content

Commit

Permalink
Add tests, use prettier, and add a workaround for comments-need-a-sta…
Browse files Browse the repository at this point in the history
…tement constraint
  • Loading branch information
etrepum committed May 1, 2024
1 parent 4aae60d commit d4f2423
Showing 1 changed file with 1 addition and 55 deletions.
56 changes: 1 addition & 55 deletions scripts/www/rewriteImports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} 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
Expand Down

0 comments on commit d4f2423

Please sign in to comment.