diff --git a/Gulpfile.mjs b/Gulpfile.mjs index a9b0ccbe7511..40db2f5bbcef 100644 --- a/Gulpfile.mjs +++ b/Gulpfile.mjs @@ -28,7 +28,7 @@ import rollupBabelSource from "./scripts/rollup-plugin-babel-source.js"; import formatCode from "./scripts/utils/formatCode.js"; const require = createRequire(import.meta.url); -const monorepoRoot = path.dirname(fileURLToPath(import.meta.url)); +const monorepoRoot = new URL(".", import.meta.url).pathname; const defaultPackagesGlob = "./@(codemods|packages|eslint)/*"; const defaultSourcesGlob = `${defaultPackagesGlob}/src/**/{*.js,*.cjs,!(*.d).ts}`; diff --git a/babel.config.js b/babel.config.js index 6b15942a97d3..6c88684f7e53 100644 --- a/babel.config.js +++ b/babel.config.js @@ -307,13 +307,25 @@ function pluginPolyfillsOldNode({ template, types: t }) { process.allowedNodeEnvironmentFlags || require("node-environment-flags") `, }, + { + // This became a global in Node.js 10 + name: "URL", + necessary: () => true, + supported: () => true, + replacement: template({ placeholderPattern: false })`require("url").URL`, + }, ]; return { visitor: { - MemberExpression(path) { + "MemberExpression|ReferencedIdentifier"(path) { for (const polyfill of polyfills) { - if (!path.matchesPattern(polyfill.name)) continue; + if ( + !path.matchesPattern(polyfill.name) && + !t.isIdentifier(path.node, { name: polyfill.name }) + ) { + continue; + } if (!polyfill.necessary(path)) return; if (!polyfill.supported(path)) { diff --git a/eslint/babel-eslint-parser/test/index.js b/eslint/babel-eslint-parser/test/index.js index e8a2d73b8544..d8716e50e83b 100644 --- a/eslint/babel-eslint-parser/test/index.js +++ b/eslint/babel-eslint-parser/test/index.js @@ -1,15 +1,14 @@ import path from "path"; import escope from "eslint-scope"; import unpad from "dedent"; -import { fileURLToPath } from "url"; import { createRequire } from "module"; import { parseForESLint } from "../src"; const BABEL_OPTIONS = { - configFile: path.resolve( - path.dirname(fileURLToPath(import.meta.url)), + configFile: new URL( "../../babel-eslint-shared-fixtures/config/babel.config.js", - ), + import.meta.url, + ).pathname, }; const PROPS_TO_REMOVE = [ "importKind", diff --git a/eslint/babel-eslint-plugin-development-internal/test/rules/dry-error-messages.js b/eslint/babel-eslint-plugin-development-internal/test/rules/dry-error-messages.js index e5fc984d3d9e..eb337402650e 100644 --- a/eslint/babel-eslint-plugin-development-internal/test/rules/dry-error-messages.js +++ b/eslint/babel-eslint-plugin-development-internal/test/rules/dry-error-messages.js @@ -1,9 +1,8 @@ import path from "path"; import rule from "../../src/rules/dry-error-messages"; import RuleTester from "../../../babel-eslint-shared-fixtures/utils/RuleTester"; -import { fileURLToPath } from "url"; -const dirname = path.dirname(fileURLToPath(import.meta.url)); +const dirname = new URL(".", import.meta.url).pathname; const FILENAME = path.resolve(dirname, "test/lib/index.js"); const ERRORS_MODULE = "errorsModule"; diff --git a/eslint/babel-eslint-tests/test/helpers/verifyAndAssertMessages.js b/eslint/babel-eslint-tests/test/helpers/verifyAndAssertMessages.js index b8cec5e450d9..3f7381060855 100644 --- a/eslint/babel-eslint-tests/test/helpers/verifyAndAssertMessages.js +++ b/eslint/babel-eslint-tests/test/helpers/verifyAndAssertMessages.js @@ -1,7 +1,5 @@ import eslint from "eslint"; import unpad from "dedent"; -import path from "path"; -import { fileURLToPath } from "url"; import * as parser from "../../../babel-eslint-parser"; export default function verifyAndAssertMessages( @@ -26,10 +24,10 @@ export default function verifyAndAssertMessages( sourceType, requireConfigFile: false, babelOptions: { - configFile: path.resolve( - path.dirname(fileURLToPath(import.meta.url)), + configFile: new URL( "../../../babel-eslint-shared-fixtures/config/babel.config.js", - ), + import.meta.url, + ).pathname, }, ...overrideConfig?.parserOptions, }, diff --git a/eslint/babel-eslint-tests/test/integration/eslint-plugin-import.js b/eslint/babel-eslint-tests/test/integration/eslint-plugin-import.js index 4b5fdc55cb81..bf705a06daed 100644 --- a/eslint/babel-eslint-tests/test/integration/eslint-plugin-import.js +++ b/eslint/babel-eslint-tests/test/integration/eslint-plugin-import.js @@ -1,16 +1,13 @@ import eslint from "eslint"; -import path from "path"; -import { fileURLToPath } from "url"; describe("https://github.com/babel/babel-eslint/issues/558", () => { it("doesn't crash with eslint-plugin-import", () => { const engine = new eslint.CLIEngine({ ignore: false }); engine.executeOnFiles( - ["a.js", "b.js", "c.js"].map(file => - path.resolve( - path.dirname(fileURLToPath(import.meta.url)), - `../fixtures/eslint-plugin-import/${file}`, - ), + ["a.js", "b.js", "c.js"].map( + file => + new URL(`../fixtures/eslint-plugin-import/${file}`, import.meta.url) + .pathname, ), ); }); diff --git a/eslint/babel-eslint-tests/test/integration/eslint/config.js b/eslint/babel-eslint-tests/test/integration/eslint/config.js index 989e9a8287d0..23e5ac93af68 100644 --- a/eslint/babel-eslint-tests/test/integration/eslint/config.js +++ b/eslint/babel-eslint-tests/test/integration/eslint/config.js @@ -1,6 +1,4 @@ import eslint from "eslint"; -import path from "path"; -import { fileURLToPath } from "url"; import * as parser from "@babel/eslint-parser"; describe("ESLint config", () => { @@ -12,10 +10,10 @@ describe("ESLint config", () => { parser: "@babel/eslint-parser", parserOptions: { babelOptions: { - configFile: path.resolve( - path.dirname(fileURLToPath(import.meta.url)), + configFile: new URL( "../../../../babel-eslint-shared-fixtures/config/babel.config.js", - ), + import.meta.url, + ).pathname, }, }, }); diff --git a/eslint/babel-eslint-tests/test/integration/eslint/rules/strict.js b/eslint/babel-eslint-tests/test/integration/eslint/rules/strict.js index 769ba76d2f29..f8cc570c8c84 100644 --- a/eslint/babel-eslint-tests/test/integration/eslint/rules/strict.js +++ b/eslint/babel-eslint-tests/test/integration/eslint/rules/strict.js @@ -2,17 +2,11 @@ import eslint from "eslint"; import fs from "fs"; import path from "path"; import * as parser from "../../../../../babel-eslint-parser"; -import { fileURLToPath } from "url"; eslint.linter.defineParser("@babel/eslint-parser", parser); const paths = { - fixtures: path.join( - path.dirname(fileURLToPath(import.meta.url)), - "../../..", - "fixtures", - "rules", - ), + fixtures: new URL("../../../fixtures/rules", import.meta.url).pathname, }; const encoding = "utf8"; diff --git a/eslint/babel-eslint-tests/test/integration/eslint/verify.js b/eslint/babel-eslint-tests/test/integration/eslint/verify.js index a025c55fdc58..bdd4c428d312 100644 --- a/eslint/babel-eslint-tests/test/integration/eslint/verify.js +++ b/eslint/babel-eslint-tests/test/integration/eslint/verify.js @@ -1,6 +1,4 @@ import verifyAndAssertMessages from "../../helpers/verifyAndAssertMessages"; -import path from "path"; -import { fileURLToPath } from "url"; describe("verify", () => { it("arrow function support (issue #1)", () => { @@ -1082,10 +1080,10 @@ describe("verify", () => { parserOptions: { sourceType, babelOptions: { - configFile: path.resolve( - path.dirname(fileURLToPath(import.meta.url)), + configFile: new URL( "../../../../babel-eslint-shared-fixtures/config/babel.config.decorators-legacy.js", - ), + import.meta.url, + ).pathname, }, }, }; diff --git a/packages/babel-cli/test/index.js b/packages/babel-cli/test/index.js index 0837afd80b0d..e633dbb7c98d 100644 --- a/packages/babel-cli/test/index.js +++ b/packages/babel-cli/test/index.js @@ -7,17 +7,15 @@ import escapeRegExp from "lodash/escapeRegExp"; import merge from "lodash/merge"; import path from "path"; import fs from "fs"; -import { fileURLToPath } from "url"; import { createRequire } from "module"; import { chmod } from "../lib/babel/util"; const require = createRequire(import.meta.url); -const dirname = path.dirname(fileURLToPath(import.meta.url)); -const fixtureLoc = path.join(dirname, "fixtures"); -const tmpLoc = path.join(dirname, "tmp"); -const rootDir = path.resolve(dirname, "../../.."); +const fixtureLoc = new URL("./fixtures", import.meta.url).pathname; +const tmpLoc = new URL("./tmp", import.meta.url).pathname; +const rootDir = new URL("../../..", import.meta.url).pathname; const fileFilter = function (x) { return x !== ".DS_Store"; @@ -64,13 +62,13 @@ const normalizeOutput = function (str, cwd) { .replace(new RegExp(escapeRegExp(cwd), "g"), "") // (non-win32) /foo/babel/packages -> /packages // (win32) C:\foo\babel\packages -> \packages - .replace(new RegExp(escapeRegExp(rootDir), "g"), ""); + .replace(new RegExp(escapeRegExp(rootDir), "g"), "/"); if (process.platform === "win32") { result = result // C:\\foo\\babel\\packages -> \\packages (in js string literal) .replace( - new RegExp(escapeRegExp(rootDir.replace(/\\/g, "\\\\")), "g"), - "", + new RegExp(escapeRegExp(rootDir.replace(/\//g, "\\\\")), "g"), + "\\", ); } return result; @@ -137,7 +135,7 @@ const assertTest = function (stdout, stderr, opts, cwd) { }; const buildTest = function (binName, testName, opts) { - const binLoc = path.join(dirname, "../lib", binName); + const binLoc = new URL(`../lib/${binName}`, import.meta.url).pathname; return function (callback) { saveInFiles(opts.inFiles); diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index ed58229050ac..225d884c352d 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -3,13 +3,12 @@ import sourceMap from "source-map"; import path from "path"; import Plugin from "../lib/config/plugin"; import generator from "@babel/generator"; -import { fileURLToPath } from "url"; import presetEnv from "../../babel-preset-env"; import pluginSyntaxFlow from "../../babel-plugin-syntax-flow"; import pluginFlowStripTypes from "../../babel-plugin-transform-flow-strip-types"; -const cwd = path.dirname(fileURLToPath(import.meta.url)); +const cwd = new URL(".", import.meta.url).pathname; function assertIgnored(result) { expect(result).toBeNull(); diff --git a/packages/babel-core/test/assumptions.js b/packages/babel-core/test/assumptions.js index bf97c3bbb6af..ad8f56692f37 100644 --- a/packages/babel-core/test/assumptions.js +++ b/packages/babel-core/test/assumptions.js @@ -1,8 +1,6 @@ -import path from "path"; -import { fileURLToPath } from "url"; import { loadOptions as loadOptionsOrig, transformSync } from "../lib"; -const cwd = path.dirname(fileURLToPath(import.meta.url)); +const cwd = new URL(".", import.meta.url).pathname; function loadOptions(opts) { return loadOptionsOrig({ cwd, ...opts }); diff --git a/packages/babel-core/test/async.js b/packages/babel-core/test/async.js index d7996c10baf3..068c0c69f633 100644 --- a/packages/babel-core/test/async.js +++ b/packages/babel-core/test/async.js @@ -1,5 +1,3 @@ -import path from "path"; -import { fileURLToPath } from "url"; import * as babel from ".."; import { @@ -15,11 +13,7 @@ const nodeGte8 = (...args) => { }; describe("asynchronicity", () => { - const base = path.join( - path.dirname(fileURLToPath(import.meta.url)), - "fixtures", - "async", - ); + const base = new URL("./fixtures/async", import.meta.url).pathname; let cwd; beforeEach(function () { diff --git a/packages/babel-core/test/config-chain.js b/packages/babel-core/test/config-chain.js index 7e345fd013bd..b90028682d5d 100644 --- a/packages/babel-core/test/config-chain.js +++ b/packages/babel-core/test/config-chain.js @@ -1,12 +1,9 @@ import fs from "fs"; import os from "os"; import path from "path"; -import { fileURLToPath } from "url"; import escapeRegExp from "lodash/escapeRegExp"; import * as babel from "../lib"; -const dirname = path.dirname(fileURLToPath(import.meta.url)); - import { isMJS, loadOptionsAsync, skipUnsupportedESM } from "./helpers/esm"; // TODO: In Babel 8, we can directly uses fs.promises which is supported by @@ -45,11 +42,14 @@ const pfs = }); function fixture(...args) { - return path.join(dirname, "fixtures", "config", ...args); + return new URL(`./fixtures/config/${args.join("/")}`, import.meta.url) + .pathname; } +const cwd = new URL(".", import.meta.url).pathname; + function loadOptions(opts) { - return babel.loadOptions({ cwd: dirname, ...opts }); + return babel.loadOptions({ cwd, ...opts }); } function pairs(items) { diff --git a/packages/babel-core/test/config-loading.js b/packages/babel-core/test/config-loading.js index da84e590d420..99a6a97bf590 100644 --- a/packages/babel-core/test/config-loading.js +++ b/packages/babel-core/test/config-loading.js @@ -3,7 +3,6 @@ import loadConfigRunner, { createConfigItem, } from "../lib/config"; import path from "path"; -import { fileURLToPath } from "url"; import { createRequire } from "module"; const require = createRequire(import.meta.url); @@ -11,13 +10,10 @@ const require = createRequire(import.meta.url); const loadConfig = loadConfigRunner.sync; describe("@babel/core config loading", () => { - const FILEPATH = path.join( - path.dirname(fileURLToPath(import.meta.url)), - "fixtures", - "config-loading", - "folder", - "example.js", - ); + const FILEPATH = new URL( + "./fixtures/config-loading/folder/example.js", + import.meta.url, + ).pathname; afterEach(() => { delete process.env.INVALIDATE_BABELRC; diff --git a/packages/babel-core/test/helpers/esm.js b/packages/babel-core/test/helpers/esm.js index c485f1fa8422..fa082b24f40b 100644 --- a/packages/babel-core/test/helpers/esm.js +++ b/packages/babel-core/test/helpers/esm.js @@ -1,13 +1,12 @@ import cp from "child_process"; import util from "util"; import path from "path"; -import { fileURLToPath } from "url"; import { createRequire } from "module"; import * as babel from "../../lib"; const require = createRequire(import.meta.url); -const dirname = path.dirname(fileURLToPath(import.meta.url)); +const dirname = new URL(".", import.meta.url).pathname; // "minNodeVersion": "10.0.0" <-- For Ctrl+F when dropping node 10 const nodeSupportsESM = parseInt(process.versions.node) >= 12; diff --git a/packages/babel-core/test/option-manager.js b/packages/babel-core/test/option-manager.js index 3bdd98beb44d..0eb27002da68 100644 --- a/packages/babel-core/test/option-manager.js +++ b/packages/babel-core/test/option-manager.js @@ -1,8 +1,7 @@ import { loadOptions as loadOptionsOrig } from "../lib"; import path from "path"; -import { fileURLToPath } from "url"; -const cwd = path.dirname(fileURLToPath(import.meta.url)); +const cwd = new URL(".", import.meta.url).pathname; function loadOptions(opts) { return loadOptionsOrig({ cwd, ...opts }); diff --git a/packages/babel-core/test/parse.js b/packages/babel-core/test/parse.js index b22ff86b4a90..1b04dcbfd696 100644 --- a/packages/babel-core/test/parse.js +++ b/packages/babel-core/test/parse.js @@ -1,18 +1,12 @@ import fs from "fs"; -import path from "path"; import { parse } from "../lib"; -import { fileURLToPath } from "url"; import { createRequire } from "module"; const require = createRequire(import.meta.url); function fixture(...args) { - return path.join( - path.dirname(fileURLToPath(import.meta.url)), - "fixtures", - "parse", - ...args, - ); + return new URL(`./fixtures/parse/${args.join("/")}`, import.meta.url) + .pathname; } describe("parse", function () { diff --git a/packages/babel-core/test/path.js b/packages/babel-core/test/path.js index 416bb4435790..19ab42e92a8c 100644 --- a/packages/babel-core/test/path.js +++ b/packages/babel-core/test/path.js @@ -1,9 +1,7 @@ import { transform } from "../lib/index"; import Plugin from "../lib/config/plugin"; -import { fileURLToPath } from "url"; -import path from "path"; -const cwd = path.dirname(fileURLToPath(import.meta.url)); +const cwd = new URL(".", import.meta.url).pathname; describe("traversal path", function () { it("replaceWithSourceString", function () { diff --git a/packages/babel-core/test/plugins.js b/packages/babel-core/test/plugins.js index a7851bde5c3d..8f4258005c64 100644 --- a/packages/babel-core/test/plugins.js +++ b/packages/babel-core/test/plugins.js @@ -1,8 +1,3 @@ import runner from "@babel/helper-transform-fixture-test-runner"; -import { fileURLToPath } from "url"; -import path from "path"; -runner( - path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures/plugins"), - "plugins", -); +runner(new URL("./fixtures/plugins", import.meta.url).pathname, "plugins"); diff --git a/packages/babel-core/test/resolution.js b/packages/babel-core/test/resolution.js index 191e5436a01b..c763a3f5d230 100644 --- a/packages/babel-core/test/resolution.js +++ b/packages/babel-core/test/resolution.js @@ -1,13 +1,7 @@ import * as babel from "../lib/index"; -import path from "path"; -import { fileURLToPath } from "url"; describe("addon resolution", function () { - const base = path.join( - path.dirname(fileURLToPath(import.meta.url)), - "fixtures", - "resolution", - ); + const base = new URL("./fixtures/resolution", import.meta.url).pathname; let cwd; beforeEach(function () { diff --git a/packages/babel-core/test/transformation.js b/packages/babel-core/test/transformation.js index f4eb1ef790a4..aed227e2dae7 100644 --- a/packages/babel-core/test/transformation.js +++ b/packages/babel-core/test/transformation.js @@ -1,11 +1,6 @@ import runner from "@babel/helper-transform-fixture-test-runner"; -import { fileURLToPath } from "url"; -import path from "path"; runner( - path.join( - path.dirname(fileURLToPath(import.meta.url)), - "/fixtures/transformation", - ), + new URL("./fixtures/transformation", import.meta.url).pathname, "transformation", ); diff --git a/packages/babel-generator/test/index.js b/packages/babel-generator/test/index.js index adbee798e783..71eb4cac612c 100644 --- a/packages/babel-generator/test/index.js +++ b/packages/babel-generator/test/index.js @@ -6,7 +6,6 @@ import fs from "fs"; import path from "path"; import fixtures from "@babel/helper-fixtures"; import sourcemap from "source-map"; -import { fileURLToPath } from "url"; describe("generation", function () { it("completeness", function () { @@ -759,9 +758,7 @@ describe("CodeGenerator", function () { }); }); -const suites = fixtures( - path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures"), -); +const suites = fixtures(new URL("./fixtures", import.meta.url).pathname); suites.forEach(function (testSuite) { describe("generation/" + testSuite.title, function () { @@ -787,7 +784,7 @@ suites.forEach(function (testSuite) { }); const options = { sourceFileName: path.relative( - path.dirname(fileURLToPath(import.meta.url)), + new URL(".", import.meta.url).pathname, actual.loc, ), ...task.options, diff --git a/packages/babel-helper-compilation-targets/test/custom-browserslist-env/custom-browserslist-env.spec.js b/packages/babel-helper-compilation-targets/test/custom-browserslist-env/custom-browserslist-env.spec.js index 4fa8a4a304ba..08855e8911b6 100644 --- a/packages/babel-helper-compilation-targets/test/custom-browserslist-env/custom-browserslist-env.spec.js +++ b/packages/babel-helper-compilation-targets/test/custom-browserslist-env/custom-browserslist-env.spec.js @@ -1,12 +1,10 @@ import getTargets from "../.."; -import { fileURLToPath } from "url"; -import path from "path"; it("allows custom browserslist env", () => { const actual = getTargets( {}, { - configPath: path.dirname(fileURLToPath(import.meta.url)), + configPath: new URL(".", import.meta.url).pathname, browserslistEnv: "custom", }, ); diff --git a/packages/babel-helper-fixtures/src/index.ts b/packages/babel-helper-fixtures/src/index.ts index 2f8beb723497..1612cb61b428 100644 --- a/packages/babel-helper-fixtures/src/index.ts +++ b/packages/babel-helper-fixtures/src/index.ts @@ -2,9 +2,11 @@ import cloneDeep from "lodash/cloneDeep"; import semver from "semver"; import path from "path"; import fs from "fs"; -import { fileURLToPath } from "url"; import { createRequire } from "module"; +// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34960 +declare let URL: typeof import("url").URL; + const require = createRequire(import.meta.url); const nodeVersion = semver.clean(process.version.slice(1)); @@ -283,11 +285,10 @@ function wrapPackagesArray(type, names, optionsDir) { val[0] = path.resolve(optionsDir, val[0]); } else { - const monorepoPath = path.join( - path.dirname(fileURLToPath(import.meta.url)), - "../..", - `babel-${type}-${val[0]}`, - ); + const monorepoPath = new URL( + `../../babel-${type}-${val[0]}`, + import.meta.url, + ).pathname; if (fs.existsSync(monorepoPath)) { val[0] = monorepoPath; diff --git a/packages/babel-helper-module-imports/test/index.js b/packages/babel-helper-module-imports/test/index.js index c1ef591d8aed..8e52ccafe59d 100644 --- a/packages/babel-helper-module-imports/test/index.js +++ b/packages/babel-helper-module-imports/test/index.js @@ -1,10 +1,8 @@ import * as babel from "@babel/core"; -import { fileURLToPath } from "url"; -import path from "path"; import { ImportInjector } from "../"; -const cwd = path.dirname(fileURLToPath(import.meta.url)); +const cwd = new URL(".", import.meta.url).pathname; function test(sourceType, opts, initializer, inputCode, expectedCode) { if (typeof opts === "function") { diff --git a/packages/babel-helper-plugin-test-runner/src/index.js b/packages/babel-helper-plugin-test-runner/src/index.js index a721bacc4d12..5321829d45ab 100644 --- a/packages/babel-helper-plugin-test-runner/src/index.js +++ b/packages/babel-helper-plugin-test-runner/src/index.js @@ -1,6 +1,5 @@ import testRunner from "@babel/helper-transform-fixture-test-runner"; import path from "path"; -import { URL } from "url"; export default function (loc) { if (!process.env.BABEL_8_BREAKING) { diff --git a/packages/babel-helper-transform-fixture-test-runner/src/index.js b/packages/babel-helper-transform-fixture-test-runner/src/index.js index 146f1ae90454..d8fac75a69ec 100644 --- a/packages/babel-helper-transform-fixture-test-runner/src/index.js +++ b/packages/babel-helper-transform-fixture-test-runner/src/index.js @@ -54,10 +54,8 @@ function createContext() { // Populate the "babelHelpers" global with Babel's helper utilities. runCacheableScriptInTestContext( - path.join( - path.dirname(fileURLToPath(import.meta.url)), - "babel-helpers-in-memory.js", - ), + new URL("./babel-helpers-in-memory.js", import.meta.url).pathname, + buildExternalHelpers, context, moduleCache, @@ -338,7 +336,7 @@ function validateFile(actualCode, expectedLoc, expectedCode) { function normalizeOutput(code, normalizePathSeparator) { const projectRoot = path.resolve( - path.dirname(fileURLToPath(import.meta.url)), + new URL(".", import.meta.url).pathname, "../../../", ); const cwdSymbol = ""; diff --git a/packages/babel-node/src/babel-node.js b/packages/babel-node/src/babel-node.js index bbd64140b970..433cef6826b0 100755 --- a/packages/babel-node/src/babel-node.js +++ b/packages/babel-node/src/babel-node.js @@ -4,13 +4,9 @@ */ import getV8Flags from "v8flags"; -import path from "path"; import child_process from "child_process"; -import { fileURLToPath } from "url"; -let args = [ - path.join(path.dirname(fileURLToPath(import.meta.url)), "_babel-node"), -]; +let args = [new URL("./_babel-node", import.meta.url).pathname]; let babelArgs = process.argv.slice(2); let userArgs; diff --git a/packages/babel-node/test/index.js b/packages/babel-node/test/index.js index f9fcdbb6d88d..7675f9a69aca 100644 --- a/packages/babel-node/test/index.js +++ b/packages/babel-node/test/index.js @@ -7,14 +7,12 @@ import child from "child_process"; import merge from "lodash/merge"; import path from "path"; import fs from "fs"; -import { fileURLToPath } from "url"; import { createRequire } from "module"; const require = createRequire(import.meta.url); -const dirname = path.dirname(fileURLToPath(import.meta.url)); -const fixtureLoc = path.join(dirname, "fixtures"); -const tmpLoc = path.join(dirname, "tmp"); +const fixtureLoc = new URL("./fixtures", import.meta.url).pathname; +const tmpLoc = new URL("./tmp", import.meta.url).pathname; const fileFilter = function (x) { return x !== ".DS_Store"; @@ -96,7 +94,7 @@ const assertTest = function (stdout, stderr, opts) { }; const buildTest = function (binName, testName, opts) { - const binLoc = path.join(dirname, "../lib", binName); + const binLoc = new URL(`../lib/${binName}`, import.meta.url).pathname; return function (callback) { saveInFiles(opts.inFiles); diff --git a/packages/babel-parser/test/estree-throws.js b/packages/babel-parser/test/estree-throws.js index c2f45bef2314..a73e3a8c9d4e 100644 --- a/packages/babel-parser/test/estree-throws.js +++ b/packages/babel-parser/test/estree-throws.js @@ -1,9 +1,4 @@ -import path from "path"; import { runThrowTestsWithEstree } from "./helpers/runFixtureTests"; import { parse } from "../lib"; -import { fileURLToPath } from "url"; -runThrowTestsWithEstree( - path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures"), - parse, -); +runThrowTestsWithEstree(new URL("./fixtures", import.meta.url).pathname, parse); diff --git a/packages/babel-parser/test/expressions.js b/packages/babel-parser/test/expressions.js index 814ebbd65ade..58c17a95b517 100644 --- a/packages/babel-parser/test/expressions.js +++ b/packages/babel-parser/test/expressions.js @@ -1,11 +1,6 @@ -import path from "path"; import { runFixtureTests } from "./helpers/runFixtureTests"; import { parseExpression } from "../lib"; -import { fileURLToPath } from "url"; -const fixtures = path.join( - path.dirname(fileURLToPath(import.meta.url)), - "expressions", -); +const fixtures = new URL("./expressions", import.meta.url).pathname; runFixtureTests(fixtures, parseExpression); diff --git a/packages/babel-parser/test/helpers/runFixtureTests.js b/packages/babel-parser/test/helpers/runFixtureTests.js index 165792e80272..e8a798f40c89 100644 --- a/packages/babel-parser/test/helpers/runFixtureTests.js +++ b/packages/babel-parser/test/helpers/runFixtureTests.js @@ -2,12 +2,8 @@ import { multiple as getFixtures } from "@babel/helper-fixtures"; import { codeFrameColumns } from "@babel/code-frame"; import fs from "fs"; import path from "path"; -import { fileURLToPath } from "url"; -const rootPath = path.join( - path.dirname(fileURLToPath(import.meta.url)), - "../../../..", -); +const rootPath = new URL("../../../..", import.meta.url).pathname; const serialized = "$$ babel internal serialized type"; diff --git a/packages/babel-parser/test/index.js b/packages/babel-parser/test/index.js index a87e7b44cc9d..7ddf3699fb5e 100644 --- a/packages/babel-parser/test/index.js +++ b/packages/babel-parser/test/index.js @@ -1,10 +1,5 @@ -import path from "path"; import { runFixtureTests } from "./helpers/runFixtureTests"; import { parse } from "../lib"; -import { fileURLToPath } from "url"; -const fixtures = path.join( - path.dirname(fileURLToPath(import.meta.url)), - "fixtures", -); +const fixtures = new URL("./fixtures", import.meta.url).pathname; runFixtureTests(fixtures, parse); diff --git a/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js b/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js index 4fb76081850a..a597540123b1 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js @@ -1,6 +1,4 @@ import * as babel from "@babel/core"; -import { fileURLToPath } from "url"; -import path from "path"; import transformCommonJS from ".."; @@ -8,7 +6,7 @@ test("Doesn't use the same object for two different nodes in the AST", function const code = 'import Foo from "bar"; Foo; Foo;'; const ast = babel.transform(code, { - cwd: path.dirname(fileURLToPath(import.meta.url)), + cwd: new URL(".", import.meta.url).pathname, ast: true, plugins: [[transformCommonJS, { loose: true }]], }).ast; diff --git a/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js b/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js index cdb44988f7a4..484567a218f5 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js @@ -1,7 +1,5 @@ import * as babel from "@babel/core"; import vm from "vm"; -import { fileURLToPath } from "url"; -import path from "path"; import transformCommonJS from ".."; @@ -23,7 +21,7 @@ test("Re-export doesn't overwrite __esModule flag", function () { context.exports = context.module.exports; code = babel.transform(code, { - cwd: path.dirname(fileURLToPath(import.meta.url)), + cwd: new URL(".", import.meta.url).pathname, plugins: [[transformCommonJS, { loose: true }]], ast: false, }).code; diff --git a/packages/babel-preset-env/test/regressions.js b/packages/babel-preset-env/test/regressions.js index 9f201b0adb3e..7ee4063def86 100644 --- a/packages/babel-preset-env/test/regressions.js +++ b/packages/babel-preset-env/test/regressions.js @@ -1,7 +1,5 @@ import * as babel7_12 from "@babel/core"; import env from ".."; -import path from "path"; -import { fileURLToPath } from "url"; describe("#12880", () => { it("read the .browserslistrc file when using @babel/core < 7.13.0", () => { @@ -11,11 +9,7 @@ describe("#12880", () => { const out = babel7_12.transformSync("a ** b; a => b;", { configFile: false, presets: [[env, { modules: false }]], - filename: path.join( - path.dirname(fileURLToPath(import.meta.url)), - "regressions", - "input.js", - ), + filename: new URL("./regressions/input.js", import.meta.url).pathname, }); expect(out.code).toMatchInlineSnapshot(` diff --git a/packages/babel-register/test/browserify.js b/packages/babel-register/test/browserify.js index 8fb983b97b8f..cec0d9f16b73 100644 --- a/packages/babel-register/test/browserify.js +++ b/packages/babel-register/test/browserify.js @@ -1,15 +1,10 @@ import browserify from "browserify"; -import path from "path"; import vm from "vm"; -import { fileURLToPath } from "url"; describe("browserify", function () { it("@babel/register may be used without breaking browserify", function (done) { const bundler = browserify( - path.join( - path.dirname(fileURLToPath(import.meta.url)), - "fixtures/browserify/register.js", - ), + new URL("./fixtures/browserify/register.js", import.meta.url).pathname, ); bundler.bundle(function (err, bundle) { diff --git a/packages/babel-register/test/cache.js b/packages/babel-register/test/cache.js index 222889871af8..995b6ac31db3 100644 --- a/packages/babel-register/test/cache.js +++ b/packages/babel-register/test/cache.js @@ -1,11 +1,6 @@ import fs from "fs"; -import path from "path"; -import { fileURLToPath } from "url"; -const testCacheFilename = path.join( - path.dirname(fileURLToPath(import.meta.url)), - ".babel", -); +const testCacheFilename = new URL("./.babel", import.meta.url).pathname; const oldBabelDisableCacheValue = process.env.BABEL_DISABLE_CACHE; process.env.BABEL_CACHE_PATH = testCacheFilename; diff --git a/scripts/generators/tsconfig.js b/scripts/generators/tsconfig.js index eee32f516d98..f8f11e82410e 100644 --- a/scripts/generators/tsconfig.js +++ b/scripts/generators/tsconfig.js @@ -1,14 +1,10 @@ import path from "path"; import fs from "fs"; import { createRequire } from "module"; -import { fileURLToPath } from "url"; const require = createRequire(import.meta.url); -const root = path.resolve( - path.dirname(fileURLToPath(import.meta.url)), - "../../" -); +const root = new URL("../..", import.meta.url).pathname; function getTsPkgs(subRoot) { return fs diff --git a/scripts/parser-tests/flow/index.js b/scripts/parser-tests/flow/index.js index 7b7e8a0b0c8e..4256aac51ceb 100644 --- a/scripts/parser-tests/flow/index.js +++ b/scripts/parser-tests/flow/index.js @@ -1,11 +1,8 @@ import fs from "fs/promises"; import path from "path"; -import { fileURLToPath } from "url"; import merge from "mergeiterator"; import TestRunner from "../utils/parser-test-runner.js"; -const dirname = path.dirname(fileURLToPath(import.meta.url)); - const flowOptionsMapping = { esproposal_class_instance_fields: "classProperties", esproposal_class_static_fields: "classProperties", @@ -91,8 +88,9 @@ async function* loadTests(root) { } const runner = new TestRunner({ - testDir: path.join(dirname, "../../../build/flow/src/parser/test/flow"), - allowlist: path.join(dirname, "allowlist.txt"), + testDir: new URL("../../../build/flow/src/parser/test/flow", import.meta.url) + .pathname, + allowlist: new URL("./allowlist.txt", import.meta.url).pathname, shouldUpdate: process.argv.includes("--update-allowlist"), async *getTests() { diff --git a/scripts/parser-tests/test262/index.js b/scripts/parser-tests/test262/index.js index 3c4d1856ad2b..18f284ecd21f 100644 --- a/scripts/parser-tests/test262/index.js +++ b/scripts/parser-tests/test262/index.js @@ -1,10 +1,6 @@ -import path from "path"; -import { fileURLToPath } from "url"; import TestStream from "test262-stream"; import TestRunner from "../utils/parser-test-runner.js"; -const dirname = path.dirname(fileURLToPath(import.meta.url)); - const ignoredFeatures = [ "__getter__", "__setter__", @@ -166,8 +162,8 @@ function* getPlugins(features) { } const runner = new TestRunner({ - testDir: path.join(dirname, "../../../build/test262"), - allowlist: path.join(dirname, "allowlist.txt"), + testDir: new URL("../../../build/test262", import.meta.url).pathname, + allowlist: new URL("./allowlist.txt", import.meta.url).pathname, logInterval: 500, shouldUpdate: process.argv.includes("--update-allowlist"), diff --git a/scripts/parser-tests/typescript/index.js b/scripts/parser-tests/typescript/index.js index 52813b16587d..442512f674a3 100644 --- a/scripts/parser-tests/typescript/index.js +++ b/scripts/parser-tests/typescript/index.js @@ -1,12 +1,9 @@ import path from "path"; import fs from "fs/promises"; -import { fileURLToPath } from "url"; import ts from "../../../build/typescript/lib/typescript.js"; import TestRunner from "../utils/parser-test-runner.js"; import parsingErrorCodes from "./error-codes.js"; -const dirname = path.dirname(fileURLToPath(import.meta.url)); - async function* loadTests(dir) { const names = await fs.readdir(dir); @@ -24,17 +21,16 @@ const plugins = [ "dynamicImport", ]; -const TSTestsPath = path.join(dirname, "../../../build/typescript/tests"); +const TSTestsUrl = new URL("../../../build/typescript/tests/", import.meta.url); // Check if the baseline errors contain the codes that should also be thrown from babel-parser async function baselineContainsParserErrorCodes(testName) { try { const baselineErrors = await fs.readFile( - path.join( - TSTestsPath, - "baselines/reference", - testName.replace(/\.tsx?$/, ".errors.txt") - ), + new URL( + `./baselines/reference/${testName.replace(/\.tsx?$/, ".errors.txt")}`, + TSTestsUrl + ).pathname, "utf8" ); return parsingErrorCodes.some(code => baselineErrors.includes(code)); @@ -47,8 +43,8 @@ async function baselineContainsParserErrorCodes(testName) { } const runner = new TestRunner({ - testDir: path.join(TSTestsPath, "./cases/compiler"), - allowlist: path.join(dirname, "allowlist.txt"), + testDir: new URL("./cases/compiler", TSTestsUrl).pathname, + allowlist: new URL("./allowlist.txt", import.meta.url).pathname, logInterval: 50, shouldUpdate: process.argv.includes("--update-allowlist"), diff --git a/scripts/rollup-plugin-babel-source.js b/scripts/rollup-plugin-babel-source.js index 522dfd9789ee..5d723b5adf8f 100644 --- a/scripts/rollup-plugin-babel-source.js +++ b/scripts/rollup-plugin-babel-source.js @@ -1,13 +1,9 @@ import path from "path"; import fs from "fs"; -import { fileURLToPath } from "url"; import { createRequire } from "module"; const require = createRequire(import.meta.url); -const monorepoRoot = path.join( - path.dirname(fileURLToPath(import.meta.url)), - ".." -); +const monorepoRoot = new URL("..", import.meta.url).pathname; const BABEL_SRC_REGEXP = path.sep === "/"