Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"gzip-size": "^5.1.1",
"hermes-eslint": "^0.20.1",
"hermes-parser": "^0.20.1",
"hermes-transform": "^0.20.1",
"jest": "^29.4.2",
"jest-cli": "^29.4.2",
"jest-diff": "^29.4.2",
Expand Down Expand Up @@ -132,6 +133,7 @@
"download-build-for-head": "node ./scripts/release/download-experimental-build.js --commit=$(git rev-parse HEAD)",
"download-build-in-codesandbox-ci": "cd scripts/release && yarn install && cd ../../ && yarn download-build-for-head || yarn build --type=node react/index react-dom/index react-dom/src/server react-dom/test-utils scheduler/index react/jsx-runtime react/jsx-dev-runtime",
"check-release-dependencies": "node ./scripts/release/check-release-dependencies",
"codemod": "flow-node scripts/codemod/index.js",
"generate-inline-fizz-runtime": "node ./scripts/rollup/generate-inline-fizz-runtime.js",
"flags": "node ./scripts/flags/flags.js"
},
Expand Down
262 changes: 262 additions & 0 deletions scripts/codemod/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {
ESNode,
IfStatement,
Statement,
ImportDeclaration,
LogicalExpression,
CallExpression,
ConditionalExpression,
UnaryExpression,
Identifier,
MemberExpression,
} from 'hermes-estree';
import type {TransformContext} from 'hermes-transform';

const {transform, t} = require('hermes-transform');
const {SimpleTraverser} = require('hermes-parser');
const Glob = require('glob');
const {readFileSync, writeFileSync} = require('fs');
const Prettier = require('prettier');

/* eslint-disable no-for-of-loops/no-for-of-loops */

function createReplaceFlagWithValue(flagName: string, flagValue: boolean) {
return function replaceFlagWithValue(context: TransformContext) {
return {
ImportDeclaration(node: ImportDeclaration) {
context.skipTraversal();
},
Identifier(node: Identifier) {
if (node.parent.type === 'VariableDeclarator') {
return;
}
if (node.type === 'Identifier' && node.name === flagName) {
context.replaceNode(node, t.BooleanLiteral({value: flagValue}));
}
},
MemberExpression(node: MemberExpression) {
if (
node.object.type === 'Identifier' &&
node.object.name === 'flags' &&
node.property.type === 'Identifier' &&
node.property.name === flagName
) {
context.replaceNode(node, t.BooleanLiteral({value: flagValue}));
context.skipTraversal();
}
},
};
};
}

function simplifyNotBoolean(context: TransformContext) {
return {
UnaryExpression(node: UnaryExpression) {
if (node.operator === '!' && node.argument.type === 'Literal') {
context.replaceNode(
node,
t.BooleanLiteral({value: !node.argument.value})
);
}
},
};
}

function simplifyGate(context: TransformContext) {
return {
CallExpression(node: CallExpression) {
if (
node.callee.type === 'Identifier' &&
node.callee.name === 'gate' &&
node.arguments.length === 1 &&
node.arguments[0].type === 'ArrowFunctionExpression' &&
node.arguments[0].body.type === 'Literal'
) {
context.replaceNode(node, node.arguments[0].body);
}
},
};
}

function simplifyLogicalExpression(context: TransformContext) {
return {
LogicalExpression(node: LogicalExpression) {
if (
node.operator === '&&' &&
node.left.type === 'Literal' &&
node.left.value === true
) {
context.replaceNode(node, node.right);
}
if (
node.operator === '&&' &&
node.left.type === 'Literal' &&
node.left.value === false
) {
context.replaceNode(node, node.left);
}
if (
node.operator === '&&' &&
node.right.type === 'Literal' &&
node.right.value === true
) {
context.replaceNode(node, node.left);
}
if (
node.operator === '||' &&
node.left.type === 'Literal' &&
node.left.value === false
) {
context.replaceNode(node, node.right);
}
if (
node.operator === '||' &&
node.right.type === 'Literal' &&
node.right.value === false
) {
context.replaceNode(node, node.left);
}
},
};
}

function simplifyTernary(context: TransformContext) {
return {
ConditionalExpression(node: ConditionalExpression) {
if (node.test.type === 'Literal' && node.test.value === true) {
context.replaceNode(node, node.consequent);
}
if (node.test.type === 'Literal' && node.test.value === false) {
context.replaceNode(node, node.alternate);
}
},
};
}

function simplifyCondition(context: TransformContext) {
let lastParent: ?ESNode = null;
return {
IfStatement(node: IfStatement) {
if (node.parent === lastParent) {
// a bug in hermes-transform prevents multiple replaceStatementWithMany
// with the same parent
return;
}
if (node.test.type === 'Literal' && node.test.value === true) {
lastParent = node.parent;
context.replaceStatementWithMany(
node,
unwrapBlockStatment(node.consequent)
);
}
if (node.test.type === 'Literal' && node.test.value === false) {
lastParent = node.parent;
if (node.alternate == null) {
context.removeStatement(node);
} else {
context.replaceStatementWithMany(
node,
unwrapBlockStatment(node.alternate)
);
}
}
},
};
}

function unwrapBlockStatment(statement: Statement): $ReadOnlyArray<Statement> {
return statement.type === 'BlockStatement' ? statement.body : [statement];
}

async function transformFile(
filename: string,
flagName: string,
flagValue: boolean
) {
const originalCode = readFileSync(filename, 'utf8');
if (!originalCode.includes(flagName)) {
return false;
}
const prettierConfig = await Prettier.resolveConfig(filename);
let transformedCode = originalCode;
transformedCode = transformedCode.replaceAll(`// @gate ${flagName}\n`, '');
transformedCode = transformedCode.replaceAll(
`// @gate ${flagName} && `,
'// @gate '
);
transformedCode = transformedCode.replaceAll(
`// @gate ${flagName} || `,
'// XXX REMOVE XXX'
);
for (const createVisitors of [
createReplaceFlagWithValue(flagName, flagValue),
simplifyNotBoolean,
simplifyGate,
simplifyLogicalExpression,
simplifyLogicalExpression,
simplifyTernary,
simplifyCondition,
simplifyCondition,
simplifyCondition,
]) {
transformedCode = await transform(
transformedCode,
createVisitors,
prettierConfig
);
}
if (originalCode !== transformedCode) {
writeFileSync(filename, transformedCode, 'utf8');
return true;
}
return false;
}

async function main(args: $ReadOnlyArray<string>) {
if (args.length < 1) {
console.error('Usage: yarn codemod <PATTERNS>');
process.exit(1);
}
const {FLAG_NAME: flagName, FLAG_VALUE: flagValue} = process.env;

if (flagName == null || flagValue == null) {
console.error('Please set FLAG_NAME and FLAG_VALUE environment variables');
process.exit(1);
return;
}

let flagValueBoolean = flagValue !== 'false';

const files = new Set<string>();
for (const arg of args) {
for (const file of Glob.sync(arg)) {
files.add(file);
}
}
let updatedCount = 0;
for (const file of files) {
try {
const updated = await transformFile(file, flagName, flagValueBoolean);
if (updated) {
updatedCount++;
console.log(`updated ${file}`);
}
} catch (err) {
console.log(`Error transforming ${file}`, err);
}
}
console.log(`${files.size} processed, ${updatedCount} updated`);
}

main(process.argv.slice(2)).catch(err => {
console.error('Error while transforming:', err);
});
1 change: 1 addition & 0 deletions scripts/shared/pathsByLanguageVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const esNextPaths = [
'packages/react-interactions/**/*.js',
'packages/shared/**/*.js',
// Shims and Flow environment
'scripts/codemod/*.js',
'scripts/flow/*.js',
'scripts/rollup/shims/**/*.js',
];
Expand Down
Loading