How to use @babel/helpers - 10 common examples

To help you get started, we’ve selected a few @babel/helpers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github babel / babel / packages / babel-core / src / transformation / file / file.js View on Github external
if (res) return res;
    }

    // make sure that the helper exists
    helpers.ensure(name);

    const uid = (this.declarations[name] = this.scope.generateUidIdentifier(
      name,
    ));

    const dependencies = {};
    for (const dep of helpers.getDependencies(name)) {
      dependencies[dep] = this.addHelper(dep);
    }

    const { nodes, globals } = helpers.get(
      name,
      dep => dependencies[dep],
      uid,
      Object.keys(this.scope.getAllBindings()),
    );

    globals.forEach(name => {
      if (this.path.scope.hasBinding(name, true /* noGlobals */)) {
        this.path.scope.rename(name);
      }
    });

    nodes.forEach(node => {
      node._compact = true;
    });
github babel / babel / packages / babel-core / src / transformation / file / file.js View on Github external
const generator = this.get("helperGenerator");
    if (generator) {
      const res = generator(name);
      if (res) return res;
    }

    // make sure that the helper exists
    helpers.ensure(name);

    const uid = (this.declarations[name] = this.scope.generateUidIdentifier(
      name,
    ));

    const dependencies = {};
    for (const dep of helpers.getDependencies(name)) {
      dependencies[dep] = this.addHelper(dep);
    }

    const { nodes, globals } = helpers.get(
      name,
      dep => dependencies[dep],
      uid,
      Object.keys(this.scope.getAllBindings()),
    );

    globals.forEach(name => {
      if (this.path.scope.hasBinding(name, true /* noGlobals */)) {
        this.path.scope.rename(name);
      }
    });
github babel / babel / packages / babel-core / src / transformation / file / file.js View on Github external
addHelper(name: string): Object {
    const declar = this.declarations[name];
    if (declar) return t.cloneNode(declar);

    const generator = this.get("helperGenerator");
    if (generator) {
      const res = generator(name);
      if (res) return res;
    }

    // make sure that the helper exists
    helpers.ensure(name);

    const uid = (this.declarations[name] = this.scope.generateUidIdentifier(
      name,
    ));

    const dependencies = {};
    for (const dep of helpers.getDependencies(name)) {
      dependencies[dep] = this.addHelper(dep);
    }

    const { nodes, globals } = helpers.get(
      name,
      dep => dependencies[dep],
      uid,
      Object.keys(this.scope.getAllBindings()),
    );
github instructure / instructure-ui / packages / ui-babel-preset / lib / index.js View on Github external
transform: (importName, matches) => {
            if (!matches || !matches[1] || matches[1] === '@instructure/ui-test-utils') return
            return `${matches[1]}/lib/${importName}`
          }
        }
      }
    ])
  }

  // Work around https://github.com/babel/babel/issues/10261, which causes
  // Babel to not use the runtime helpers for things like _objectSpread.
  // Remove this once that babel issue is fixed
  let babelHelperVersion = {}
  try {
    // eslint-disable-next-line import/no-extraneous-dependencies
    const version = require('@babel/helpers/package.json').version
    babelHelperVersion.version = version
  } catch (e) {
    // if something goes wrong, continue and don't try to explicitly set a helper version
  }

  plugins = plugins.concat([
    require('babel-plugin-macros'),
    require('@babel/plugin-transform-destructuring').default,
    [require('@babel/plugin-proposal-decorators').default, { legacy: true }], // must run before themeable-styles plugin below
    [require('@babel/plugin-proposal-class-properties').default, { loose: true }],
    require('@babel/plugin-proposal-export-default-from').default,
    [require('@babel/plugin-proposal-object-rest-spread').default, { useBuiltIns: true }],
    require('@babel/plugin-proposal-optional-chaining').default,
    [require('@babel/plugin-transform-runtime').default, {
      ...babelHelperVersion,
      corejs: false,
github Automattic / wp-calypso / packages / calypso-build / babel / default.js View on Github external
plugins: [
		require.resolve( '@babel/plugin-proposal-class-properties' ),
		require.resolve( '@babel/plugin-proposal-nullish-coalescing-operator' ),
		require.resolve( '@babel/plugin-proposal-optional-chaining' ),
		require.resolve( '@babel/plugin-syntax-dynamic-import' ),
		[
			require.resolve( '@babel/plugin-transform-runtime' ),
			{
				corejs: false, // we polyfill so we don't need core-js
				helpers: true,
				regenerator: false,
				useESModules: false,
				// Needed so that helpers aren't duplicated.
				// This will need to be kept up to date while https://github.com/babel/babel/issues/10261 is unresolved.
				// eslint-disable-next-line import/no-extraneous-dependencies
				version: require( '@babel/helpers/package.json' ).version,
			},
		],
	],
} );
github babel / babel / packages / babel-core / src / transformation / file / file.js View on Github external
availableHelper(name: string, versionRange: ?string): boolean {
    let minVersion;
    try {
      minVersion = helpers.minVersion(name);
    } catch (err) {
      if (err.code !== "BABEL_HELPER_UNKNOWN") throw err;

      return false;
    }

    if (typeof versionRange !== "string") return true;

    // semver.intersects() has some surprising behavior with comparing ranges
    // with preprelease versions. We add '^' to ensure that we are always
    // comparing ranges with ranges, which sidesteps this logic.
    // For example:
    //
    //   semver.intersects(`<7.0.1`, "7.0.0-beta.0") // false - surprising
    //   semver.intersects(`<7.0.1`, "^7.0.0-beta.0") // true - expected
    //
github downshift-js / downshift / other / misc-tests / jest.config.js View on Github external
const jestConfig = require('kcd-scripts/config').jest
const babelHelpersList = require('@babel/helpers').list

module.exports = Object.assign(jestConfig, {
  roots: ['.'],
  testEnvironment: 'jsdom',
  moduleNameMapper: babelHelpersList.reduce((aliasMap, helper) => {
    aliasMap[
      `@babel/runtime/helpers/esm/${helper}`
    ] = `@babel/runtime/helpers/${helper}`
    return aliasMap
  }, {}),
})
github downshift-js / downshift / other / misc-tests / jest.config.js View on Github external
const jestConfig = require('kcd-scripts/config').jest
const babelHelpersList = require('@babel/helpers').list

module.exports = Object.assign(jestConfig, {
  roots: ['.'],
  testEnvironment: 'jsdom',
  moduleNameMapper: babelHelpersList.reduce((aliasMap, helper) => {
    aliasMap[
      `@babel/runtime/helpers/esm/${helper}`
    ] = `@babel/runtime/helpers/${helper}`
    return aliasMap
  }, {}),
})
github babel / babel / packages / babel-plugin-transform-runtime / scripts / build-dist.js View on Github external
const tree = t.program([], [], esm ? "module" : "script");
  const dependencies = {};
  let bindings = null;

  if (!esm) {
    bindings = [];
    for (const dep of helpers.getDependencies(helperName)) {
      const id = (dependencies[dep] = t.identifier(t.toIdentifier(dep)));
      tree.body.push(template.statement.ast`
        var ${id} = require("${`./${dep}`}");
      `);
      bindings.push(id.name);
    }
  }

  const helper = helpers.get(
    helperName,
    dep => dependencies[dep],
    esm ? null : template.expression.ast`module.exports`,
    bindings
  );
  tree.body.push(...helper.nodes);

  return babel.transformFromAst(tree, null, {
    filename: helperFilename,
    presets: [
      [
        "@babel/preset-env",
        { modules: false, exclude: ["@babel/plugin-transform-typeof-symbol"] },
      ],
    ],
    plugins: [
github babel / babel / packages / babel-core / src / tools / build-external-helpers.js View on Github external
helpers.list.forEach(function(name) {
    if (whitelist && whitelist.indexOf(name) < 0) return;

    const ref = (refs[name] = getHelperReference(name));

    const { nodes } = helpers.get(name, getHelperReference, ref);

    body.push(...nodes);
  });
  return refs;

@babel/helpers

Collection of helper functions used by Babel transforms.

MIT
Latest version published 9 days ago

Package Health Score

95 / 100
Full package analysis

Similar packages