Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 4, 2021
1 parent b338001 commit 6dced96
Show file tree
Hide file tree
Showing 45 changed files with 97 additions and 225 deletions.
2 changes: 1 addition & 1 deletion Gulpfile.mjs
Expand Up @@ -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}`;
Expand Down
9 changes: 8 additions & 1 deletion babel.config.js
Expand Up @@ -307,11 +307,18 @@ 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`require("url").URL`,
},
];

return {
visitor: {
MemberExpression(path) {
"MemberExpression|ReferencedIdentifier"(path) {
for (const polyfill of polyfills) {
if (!path.matchesPattern(polyfill.name)) continue;

Expand Down
7 changes: 3 additions & 4 deletions 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",
Expand Down
@@ -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";
Expand Down
@@ -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(
Expand All @@ -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,
},
Expand Down
@@ -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,
),
);
});
Expand Down
8 changes: 3 additions & 5 deletions 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", () => {
Expand All @@ -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,
},
},
});
Expand Down
Expand Up @@ -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";
Expand Down
8 changes: 3 additions & 5 deletions 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)", () => {
Expand Down Expand Up @@ -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,
},
},
};
Expand Down
16 changes: 7 additions & 9 deletions packages/babel-cli/test/index.js
Expand Up @@ -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";
Expand Down Expand Up @@ -64,13 +62,13 @@ const normalizeOutput = function (str, cwd) {
.replace(new RegExp(escapeRegExp(cwd), "g"), "<CWD>")
// (non-win32) /foo/babel/packages -> <CWD>/packages
// (win32) C:\foo\babel\packages -> <CWD>\packages
.replace(new RegExp(escapeRegExp(rootDir), "g"), "<ROOTDIR>");
.replace(new RegExp(escapeRegExp(rootDir), "g"), "<ROOTDIR>/");
if (process.platform === "win32") {
result = result
// C:\\foo\\babel\\packages -> <CWD>\\packages (in js string literal)
.replace(
new RegExp(escapeRegExp(rootDir.replace(/\\/g, "\\\\")), "g"),
"<ROOTDIR>",
new RegExp(escapeRegExp(rootDir.replace(/\//g, "\\\\")), "g"),
"<ROOTDIR>\\",
);
}
return result;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-core/test/api.js
Expand Up @@ -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();
Expand Down
4 changes: 1 addition & 3 deletions 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 });
Expand Down
8 changes: 1 addition & 7 deletions packages/babel-core/test/async.js
@@ -1,5 +1,3 @@
import path from "path";
import { fileURLToPath } from "url";
import * as babel from "..";

import {
Expand All @@ -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 () {
Expand Down
10 changes: 5 additions & 5 deletions 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
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 4 additions & 8 deletions packages/babel-core/test/config-loading.js
Expand Up @@ -3,21 +3,17 @@ import loadConfigRunner, {
createConfigItem,
} from "../lib/config";
import path from "path";
import { fileURLToPath } from "url";
import { createRequire } from "module";

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;
Expand Down
3 changes: 1 addition & 2 deletions 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;
Expand Down
3 changes: 1 addition & 2 deletions 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 });
Expand Down
10 changes: 2 additions & 8 deletions 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 () {
Expand Down
4 changes: 1 addition & 3 deletions 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 () {
Expand Down
7 changes: 1 addition & 6 deletions 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");
8 changes: 1 addition & 7 deletions 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 () {
Expand Down

0 comments on commit 6dced96

Please sign in to comment.