From d5b1c71b3f44f626d0e1d8fe33d4dd0965cb3ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Wed, 13 Dec 2023 09:43:14 -0800 Subject: [PATCH] fix: reformat RCTLegacyInteropComponents (#41908) Summary: When Codegen generates interop components it reformats `RCTLegacyInteropComponents.mm` to have each component in a new line. This creates this unnecessary diff every time I run `pod install`. This PR aims to commit a formatted version of the codegen-generated file to avoid creating this diff every time. Here is a reference of Codegen function generating this: https://github.com/facebook/react-native/blob/44d6e4310cc9ad0d711d05e8dd5ee5220738e5b5/packages/react-native/scripts/codegen/generate-legacy-interop-components.js#L53 ![CleanShot 2023-12-12 at 14 09 34@2x](https://github.com/facebook/react-native/assets/52801365/2d5454d0-3bcb-484c-aa45-b5286c3ac5ba) ## Changelog: [INTERNAL] [CHANGED] - Reformat `RCTLegacyInteropComponents` to follow Codegen generated formatting Pull Request resolved: https://github.com/facebook/react-native/pull/41908 Test Plan: CI Green Reviewed By: cipolleschi Differential Revision: D52077611 Pulled By: cortinico fbshipit-source-id: 12fbbfacc7e73147a988d321c8bc771acaaad8bb --- .../generate-legacy-interop-components.js | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/react-native/scripts/codegen/generate-legacy-interop-components.js b/packages/react-native/scripts/codegen/generate-legacy-interop-components.js index e1fba0dd8c6..2ef14fef423 100644 --- a/packages/react-native/scripts/codegen/generate-legacy-interop-components.js +++ b/packages/react-native/scripts/codegen/generate-legacy-interop-components.js @@ -18,6 +18,7 @@ const PROJECT_FIELD = 'project'; const IOS_FIELD = 'ios'; const LEGACY_COMPONENTS_FIELD = 'unstable_reactLegacyComponentNames'; const OUTPUT_FILE_NAME = 'RCTLegacyInteropComponents.mm'; +const MAX_CHARS_IN_ONE_LINE = 80; const argv = yargs .option('p', { @@ -34,7 +35,14 @@ const argv = yargs const appRoot = argv.path; const outputPath = argv.outputPath; -function fileBody(components) { +function fileBody(components, isOneLine) { + // Generate components array string with proper formatting + const componentsString = components.join(isOneLine ? '' : '\n'); + const componentsArray = isOneLine + ? `@[${componentsString} ];` + : `@[ +${componentsString} + ];`; // eslint-disable duplicate-license-header return `/* * Copyright (c) Meta Platforms, Inc. and affiliates. @@ -49,9 +57,7 @@ function fileBody(components) { + (NSArray *)legacyInteropComponents { - return @[ -${components} - ]; + return ${componentsArray} } @end @@ -112,7 +118,10 @@ function generateRCTLegacyInteropComponents() { return; } console.log(`Components found: ${componentNames}`); - let componentsArray = componentNames.map(name => `\t\t\t@"${name}",`); + const isOneLine = componentNames.join().length < MAX_CHARS_IN_ONE_LINE; + let componentsArray = componentNames.map( + (name, index) => `${isOneLine ? ' ' : '\t\t\t'}@"${name}",`, + ); // Remove the last comma if (componentsArray.length > 0) { componentsArray[componentsArray.length - 1] = componentsArray[ @@ -121,7 +130,7 @@ function generateRCTLegacyInteropComponents() { } const filePath = `${outputPath}/${OUTPUT_FILE_NAME}`; - fs.writeFileSync(filePath, fileBody(componentsArray.join('\n'))); + fs.writeFileSync(filePath, fileBody(componentsArray, isOneLine)); console.log(`${filePath} updated!`); }