Skip to content

Commit

Permalink
fix(babel-preset): deprecate looseClassTransform (#3042)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Apr 2, 2024
1 parent b6610b5 commit 6dd0e03
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-guests-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnx-kit/babel-preset-metro-react-native": patch
---

Deprecate `looseClassTransform`
1 change: 1 addition & 0 deletions packages/babel-preset-metro-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"test": "NODE_OPTIONS=--experimental-vm-modules rnx-kit-scripts test"
},
"dependencies": {
"@rnx-kit/console": "^1.0.12",
"babel-plugin-const-enum": "^1.0.0"
},
"peerDependencies": {
Expand Down
30 changes: 27 additions & 3 deletions packages/babel-preset-metro-react-native/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@
* @typedef {MetroPresetOptions & RnxPresetOptions} PresetOptions
*/

/**
* Converts version string to a number.
* @param {string} version
* @returns {number}
*/
function parseVersion(version) {
const [major, minor = 0] = version.split(".");
return Number(major) * 1000 + Number(minor);
}

/**
* Returns whether Babel implements compiler assumptions.
* @param {ConfigAPI | undefined} api
* @returns {boolean}
*/
function hasCompilerAssumptions(api) {
return Boolean(api?.version && parseVersion(api.version) >= 7013);
}

/**
* Returns plugin for transforming `const enum` if necessary.
*
Expand All @@ -36,8 +55,7 @@ function constEnumPlugin() {
const {
version,
} = require("@babel/plugin-transform-typescript/package.json");
const { [0]: major, [1]: minor } = version.split(".");
if (Number(major) * 1000 + Number(minor) >= 7015) {
if (parseVersion(version) >= 7015) {
return [];
}
} catch (_) {
Expand Down Expand Up @@ -136,7 +154,7 @@ function overridesFor(transformProfile, env) {
* @type {(api?: ConfigAPI, opts?: PresetOptions) => TransformOptions}
*/
module.exports = (
_,
api,
{
additionalPlugins,
looseClassTransform,
Expand Down Expand Up @@ -164,6 +182,12 @@ module.exports = (
});

if (looseClassTransform) {
if (hasCompilerAssumptions(api)) {
const { warn } = require("@rnx-kit/console");
warn(
"`looseClassTransform` is deprecated — consider migrating to the top level assumptions for more granular control (see https://babeljs.io/docs/babel-plugin-transform-classes#loose)"
);
}
const pluginClasses = require.resolve("@babel/plugin-transform-classes", {
paths: [babelPreset],
});
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3549,6 +3549,7 @@ __metadata:
"@babel/core": "npm:^7.20.0"
"@babel/plugin-transform-typescript": "npm:^7.20.0"
"@rnx-kit/babel-plugin-import-path-remapper": "npm:*"
"@rnx-kit/console": "npm:^1.0.12"
"@rnx-kit/eslint-config": "npm:*"
"@rnx-kit/scripts": "npm:*"
"@rnx-kit/tsconfig": "npm:*"
Expand Down

0 comments on commit 6dd0e03

Please sign in to comment.