How to use the @babel/traverse.cache function in @babel/traverse

To help you get started, we’ve selected a few @babel/traverse 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 parcel-bundler / parcel / packages / shared / scope-hoisting / src / hoist.js View on Github external
path.replaceWith(
          t.program([
            WRAPPER_TEMPLATE({
              NAME: getIdentifier(asset, 'exports'),
              BODY: path.node.body,
            }),
          ]),
        );

        asset.symbols.clear();
        asset.meta.isCommonJS = true;
        asset.meta.isES6Module = false;
      } else {
        // Re-crawl scope so we are sure to have all bindings.
        traverse.cache.clearScope();
        scope.crawl();

        // Rename each binding in the top-level scope to something unique.
        for (let name in scope.bindings) {
          if (!name.startsWith('$' + t.toIdentifier(asset.id))) {
            let newName = getName(asset, 'var', name);
            rename(scope, name, newName);
          }
        }

        let exportsIdentifier = getIdentifier(asset, 'exports');

        // Add variable that represents module.exports if it is referenced and not declared.
        if (
          scope.hasGlobal(exportsIdentifier.name) &&
          !scope.hasBinding(exportsIdentifier.name)
github facebook / prepack / src / react / utils.js View on Github external
if (t.isMemberExpression(parentNode)) {
              // remove the "this" from the member
              parentPath.replaceWith(parentNode.property);
            } else {
              throw new FatalError(
                `conversion of a simple class component to functional component failed due to "this" not being replaced`
              );
            }
          }
        },
      },
      undefined,
      {},
      undefined
    );
    traverse.cache.clear();
  });
}
github parcel-bundler / parcel / packages / shared / scope-hoisting / src / hoist.js View on Github external
enter(path, asset: MutableAsset) {
      asset.meta.id = asset.id;
      asset.meta.exportsIdentifier = getName(asset, 'exports');

      traverse.cache.clearScope();
      path.scope.crawl();

      let shouldWrap = false;
      path.traverse({
        CallExpression(path) {
          // If we see an `eval` call, wrap the module in a function.
          // Otherwise, local variables accessed inside the eval won't work.
          let callee = path.node.callee;
          if (
            t.isIdentifier(callee) &&
            callee.name === 'eval' &&
            !path.scope.hasBinding('eval', true)
          ) {
            asset.meta.isCommonJS = true;
            shouldWrap = true;
            path.stop();
github facebook / prepack / src / evaluators / ForStatement.js View on Github external
let functionInfo = {
    usesArguments: false,
    usesThis: false,
    usesReturn: false,
    usesGotoToLabel: false,
    usesThrow: false,
    varPatternUnsupported: false,
  };

  traverse(
    t.file(t.program([t.expressionStatement(t.functionExpression(null, [], body))])),
    BailOutWrapperClosureRefVisitor,
    null,
    functionInfo
  );
  traverse.cache.clear();
  let { usesReturn, usesThrow, usesArguments, usesGotoToLabel, varPatternUnsupported, usesThis } = functionInfo;

  if (usesReturn || usesThrow || usesArguments || usesGotoToLabel || varPatternUnsupported) {
    // We do not have support for these yet
    let diagnostic = new CompilerDiagnostic(
      `failed to recover from a for/while loop bail-out due to unsupported logic in loop body`,
      realm.currentLocation,
      "PP0037",
      "FatalError"
    );
    realm.handleError(diagnostic);
    throw new FatalError();
  }
  let args = [wrapperFunction];

  if (usesThis) {
github facebook / prepack / src / serializer / ResidualHeapVisitor.js View on Github external
usesArguments: false,
        usesThis: false,
      };
      let state = {
        functionInfo,
        realm: this.realm,
        getModuleIdIfNodeIsRequireFunction: this.modules.getGetModuleIdIfNodeIsRequireFunction(formalParameters, [val]),
      };

      traverse(
        t.file(t.program([t.expressionStatement(t.functionExpression(null, formalParameters, code))])),
        ClosureRefVisitor,
        null,
        state
      );
      traverse.cache.clear();
      this.functionInfos.set(code, functionInfo);

      if (val.isResidual && functionInfo.unbound.size) {
        if (!val.isUnsafeResidual) {
          this.logger.logError(
            val,
            `residual function ${describeLocation(this.realm, val, undefined, code.loc) ||
              "(unknown)"} refers to the following identifiers defined outside of the local scope: ${Object.keys(
              functionInfo.unbound
            ).join(", ")}`
          );
        }
      }
    }

    let additionalFunctionEffects = this.additionalFunctionValuesAndEffects.get(val);

@babel/traverse

The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes

MIT
Latest version published 26 days ago

Package Health Score

95 / 100
Full package analysis