Repository: babel/babel
File: packages/babel-core/src/config/helpers/config-api.ts (TypeScript)

Code before:
}

export function makePluginAPI<SideChannel extends Context.SimplePlugin>(
  cache: CacheConfigurator<SideChannel>,
  externalDependencies: Array<string>,
): PluginAPI {
  const assumption = (name: string) =>
    cache.using(data => data.assumptions[name]);

  return { ...makePresetAPI(cache, externalDependencies), assumption };
}

function assertVersion(range: string | number): void {
  if (typeof range === "number") {
    if (!Number.isInteger(range)) {
      throw new Error("Expected string or integer value.");
    }
    range = `^${range}.0.0-0`;
  }
  if (typeof range !== "string") {
    throw new Error("Expected string or integer value.");
  }

  if (
    typeof process !== "undefined" &&
    process.env.BABEL_ALLOW_VERSION_MISMATCH
  ) {
    return;
  }

  // We want "*" to also allow any pre-release, but we do not pass
  // the includePrerelease option to semver.satisfies because we
  // do not want ^7.0.0 to match 8.0.0-alpha.1.
  if (range === "*" || semver.satisfies(coreVersion, range)) return;

  const limit = Error.stackTraceLimit;

  if (typeof limit === "number" && limit < 25) {
    // Bump up the limit if needed so that users are more likely
    // to be able to see what is calling Babel.
    Error.stackTraceLimit = 25;
  }

  const err = new Error(
    `Requires Babel "${range}", but was loaded with "${coreVersion}". ` +
      `If you are sure you have a compatible version of @babel/core, ` +
      `it is likely that something in your build process is loading the ` +
      `wrong version. Inspect the stack trace of this error to look for ` +
      `the first entry that doesn't mention "@babel/core" or "babel-core" ` +
      `to see what is calling Babel.`,
  );

Review comment (refactor):
The naming seems a bit neutral. Could we rename it to `BABEL_8_DANGEROUSLY_DISABLE_ASSERT_VERSION`? Such that:
- The naming conveys that it is a workaround and should be removed once plugins migrated to Babel 8
- This temporary flag should be only shipped to Babel 8. It does not skip the assertVersion check in Babel 7
- This flag only affects the behaviour of the `assertVersion` helper. It does not revert any AST differences


Code after:
}

export function makePluginAPI<SideChannel extends Context.SimplePlugin>(
  cache: CacheConfigurator<SideChannel>,
  externalDependencies: Array<string>,
): PluginAPI {
  const assumption = (name: string) =>
    cache.using(data => data.assumptions[name]);

  return { ...makePresetAPI(cache, externalDependencies), assumption };
}

function assertVersion(range: string | number): void {
  if (typeof range === "number") {
    if (!Number.isInteger(range)) {
      throw new Error("Expected string or integer value.");
    }
    range = `^${range}.0.0-0`;
  }
  if (typeof range !== "string") {
    throw new Error("Expected string or integer value.");
  }

  // We want "*" to also allow any pre-release, but we do not pass
  // the includePrerelease option to semver.satisfies because we
  // do not want ^7.0.0 to match 8.0.0-alpha.1.
  if (range === "*" || semver.satisfies(coreVersion, range)) return;

  const message =
    `Requires Babel "${range}", but was loaded with "${coreVersion}". ` +
    `If you are sure you have a compatible version of @babel/core, ` +
    `it is likely that something in your build process is loading the ` +
    `wrong version. Inspect the stack trace of this error to look for ` +
    `the first entry that doesn't mention "@babel/core" or "babel-core" ` +
    `to see what is calling Babel.`;

  if (
    typeof process !== "undefined" &&
    process.env.BABEL_8_BREAKING &&
    process.env.BABEL_7_TO_8_DANGEROUSLY_DISABLE_VERSION_CHECK
  ) {
    console.warn(message);
    return;
  }

  const limit = Error.stackTraceLimit;

  if (typeof limit === "number" && limit < 25) {
    // Bump up the limit if needed so that users are more likely
    // to be able to see what is calling Babel.
    Error.stackTraceLimit = 25;