From 9ad8c830bdaefb52ce42b0274458421657fc556b Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Wed, 2 Sep 2026 13:47:10 -0700 Subject: [PATCH] Apply preset defaults without a Babel API (#58254) Summary: The preset's Babel-API accesses are optional, but invoking it without the Babel API still has two incomplete fallback paths: `preset()` can read properties from an undefined options object, and `getPreset(code, {dev: false})` receives no transform profile and silently falls back to legacy class transforms instead of the normal `hermes-stable` default. Default options to an empty object, consistently rely on that non-null invariant throughout the configuration builder, and apply `hermes-stable` when neither explicit options nor `babel.caller` provides a profile. Explicit options and caller-provided profiles retain precedence. ## Changelog: [GENERAL] [FIXED] - Apply normal preset defaults when invoked without a Babel API or options object. Pull Request resolved: https://github.com/react/react-native/pull/58254 Test Plan: - Added a regression proving `preset()` builds without options or a Babel API. - Added a transform regression proving no-API invocation preserves classes under the default `hermes-stable` profile; exact baseline lowers the class to helpers. - Full preset Jest passes: 4/4 suites, 112/112 tests, 16 snapshots. - Fresh Flow check reports 0 errors. - Targeted no-ignore ESLint, Prettier, and `git diff --check` pass. No breaking change: explicit profiles/caller behavior are unchanged, while standalone invocation now matches the preset's documented default path. Reviewed By: javache Differential Revision: D118438065 Pulled By: vzaidman fbshipit-source-id: e952eece72664dfa9d60d25d26955655e057f388 --- .../src/__tests__/transform-snapshot-test.js | 21 ++++++++++++ .../src/configs/main.js | 32 +++++++++++-------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js b/packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js index 29ec5148cb0..97b70c65e0d 100644 --- a/packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js +++ b/packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js @@ -186,6 +186,18 @@ function transformCodeWithSourceOptimization( return result?.code ?? null; } +function transformWithoutBabelApi(code: string): string | null { + const config = preset.getPreset(code, {dev: false}); + const result = babel.transformSync(code, { + ...config, + babelrc: false, + configFile: false, + filename: MOCK_FILENAME, + sourceMaps: false, + }); + return result?.code ?? null; +} + function getSnapshotPath(configName: string): string { return path.join(OUTPUT_DIR, `${configName}.js`); } @@ -282,6 +294,15 @@ describe('react-native-babel-preset transform snapshots', () => { ); describe('specific feature transformations', () => { + it('builds the default config without options or a Babel API', () => { + expect(() => preset()).not.toThrow(); + }); + + it('uses the default transform profile without a Babel API', () => { + const result = transformWithoutBabelApi('class Animal {}'); + expect(result).toContain('class Animal'); + }); + it('adds display names when React.createClass contains trivia', () => { const code = ` const Component = React /* comment */ . createClass({ diff --git a/packages/react-native-babel-preset/src/configs/main.js b/packages/react-native-babel-preset/src/configs/main.js index 970853f3c01..24d23d46af0 100644 --- a/packages/react-native-babel-preset/src/configs/main.js +++ b/packages/react-native-babel-preset/src/configs/main.js @@ -64,15 +64,19 @@ function getInlinePlatform(caller) { const loose = true; const getPreset = (src, options, babel) => { + options = options ?? {}; + const transformProfile = - options?.unstable_transformProfile ?? babel?.caller(getTransformProfile); + options.unstable_transformProfile ?? + babel?.caller(getTransformProfile) ?? + 'hermes-stable'; - const dev = options?.dev ?? babel?.env('development') ?? false; + const dev = options.dev ?? babel?.env('development') ?? false; - const platform = options?.platform ?? babel?.caller(getPlatform); + const platform = options.platform ?? babel?.caller(getPlatform); const inlinePlatform = - options?.inlinePlatform ?? babel?.caller(getInlinePlatform) ?? false; + options.inlinePlatform ?? babel?.caller(getInlinePlatform) ?? false; // Hermes V1 uses more optimised transform profiles. There is currently no // difference between stable and canary, but canary may in future be used to @@ -96,22 +100,22 @@ const getPreset = (src, options, babel) => { // Preserve private class fields and methods if the experiment is enabled. const preserveClassPrivate = TRUE_VALS.has( - options?.customTransformOptions?.unstable_preserveClassPrivate, + options.customTransformOptions?.unstable_preserveClassPrivate, ); // Preserve async/await syntax if the experiment is enabled. const preserveAsync = TRUE_VALS.has( - options?.customTransformOptions?.unstable_preserveAsync, + options.customTransformOptions?.unstable_preserveAsync, ); // Preserve block scoping (let/const) if the experiment is enabled. const preserveBlockScoping = TRUE_VALS.has( - options?.customTransformOptions?.unstable_preserveBlockScoping, + options.customTransformOptions?.unstable_preserveBlockScoping, ); // Preserve destructuring syntax if the experiment is enabled. const preserveDestructuring = TRUE_VALS.has( - options?.customTransformOptions?.unstable_preserveDestructuring, + options.customTransformOptions?.unstable_preserveDestructuring, ); const isNull = src == null; @@ -144,7 +148,7 @@ const getPreset = (src, options, babel) => { extraPlugins.push([require('@react-native/babel-plugin-codegen')]); } - if (!options || !options.disableImportExportTransform) { + if (!options.disableImportExportTransform) { extraPlugins.push( [require('@babel/plugin-proposal-export-default-from')], [ @@ -153,7 +157,7 @@ const getPreset = (src, options, babel) => { strict: false, strictMode: false, // prevent "use strict" injections lazy: - options && options.lazyImportExportTransform != null + options.lazyImportExportTransform != null ? options.lazyImportExportTransform : importSpecifier => lazyImports.has(importSpecifier), allowTopLevelThis: true, // dont rewrite global `this` -> `undefined` @@ -210,11 +214,11 @@ const getPreset = (src, options, babel) => { ]); } - if (options && dev && !options.disableDeepImportWarnings) { + if (dev && !options.disableDeepImportWarnings) { firstPartyPlugins.push([require('../plugin-warn-on-deep-imports.js')]); } - if (options && dev && !options.useTransformReactJSXExperimental) { + if (dev && !options.useTransformReactJSXExperimental) { extraPlugins.push([require('@babel/plugin-transform-react-jsx-source')]); extraPlugins.push([require('@babel/plugin-transform-react-jsx-self')]); } @@ -230,9 +234,9 @@ const getPreset = (src, options, babel) => { ]); } - if (!options || options.enableBabelRuntime !== false) { + if (options.enableBabelRuntime !== false) { // Allows configuring a specific runtime version to optimize output - const isVersion = typeof options?.enableBabelRuntime === 'string'; + const isVersion = typeof options.enableBabelRuntime === 'string'; extraPlugins.push([ require('@babel/plugin-transform-runtime'),