ronantakizawa/codereview-bench huggingface dataset:
Repository: babel/babel
File: packages/babel-cli/src/babel/dir.js (JavaScript)

Code before:
import slash from "slash";
import path from "path";
import fs from "fs";

import * as util from "./util";
import { type CmdOptions } from "./options";

const FILE_TYPE = Object.freeze({
  NON_COMPACTIBLE: "NON_COMPACTIBLE",
  COMPILED: "COMPILED",
  IGNORED: "IGNORED",
  ERR_COMPILATION: "ERR_COMPILATION",
});

function outputFileSync(filePath: string, data: string | Buffer): void {
  makeDirSync(path.dirname(filePath));
  fs.writeFileSync(filePath, data);
}

export default async function({
  cliOptions,
  babelOptions,
}: CmdOptions): Promise<void> {
  const filenames = cliOptions.filenames;

  async function write(src: string, base: string): Promise<string> {
    let relative = path.relative(base, src);

    if (!util.isCompilableExtension(relative, cliOptions.extensions)) {
      return FILE_TYPE.NON_COMPACTIBLE;
    }

    // remove extension and then append back on .js
    relative = util.adjustRelative(relative, cliOptions.keepFileExtension);

    const dest = getDest(relative, base);

    try {
      const res = await util.compile(
        src,
        defaults(
          {
            sourceFileName: slash(path.relative(dest + "/..", src)),
          },
          babelOptions,
        ),
      );

      if (!res) return FILE_TYPE.IGNORED;

      // we've requested explicit sourcemaps to be written to disk

Review comment (suggestion):
```suggestion
  async function write(src: string, base: string): Promise<$Keys<typeof FILE_TYPE>> {
```

Code after:
import slash from "slash";
import path from "path";
import fs from "fs";

import * as util from "./util";
import { type CmdOptions } from "./options";

const FILE_TYPE = Object.freeze({
  NON_COMPILABLE: "NON_COMPILABLE",
  COMPILED: "COMPILED",
  IGNORED: "IGNORED",
  ERR_COMPILATION: "ERR_COMPILATION",
});

function outputFileSync(filePath: string, data: string | Buffer): void {
  makeDirSync(path.dirname(filePath));
  fs.writeFileSync(filePath, data);
}

export default async function({
  cliOptions,
  babelOptions,
}: CmdOptions): Promise<void> {
  const filenames = cliOptions.filenames;

  async function write(
    src: string,
    base: string,
  ): Promise<$Keys<typeof FILE_TYPE>> {
    let relative = path.relative(base, src);

    if (!util.isCompilableExtension(relative, cliOptions.extensions)) {
      return FILE_TYPE.NON_COMPILABLE;
    }

    // remove extension and then append back on .js
    relative = util.adjustRelative(relative, cliOptions.keepFileExtension);

    const dest = getDest(relative, base);

    try {
      const res = await util.compile(
        src,
        defaults(
          {
            sourceFileName: slash(path.relative(dest + "/..", src)),
          },
          babelOptions,
        ),
      );
Repository: babel/babel
File: packages/babel-preset-react/src/index.js (JavaScript)

Code before:
import transformReactJSXSource from "@babel/plugin-transform-react-jsx-source";
import transformReactJSXSelf from "@babel/plugin-transform-react-jsx-self";
import transformReactPure from "@babel/plugin-transform-react-pure-annotations";

export default declare((api, opts) => {
  api.assertVersion(7);

  const {
    pure,
    pragma,
    pragmaFrag,
    throwIfNamespace = true,
    useSpread,
    runtime = "automatic",
    importSource,
  } = opts;

  const { development = false, useBuiltIns = false } = opts;

  if (typeof development !== "boolean") {
    throw new Error(
      "@babel/preset-react 'development' option must be a boolean.",
    );
  }

  if (typeof useBuiltIns !== "boolean") {
    throw new Error(
      "@babel/preset-react 'useBuiltIns' option must be a boolean.",
    );
  }

  const transformReactJSXPlugin =
    runtime === "automatic" && development
      ? transformReactJSXDevelopment
      : transformReactJSX;

  return {
    plugins: [
      [
        transformReactJSXPlugin,
        {
          importSource,
          pragma,
          pragmaFrag,
          runtime,
          throwIfNamespace,
          useBuiltIns,
          useSpread,
          pure,
        },
      ],

Review comment (suggestion):
`useBuiltIns` will be removed in Babel 8, I suggest we leave it as-is and focus on default value of `"runtime"` only.

Code after:
import transformReactJSXSource from "@babel/plugin-transform-react-jsx-source";
import transformReactJSXSelf from "@babel/plugin-transform-react-jsx-self";
import transformReactPure from "@babel/plugin-transform-react-pure-annotations";

export default declare((api, opts) => {
  api.assertVersion(7);

  const {
    pure,
    pragma,
    pragmaFrag,
    throwIfNamespace = true,
    runtime = "automatic",
    importSource,
  } = opts;

  // TODO: (Babel 8) Don't cast these options but validate it
  const development = !!opts.development;

  if ("useSpread" in opts) {
    throw new Error(
      '@babel/preset-react: Since Babel 8, an inline object with spread elements is always used, and the "useSpread" option is no longer available. Please remove it from your config.',
    );
  }

  if ("useBuiltIns" in opts) {
    const useBuiltInsFormatted = JSON.stringify(opts.useBuiltIns);
    throw new Error(
      `@babel/preset-react: Since "useBuiltIns" is removed in Babel 8, you can remove it from the config.
- Babel 8 now transforms JSX spread to object spread. If you need to transpile object spread with
\`useBuiltIns: ${useBuiltInsFormatted}\`, you can use the following config
{
  "plugins": [
    ["@babel/plugin-proposal-object-rest-spread", { "loose": true, "useBuiltIns": ${useBuiltInsFormatted} }]
  ],
  "presets": ["@babel/preset-react"]
}`,
    );
  }

  if (typeof development !== "boolean") {
    throw new Error(
      "@babel/preset-react 'development' option must be a boolean.",
    );
  }

  const transformReactJSXPlugin =
    runtime === "automatic" && development
      ? transformReactJSXDevelopment
      : transformReactJSX;
Repository: babel/babel
File: packages/babel-preset-env/src/filter-items.js (JavaScript)

Code before:
// @flow

import { lt } from "semver";
import { minVersions } from "./available-plugins";

const has = Function.call.bind(Object.hasOwnProperty);

export function removeUnnecessaryItems(
  items: Set<string>,
  overlapping: { [name: string]: string[] },
) {
  items.forEach(item => {
    overlapping[item]?.forEach(name => items.delete(name));
  });
}
export function removeUnsupportedItems(
  items: Set<string>,
  babelVersion: string,
) {
  items.forEach(item => {
    if (has(minVersions, item) && lt(babelVersion, minVersions[item])) {
      items.delete(item);
    }
  });
}


Review comment (suggestion):
```suggestion
// $FlowIgnore
const has = Function.call.bind(Object.hasOwnProperty);
```

Code after:
// @flow

import { lt } from "semver";
import { minVersions } from "./available-plugins";

// $FlowIgnore
const has = Function.call.bind(Object.hasOwnProperty);

export function removeUnnecessaryItems(
  items: Set<string>,
  overlapping: { [name: string]: string[] },
) {
  items.forEach(item => {
    overlapping[item]?.forEach(name => items.delete(name));
  });
}
export function removeUnsupportedItems(
  items: Set<string>,
  babelVersion: string,
) {
  items.forEach(item => {
    if (has(minVersions, item) && lt(babelVersion, minVersions[item])) {
      items.delete(item);
    }
  });
}
Repository: babel/babel
File: packages/babel-helper-environment-visitor/src/index.ts (TypeScript)

Code before:
import type { NodePath, Visitor } from "@babel/traverse";
import type * as t from "@babel/types";

if (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {
  // eslint-disable-next-line no-restricted-globals
  exports.skipAllButComputedKey = function skipAllButComputedKey(
    path: NodePath<t.Method | t.ClassProperty>,
  ) {
    path.skip();
    if (path.node.computed) {
      // requeue the computed key
      path.context.maybeQueue(path.get("key"));
    }
  };
}

export function requeueComputedKeyAndDecorators(
  path: NodePath<t.Method | t.Property>,
) {
  const { context, node } = path;
  // @ts-expect-error ClassPrivateProperty does not have computed
  if (node.computed) {
    // requeue the computed key
    context.maybeQueue(path.get("key"));
  }
  if (node.decorators) {

Review comment (suggestion):
There are 4 packages whose types circularly depend on `@babel/traverse`.  Lets just ignore those errors for now (`@babel/traverse` will be in the dependencies anyway), but I will think of a better solution before going stable.

Code after:
// eslint-disable-next-line import/no-extraneous-dependencies -- TODO: Avoid cycle
import type { NodePath, Visitor } from "@babel/traverse";
import type * as t from "@babel/types";

if (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {
  // eslint-disable-next-line no-restricted-globals
  exports.skipAllButComputedKey = function skipAllButComputedKey(
    path: NodePath<t.Method | t.ClassProperty>,
  ) {
    path.skip();
    if (path.node.computed) {
      // requeue the computed key
      path.context.maybeQueue(path.get("key"));
    }
  };
}

export function requeueComputedKeyAndDecorators(
  path: NodePath<t.Method | t.Property>,
) {
  const { context, node } = path;
  // @ts-expect-error ClassPrivateProperty does not have computed
  if (node.computed) {
    // requeue the computed key
    context.maybeQueue(path.get("key"));
  }
Repository: babel/babel
File: packages/babel-helpers/src/helpers/defaults.ts (TypeScript)

Code before:
/* @minVersion 7.0.0-beta.0 */
export default function _defaults<T extends object, S extends object>(
  obj: T,
  defaults: S,
): NonNullable<T & S> {
  for (
    var keys: string[] = Object.getOwnPropertyNames(defaults), i = 0;
    i < keys.length;
    i++
  ) {
    var key: string = keys[i],
      value: PropertyDescriptor | undefined = Object.getOwnPropertyDescriptor(
        defaults,
        key,
      );
    if (value && value.configurable && obj[key as keyof T] === undefined) {
      Object.defineProperty(obj, key, value);
    }
  }
  return obj as NonNullable<T & S>;
}


Review comment (suggestion):
If I hoist the `keys` to the scope of the `_defaults`, the generated size will be increased by 3 ~ 5 bytes.

Code after:
/* @minVersion 7.0.0-beta.0 */

export default function _defaults<T extends object, S extends object>(
  obj: T,
  defaults: S,
): NonNullable<T & S> {
  for (
    var keys: string[] = Object.getOwnPropertyNames(defaults), i = 0;
    i < keys.length;
    i++
  ) {
    var key: string = keys[i],
      value: PropertyDescriptor | undefined = Object.getOwnPropertyDescriptor(
        defaults,
        key,
      );
    if (value && value.configurable && obj[key as keyof T] === undefined) {
      Object.defineProperty(obj, key, value);
    }
  }
  return obj as NonNullable<T & S>;
}
Repository: babel/babel
File: packages/babel-helpers/src/helpers/extends.ts (TypeScript)

Code before:
/* @minVersion 7.0.0-beta.0 */

export default function _extends<T extends {}, U>(
  this: any,
  target: T,
  source: U,
): T & U;
export default function _extends<T extends {}, U, V>(
  this: any,
  target: T,
  source1: U,
  source2: V,
): T & U & V;
export default function _extends<T extends {}, U, V, W>(
  this: any,
  target: T,
  source1: U,
  source2: V,
  source3: W,
): T & U & V & W;
export default function _extends(
  this: any,
  target: object,
  ...sources: any[]
): any;
export default function _extends(this: any) {
  // @ts-expect-error explicitly assign to function
  _extends = Object.assign
    ? // need a bind because https://github.com/babel/babel/issues/14527
      // @ts-expect-error -- intentionally omitting the argument
      Object.assign.bind(/* undefined */)
    : function (this: any, target: any) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }
        return target;

Review comment (suggestion):
If `undefined` is explicitly given, the generated size will increase by about 6 bytes. So I omit the `undefined` and add a `ts-expect-error`.

Code after:
/* @minVersion 7.0.0-beta.0 */

type Intersection<R extends any[]> = R extends [infer H, ...infer S]
  ? H & Intersection<S>
  : unknown;

export default function _extends<T extends object, U extends unknown[]>(
  target: T,
  ...sources: U
): T & Intersection<U>;
export default function _extends() {
  // @ts-expect-error explicitly assign to function
  _extends = Object.assign
    ? // need a bind because https://github.com/babel/babel/issues/14527
      // @ts-expect-error -- intentionally omitting the argument
      Object.assign.bind(/* undefined */)
    : function (target: any) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }
        return target;
      };

  return _extends.apply(
    null,
    arguments as any as [source: object, ...target: any[]],
  );
}
Repository: babel/babel
File: packages/babel-helpers/src/helpers/objectSpread.ts (TypeScript)

Code before:
/* @minVersion 7.0.0-beta.0 */
/* @onlyBabel7 */

// @ts-expect-error Migrate in another PR
import defineProperty from "defineProperty.ts";

export default function _objectSpread<T extends {}, U>(
  target: T,
  source: U,
): T & U;
export default function _objectSpread<T extends {}, U, V>(
  target: T,
  source1: U,
  source2: V,
): T & U & V;
export default function _objectSpread<T extends {}, U, V, W>(
  target: T,
  source1: U,
  source2: V,
  source3: W,
): T & U & V & W;
export default function _objectSpread(target: object, ...sources: any[]): any;
export default function _objectSpread(target: object) {
  for (var i = 1; i < arguments.length; i++) {
    var source: object = arguments[i] != null ? Object(arguments[i]) : {};
    var ownKeys: PropertyKey[] = Object.keys(source);
    if (typeof Object.getOwnPropertySymbols === "function") {
      ownKeys.push.apply(
        ownKeys,
        Object.getOwnPropertySymbols(source).filter(function (sym) {

Review comment (suggestion):
```suggestion
import defineProperty from "./defineProperty.ts";
```

Code after:
/* @minVersion 7.0.0-beta.0 */
/* @onlyBabel7 */

// @ts-expect-error Migrate in another PR
import defineProperty from "./defineProperty.ts";

type Intersection<R extends any[]> = R extends [infer H, ...infer S]
  ? H & Intersection<S>
  : unknown;

export default function _objectSpread<T extends object, U extends unknown[]>(
  target: T,
  ...sources: U
): T & Intersection<U>;
export default function _objectSpread(target: object) {
  for (var i = 1; i < arguments.length; i++) {
    var source: object = arguments[i] != null ? Object(arguments[i]) : {};
    var ownKeys: PropertyKey[] = Object.keys(source);
    if (typeof Object.getOwnPropertySymbols === "function") {
      ownKeys.push.apply(
        ownKeys,
        Object.getOwnPropertySymbols(source).filter(function (sym) {
          // sym already comes from `Object.getOwnPropertySymbols`, so getOwnPropertyDescriptor should always be defined
          return Object.getOwnPropertyDescriptor(source, sym)!.enumerable;
        }),
      );
    }
    ownKeys.forEach(function (key) {
      defineProperty(target, key, source[key as keyof typeof source]);
    });
Repository: babel/babel
File: packages/babel-helpers/src/helpers/objectSpread2.ts (TypeScript)

Code before:
/* @minVersion 7.5.0 */

// @ts-expect-error Migrate in another PR
import defineProperty from "defineProperty";

// This function is different to "Reflect.ownKeys". The enumerableOnly
// filters on symbol properties only. Returned string properties are always
// enumerable. It is good to use in objectSpread.

function ownKeys(
  object: object,
  enumerableOnly?: boolean | undefined,
): PropertyKey[] {
  var keys: PropertyKey[] = Object.keys(object);
  if (Object.getOwnPropertySymbols) {
    var symbols = Object.getOwnPropertySymbols(object);
    if (enumerableOnly) {
      symbols = symbols.filter(function (sym) {
        // sym already comes from `Object.getOwnPropertySymbols`, so getOwnPropertyDescriptor should always be defined
        return Object.getOwnPropertyDescriptor(object, sym)!.enumerable;
      });
    }
    keys.push.apply(keys, symbols);
  }
  return keys;
}

export default function _objectSpread2<T extends {}, U>(
  target: T,

Review comment (suggestion):
```suggestion
import defineProperty from "./defineProperty.ts";
```

(this need to match the final path, so that TS will stop complaining once `defineProperty` is migrated and it will tell us to remove the `@ts-expect-error`)

Code after:
/* @minVersion 7.5.0 */

// @ts-expect-error Migrate in another PR
import defineProperty from "./defineProperty.ts";

// This function is different to "Reflect.ownKeys". The enumerableOnly
// filters on symbol properties only. Returned string properties are always
// enumerable. It is good to use in objectSpread.

function ownKeys(
  object: object,
  enumerableOnly?: boolean | undefined,
): (string | symbol)[] {
  var keys: (string | symbol)[] = Object.keys(object);
  if (Object.getOwnPropertySymbols) {
    var symbols = Object.getOwnPropertySymbols(object);
    if (enumerableOnly) {
      symbols = symbols.filter(function (sym) {
        // sym already comes from `Object.getOwnPropertySymbols`, so getOwnPropertyDescriptor should always be defined
        return Object.getOwnPropertyDescriptor(object, sym)!.enumerable;
      });
    }
    keys.push.apply(keys, symbols);
  }
  return keys;
}

type Intersection<R extends any[]> = R extends [infer H, ...infer S]
  ? H & Intersection<S>
Repository: babel/babel
File: packages/babel-helpers/src/helpers/defaults.ts (TypeScript)

Code before:
/* @minVersion 7.0.0-beta.0 */
export default function _defaults<T extends object, S extends object>(
  obj: T,
  defaults: S,
): NonNullable<T & S> {
  for (
    var keys: string[] = Object.getOwnPropertyNames(defaults), i = 0;
    i < keys.length;
    i++
  ) {
    var key: string = keys[i],
      value: PropertyDescriptor | undefined = Object.getOwnPropertyDescriptor(
        defaults,
        key,
      );
    if (value && value.configurable && obj[key as keyof T] === undefined) {
      Object.defineProperty(obj, key, value);
    }
  }
  return obj as NonNullable<T & S>;
}


Review comment (suggestion):
```suggestion

export default function _defaults<T extends object, S extends object>(
```

Code after:
/* @minVersion 7.0.0-beta.0 */

export default function _defaults<T extends object, S extends object>(
  obj: T,
  defaults: S,
): NonNullable<T & S> {
  for (
    var keys: string[] = Object.getOwnPropertyNames(defaults), i = 0;
    i < keys.length;
    i++
  ) {
    var key: string = keys[i],
      value: PropertyDescriptor | undefined = Object.getOwnPropertyDescriptor(
        defaults,
        key,
      );
    if (value && value.configurable && obj[key as keyof T] === undefined) {
      Object.defineProperty(obj, key, value);
    }
  }
  return obj as NonNullable<T & S>;
}
Repository: babel/babel
File: packages/babel-helpers/src/helpers/objectSpread2.ts (TypeScript)

Code before:
/* @minVersion 7.5.0 */

// @ts-expect-error Migrate in another PR
import defineProperty from "./defineProperty.ts";

// This function is different to "Reflect.ownKeys". The enumerableOnly
// filters on symbol properties only. Returned string properties are always
// enumerable. It is good to use in objectSpread.

function ownKeys(
  object: object,
  enumerableOnly?: boolean | undefined,
): PropertyKey[] {
  var keys: PropertyKey[] = Object.keys(object);
  if (Object.getOwnPropertySymbols) {
    var symbols = Object.getOwnPropertySymbols(object);
    if (enumerableOnly) {
      symbols = symbols.filter(function (sym) {
        // sym already comes from `Object.getOwnPropertySymbols`, so getOwnPropertyDescriptor should always be defined
        return Object.getOwnPropertyDescriptor(object, sym)!.enumerable;
      });
    }
    keys.push.apply(keys, symbols);
  }
  return keys;
}

export default function _objectSpread2<T extends {}, U>(
  target: T,
  source: U,
): T & U;
export default function _objectSpread2<T extends {}, U, V>(
  target: T,
  source1: U,
  source2: V,
): T & U & V;
export default function _objectSpread2<T extends {}, U, V, W>(
  target: T,
  source1: U,

Review comment (suggestion):
This can be narrowed down to `(string | symbol)[]`. The `PropertyKey` is supposed to type the valid non-computed key syntax in the object literal, which also contains `number`, however, `Object.keys()` will always return `string`.

Code after:
/* @minVersion 7.5.0 */

// @ts-expect-error Migrate in another PR
import defineProperty from "./defineProperty.ts";

// This function is different to "Reflect.ownKeys". The enumerableOnly
// filters on symbol properties only. Returned string properties are always
// enumerable. It is good to use in objectSpread.

function ownKeys(
  object: object,
  enumerableOnly?: boolean | undefined,
): (string | symbol)[] {
  var keys: (string | symbol)[] = Object.keys(object);
  if (Object.getOwnPropertySymbols) {
    var symbols = Object.getOwnPropertySymbols(object);
    if (enumerableOnly) {
      symbols = symbols.filter(function (sym) {
        // sym already comes from `Object.getOwnPropertySymbols`, so getOwnPropertyDescriptor should always be defined
        return Object.getOwnPropertyDescriptor(object, sym)!.enumerable;
      });
    }
    keys.push.apply(keys, symbols);
  }
  return keys;
}

type Intersection<R extends any[]> = R extends [infer H, ...infer S]
  ? H & Intersection<S>
  : unknown;

export default function _objectSpread2<T extends object, U extends unknown[]>(
  target: T,
  ...sources: U
): T & Intersection<U>;
export default function _objectSpread2(target: object) {
  for (var i = 1; i < arguments.length; i++) {
    var source = arguments[i] != null ? arguments[i] : {};
    if (i % 2) {