Skip to content

Commit

Permalink
Use semver package to compare versions (#15548)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Apr 5, 2023
1 parent 0a1cfc0 commit c45fa42
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
Expand Up @@ -25,7 +25,8 @@
"@babel/helper-optimise-call-expression": "workspace:^",
"@babel/helper-replace-supers": "workspace:^",
"@babel/helper-skip-transparent-expression-wrappers": "workspace:^",
"@babel/helper-split-export-declaration": "workspace:^"
"@babel/helper-split-export-declaration": "workspace:^",
"semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
Expand Down
22 changes: 10 additions & 12 deletions packages/babel-helper-create-class-features-plugin/src/index.ts
Expand Up @@ -3,6 +3,9 @@ import type { PluginAPI, PluginObject } from "@babel/core";
import type { NodePath } from "@babel/traverse";
import nameFunction from "@babel/helper-function-name";
import splitExportDeclaration from "@babel/helper-split-export-declaration";

import semver from "semver";

import {
buildPrivateNamesNodes,
buildPrivateNamesMap,
Expand All @@ -19,14 +22,6 @@ import { assertFieldTransformed } from "./typescript";
export { FEATURES, enableFeature, injectInitialization, buildCheckInRHS };

declare const PACKAGE_JSON: { name: string; version: string };

// Note: Versions are represented as an integer. e.g. 7.1.5 is represented
// as 70000100005. This method is easier than using a semver-parsing
// package, but it breaks if we release x.y.z where x, y or z are
// greater than 99_999.
const version = PACKAGE_JSON.version
.split(".")
.reduce((v, x) => v * 1e5 + +x, 0);
const versionKey = "@babel/plugin-class-features/version";

interface Options {
Expand Down Expand Up @@ -99,14 +94,17 @@ export function createClassFeaturePlugin({
pre(file) {
enableFeature(file, feature, loose);

if (!file.get(versionKey) || file.get(versionKey) < version) {
file.set(versionKey, version);
if (
!file.get(versionKey) ||
semver.lt(file.get(versionKey), PACKAGE_JSON.version)
) {
file.set(versionKey, PACKAGE_JSON.version);
}
},

visitor: {
Class(path, { file }) {
if (file.get(versionKey) !== version) return;
if (file.get(versionKey) !== PACKAGE_JSON.version) return;

if (!shouldTransform(path, file)) return;

Expand Down Expand Up @@ -301,7 +299,7 @@ export function createClassFeaturePlugin({

ExportDefaultDeclaration(path, { file }) {
if (!process.env.BABEL_8_BREAKING) {
if (file.get(versionKey) !== version) return;
if (file.get(versionKey) !== PACKAGE_JSON.version) return;

const decl = path.get("declaration");

Expand Down
Expand Up @@ -19,7 +19,8 @@
],
"dependencies": {
"@babel/helper-annotate-as-pure": "workspace:^",
"regexpu-core": "^5.3.1"
"regexpu-core": "^5.3.1",
"semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
Expand Down
26 changes: 11 additions & 15 deletions packages/babel-helper-create-regexp-features-plugin/src/index.ts
@@ -1,4 +1,10 @@
import rewritePattern from "regexpu-core";
import type { NodePath } from "@babel/traverse";
import { types as t, type PluginObject } from "@babel/core";
import annotateAsPure from "@babel/helper-annotate-as-pure";

import semver from "semver";

import {
featuresKey,
FEATURES,
Expand All @@ -7,21 +13,8 @@ import {
hasFeature,
} from "./features";
import { generateRegexpuOptions, canSkipRegexpu, transformFlags } from "./util";
import type { NodePath } from "@babel/traverse";

import { types as t } from "@babel/core";
import type { PluginObject } from "@babel/core";
import annotateAsPure from "@babel/helper-annotate-as-pure";

declare const PACKAGE_JSON: { name: string; version: string };

// Note: Versions are represented as an integer. e.g. 7.1.5 is represented
// as 70000100005. This method is easier than using a semver-parsing
// package, but it breaks if we release x.y.z where x, y or z are
// greater than 99_999.
const version = PACKAGE_JSON.version
.split(".")
.reduce((v, x) => v * 1e5 + +x, 0);
const versionKey = "@babel/plugin-regexp-features/version";

export interface Options {
Expand Down Expand Up @@ -83,8 +76,11 @@ export function createRegExpFeaturePlugin({
}
}

if (!file.has(versionKey) || file.get(versionKey) < version) {
file.set(versionKey, version);
if (
!file.has(versionKey) ||
semver.lt(file.get(versionKey), PACKAGE_JSON.version)
) {
file.set(versionKey, PACKAGE_JSON.version);
}
},

Expand Down
2 changes: 2 additions & 0 deletions yarn.lock
Expand Up @@ -613,6 +613,7 @@ __metadata:
"@babel/helper-split-export-declaration": "workspace:^"
"@babel/plugin-syntax-class-static-block": ^7.14.5
"@babel/preset-env": "workspace:^"
semver: "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
peerDependencies:
"@babel/core": ^7.0.0
languageName: unknown
Expand All @@ -638,6 +639,7 @@ __metadata:
"@babel/helper-annotate-as-pure": "workspace:^"
"@babel/helper-plugin-test-runner": "workspace:^"
regexpu-core: ^5.3.1
semver: "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
peerDependencies:
"@babel/core": ^7.0.0
languageName: unknown
Expand Down

0 comments on commit c45fa42

Please sign in to comment.