Skip to content

Commit 474be25

Browse files
committed
Fix the bad minification due to ternary expression
1 parent 4f7d73b commit 474be25

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module.exports = function transformer( file, api ) {
2+
const j = api.jscodeshift;
3+
4+
return j( file.source )
5+
.find( j.ConditionalExpression )
6+
.filter( path => {
7+
const { consequent, alternate } = path.value;
8+
9+
// Check if the consequent or alternate is a function call to targetFunctionName
10+
return (
11+
isFunctionCallWithName( consequent, '__' ) || isFunctionCallWithName( alternate, '__' )
12+
);
13+
} )
14+
.forEach( path => {
15+
const { alternate } = path.value;
16+
17+
// Add the new argument to the alternate function call
18+
const newAlternateArguments = [
19+
...alternate.arguments,
20+
j.literal( 0 ), // Create a literal AST node for the new argument
21+
];
22+
23+
// Create a new CallExpression node with the updated arguments
24+
const newAlternate = j.callExpression( alternate.callee, newAlternateArguments );
25+
26+
// Update the alternate branch of the ternary expression
27+
path.value.alternate = newAlternate;
28+
} )
29+
.toSource();
30+
};
31+
32+
/**
33+
* Whether the node is a function call with the specific name
34+
*
35+
* @param node - The AST node
36+
* @param functionName - The function name we want to check
37+
*/
38+
function isFunctionCallWithName( node, functionName ) {
39+
return (
40+
node &&
41+
node.type === 'CallExpression' &&
42+
node.callee &&
43+
node.callee.type === 'Identifier' &&
44+
node.callee.name === functionName
45+
);
46+
}

projects/packages/jetpack-mu-wpcom/bin/sync-newspack-blocks.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ sed "${sedi[@]}" -e "s| function| Function|g" "$TARGET/types/index.d.ts"
147147
echo "Changing JS textdomain to match jetpack-mu-wpcom..."
148148
pnpm --package=eslint@8.57.0 dlx eslint --no-ignore --rule '"@wordpress/i18n-text-domain":["error",{"allowedTextDomain":"jetpack-mu-wpcom"}]' --fix $TARGET > /dev/null
149149

150+
echo "Changing JS translation function call to avoid bad minification..."
151+
pnpm --package=jscodeshift dlx jscodeshift -t ./bin/sync-newspack-blocks-formatter.js --extensions=js $TARGET
152+
150153
echo "Changing PHP textdomain to match jetpack-mu-wpcom..."
151154
../../../vendor/bin/phpcbf --standard=./.phpcs.dir.xml --filter=../../../vendor/automattic/jetpack-phpcs-filter/src/PhpcsFilter.php --runtime-set jetpack-filter-no-ignore -q $TARGET
152155

0 commit comments

Comments
 (0)