diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 430e454ded3f..fd08ad176246 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -295,7 +295,7 @@ Other than normal Babel options, `options.json` can contain other properties to - **`minNodeVersion`** (string) - If the test requires a minimum Node version, you can add `minNodeVersion` (must be in semver format). + If an `exec.js`` test requires a minimum Node version, you can add `minNodeVersion` (must be in semver format). ```jsonc // options.json example @@ -304,6 +304,8 @@ Other than normal Babel options, `options.json` can contain other properties to } ``` + Use `minNodeVersionTransform` if an `input.js` test requires a minimum Node version. + - **`externalHelpers`** (boolean) By default, all the tests run with the [`@babel/plugin-external-helpers`](https://babel.dev/docs/en/babel-plugin-external-helpers) enabled. You can disable this behavior with diff --git a/packages/babel-helper-fixtures/data/schema.json b/packages/babel-helper-fixtures/data/schema.json index 4acbff00af3d..c65781af8bba 100644 --- a/packages/babel-helper-fixtures/data/schema.json +++ b/packages/babel-helper-fixtures/data/schema.json @@ -4,11 +4,14 @@ "description": "JSON schema for Babel Fixture Test Runner", "allOf": [ { "$ref": "#/definitions/TaskOption" }, - { "$ref": "https://json.schemastore.org/babelrc", "$comment": "Todo: switch to our inhouse schema" } + { + "$ref": "https://json.schemastore.org/babelrc", + "$comment": "Todo: switch to our inhouse schema" + } ], "definitions": { "OS": { - "type":"string", + "type": "string", "description": "The possible values of `process.platform`. See https://nodejs.org/api/process.html#process_process_platform", "enum": ["aix", "darwin", "freebsd", "linux", "openbsd", "sunos", "win32"] }, @@ -17,14 +20,19 @@ "properties": { "BABEL_8_BREAKING": { "description": "Whether this test should run when BABEL_8_BREAKING is enabled", - "type":"boolean" + "type": "boolean" }, "ignoreOutput": { "description": "Whether the test should generate and compare with output.js", "type": "boolean" }, "minNodeVersion": { - "description": "The minimum Node.js version this test should run on", + "description": "The minimum Node.js version the exec.js test should run on", + "type": "string", + "pattern": "^\\d+(\\.\\d+){0,2}$" + }, + "minNodeVersionTransform": { + "description": "The minimum Node.js version the input.js transform test should run on", "type": "string", "pattern": "^\\d+(\\.\\d+){0,2}$" }, @@ -39,12 +47,12 @@ }, "throws": { "description": "Expected thrown error message", - "type":"string", + "type": "string", "default": "" }, "validateLogs": { "description": "Whether this test should validate the stdout and stderr", - "type":"boolean", + "type": "boolean", "default": false } } diff --git a/packages/babel-helper-fixtures/src/index.ts b/packages/babel-helper-fixtures/src/index.ts index 788e9a3970f9..04759bca21c0 100644 --- a/packages/babel-helper-fixtures/src/index.ts +++ b/packages/babel-helper-fixtures/src/index.ts @@ -53,6 +53,7 @@ export interface TaskOptions extends InputOptions { externalHelpers?: boolean; ignoreOutput?: boolean; minNodeVersion?: string; + minNodeVersionTransform?: string; sourceMap?: boolean; os?: string | string[]; validateLogs?: boolean; @@ -269,13 +270,34 @@ function pushTask( } if (semver.lt(nodeVersion, minimumVersion)) { - return; + if (test.actual.code) { + test.exec.code = null; + } else { + return; + } } // Delete to avoid option validation error delete taskOpts.minNodeVersion; } + if (taskOpts.minNodeVersionTransform) { + const minimumVersion = semver.clean(taskOpts.minNodeVersionTransform); + + if (minimumVersion == null) { + throw new Error( + `'minNodeVersionTransform' has invalid semver format: ${taskOpts.minNodeVersionTransform}`, + ); + } + + if (semver.lt(nodeVersion, minimumVersion)) { + return; + } + + // Delete to avoid option validation error + delete taskOpts.minNodeVersionTransform; + } + if (taskOpts.os) { let os = taskOpts.os; diff --git a/packages/babel-helpers/test/fixtures/dependencies/options.json b/packages/babel-helpers/test/fixtures/dependencies/options.json index 786c2fed1398..0911a5a4cf02 100644 --- a/packages/babel-helpers/test/fixtures/dependencies/options.json +++ b/packages/babel-helpers/test/fixtures/dependencies/options.json @@ -1,4 +1,4 @@ { - "minNodeVersion": "12.22.0", + "minNodeVersionTransform": "12.22.0", "externalHelpers": false } diff --git a/packages/babel-helpers/test/fixtures/misc/9496/options.json b/packages/babel-helpers/test/fixtures/misc/9496/options.json index 484633d64278..1d610644a18a 100644 --- a/packages/babel-helpers/test/fixtures/misc/9496/options.json +++ b/packages/babel-helpers/test/fixtures/misc/9496/options.json @@ -1,4 +1,4 @@ { - "minNodeVersion": "12.22.0", + "minNodeVersionTransform": "12.22.0", "plugins": ["./plugin"] } diff --git a/packages/babel-helpers/test/fixtures/misc/arguments-identifier-in-function/options.json b/packages/babel-helpers/test/fixtures/misc/arguments-identifier-in-function/options.json index 96a77b3b4890..1d610644a18a 100644 --- a/packages/babel-helpers/test/fixtures/misc/arguments-identifier-in-function/options.json +++ b/packages/babel-helpers/test/fixtures/misc/arguments-identifier-in-function/options.json @@ -1,4 +1,4 @@ { - "plugins": ["./plugin"], - "minNodeVersion": "12.0.0" + "minNodeVersionTransform": "12.22.0", + "plugins": ["./plugin"] } diff --git a/packages/babel-helpers/test/fixtures/misc/declaration-name-conflict-helper-entrypoint/options.json b/packages/babel-helpers/test/fixtures/misc/declaration-name-conflict-helper-entrypoint/options.json index 484633d64278..1d610644a18a 100644 --- a/packages/babel-helpers/test/fixtures/misc/declaration-name-conflict-helper-entrypoint/options.json +++ b/packages/babel-helpers/test/fixtures/misc/declaration-name-conflict-helper-entrypoint/options.json @@ -1,4 +1,4 @@ { - "minNodeVersion": "12.22.0", + "minNodeVersionTransform": "12.22.0", "plugins": ["./plugin"] } diff --git a/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/options.json b/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/options.json index 4597692aea15..ac9b231f7ce2 100644 --- a/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/options.json +++ b/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/options.json @@ -1,5 +1,5 @@ { - "minNodeVersion": "12.11.0", + "minNodeVersionTransform": "12.11.0", "plugins": [ "./plugin.mjs", "bugfix-safari-id-destructuring-collision-in-function-expression" diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/pipe-body-with-await/options.json b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/pipe-body-with-await/options.json index 60d482269ddd..f8b0106ade5d 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/pipe-body-with-await/options.json +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/pipe-body-with-await/options.json @@ -1,6 +1,6 @@ { - "minNodeVersion": "8.0.0", + "minNodeVersionTransform": "8.0.0", "parserOpts": { - "allowReturnOutsideFunction": true - } + "allowReturnOutsideFunction": true + } } diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/transform-await-and-arrow-functions/options.json b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/transform-await-and-arrow-functions/options.json index 6a7e42fdd0c8..c45028fbe477 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/transform-await-and-arrow-functions/options.json +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/transform-await-and-arrow-functions/options.json @@ -6,5 +6,5 @@ "parserOpts": { "allowReturnOutsideFunction": true }, - "minNodeVersion": "8.0.0" + "minNodeVersionTransform": "8.0.0" } diff --git a/packages/babel-plugin-transform-unicode-sets-regex/test/fixtures/basic/string-properties/options.json b/packages/babel-plugin-transform-unicode-sets-regex/test/fixtures/basic/string-properties/options.json index 22b476c4bb57..f7ec9e02cfe1 100644 --- a/packages/babel-plugin-transform-unicode-sets-regex/test/fixtures/basic/string-properties/options.json +++ b/packages/babel-plugin-transform-unicode-sets-regex/test/fixtures/basic/string-properties/options.json @@ -1,3 +1,3 @@ { - "minNodeVersion": "12.0.0" + "minNodeVersionTransform": "12.0.0" } diff --git a/packages/babel-plugin-transform-unicode-sets-regex/test/fixtures/transform-u/string-properties/options.json b/packages/babel-plugin-transform-unicode-sets-regex/test/fixtures/transform-u/string-properties/options.json index 22b476c4bb57..f7ec9e02cfe1 100644 --- a/packages/babel-plugin-transform-unicode-sets-regex/test/fixtures/transform-u/string-properties/options.json +++ b/packages/babel-plugin-transform-unicode-sets-regex/test/fixtures/transform-u/string-properties/options.json @@ -1,3 +1,3 @@ { - "minNodeVersion": "12.0.0" + "minNodeVersionTransform": "12.0.0" } diff --git a/packages/babel-preset-env/test/fixtures/preset-options-babel-7/unicode-sets-regex-chrome-111/options.json b/packages/babel-preset-env/test/fixtures/preset-options-babel-7/unicode-sets-regex-chrome-111/options.json index 3b8817288759..ca73025b99b5 100644 --- a/packages/babel-preset-env/test/fixtures/preset-options-babel-7/unicode-sets-regex-chrome-111/options.json +++ b/packages/babel-preset-env/test/fixtures/preset-options-babel-7/unicode-sets-regex-chrome-111/options.json @@ -1,5 +1,5 @@ { - "minNodeVersion": "12.0.0", + "minNodeVersionTransform": "12.0.0", "presets": [ [ "env", diff --git a/packages/babel-preset-flow/test/fixtures/flow-parser/hermes/options.json b/packages/babel-preset-flow/test/fixtures/flow-parser/hermes/options.json index 80adf8f23237..cb00f51696fd 100644 --- a/packages/babel-preset-flow/test/fixtures/flow-parser/hermes/options.json +++ b/packages/babel-preset-flow/test/fixtures/flow-parser/hermes/options.json @@ -1,5 +1,5 @@ { "BABEL_8_BREAKING": false, "presets": [["flow", { "experimental_useHermesParser": true }]], - "minNodeVersion": "12.0.0" + "minNodeVersionTransform": "12.0.0" }