Skip to content

Commit

Permalink
Use Node.js interop when importing lib in test (#13995)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 29, 2021
1 parent 1bbe1fc commit 29de280
Show file tree
Hide file tree
Showing 51 changed files with 186 additions and 73 deletions.
44 changes: 39 additions & 5 deletions babel.config.js
Expand Up @@ -165,9 +165,6 @@ module.exports = function (api) {
["@babel/proposal-object-rest-spread", { useBuiltIns: true }],

convertESM ? "@babel/proposal-export-namespace-from" : null,
convertESM
? ["@babel/transform-modules-commonjs", { importInterop }]
: null,
convertESM ? pluginImportMetaUrl : null,

pluginPackageJsonMacro,
Expand Down Expand Up @@ -202,7 +199,10 @@ module.exports = function (api) {
].map(normalize),
plugins: [
// Explicitly use the lazy version of CommonJS modules.
["@babel/transform-modules-commonjs", { importInterop, lazy: true }],
[
"@babel/transform-modules-commonjs",
{ importInterop: importInteropSrc, lazy: true },
],
],
},
convertESM && {
Expand All @@ -215,6 +215,24 @@ module.exports = function (api) {
assumptions: sourceAssumptions,
plugins: [transformNamedBabelTypesImportToDestructuring],
},
convertESM && {
test: sources.map(normalize),
plugins: [
[
"@babel/transform-modules-commonjs",
{ importInterop: importInteropSrc },
],
],
},
{
test: sources.map(source => normalize(source.replace("/src", "/test"))),
plugins: [
[
"@babel/transform-modules-commonjs",
{ importInterop: importInteropTest },
],
],
},
{
test: unambiguousSources.map(normalize),
sourceType: "unambiguous",
Expand Down Expand Up @@ -244,7 +262,7 @@ const monorepoPackages = ["codemods", "eslint", "packages"]
.reduce((a, b) => a.concat(b))
.map(name => name.replace(/^babel-/, "@babel/"));

function importInterop(source) {
function importInteropSrc(source) {
if (
// These internal files are "real CJS" (whose default export is
// on module.exports) and not compiled ESM.
Expand All @@ -266,6 +284,22 @@ function importInterop(source) {
return "node";
}

function importInteropTest(source) {
// This file will soon have an esm entrypoint
if (source === "@babel/helper-plugin-test-runner") {
return "none";
}
if (
// non-test files
!source.startsWith(".") ||
// lib files
/(?:\.\.\/)+(?:babel-[a-z-]+\/)?lib/.test(source)
) {
return "node";
}
return "babel";
}

// env vars from the cli are always strings, so !!ENV_VAR returns true for "false"
function bool(value) {
return value && value !== "false" && value !== "0";
Expand Down
Expand Up @@ -12,7 +12,7 @@ const MODULE_PARENT_DIR = path.resolve(dirname, "test/errorsModule.js");

const ruleTester = new RuleTester();

ruleTester.run("dry-error-messages", rule, {
ruleTester.run("dry-error-messages", rule.default, {
valid: [
// Ignores malformed `this.raise` invocations.
{
Expand Down
Expand Up @@ -3,7 +3,7 @@ import rule from "../../lib/rules/report-error-message-format.js";

const ruleTester = new RuleTester();

ruleTester.run("report-error-message-format", rule, {
ruleTester.run("report-error-message-format", rule.default, {
valid: [
"makeErrorTemplates({});",
'makeErrorTemplates({ ThisIsAnError: "This is an error." });',
Expand Down
Expand Up @@ -9,7 +9,7 @@ const ruleTester = new RuleTester({
parserOptions: { sourceType: "module" },
});

ruleTester.run("no-deprecated-clone", rule, {
ruleTester.run("no-deprecated-clone", rule.default, {
valid: [
`_.clone(obj)`,
`_.cloneDeep(obj)`,
Expand Down
Expand Up @@ -8,7 +8,7 @@ const ruleTester = new RuleTester({
parserOptions: { sourceType: "module" },
});

ruleTester.run("no-undefined-identifier", rule, {
ruleTester.run("no-undefined-identifier", rule.default, {
valid: [
`_.identifier("undefined")`,
`_.Identifier("undefined")`,
Expand Down
Expand Up @@ -6,7 +6,7 @@ const missingNameError = "This Babel plugin doesn't have a 'name' property.";

const ruleTester = new RuleTester();

ruleTester.run("plugin-name", rule, {
ruleTester.run("plugin-name", rule.default, {
valid: [
`export default function () { return { name: "test-plugin" } }`,
`import { declare } from "@babel/helper-plugin-utils"; declare(() => { return { name: "test-plugin" } })`,
Expand Down
2 changes: 1 addition & 1 deletion eslint/babel-eslint-plugin/test/rules/new-cap.js
Expand Up @@ -2,7 +2,7 @@ import rule from "../../lib/rules/new-cap.js";
import RuleTester from "../../../babel-eslint-shared-fixtures/utils/RuleTester.js";

const ruleTester = new RuleTester();
ruleTester.run("@babel/new-cap", rule, {
ruleTester.run("@babel/new-cap", rule.default, {
valid: [
{
code: "@MyDecorator(123) class MyClass{}",
Expand Down
2 changes: 1 addition & 1 deletion eslint/babel-eslint-plugin/test/rules/no-invalid-this.js
Expand Up @@ -165,7 +165,7 @@ const patterns = [
];

const ruleTester = new RuleTester();
ruleTester.run("@babel/no-invalid-this", rule, {
ruleTester.run("@babel/no-invalid-this", rule.default, {
valid: extractPatterns(patterns, "valid"),
invalid: extractPatterns(patterns, "invalid"),
});
Expand Up @@ -2,7 +2,7 @@ import rule from "../../lib/rules/no-unused-expressions.js";
import RuleTester from "../../../babel-eslint-shared-fixtures/utils/RuleTester.js";

const ruleTester = new RuleTester();
ruleTester.run("@babel/no-unused-expressions", rule, {
ruleTester.run("@babel/no-unused-expressions", rule.default, {
valid: [
"let a = do { if (foo) { foo.bar; } }",
"let a = do { foo; }",
Expand Down
Expand Up @@ -2,7 +2,7 @@ import rule from "../../lib/rules/object-curly-spacing.js";
import RuleTester from "../../../babel-eslint-shared-fixtures/utils/RuleTester.js";

const ruleTester = new RuleTester();
ruleTester.run("@babel/object-curly-spacing", rule, {
ruleTester.run("@babel/object-curly-spacing", rule.default, {
valid: ['export x from "mod";'],
invalid: [],
});
2 changes: 1 addition & 1 deletion eslint/babel-eslint-plugin/test/rules/semi.js
Expand Up @@ -3,7 +3,7 @@ import RuleTester from "../../../babel-eslint-shared-fixtures/utils/RuleTester.j

const ruleTester = new RuleTester();

ruleTester.run("semi", rule, {
ruleTester.run("semi", rule.default, {
valid: [
"class Foo { bar = 'example'; }",
"class Foo { #bar = 'example'; }",
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-code-frame/test/index.js
@@ -1,6 +1,8 @@
import chalk from "chalk";
import stripAnsi from "strip-ansi";
import codeFrame, { codeFrameColumns } from "../lib/index.js";

import _codeFrame, { codeFrameColumns } from "../lib/index.js";
const codeFrame = _codeFrame.default;

describe("@babel/code-frame", function () {
test("basic usage", function () {
Expand Down
6 changes: 4 additions & 2 deletions packages/babel-core/test/api.js
@@ -1,10 +1,12 @@
import * as babel from "../lib/index.js";
import babel from "../lib/index.js";
import sourceMap from "source-map";
import path from "path";
import Plugin from "../lib/config/plugin.js";
import generator from "@babel/generator";
import { fileURLToPath } from "url";

import _Plugin from "../lib/config/plugin.js";
const Plugin = _Plugin.default;

import presetEnv from "../../babel-preset-env/lib/index.js";
import pluginSyntaxFlow from "../../babel-plugin-syntax-flow/lib/index.js";
import pluginFlowStripTypes from "../../babel-plugin-transform-flow-strip-types/lib/index.js";
Expand Down
6 changes: 4 additions & 2 deletions packages/babel-core/test/config-chain.js
Expand Up @@ -2,8 +2,10 @@ import fs from "fs";
import os from "os";
import path from "path";
import { fileURLToPath } from "url";
import * as babel from "../lib/index.js";
import getTargets from "@babel/helper-compilation-targets";
import babel from "../lib/index.js";

import _getTargets from "@babel/helper-compilation-targets";
const getTargets = _getTargets.default;

const dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down
4 changes: 2 additions & 2 deletions packages/babel-core/test/config-loading.js
@@ -1,4 +1,4 @@
import loadConfigRunner, {
import _loadConfigRunner, {
loadPartialConfig,
createConfigItem,
} from "../lib/config/index.js";
Expand All @@ -8,7 +8,7 @@ import { createRequire } from "module";

const require = createRequire(import.meta.url);

const loadConfig = loadConfigRunner.sync;
const loadConfig = _loadConfigRunner.default.sync;

describe("@babel/core config loading", () => {
const FILEPATH = path.join(
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-core/test/evaluation.js
@@ -1,6 +1,8 @@
import traverse from "@babel/traverse";
import { parse } from "@babel/parser";

import _traverse from "@babel/traverse";
const traverse = _traverse.default;

describe("evaluation", function () {
function addTest(code, type, value, notConfident) {
it(type + ": " + code, function () {
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-core/test/path.js
@@ -1,8 +1,10 @@
import { transform } from "../lib/index.js";
import Plugin from "../lib/config/plugin.js";
import { fileURLToPath } from "url";
import path from "path";

import _Plugin from "../lib/config/plugin.js";
const Plugin = _Plugin.default;

const cwd = path.dirname(fileURLToPath(import.meta.url));

describe("traversal path", function () {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/test/plugins.js
Expand Up @@ -2,7 +2,7 @@ import runner from "@babel/helper-transform-fixture-test-runner";
import { fileURLToPath } from "url";
import path from "path";

runner(
runner.default(
path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures/plugins"),
"plugins",
);
2 changes: 1 addition & 1 deletion packages/babel-core/test/transformation.js
Expand Up @@ -2,7 +2,7 @@ import runner from "@babel/helper-transform-fixture-test-runner";
import { fileURLToPath } from "url";
import path from "path";

runner(
runner.default(
path.join(
path.dirname(fileURLToPath(import.meta.url)),
"/fixtures/transformation",
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-generator/test/arrow-functions.js
@@ -1,6 +1,8 @@
import generate from "../lib/index.js";
import { parse } from "@babel/parser";

import _generate from "../lib/index.js";
const generate = _generate.default;

describe("parameter parentheses", () => {
// Common source text for several snapshot tests
const source = `
Expand Down
9 changes: 6 additions & 3 deletions packages/babel-generator/test/index.js
@@ -1,5 +1,3 @@
import Printer from "../lib/printer.js";
import generate, { CodeGenerator } from "../lib/index.js";
import { parse } from "@babel/parser";
import * as t from "@babel/types";
import fs from "fs";
Expand All @@ -8,6 +6,11 @@ import fixtures from "@babel/helper-fixtures";
import sourcemap from "source-map";
import { fileURLToPath } from "url";

import _Printer from "../lib/printer.js";
import _generate, { CodeGenerator } from "../lib/index.js";
const Printer = _Printer.default;
const generate = _generate.default;

describe("generation", function () {
it("completeness", function () {
Object.keys(t.VISITOR_KEYS).forEach(function (type) {
Expand Down Expand Up @@ -795,7 +798,7 @@ describe("CodeGenerator", function () {
});
});

const suites = fixtures(
const suites = fixtures.default(
path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures"),
);

Expand Down
3 changes: 2 additions & 1 deletion packages/babel-helper-annotate-as-pure/test/index.js
@@ -1,4 +1,5 @@
import annotateAsPure from "../lib/index.js";
import _annotateAsPure from "../lib/index.js";
const annotateAsPure = _annotateAsPure.default;

describe("@babel/helper-annotate-as-pure", () => {
it("will add leading comment", () => {
Expand Down
@@ -1,6 +1,8 @@
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";
import getTargets from "../../lib/index.js";

import _getTargets from "../../lib/index.js";
const getTargets = _getTargets.default;

const currentDir = dirname(fileURLToPath(import.meta.url));

Expand Down
@@ -1,7 +1,9 @@
import getTargets from "../../lib/index.js";
import { fileURLToPath } from "url";
import path from "path";

import _getTargets from "../../lib/index.js";
const getTargets = _getTargets.default;

it("allows custom browserslist env", () => {
const actual = getTargets(
{},
Expand Down
@@ -1,7 +1,9 @@
import getTargets from "../../lib/index.js";
import { fileURLToPath } from "url";
import path from "path";

import _getTargets from "../../lib/index.js";
const getTargets = _getTargets.default;

const oldCwd = process.cwd();

beforeAll(() => {
Expand Down
@@ -1,7 +1,9 @@
import getTargets from "../../lib/index.js";
import { fileURLToPath } from "url";
import path from "path";

import _getTargets from "../../lib/index.js";
const getTargets = _getTargets.default;

const oldCwd = process.cwd();

beforeAll(() => {
Expand Down
@@ -1,7 +1,9 @@
import browserslist from "browserslist";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import getTargets from "../lib/index.js";

import _getTargets from "../lib/index.js";
const getTargets = _getTargets.default;

describe("getTargets", () => {
it("parses", () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/babel-helper-optimise-call-expression/test/index.js
@@ -1,7 +1,10 @@
import { parse } from "@babel/parser";
import generator from "@babel/generator";
import * as t from "@babel/types";
import optimizeCallExpression from "../lib/index.js";

import _generator from "@babel/generator";
import _optimizeCallExpression from "../lib/index.js";
const generator = _generator.default;
const optimizeCallExpression = _optimizeCallExpression.default;

function transformInput(input, thisIdentifier) {
const ast = parse(input);
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-highlight/test/index.js
@@ -1,6 +1,8 @@
import chalk from "chalk";
import stripAnsi from "strip-ansi";
import highlight, { shouldHighlight, getChalk } from "../lib/index.js";

import _highlight, { shouldHighlight, getChalk } from "../lib/index.js";
const highlight = _highlight.default;

describe("@babel/highlight", function () {
function stubColorSupport(supported) {
Expand Down
@@ -1,5 +1,8 @@
import { parse } from "@babel/parser";
import shouldStoreRHSInTemporaryVariable from "../lib/shouldStoreRHSInTemporaryVariable.js";

import _shouldStoreRHSInTemporaryVariable from "../lib/shouldStoreRHSInTemporaryVariable.js";
const shouldStoreRHSInTemporaryVariable =
_shouldStoreRHSInTemporaryVariable.default;

function getFistObjectPattern(program) {
return parse(program, { sourceType: "module" }).program.body[0]
Expand Down
@@ -1,4 +1,5 @@
import getOptionSpecificExcludesFor from "../lib/get-option-specific-excludes.js";
import _getOptionSpecificExcludesFor from "../lib/get-option-specific-excludes.js";
const getOptionSpecificExcludesFor = _getOptionSpecificExcludesFor.default;

describe("defaults", () => {
describe("getOptionSpecificExcludesFor", () => {
Expand Down

0 comments on commit 29de280

Please sign in to comment.