Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use semver package to compare versions #15548

Merged
merged 1 commit into from Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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