From 39e5d97b6e4a9d48f73a01b1c832853749672f76 Mon Sep 17 00:00:00 2001 From: Marin Atanasov <8436925+tyxla@users.noreply.github.com> Date: Mon, 7 Nov 2022 16:24:17 +0200 Subject: [PATCH] Lodash: Refactor away from _.reduce() (#45460) --- .eslintrc.js | 1 + packages/babel-plugin-makepot/index.js | 85 ++++++++++++-------------- 2 files changed, 41 insertions(+), 45 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 0f71c3660dd02..38992de398e51 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -144,6 +144,7 @@ module.exports = { 'partial', 'partialRight', 'random', + 'reduce', 'reject', 'repeat', 'reverse', diff --git a/packages/babel-plugin-makepot/index.js b/packages/babel-plugin-makepot/index.js index 2273fdb4c4264..3dc1b3ed26651 100644 --- a/packages/babel-plugin-makepot/index.js +++ b/packages/babel-plugin-makepot/index.js @@ -33,7 +33,7 @@ */ const { po } = require( 'gettext-parser' ); -const { pick, reduce, isEqual, merge, isEmpty } = require( 'lodash' ); +const { pick, isEqual, merge, isEmpty } = require( 'lodash' ); const { relative, sep } = require( 'path' ); const { writeFileSync } = require( 'fs' ); @@ -324,50 +324,45 @@ module.exports = () => { const files = Object.keys( strings ).sort(); // Combine translations from each file grouped by context. - const translations = reduce( - files, - ( memo, file ) => { - for ( const context in strings[ file ] ) { - // Within the same file, sort translations by line. - const sortedTranslations = sortByReference( - Object.values( strings[ file ][ context ] ) - ); - - sortedTranslations.forEach( ( translation ) => { - const { msgctxt = '', msgid } = translation; - if ( ! memo.hasOwnProperty( msgctxt ) ) { - memo[ msgctxt ] = {}; - } - - // Merge references if translation already exists. - if ( - isSameTranslation( - translation, - memo[ msgctxt ][ msgid ] - ) - ) { - translation.comments.reference = [ - ...new Set( - [ - memo[ msgctxt ][ msgid ] - .comments.reference, - translation.comments - .reference, - ] - .join( '\n' ) - .split( '\n' ) - ), - ].join( '\n' ); - } - - memo[ msgctxt ][ msgid ] = translation; - } ); - } - - return memo; - }, - {} - ); + const translations = files.reduce( ( memo, file ) => { + for ( const context in strings[ file ] ) { + // Within the same file, sort translations by line. + const sortedTranslations = sortByReference( + Object.values( strings[ file ][ context ] ) + ); + + sortedTranslations.forEach( ( translation ) => { + const { msgctxt = '', msgid } = translation; + if ( ! memo.hasOwnProperty( msgctxt ) ) { + memo[ msgctxt ] = {}; + } + + // Merge references if translation already exists. + if ( + isSameTranslation( + translation, + memo[ msgctxt ][ msgid ] + ) + ) { + translation.comments.reference = [ + ...new Set( + [ + memo[ msgctxt ][ msgid ] + .comments.reference, + translation.comments.reference, + ] + .join( '\n' ) + .split( '\n' ) + ), + ].join( '\n' ); + } + + memo[ msgctxt ][ msgid ] = translation; + } ); + } + + return memo; + }, {} ); // Merge translations from individual files into headers const data = merge( {}, baseData, { translations } );