@babel/traverse#Binding TypeScript Examples

The following examples show how to use @babel/traverse#Binding. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 6 votes vote down vote up
private findOrPushBindingIndex(binding: Binding): number {
    const bindingIndex = this.findBindingIndex(binding);
    if (bindingIndex === -1) {
      this.innerBindingMap.push(binding.identifier.name, binding);
      this.map.get(binding.identifier.name).push([]);
      return this.findBindingIndex(binding);
    }
    return bindingIndex;
  }
Example #2
Source File: plugin.ts    From react-native-decompiler with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
   * Does a visit of all nodes within the scope of the giving binding
   * @param binding The binding to set traversing bounds
   * @param varName The variable name for the binding
   * @param visitor A visitor object
   */
  protected bindingTraverse(binding: Binding, varName: string, visitor: Visitor): void {
    binding.scope.traverse(binding.scope.block, {
      ...visitor,
      Scope: (path) => {
        if (!path.scope.bindingIdentifierEquals(varName, binding.identifier)) {
          path.skip();
        }
      },
    });
  }
Example #3
Source File: resolveJsxComponent.ts    From react-optimized-image with MIT License 6 votes vote down vote up
resolveStyledComponentsImport = (
  node: CallExpression,
  binding: Binding,
): { exportName?: string; moduleName?: string } | undefined => {
  if (node.callee.type !== 'Identifier') {
    return;
  }

  const resolved = resolveImport(binding.scope.getBinding(node.callee.name)); // eslint-disable-line no-use-before-define

  if (
    resolved &&
    resolved.moduleName &&
    (isModule(resolved.moduleName, 'styled-components') || isModule(resolved.moduleName, '@emotion/styled'))
  ) {
    if (node.arguments.length > 0 && node.arguments[0].type === 'Identifier') {
      return resolveImport(binding.scope.getBinding(node.arguments[0].name)); // eslint-disable-line no-use-before-define
    }
  }
}
Example #4
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 6 votes vote down vote up
private findOrPushBindingIndex(binding: Binding): number {
    const bindingIndex = this.findBindingIndex(binding);
    if (bindingIndex === -1) {
      this.innerBindingMap.push(binding.identifier.name, binding);
      this.map.get(binding.identifier.name).push([]);
      return this.findBindingIndex(binding);
    }
    return bindingIndex;
  }
Example #5
Source File: plugin.ts    From react-native-decompiler with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
   * Does a visit of all nodes within the scope of the giving binding
   * @param binding The binding to set traversing bounds
   * @param varName The variable name for the binding
   * @param visitor A visitor object
   */
  protected bindingTraverse(binding: Binding, varName: string, visitor: Visitor): void {
    binding.scope.traverse(binding.scope.block, {
      ...visitor,
      Scope: (path) => {
        if (!path.scope.bindingIdentifierEquals(varName, binding.identifier)) {
          path.skip();
        }
      },
    });
  }
Example #6
Source File: resolveJsxComponent.ts    From react-optimized-image with MIT License 6 votes vote down vote up
resolveLocalImportBinding = (binding: Binding, moduleName: string, exportName: string): Binding | undefined => {
  if (binding.path.hub.file.opts.filename) {
    // resolve and parse file
    const filePath = resolveFilePathSync(binding.path, moduleName);

    if (!filePath) {
      return undefined;
    }

    const parsedFile = loadFileSync(filePath, binding.path.hub.file.opts.parserOpts);
    const exploded = explodeModule(parsedFile.path.parent);
    const exportStatement = exploded.exports.find((e: { external: string }) => e.external === exportName);

    if (!exportStatement) {
      return undefined;
    }

    return parsedFile.scope.getBinding(exportStatement.local);
  }

  return undefined;
}
Example #7
Source File: resolveJsxComponent.ts    From react-optimized-image with MIT License 6 votes vote down vote up
getImportedJsxComponent = (binding: Binding | undefined): string | undefined => {
  const resolved = resolveImport(binding);

  // follow local imports
  if (
    resolved &&
    resolved.moduleName &&
    (resolved.moduleName.startsWith('./') || resolved.moduleName.startsWith('../')) &&
    resolved.exportName &&
    binding
  ) {
    const resolvedBinding = resolveLocalImportBinding(binding, resolved.moduleName, resolved.exportName);
    return getImportedJsxComponent(resolvedBinding);
  }

  if (
    resolved &&
    resolved.exportName &&
    resolved.moduleName &&
    isModule(resolved.moduleName, 'react-optimized-image')
  ) {
    return simplifyExportName(resolved.exportName, resolved.moduleName);
  }
}
Example #8
Source File: traverse.ts    From react-optimized-image with MIT License 6 votes vote down vote up
resolveRequireExportName = (node: VariableDeclarator, binding: Binding): string | undefined => {
  // check for const { Svg } = require('react-optimized-image') calls
  if (node.id.type === 'ObjectPattern') {
    return (node.id.properties.find(
      (property) =>
        property.type === 'ObjectProperty' &&
        property.value.type === 'Identifier' &&
        property.value.name === binding.identifier.name,
    ) as ObjectProperty).key.name;
  }

  // check for require('react-optimized-image').default calls
  if (
    node.init &&
    node.init.type === 'MemberExpression' &&
    node.init.object.type === 'CallExpression' &&
    node.init.property.type === 'Identifier'
  ) {
    return node.init.property.name;
  }
}
Example #9
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
forEach(_callbackfn: (value: V[], key: Binding, map: Map<Binding, V[]>) => void, _thisArg?: unknown): void {
    throw new Error('Method not implemented.');
  }
Example #10
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Map that supports multiple values in one key
 */
export default class BindingArrayMap<V> implements Map<Binding, V[]> {
  private map: ArrayMap<string, V[]> = new ArrayMap<string, V[]>();
  private innerBindingMap: ArrayMap<string, Binding> = new ArrayMap<string, Binding>();

  get size(): number {
    let size = 0;
    this.innerBindingMap.forEach((e) => {
      size += e.length;
    });
    return size;
  }

  get(key: Binding): V[] {
    const bindingIndex = this.findBindingIndex(key);
    return bindingIndex === -1 ? [] : this.map.get(key.identifier.name)[bindingIndex];
  }

  push(key: Binding, value: V): void {
    const bindingIndex = this.findOrPushBindingIndex(key);
    this.map.get(key.identifier.name)[bindingIndex].push(value);
  }

  forEachElement(key: Binding, callbackfn: (value: V) => void): void {
    this.get(key).forEach(callbackfn);
  }

  private findBindingIndex(binding: Binding): number {
    return this.innerBindingMap.get(binding.identifier.name).findIndex((e) => e === binding);
  }

  private findOrPushBindingIndex(binding: Binding): number {
    const bindingIndex = this.findBindingIndex(binding);
    if (bindingIndex === -1) {
      this.innerBindingMap.push(binding.identifier.name, binding);
      this.map.get(binding.identifier.name).push([]);
      return this.findBindingIndex(binding);
    }
    return bindingIndex;
  }

  clear(): void {
    throw new Error('Method not implemented.');
  }
  delete(_key: Binding): boolean {
    throw new Error('Method not implemented.');
  }
  forEach(_callbackfn: (value: V[], key: Binding, map: Map<Binding, V[]>) => void, _thisArg?: unknown): void {
    throw new Error('Method not implemented.');
  }
  has(_key: Binding): boolean {
    throw new Error('Method not implemented.');
  }
  set(_key: Binding, _value: V[]): this {
    throw new Error('Method not implemented.');
  }
  [Symbol.iterator](): IterableIterator<[Binding, V[]]> {
    throw new Error('Method not implemented.');
  }
  entries(): IterableIterator<[Binding, V[]]> {
    throw new Error('Method not implemented.');
  }
  keys(): IterableIterator<Binding> {
    throw new Error('Method not implemented.');
  }
  values(): IterableIterator<V[]> {
    throw new Error('Method not implemented.');
  }
  [Symbol.toStringTag]: string;
}
Example #11
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
get(key: Binding): V[] {
    const bindingIndex = this.findBindingIndex(key);
    return bindingIndex === -1 ? [] : this.map.get(key.identifier.name)[bindingIndex];
  }
Example #12
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
push(key: Binding, value: V): void {
    const bindingIndex = this.findOrPushBindingIndex(key);
    this.map.get(key.identifier.name)[bindingIndex].push(value);
  }
Example #13
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
forEachElement(key: Binding, callbackfn: (value: V) => void): void {
    this.get(key).forEach(callbackfn);
  }
Example #14
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
private findBindingIndex(binding: Binding): number {
    return this.innerBindingMap.get(binding.identifier.name).findIndex((e) => e === binding);
  }
Example #15
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
delete(_key: Binding): boolean {
    throw new Error('Method not implemented.');
  }
Example #16
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
has(_key: Binding): boolean {
    throw new Error('Method not implemented.');
  }
Example #17
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
keys(): IterableIterator<Binding> {
    throw new Error('Method not implemented.');
  }
Example #18
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
set(_key: Binding, _value: V[]): this {
    throw new Error('Method not implemented.');
  }
Example #19
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
[Symbol.iterator](): IterableIterator<[Binding, V[]]> {
    throw new Error('Method not implemented.');
  }
Example #20
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
entries(): IterableIterator<[Binding, V[]]> {
    throw new Error('Method not implemented.');
  }
Example #21
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
keys(): IterableIterator<Binding> {
    throw new Error('Method not implemented.');
  }
Example #22
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
private innerBindingMap: ArrayMap<string, Binding> = new ArrayMap<string, Binding>();
Example #23
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Map that supports multiple values in one key
 */
export default class BindingArrayMap<V> implements Map<Binding, V[]> {
  private map: ArrayMap<string, V[]> = new ArrayMap<string, V[]>();
  private innerBindingMap: ArrayMap<string, Binding> = new ArrayMap<string, Binding>();

  get size(): number {
    let size = 0;
    this.innerBindingMap.forEach((e) => {
      size += e.length;
    });
    return size;
  }

  get(key: Binding): V[] {
    const bindingIndex = this.findBindingIndex(key);
    return bindingIndex === -1 ? [] : this.map.get(key.identifier.name)[bindingIndex];
  }

  push(key: Binding, value: V): void {
    const bindingIndex = this.findOrPushBindingIndex(key);
    this.map.get(key.identifier.name)[bindingIndex].push(value);
  }

  forEachElement(key: Binding, callbackfn: (value: V) => void): void {
    this.get(key).forEach(callbackfn);
  }

  private findBindingIndex(binding: Binding): number {
    return this.innerBindingMap.get(binding.identifier.name).findIndex((e) => e === binding);
  }

  private findOrPushBindingIndex(binding: Binding): number {
    const bindingIndex = this.findBindingIndex(binding);
    if (bindingIndex === -1) {
      this.innerBindingMap.push(binding.identifier.name, binding);
      this.map.get(binding.identifier.name).push([]);
      return this.findBindingIndex(binding);
    }
    return bindingIndex;
  }

  clear(): void {
    throw new Error('Method not implemented.');
  }
  delete(_key: Binding): boolean {
    throw new Error('Method not implemented.');
  }
  forEach(_callbackfn: (value: V[], key: Binding, map: Map<Binding, V[]>) => void, _thisArg?: any): void {
    throw new Error('Method not implemented.');
  }
  has(_key: Binding): boolean {
    throw new Error('Method not implemented.');
  }
  set(_key: Binding, _value: V[]): this {
    throw new Error('Method not implemented.');
  }
  [Symbol.iterator](): IterableIterator<[Binding, V[]]> {
    throw new Error('Method not implemented.');
  }
  entries(): IterableIterator<[Binding, V[]]> {
    throw new Error('Method not implemented.');
  }
  keys(): IterableIterator<Binding> {
    throw new Error('Method not implemented.');
  }
  values(): IterableIterator<V[]> {
    throw new Error('Method not implemented.');
  }
  [Symbol.toStringTag]: string;
}
Example #24
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
private innerBindingMap: ArrayMap<string, Binding> = new ArrayMap<string, Binding>();
Example #25
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
entries(): IterableIterator<[Binding, V[]]> {
    throw new Error('Method not implemented.');
  }
Example #26
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
[Symbol.iterator](): IterableIterator<[Binding, V[]]> {
    throw new Error('Method not implemented.');
  }
Example #27
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
set(_key: Binding, _value: V[]): this {
    throw new Error('Method not implemented.');
  }
Example #28
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
has(_key: Binding): boolean {
    throw new Error('Method not implemented.');
  }
Example #29
Source File: bindingArrayMap.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
forEach(_callbackfn: (value: V[], key: Binding, map: Map<Binding, V[]>) => void, _thisArg?: any): void {
    throw new Error('Method not implemented.');
  }