Avoid using CJS globals in internal source files (#12963)

* Lint against CJS globals in modules

* Use `import.meta.url` instead of `__filename` in `src` files

* Prepare fixtures runner for `import.meta.url`

* Use `import.meta.url` instead of `__filename` in `test/index` files

* Remove `__dirname` from remaining test files

dirname

* Avoid using `module` in `src` files

* Avoid using `require` in `src` files

* Avoid using `require` in `test` files

* Update `@types/node`

* Compile dynamic import in `@babel/node`

* Fix windows

* Use `@babel/plugin-proposal-dynamic-import` from npm
This commit is contained in:
Nicolò Ribaudo 2021-03-05 19:55:36 +01:00 committed by GitHub
parent ea620e822e
commit d04842a700
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
147 changed files with 651 additions and 346 deletions

View File

@ -2,6 +2,8 @@
const path = require("path");
const cjsGlobals = ["__dirname", "__filename", "require", "module", "exports"];
module.exports = {
root: true,
plugins: [
@ -68,6 +70,25 @@ module.exports = {
"import/extensions": ["error", { json: "always", cjs: "always" }],
},
},
{
files: [
"packages/*/src/**/*.{js,ts}",
"codemods/*/src/**/*.{js,ts}",
"eslint/*/src/**/*.{js,ts}",
"packages/*/test/**/*.js",
"codemods/*/test/**/*.js",
"eslint/*/test/**/*.js",
"packages/babel-helper-transform-fixture-test-runner/src/helpers.{ts,js}",
"test/**/*.js",
],
excludedFiles: [
// @babel/register is the require() hook, so it will always be CJS-based
"packages/babel-register/**/*.js",
],
rules: {
"no-restricted-globals": ["error", ...cjsGlobals],
},
},
{
files: ["packages/babel-plugin-*/src/index.{js,ts}"],
excludedFiles: ["packages/babel-plugin-transform-regenerator/**/*.js"],

View File

@ -159,6 +159,7 @@ module.exports = function (api) {
convertESM ? "@babel/proposal-export-namespace-from" : null,
convertESM ? "@babel/transform-modules-commonjs" : null,
convertESM ? pluginNodeImportInterop : null,
convertESM ? pluginImportMetaUrl : null,
pluginPackageJsonMacro,
@ -177,14 +178,17 @@ module.exports = function (api) {
plugins: ["babel-plugin-transform-charcodes"],
assumptions: parserAssumptions,
},
{
convertESM && {
test: ["./packages/babel-cli", "./packages/babel-core"].map(normalize),
plugins: [
// Explicitly use the lazy version of CommonJS modules.
convertESM
? ["@babel/transform-modules-commonjs", { lazy: true }]
: null,
].filter(Boolean),
["@babel/transform-modules-commonjs", { lazy: true }],
],
},
convertESM && {
test: ["./packages/babel-node/src"].map(normalize),
// Used to conditionally import kexec
plugins: ["@babel/plugin-proposal-dynamic-import"],
},
{
test: sources.map(normalize),
@ -465,3 +469,83 @@ function pluginNodeImportInterop({ template }) {
},
};
}
function pluginImportMetaUrl({ types: t, template }) {
const isImportMeta = node =>
t.isMetaProperty(node) &&
t.isIdentifier(node.meta, { name: "import" }) &&
t.isIdentifier(node.property, { name: "meta" });
const isImportMetaUrl = node =>
t.isMemberExpression(node, { computed: false }) &&
t.isIdentifier(node.property, { name: "url" }) &&
isImportMeta(node.object);
return {
visitor: {
Program(programPath) {
// We must be sure to run this before the instanbul plugins, because its
// instrumentation breaks our detection.
programPath.traverse({
// fileURLToPath(import.meta.url)
CallExpression(path) {
const { node } = path;
if (
!t.isIdentifier(node.callee, { name: "fileURLToPath" }) ||
node.arguments.length !== 1
) {
return;
}
const arg = node.arguments[0];
if (
!t.isMemberExpression(arg, { computed: false }) ||
!t.isIdentifier(arg.property, { name: "url" }) ||
!isImportMeta(arg.object)
) {
return;
}
path.replaceWith(t.identifier("__filename"));
},
// const require = createRequire(import.meta.url)
VariableDeclarator(path) {
const { node } = path;
if (
!t.isIdentifier(node.id, { name: "require" }) ||
!t.isCallExpression(node.init) ||
!t.isIdentifier(node.init.callee, { name: "createRequire" }) ||
node.init.arguments.length !== 1 ||
!isImportMetaUrl(node.init.arguments[0])
) {
return;
}
// Let's just remove this declaration to unshadow the "global" cjs require.
path.remove();
},
// import.meta.url
MemberExpression(path) {
if (!isImportMetaUrl(path.node)) return;
path.replaceWith(
template.expression
.ast`\`file://\${__filename.replace(/\\\\/g, "/")}\``
);
},
MetaProperty(path) {
if (isImportMeta(path.node)) {
throw path.buildCodeFrameError("Unsupported import.meta");
}
},
});
},
},
};
}

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,10 +1,13 @@
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: require.resolve(
configFile: path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../../babel-eslint-shared-fixtures/config/babel.config.js",
),
};
@ -73,6 +76,8 @@ describe("Babel and Espree", () => {
}
beforeAll(async () => {
const require = createRequire(import.meta.url);
// Use the version of Espree that is a dependency of
// the version of ESLint we are testing against.
const espreePath = require.resolve("espree", {

View File

@ -1,7 +1,7 @@
import dryErrorMessages from "./rules/dry-error-messages";
module.exports = {
rules: {
export const rules = {
"dry-error-messages": dryErrorMessages,
},
};
export default { rules };

View File

@ -1,11 +1,14 @@
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 FILENAME = path.resolve(__dirname, "test/lib/index.js");
const dirname = path.dirname(fileURLToPath(import.meta.url));
const FILENAME = path.resolve(dirname, "test/lib/index.js");
const ERRORS_MODULE = "errorsModule";
const MODULE_SAME_DIR = path.resolve(__dirname, "test/lib/errorsModule.js");
const MODULE_PARENT_DIR = path.resolve(__dirname, "test/errorsModule.js");
const MODULE_SAME_DIR = path.resolve(dirname, "test/lib/errorsModule.js");
const MODULE_PARENT_DIR = path.resolve(dirname, "test/errorsModule.js");
const ruleTester = new RuleTester();

View File

@ -2,10 +2,10 @@ import noDeprecatedClone from "./rules/no-deprecated-clone";
import noUndefinedIdentifier from "./rules/no-undefined-identifier";
import pluginName from "./rules/plugin-name";
module.exports = {
rules: {
export const rules = {
"no-deprecated-clone": noDeprecatedClone,
"no-undefined-identifier": noUndefinedIdentifier,
"plugin-name": pluginName,
},
};
export default { rules };

View File

@ -4,19 +4,20 @@ import noUnusedExpressions from "./rules/no-unused-expressions";
import objectCurlySpacing from "./rules/object-curly-spacing";
import semi from "./rules/semi";
module.exports = {
rules: {
export const rules = {
"new-cap": newCap,
"no-invalid-this": noInvalidThis,
"no-unused-expressions": noUnusedExpressions,
"object-curly-spacing": objectCurlySpacing,
semi,
},
rulesConfig: {
};
export const rulesConfig = {
"new-cap": "off",
"no-invalid-this": "off",
"no-unused-expressions": "off",
"object-curly-spacing": "off",
semi: "off",
},
};
export default { rules, rulesConfig };

View File

@ -1,5 +1,7 @@
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(
@ -24,7 +26,8 @@ export default function verifyAndAssertMessages(
sourceType,
requireConfigFile: false,
babelOptions: {
configFile: require.resolve(
configFile: path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../../../babel-eslint-shared-fixtures/config/babel.config.js",
),
},

View File

@ -1,12 +1,16 @@
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(__dirname, `../fixtures/eslint-plugin-import/${file}`),
path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
`../fixtures/eslint-plugin-import/${file}`,
),
),
);
});

View File

@ -1,4 +1,6 @@
import eslint from "eslint";
import path from "path";
import { fileURLToPath } from "url";
import * as parser from "@babel/eslint-parser";
describe("ESLint config", () => {
@ -10,7 +12,8 @@ describe("ESLint config", () => {
parser: "@babel/eslint-parser",
parserOptions: {
babelOptions: {
configFile: require.resolve(
configFile: path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../../../../babel-eslint-shared-fixtures/config/babel.config.js",
),
},

View File

@ -2,11 +2,17 @@ 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(__dirname, "../../..", "fixtures", "rules"),
fixtures: path.join(
path.dirname(fileURLToPath(import.meta.url)),
"../../..",
"fixtures",
"rules",
),
};
const encoding = "utf8";

View File

@ -1,4 +1,6 @@
import verifyAndAssertMessages from "../../helpers/verifyAndAssertMessages";
import path from "path";
import { fileURLToPath } from "url";
describe("verify", () => {
it("arrow function support (issue #1)", () => {
@ -1080,7 +1082,8 @@ describe("verify", () => {
parserOptions: {
sourceType,
babelOptions: {
configFile: require.resolve(
configFile: path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../../../../babel-eslint-shared-fixtures/config/babel.config.decorators-legacy.js",
),
},

View File

@ -2,6 +2,10 @@
* Basic declarations for the npm modules we use.
*/
declare module "module" {
declare export function createRequire(url: any): any;
}
declare module "debug" {
declare export default (namespace: string) => (formatter: string, ...args: any[]) => void;
}

View File

@ -24,6 +24,7 @@
"@babel/eslint-parser": "workspace:*",
"@babel/eslint-plugin-development": "workspace:*",
"@babel/eslint-plugin-development-internal": "workspace:*",
"@babel/plugin-proposal-dynamic-import": "^7.13.8",
"@babel/plugin-proposal-export-namespace-from": "^7.12.13",
"@babel/plugin-proposal-object-rest-spread": "^7.13.0",
"@babel/plugin-transform-modules-commonjs": "^7.13.0",

View File

@ -4,6 +4,7 @@ import readdirRecursive from "fs-readdir-recursive";
import * as babel from "@babel/core";
import path from "path";
import fs from "fs";
import { createRequire } from "module";
export function chmod(src: string, dest: string): void {
try {
@ -119,6 +120,9 @@ process.on("uncaughtException", function (err) {
});
export function requireChokidar(): Object {
// $FlowIgnore - https://github.com/facebook/flow/issues/6913#issuecomment-662787504
const require = createRequire(import /*::("")*/.meta.url);
try {
// todo(babel 8): revert `@nicolo-ribaudo/chokidar-2` hack
return parseInt(process.versions.node) >= 8

View File

@ -1,17 +1,23 @@
const readdir = require("fs-readdir-recursive");
const helper = require("@babel/helper-fixtures");
const rimraf = require("rimraf");
const { sync: makeDirSync } = require("make-dir");
const child = require("child_process");
const escapeRegExp = require("lodash/escapeRegExp");
const merge = require("lodash/merge");
const path = require("path");
const fs = require("fs");
const { chmod } = require("../lib/babel/util");
import readdir from "fs-readdir-recursive";
import * as helper from "@babel/helper-fixtures";
import rimraf from "rimraf";
import { sync as makeDirSync } from "make-dir";
import child from "child_process";
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";
const fixtureLoc = path.join(__dirname, "fixtures");
const tmpLoc = path.join(__dirname, "tmp");
const rootDir = path.resolve(__dirname, "../../..");
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 fileFilter = function (x) {
return x !== ".DS_Store";
@ -131,7 +137,7 @@ const assertTest = function (stdout, stderr, opts, cwd) {
};
const buildTest = function (binName, testName, opts) {
const binLoc = path.join(__dirname, "../lib", binName);
const binLoc = path.join(dirname, "../lib", binName);
return function (callback) {
saveInFiles(opts.inFiles);

View File

@ -18,6 +18,10 @@ import type { CallerMetadata } from "../validation/options";
import * as fs from "../../gensync-utils/fs";
import { createRequire } from "module";
// $FlowIgnore - https://github.com/facebook/flow/issues/6913#issuecomment-662787504
const require = createRequire(import /*::("")*/.meta.url);
const debug = buildDebug("babel:config:loading:files:configuration");
export const ROOT_CONFIG_FILENAMES = [

View File

@ -2,6 +2,9 @@ import { isAsync, waitFor } from "../../gensync-utils/async";
import type { Handler } from "gensync";
import path from "path";
import { pathToFileURL } from "url";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
let import_;
try {

View File

@ -9,6 +9,10 @@ import path from "path";
import { type Handler } from "gensync";
import loadCjsOrMjsDefault from "./module-types";
import { createRequire } from "module";
// $FlowIgnore - https://github.com/facebook/flow/issues/6913#issuecomment-662787504
const require = createRequire(import /*::("")*/.meta.url);
const debug = buildDebug("babel:config:loading:files:plugins");
const EXACT_RE = /^module:/;

View File

@ -3,6 +3,13 @@ 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));
function assertIgnored(result) {
expect(result).toBeNull();
@ -13,54 +20,26 @@ function assertNotIgnored(result) {
}
function parse(code, opts) {
return babel.parse(code, {
cwd: __dirname,
configFile: false,
...opts,
});
return babel.parse(code, { cwd, configFile: false, ...opts });
}
function transform(code, opts) {
return babel.transform(code, {
cwd: __dirname,
configFile: false,
...opts,
});
return babel.transform(code, { cwd, configFile: false, ...opts });
}
function transformFile(filename, opts, cb) {
return babel.transformFile(
filename,
{
cwd: __dirname,
configFile: false,
...opts,
},
cb,
);
return babel.transformFile(filename, { cwd, configFile: false, ...opts }, cb);
}
function transformFileSync(filename, opts) {
return babel.transformFileSync(filename, {
cwd: __dirname,
configFile: false,
...opts,
});
return babel.transformFileSync(filename, { cwd, configFile: false, ...opts });
}
function transformAsync(code, opts) {
return babel.transformAsync(code, {
cwd: __dirname,
configFile: false,
...opts,
});
return babel.transformAsync(code, { cwd, configFile: false, ...opts });
}
function transformFromAst(ast, code, opts) {
return babel.transformFromAst(ast, code, {
cwd: __dirname,
configFile: false,
...opts,
});
return babel.transformFromAst(ast, code, { cwd, configFile: false, ...opts });
}
describe("parser and generator options", function () {
@ -111,7 +90,7 @@ describe("parser and generator options", function () {
function newTransformWithPlugins(string) {
return transform(string, {
ast: true,
plugins: [__dirname + "/../../babel-plugin-syntax-flow"],
plugins: [cwd + "/../../babel-plugin-syntax-flow"],
parserOpts: {
parser: recast.parse,
},
@ -173,17 +152,13 @@ describe("api", function () {
babelrc: false,
};
Object.freeze(options);
transformFile(
__dirname + "/fixtures/api/file.js",
options,
function (err, res) {
transformFile(cwd + "/fixtures/api/file.js", options, function (err, res) {
if (err) return done(err);
expect(res.code).toBe("foo();");
// keep user options untouched
expect(options).toEqual({ babelrc: false });
done();
},
);
});
});
it("transformFileSync", function () {
@ -191,9 +166,9 @@ describe("api", function () {
babelrc: false,
};
Object.freeze(options);
expect(
transformFileSync(__dirname + "/fixtures/api/file.js", options).code,
).toBe("foo();");
expect(transformFileSync(cwd + "/fixtures/api/file.js", options).code).toBe(
"foo();",
);
expect(options).toEqual({ babelrc: false });
});
@ -249,15 +224,15 @@ describe("api", function () {
it("options throw on falsy true", function () {
return expect(function () {
transform("", {
plugins: [__dirname + "/../../babel-plugin-syntax-jsx", false],
plugins: [cwd + "/../../babel-plugin-syntax-jsx", false],
});
}).toThrow(/.plugins\[1\] must be a string, object, function/);
});
it("options merge backwards", function () {
return transformAsync("", {
presets: [__dirname + "/../../babel-preset-env"],
plugins: [__dirname + "/../../babel-plugin-syntax-jsx"],
presets: [cwd + "/../../babel-preset-env"],
plugins: [cwd + "/../../babel-plugin-syntax-jsx"],
}).then(function (result) {
expect(result.options.plugins[0].manipulateOptions.toString()).toEqual(
expect.stringContaining("jsx"),
@ -336,18 +311,12 @@ describe("api", function () {
},
// env preset
require(__dirname + "/../../babel-preset-env"),
presetEnv,
// Third preset for Flow.
function () {
return {
plugins: [
require(__dirname + "/../../babel-plugin-syntax-flow"),
require(__dirname +
"/../../babel-plugin-transform-flow-strip-types"),
],
};
},
() => ({
plugins: [pluginSyntaxFlow, pluginFlowStripTypes],
}),
],
});
}
@ -393,9 +362,9 @@ describe("api", function () {
process.env.BABEL_ENV = "development";
const result = transform("", {
cwd: path.join(__dirname, "fixtures", "config", "complex-plugin-config"),
cwd: path.join(cwd, "fixtures", "config", "complex-plugin-config"),
filename: path.join(
__dirname,
cwd,
"fixtures",
"config",
"complex-plugin-config",
@ -792,7 +761,7 @@ describe("api", function () {
it("only syntax plugin available", function (done) {
transformFile(
__dirname + "/fixtures/api/parsing-errors/only-syntax/file.js",
cwd + "/fixtures/api/parsing-errors/only-syntax/file.js",
options,
function (err) {
expect(err.message).toMatch(
@ -809,7 +778,7 @@ describe("api", function () {
it("both syntax and transform plugin available", function (done) {
transformFile(
__dirname + "/fixtures/api/parsing-errors/syntax-and-transform/file.js",
cwd + "/fixtures/api/parsing-errors/syntax-and-transform/file.js",
options,
function (err) {
expect(err.message).toMatch(

View File

@ -1,7 +1,11 @@
import path from "path";
import { fileURLToPath } from "url";
import { loadOptions as loadOptionsOrig, transformSync } from "../lib";
const cwd = path.dirname(fileURLToPath(import.meta.url));
function loadOptions(opts) {
return loadOptionsOrig({ cwd: __dirname, ...opts });
return loadOptionsOrig({ cwd, ...opts });
}
function withAssumptions(assumptions) {

View File

@ -1,4 +1,5 @@
import { join } from "path";
import path from "path";
import { fileURLToPath } from "url";
import * as babel from "..";
import {
@ -14,7 +15,11 @@ const nodeGte8 = (...args) => {
};
describe("asynchronicity", () => {
const base = join(__dirname, "fixtures", "async");
const base = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"fixtures",
"async",
);
let cwd;
beforeEach(function () {

View File

@ -1,9 +1,12 @@
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
@ -42,11 +45,11 @@ const pfs =
});
function fixture(...args) {
return path.join(__dirname, "fixtures", "config", ...args);
return path.join(dirname, "fixtures", "config", ...args);
}
function loadOptions(opts) {
return babel.loadOptions({ cwd: __dirname, ...opts });
return babel.loadOptions({ cwd: dirname, ...opts });
}
function pairs(items) {
@ -1320,7 +1323,7 @@ describe("buildConfigChain", function () {
it("should throw when `preset` requires `filename` but it was not passed", () => {
expect(() => {
loadOptions({
presets: [require("./fixtures/config-loading/preset4")],
presets: ["./fixtures/config-loading/preset4"],
});
}).toThrow(/Preset \/\* your preset \*\/ requires a filename/);
});
@ -1328,7 +1331,7 @@ describe("buildConfigChain", function () {
it("should throw when `preset.overrides` requires `filename` but it was not passed", () => {
expect(() => {
loadOptions({
presets: [require("./fixtures/config-loading/preset5")],
presets: ["./fixtures/config-loading/preset5"],
});
}).toThrow(/Preset \/\* your preset \*\/ requires a filename/);
});

View File

@ -3,12 +3,16 @@ 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(
__dirname,
path.dirname(fileURLToPath(import.meta.url)),
"fixtures",
"config-loading",
"folder",

View File

@ -1,8 +1,14 @@
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));
// "minNodeVersion": "10.0.0" <-- For Ctrl+F when dropping node 10
const nodeSupportsESM = parseInt(process.versions.node) >= 12;
const isWindows = process.platform === "win32";
@ -30,7 +36,7 @@ export function skipUnsupportedESM(esm, name) {
return false;
}
export function loadOptionsAsync({ filename, cwd = __dirname }, mjs) {
export function loadOptionsAsync({ filename, cwd = dirname }, mjs) {
if (mjs) {
// import() crashes with jest
return spawn("load-options-async", filename, cwd);

View File

@ -1,11 +1,11 @@
import { loadOptions as loadOptionsOrig } from "../lib";
import path from "path";
import { fileURLToPath } from "url";
const cwd = path.dirname(fileURLToPath(import.meta.url));
function loadOptions(opts) {
return loadOptionsOrig({
cwd: __dirname,
...opts,
});
return loadOptionsOrig({ cwd, ...opts });
}
describe("option-manager", () => {
@ -220,9 +220,7 @@ describe("option-manager", () => {
it("throws for resolved but erroring preset", () => {
return expect(() => {
loadOptions({
presets: [
path.join(__dirname, "fixtures/option-manager/not-a-preset"),
],
presets: [path.join(cwd, "fixtures/option-manager/not-a-preset")],
});
}).toThrow(
/While processing: .*option-manager(?:\/|\\\\)not-a-preset\.js/,
@ -234,9 +232,7 @@ describe("option-manager", () => {
function presetTest(name) {
it(name, function () {
const options = loadOptions({
presets: [
path.join(__dirname, "fixtures/option-manager/presets", name),
],
presets: [path.join(cwd, "fixtures/option-manager/presets", name)],
});
expect(Array.isArray(options.plugins)).toBe(true);
@ -249,9 +245,7 @@ describe("option-manager", () => {
it(name, function () {
expect(() =>
loadOptions({
presets: [
path.join(__dirname, "fixtures/option-manager/presets", name),
],
presets: [path.join(cwd, "fixtures/option-manager/presets", name)],
}),
).toThrow(msg);
});

View File

@ -1,9 +1,18 @@
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(__dirname, "fixtures", "parse", ...args);
return path.join(
path.dirname(fileURLToPath(import.meta.url)),
"fixtures",
"parse",
...args,
);
}
describe("parse", function () {

View File

@ -1,12 +1,16 @@
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));
describe("traversal path", function () {
it("replaceWithSourceString", function () {
const expectCode = "function foo() {}";
const actualCode = transform(expectCode, {
cwd: __dirname,
cwd,
plugins: [
new Plugin({
visitor: {
@ -25,7 +29,7 @@ describe("traversal path", function () {
const expectCode = "var fn = () => true;";
const actualCode = transform(expectCode, {
cwd: __dirname,
cwd,
plugins: [
new Plugin({
visitor: {
@ -55,7 +59,7 @@ describe("traversal path", function () {
const expectCode = "var fn = () => { return true; }";
const actualCode = transform(expectCode, {
cwd: __dirname,
cwd,
plugins: [
new Plugin({
visitor: {
@ -77,7 +81,7 @@ describe("traversal path", function () {
const expectCode = "for (KEY in right);";
const actualCode = transform(expectCode, {
cwd: __dirname,
cwd,
plugins: [
new Plugin({
visitor: {
@ -108,7 +112,7 @@ describe("traversal path", function () {
const expectCode = "for (var KEY in right);";
const actualCode = transform(expectCode, {
cwd: __dirname,
cwd,
plugins: [
new Plugin({
visitor: {
@ -130,7 +134,7 @@ describe("traversal path", function () {
const expectCode = "for (KEY;;);";
const actualCode = transform(expectCode, {
cwd: __dirname,
cwd,
plugins: [
new Plugin({
visitor: {
@ -161,7 +165,7 @@ describe("traversal path", function () {
const expectCode = "for (var KEY;;);";
const actualCode = transform(expectCode, {
cwd: __dirname,
cwd,
plugins: [
new Plugin({
visitor: {

View File

@ -1,3 +1,8 @@
import runner from "@babel/helper-transform-fixture-test-runner";
import { fileURLToPath } from "url";
import path from "path";
runner(`${__dirname}/fixtures/plugins`, "plugins");
runner(
path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures/plugins"),
"plugins",
);

View File

@ -1,8 +1,13 @@
import * as babel from "../lib/index";
import path from "path";
import { fileURLToPath } from "url";
describe("addon resolution", function () {
const base = path.join(__dirname, "fixtures", "resolution");
const base = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"fixtures",
"resolution",
);
let cwd;
beforeEach(function () {

View File

@ -1,8 +1,11 @@
import { loadOptions as loadOptionsOrig } from "../lib";
import { join } from "path";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
const cwd = dirname(fileURLToPath(import.meta.url));
function loadOptions(opts) {
return loadOptionsOrig({ cwd: __dirname, ...opts });
return loadOptionsOrig({ cwd, ...opts });
}
function withTargets(targets) {
@ -83,7 +86,7 @@ describe("browserslist", () => {
it("loads .browserslistrc by default", () => {
expect(
loadOptions({
cwd: join(__dirname, "fixtures", "targets"),
cwd: join(cwd, "fixtures", "targets"),
}).targets,
).toEqual({ chrome: "80.0.0" });
});
@ -91,7 +94,7 @@ describe("browserslist", () => {
it("loads .browserslistrc relative to the input file", () => {
expect(
loadOptions({
cwd: join(__dirname, "fixtures", "targets"),
cwd: join(cwd, "fixtures", "targets"),
filename: "./nested/test.js",
}).targets,
).toEqual({ edge: "14.0.0" });
@ -101,7 +104,7 @@ describe("browserslist", () => {
it("can disable config loading", () => {
expect(
loadOptions({
cwd: join(__dirname, "fixtures", "targets"),
cwd: join(cwd, "fixtures", "targets"),
browserslistConfigFile: false,
}).targets,
).toEqual({});
@ -110,7 +113,7 @@ describe("browserslist", () => {
it("can specify a custom file", () => {
expect(
loadOptions({
cwd: join(__dirname, "fixtures", "targets"),
cwd: join(cwd, "fixtures", "targets"),
browserslistConfigFile: "./.browserslistrc-firefox",
}).targets,
).toEqual({ firefox: "74.0.0" });
@ -119,7 +122,7 @@ describe("browserslist", () => {
it("is relative to the project root", () => {
expect(
loadOptions({
cwd: join(__dirname, "fixtures", "targets"),
cwd: join(cwd, "fixtures", "targets"),
root: "..",
filename: "./nested/test.js",
browserslistConfigFile: "./targets/.browserslistrc-firefox",
@ -132,7 +135,7 @@ describe("browserslist", () => {
it("is forwarded to browserslist", () => {
expect(
loadOptions({
cwd: join(__dirname, "fixtures", "targets"),
cwd: join(cwd, "fixtures", "targets"),
browserslistEnv: "browserslist-loading-test",
}).targets,
).toEqual({ chrome: "70.0.0" });

View File

@ -1,3 +1,11 @@
import runner from "@babel/helper-transform-fixture-test-runner";
import { fileURLToPath } from "url";
import path from "path";
runner(`${__dirname}/fixtures/transformation`, "transformation");
runner(
path.join(
path.dirname(fileURLToPath(import.meta.url)),
"/fixtures/transformation",
),
"transformation",
);

View File

@ -6,6 +6,7 @@ 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 () {
@ -758,7 +759,9 @@ describe("CodeGenerator", function () {
});
});
const suites = fixtures(`${__dirname}/fixtures`);
const suites = fixtures(
path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures"),
);
suites.forEach(function (testSuite) {
describe("generation/" + testSuite.title, function () {
@ -783,7 +786,10 @@ suites.forEach(function (testSuite) {
...task.options.parserOpts,
});
const options = {
sourceFileName: path.relative(__dirname, actual.loc),
sourceFileName: path.relative(
path.dirname(fileURLToPath(import.meta.url)),
actual.loc,
),
...task.options,
sourceMaps: task.sourceMap ? true : task.options.sourceMaps,
};

View File

@ -1,9 +1,14 @@
import getTargets from "../..";
import { fileURLToPath } from "url";
import path from "path";
it("allows custom browserslist env", () => {
const actual = getTargets(
{},
{ configPath: __dirname, browserslistEnv: "custom" },
{
configPath: path.dirname(fileURLToPath(import.meta.url)),
browserslistEnv: "custom",
},
);
expect(actual).toEqual({ ie: "11.0.0" });

View File

@ -1,5 +1,6 @@
import browserslist from "browserslist";
import { join } from "path";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import getTargets from "..";
describe("getTargets", () => {
@ -250,7 +251,13 @@ describe("getTargets", () => {
{
esmodules: "intersect",
},
{ configPath: join(__dirname, "fixtures", "foo.js") },
{
configPath: join(
dirname(fileURLToPath(import.meta.url)),
"fixtures",
"foo.js",
),
},
),
).toMatchSnapshot();
});

View File

@ -1,6 +1,4 @@
"use strict";
const { targetsSupported } = require("../lib/filter-items");
import { targetsSupported } from "../lib/filter-items";
describe("targetsSupported", () => {
const MAX_VERSION = `${Number.MAX_SAFE_INTEGER}.0.0`;

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -2,6 +2,10 @@ 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";
const require = createRequire(import.meta.url);
const nodeVersion = semver.clean(process.version.slice(1));
@ -279,7 +283,11 @@ function wrapPackagesArray(type, names, optionsDir) {
val[0] = path.resolve(optionsDir, val[0]);
} else {
const monorepoPath = __dirname + "/../../babel-" + type + "-" + val[0];
const monorepoPath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"../..",
`babel-${type}-${val[0]}`,
);
if (fs.existsSync(monorepoPath)) {
val[0] = monorepoPath;

View File

@ -1,7 +1,11 @@
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));
function test(sourceType, opts, initializer, inputCode, expectedCode) {
if (typeof opts === "function") {
expectedCode = inputCode;
@ -15,7 +19,7 @@ function test(sourceType, opts, initializer, inputCode, expectedCode) {
}
const result = babel.transform(inputCode, {
cwd: __dirname,
cwd,
sourceType,
filename: "example" + (sourceType === "module" ? ".mjs" : ".js"),
babelrc: false,

View File

@ -1,7 +1,24 @@
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) {
if (!loc.startsWith("file://")) {
const name = path.basename(path.dirname(loc));
testRunner(loc + "/fixtures", name);
return;
}
}
let fixtures = new URL("./fixtures", loc).pathname;
if (process.platform === "win32") {
// Remove the leading / before the drive letter
// TODO: After dropping Node.js 10 support, use fileURLToPath
fixtures = fixtures.slice(1);
}
const name = path.basename(new URL("..", loc).pathname);
testRunner(fixtures, name);
}

View File

@ -15,6 +15,10 @@ import path from "path";
import vm from "vm";
import QuickLRU from "quick-lru";
import escapeRegExp from "./escape-regexp.cjs";
import { fileURLToPath } from "url";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
import _checkDuplicatedNodes from "babel-check-duplicated-nodes";
const checkDuplicatedNodes = _checkDuplicatedNodes.default;
@ -43,14 +47,17 @@ function createContext() {
// global creation in tests, which could cause things to bleed between tests.
runModuleInTestContext(
"regenerator-runtime",
__filename,
fileURLToPath(import.meta.url),
context,
moduleCache,
);
// Populate the "babelHelpers" global with Babel's helper utilities.
runCacheableScriptInTestContext(
path.join(__dirname, "babel-helpers-in-memory.js"),
path.join(
path.dirname(fileURLToPath(import.meta.url)),
"babel-helpers-in-memory.js",
),
buildExternalHelpers,
context,
moduleCache,
@ -330,7 +337,10 @@ function validateFile(actualCode, expectedLoc, expectedCode) {
}
function normalizeOutput(code, normalizePathSeparator) {
const projectRoot = path.resolve(__dirname, "../../../");
const projectRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../../../",
);
const cwdSymbol = "<CWD>";
let result = code
.trim()

View File

@ -1,4 +1,7 @@
import { runCodeInTestContext } from "..";
import { fileURLToPath } from "url";
const filename = fileURLToPath(import.meta.url);
describe("helper-transform-fixture-test-runner", function () {
it("should not execute code in Node's global context", function () {
@ -10,7 +13,7 @@ describe("helper-transform-fixture-test-runner", function () {
global.foo = "inner";
`,
{
filename: `${__filename}.fake1`,
filename: `${filename}.fake1`,
},
);
@ -20,7 +23,7 @@ describe("helper-transform-fixture-test-runner", function () {
expect(global.foo).toBe("inner");
`,
{
filename: `${__filename}.fake2`,
filename: `${filename}.fake2`,
},
);
} finally {
@ -30,14 +33,14 @@ describe("helper-transform-fixture-test-runner", function () {
delete global.foo;
`,
{
filename: `${__filename}.fake3`,
filename: `${filename}.fake3`,
},
);
}
});
it("should print correct trace position when error is thrown in the first line", () => {
const opts = {
filename: `${__filename}.fake4`,
filename: `${filename}.fake4`,
};
runCodeInTestContext(
`try { throw new Error() } catch (e) {

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -8,6 +8,10 @@ import vm from "vm";
import "core-js/stable";
import "regenerator-runtime/runtime";
import register from "@babel/register";
import { fileURLToPath } from "url";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const program = new commander.Command("babel-node");
@ -194,7 +198,7 @@ if (program.eval || program.print) {
// add back on node and concat the sliced args
process.argv = ["node"].concat(args);
process.execArgv.push(__filename);
process.execArgv.push(fileURLToPath(import.meta.url));
Module.runMain();
} else {

View File

@ -5,8 +5,12 @@
import getV8Flags from "v8flags";
import path from "path";
import child_process from "child_process";
import { fileURLToPath } from "url";
let args = [path.join(__dirname, "_babel-node")];
let args = [
path.join(path.dirname(fileURLToPath(import.meta.url)), "_babel-node"),
];
let babelArgs = process.argv.slice(2);
let userArgs;
@ -41,7 +45,7 @@ const aliases = new Map([
["-gc", "--expose-gc"],
]);
getV8Flags(function (err, v8Flags) {
getV8Flags(async function (err, v8Flags) {
for (let i = 0; i < babelArgs.length; i++) {
const arg = babelArgs[i];
const flag = arg.split("=")[0];
@ -69,17 +73,17 @@ getV8Flags(function (err, v8Flags) {
}
try {
const kexec = require("kexec");
const { default: kexec } = await import("kexec");
kexec(process.argv[0], args);
} catch (err) {
if (
err.code !== "ERR_MODULE_NOT_FOUND" &&
err.code !== "MODULE_NOT_FOUND" &&
err.code !== "UNDECLARED_DEPENDENCY"
) {
throw err;
}
const child_process = require("child_process");
const proc = child_process.spawn(process.argv[0], args, {
stdio: ["inherit", "inherit", "inherit", "ipc"],
});

View File

@ -1,15 +1,20 @@
const includes = require("lodash/includes");
const readdir = require("fs-readdir-recursive");
const helper = require("@babel/helper-fixtures");
const rimraf = require("rimraf");
const { sync: makeDirSync } = require("make-dir");
const child = require("child_process");
const merge = require("lodash/merge");
const path = require("path");
const fs = require("fs");
import includes from "lodash/includes";
import readdir from "fs-readdir-recursive";
import * as helper from "@babel/helper-fixtures";
import rimraf from "rimraf";
import { sync as makeDirSync } from "make-dir";
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 fixtureLoc = path.join(__dirname, "fixtures");
const tmpLoc = path.join(__dirname, "tmp");
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 fileFilter = function (x) {
return x !== ".DS_Store";
@ -91,7 +96,7 @@ const assertTest = function (stdout, stderr, opts) {
};
const buildTest = function (binName, testName, opts) {
const binLoc = path.join(__dirname, "../lib", binName);
const binLoc = path.join(dirname, "../lib", binName);
return function (callback) {
saveInFiles(opts.inFiles);

View File

@ -1,5 +1,9 @@
import path from "path";
import { runThrowTestsWithEstree } from "./helpers/runFixtureTests";
import { parse } from "../lib";
import { fileURLToPath } from "url";
runThrowTestsWithEstree(path.join(__dirname, "fixtures"), parse);
runThrowTestsWithEstree(
path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures"),
parse,
);

View File

@ -1,5 +1,11 @@
import path from "path";
import { runFixtureTests } from "./helpers/runFixtureTests";
import { parseExpression } from "../lib";
import { fileURLToPath } from "url";
runFixtureTests(path.join(__dirname, "expressions"), parseExpression);
const fixtures = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"expressions",
);
runFixtureTests(fixtures, parseExpression);

View File

@ -2,8 +2,12 @@ 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(__dirname, "../../../..");
const rootPath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"../../../..",
);
const serialized = "$$ babel internal serialized type";

View File

@ -1,5 +1,10 @@
import path from "path";
import { runFixtureTests } from "./helpers/runFixtureTests";
import { parse } from "../lib";
import { fileURLToPath } from "url";
runFixtureTests(path.join(__dirname, "fixtures"), parse);
const fixtures = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"fixtures",
);
runFixtureTests(fixtures, parse);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,2 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,2 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,12 +1,16 @@
const babel = require("@babel/core");
import * as babel from "@babel/core";
import { fileURLToPath } from "url";
import path from "path";
import transformCommonJS from "..";
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: __dirname,
cwd: path.dirname(fileURLToPath(import.meta.url)),
ast: true,
plugins: [[require("../"), { loose: true }]],
plugins: [[transformCommonJS, { loose: true }]],
}).ast;
expect(ast.program.body[0].declarations[0].id.type).toBe("Identifier");

View File

@ -1,5 +1,9 @@
const babel = require("@babel/core");
const vm = require("vm");
import * as babel from "@babel/core";
import vm from "vm";
import { fileURLToPath } from "url";
import path from "path";
import transformCommonJS from "..";
test("Re-export doesn't overwrite __esModule flag", function () {
let code = 'export * from "./dep";';
@ -13,14 +17,14 @@ test("Re-export doesn't overwrite __esModule flag", function () {
},
require: function (id) {
if (id === "./dep") return depStub;
return require(id);
throw new Error("Unexpected dependency: " + id);
},
};
context.exports = context.module.exports;
code = babel.transform(code, {
cwd: __dirname,
plugins: [[require("../"), { loose: true }]],
cwd: path.dirname(fileURLToPath(import.meta.url)),
plugins: [[transformCommonJS, { loose: true }]],
ast: false,
}).code;

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

View File

@ -1,3 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);
runner(import.meta.url);

Some files were not shown because too many files have changed in this diff Show More