@babel/types#isNumericLiteral TypeScript Examples

The following examples show how to use @babel/types#isNumericLiteral. 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: longBooleans.ts    From react-native-decompiler with GNU Affero General Public License v3.0 6 votes vote down vote up
getVisitor(): Visitor {
    return {
      UnaryExpression(path) {
        const node = path.node;
        if (node.operator !== '!' || !isNumericLiteral(node.argument) || (node.argument.value !== 0 && node.argument.value !== 1)) return;
        path.replaceWith(booleanLiteral(!node.argument.value));
      },
    };
  }
Example #2
Source File: reactNativeSingleParser.ts    From react-native-decompiler with GNU Affero General Public License v3.0 6 votes vote down vote up
async parse(args: CmdArgs): Promise<Module[]> {
    console.log('Parsing JS...');
    this.startTimer('parse-js');

    const file = await fs.readFile(args.in, 'utf8');
    const ast = babylon.parse(file);

    this.stopAndPrintTime('parse-js');

    const modules: Module[] = [];

    console.log('Finding modules...');
    this.startTimer('find-modules');

    traverse(ast, {
      CallExpression: (nodePath) => {
        if (isIdentifier(nodePath.node.callee) && nodePath.node.callee.name === '__d') {
          const functionArg = nodePath.get('arguments')[0];
          const moduleId = nodePath.get('arguments')[1];
          const dependencies = nodePath.get('arguments')[2];
          if (functionArg.isFunctionExpression() && moduleId.isNumericLiteral() && dependencies.isArrayExpression() && functionArg.node.body.body.length) {
            const dependencyValues = dependencies.node.elements.map((e) => {
              if (!isNumericLiteral(e)) throw new Error('Not numeric literal');
              return e.value;
            });
            const newModule = new Module(ast, functionArg, moduleId.node.value, dependencyValues, this.SEVEN_PARAM_MAPPING);
            newModule.calculateFields();
            modules[newModule.moduleId] = newModule;
          }
        }
        nodePath.skip();
      },
    });

    this.stopAndPrintTime('find-modules');

    return modules;
  }
Example #3
Source File: webpackParser.ts    From react-native-decompiler with GNU Affero General Public License v3.0 6 votes vote down vote up
private parseArray(file: File, ast: NodePath<ArrayExpression>, modules: Module[]): void {
    ast.get('elements').forEach((element, i) => {
      if (!element.isFunctionExpression()) return;
      if (element.node.body.body.length === 0) return;

      const dependencyValues: number[] = [];
      const requireIdentifer = element.node.params[2];
      if (isIdentifier(requireIdentifer)) {
        element.traverse({
          CallExpression: (dependencyPath) => {
            if (!isIdentifier(dependencyPath.node.callee) || !isNumericLiteral(dependencyPath.node.arguments[0])) return;
            if (dependencyPath.scope.bindingIdentifierEquals(dependencyPath.node.callee.name, requireIdentifer)) {
              dependencyValues[dependencyPath.node.arguments[0].value] = dependencyPath.node.arguments[0].value;
            }
          },
        });
      }

      const newModule = new Module(file, element, i, dependencyValues, this.PARAM_MAPPING);
      newModule.calculateFields();
      modules[i] = newModule;
    });
  }
Example #4
Source File: webpackParser.ts    From react-native-decompiler with GNU Affero General Public License v3.0 6 votes vote down vote up
private parseObject(file: File, ast: NodePath<ObjectExpression>, modules: Module[]): void {
    ast.get('properties').forEach((property) => {
      if (!property.isObjectProperty() || !isNumericLiteral(property.node.key)) return;

      const element = property.get('value');
      const i = property.node.key.value;
      if (!element.isFunctionExpression()) return;
      if (element.node.body.body.length === 0) return;

      const dependencyValues: number[] = [];
      const requireIdentifer = element.node.params[2];
      if (isIdentifier(requireIdentifer)) {
        element.traverse({
          CallExpression: (dependencyPath) => {
            if (!isIdentifier(dependencyPath.node.callee) || !isNumericLiteral(dependencyPath.node.arguments[0])) return;
            if (dependencyPath.scope.bindingIdentifierEquals(dependencyPath.node.callee.name, requireIdentifer)) {
              dependencyValues[dependencyPath.node.arguments[0].value] = dependencyPath.node.arguments[0].value;
            }
          },
        });
      }

      const newModule = new Module(file, element, i, dependencyValues, this.PARAM_MAPPING);
      newModule.calculateFields();
      modules[i] = newModule;
    });
  }
Example #5
Source File: longBooleans.ts    From react-native-decompiler with GNU Affero General Public License v3.0 6 votes vote down vote up
getVisitor(): Visitor {
    return {
      UnaryExpression(path) {
        const node = path.node;
        if (node.operator !== '!' || !isNumericLiteral(node.argument) || (node.argument.value !== 0 && node.argument.value !== 1)) return;
        path.replaceWith(booleanLiteral(!node.argument.value));
      },
    };
  }
Example #6
Source File: reactNativeSingleParser.ts    From react-native-decompiler with GNU Affero General Public License v3.0 6 votes vote down vote up
async parse(args: CmdArgs): Promise<Module[]> {
    console.log('Parsing JS...');
    this.startTimer('parse-js');

    const file = await fs.readFile(args.in, 'utf8');
    const ast = babylon.parse(file);

    this.stopAndPrintTime('parse-js');

    const modules: Module[] = [];

    console.log('Finding modules...');
    this.startTimer('find-modules');

    traverse(ast, {
      CallExpression: (nodePath) => {
        if (isIdentifier(nodePath.node.callee) && nodePath.node.callee.name === '__d') {
          const functionArg = nodePath.get('arguments')[0];
          const moduleId = nodePath.get('arguments')[1];
          const dependencies = nodePath.get('arguments')[2];
          if (functionArg.isFunctionExpression() && moduleId.isNumericLiteral() && dependencies.isArrayExpression() && functionArg.node.body.body.length) {
            const dependencyValues = dependencies.node.elements.map((e) => {
              if (!isNumericLiteral(e)) throw new Error('Not numeric literal');
              return e.value;
            });
            const newModule = new Module(ast, functionArg, moduleId.node.value, dependencyValues, this.SEVEN_PARAM_MAPPING);
            newModule.calculateFields();
            modules[newModule.moduleId] = newModule;
          }
        }
        nodePath.skip();
      },
    });

    this.stopAndPrintTime('find-modules');

    return modules;
  }
Example #7
Source File: webpackParser.ts    From react-native-decompiler with GNU Affero General Public License v3.0 6 votes vote down vote up
private parseArray(file: File, ast: NodePath<ArrayExpression>, modules: Module[]): void {
    ast.get('elements').forEach((element, i) => {
      if (!element.isFunctionExpression()) return;
      if (element.node.body.body.length === 0) return;

      const dependencyValues: number[] = [];
      const requireIdentifer = element.node.params[2];
      if (isIdentifier(requireIdentifer)) {
        element.traverse({
          CallExpression: (dependencyPath) => {
            if (!isIdentifier(dependencyPath.node.callee) || !isNumericLiteral(dependencyPath.node.arguments[0])) return;
            if (dependencyPath.scope.bindingIdentifierEquals(dependencyPath.node.callee.name, requireIdentifer)) {
              dependencyValues[dependencyPath.node.arguments[0].value] = dependencyPath.node.arguments[0].value;
            }
          },
        });
      }

      const newModule = new Module(file, element, i, dependencyValues, this.PARAM_MAPPING);
      newModule.calculateFields();
      modules[i] = newModule;
    });
  }
Example #8
Source File: webpackParser.ts    From react-native-decompiler with GNU Affero General Public License v3.0 6 votes vote down vote up
private parseObject(file: File, ast: NodePath<ObjectExpression>, modules: Module[]): void {
    ast.get('properties').forEach((property) => {
      if (!property.isObjectProperty() || !isNumericLiteral(property.node.key)) return;

      const element = property.get('value');
      const i = property.node.key.value;
      if (!element.isFunctionExpression()) return;
      if (element.node.body.body.length === 0) return;

      const dependencyValues: number[] = [];
      const requireIdentifer = element.node.params[2];
      if (isIdentifier(requireIdentifer)) {
        element.traverse({
          CallExpression: (dependencyPath) => {
            if (!isIdentifier(dependencyPath.node.callee) || !isNumericLiteral(dependencyPath.node.arguments[0])) return;
            if (dependencyPath.scope.bindingIdentifierEquals(dependencyPath.node.callee.name, requireIdentifer)) {
              dependencyValues[dependencyPath.node.arguments[0].value] = dependencyPath.node.arguments[0].value;
            }
          },
        });
      }

      const newModule = new Module(file, element, i, dependencyValues, this.PARAM_MAPPING);
      newModule.calculateFields();
      modules[i] = newModule;
    });
  }
Example #9
Source File: uselessCommaOperatorCleaner.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
getVisitor(): Visitor {
    return {
      SequenceExpression(path) {
        if (path.node.expressions.length !== 2 || !isNumericLiteral(path.node.expressions[0])) return;
        path.replaceWith(path.node.expressions[1]);
      },
    };
  }
Example #10
Source File: arrayDestructureEvaluator.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
getVisitor(): Visitor {
    if (!this.destructureUsed) return {};

    return {
      VariableDeclarator: (path) => {
        if (!isIdentifier(path.node.id) || path.node.id.start == null) return;

        const variableDeclaratorData: VariableDeclaratorData = {
          path,
          couldBeDestructure: false,
          couldBeArrayAccess: false,
          varName: path.node.id.name,
          varStart: path.node.id.start,
        };

        if (isCallExpression(path.node.init) && isIdentifier(path.node.init.callee)
          && path.node.init.arguments.length === 2 && isIdentifier(path.node.init.arguments[0]) && isNumericLiteral(path.node.init.arguments[1])) {
          variableDeclaratorData.couldBeDestructure = true;
          variableDeclaratorData.destructureBindingStart = path.scope.getBindingIdentifier(path.node.init.callee.name)?.start ?? undefined;
          variableDeclaratorData.destructureArrayBindingStart = path.scope.getBindingIdentifier(path.node.init.arguments[0].name)?.start ?? undefined;
        }
        if (isMemberExpression(path.node.init) && isIdentifier(path.node.init.object) && isNumericLiteral(path.node.init.property)) {
          variableDeclaratorData.couldBeArrayAccess = true;
          variableDeclaratorData.arrayAccessBindingStart = path.scope.getBindingIdentifier(path.node.init.object.name)?.start ?? undefined;
          variableDeclaratorData.arrayAccessVal = path.node.init.property.value;
        }

        this.variableDeclarators.push(variableDeclaratorData);

        const callExpression = path.get('init');
        if (!callExpression.isCallExpression()) return;

        const moduleDependency = this.getModuleDependency(callExpression);
        if (moduleDependency?.moduleName === '@babel/runtime/helpers/slicedToArray') {
          this.destructureFunction = path;
          this.destructureFunctionStart = path.node.id.start;
        }
      },
    };
  }
Example #11
Source File: voidZeroToUndefined.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
getVisitor(): Visitor {
    return {
      UnaryExpression(path) {
        if (path.node.operator !== 'void' || !isNumericLiteral(path.node.argument) || path.node.argument.value !== 0) return;
        path.replaceWith(identifier('undefined'));
      },
    };
  }
Example #12
Source File: reactNativeFolderParser.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
async parse(args: CmdArgs): Promise<Module[]> {
    const fileNames = (await fs.readdir(args.in)).filter((fileName) => fileName.endsWith('.js'));

    console.log('Parsing folder...');
    this.startTimer('parse');
    this.progressBar.start(0, fileNames.length);

    const modules: Module[] = [];

    await Promise.all(fileNames.map(async (fileName) => {
      const file = await fs.readFile(path.join(args.in, fileName), 'utf8');
      const ast = babylon.parse(file);

      traverse(ast, {
        CallExpression: (nodePath) => {
          if (isIdentifier(nodePath.node.callee) && nodePath.node.callee.name === '__d') {
            const functionArg = nodePath.get('arguments')[0];
            const moduleId = nodePath.get('arguments')[1];
            const dependencies = nodePath.get('arguments')[2];
            if (functionArg.isFunctionExpression() && moduleId.isNumericLiteral() && dependencies.isArrayExpression() && functionArg.node.body.body.length) {
              const dependencyValues = dependencies.node.elements.map((e) => {
                if (!isNumericLiteral(e)) throw new Error('Not numeric literal');
                return e.value;
              });
              const newModule = new Module(ast, functionArg, moduleId.node.value, dependencyValues, this.SEVEN_PARAM_MAPPING);
              newModule.calculateFields();
              modules[newModule.moduleId] = newModule;
            }
          }
          nodePath.skip();
        },
      });

      this.progressBar.increment();
    }));

    this.progressBar.stop();
    this.stopAndPrintTime('parse');

    return modules;
  }
Example #13
Source File: uselessCommaOperatorCleaner.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
getVisitor(): Visitor {
    return {
      SequenceExpression(path) {
        if (path.node.expressions.length !== 2 || !isNumericLiteral(path.node.expressions[0])) return;
        path.replaceWith(path.node.expressions[1]);
      },
    };
  }
Example #14
Source File: arrayDestructureEvaluator.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
getVisitor(): Visitor {
    if (!this.destructureUsed) return {};

    return {
      VariableDeclarator: (path) => {
        if (!isIdentifier(path.node.id) || path.node.id.start == null) return;

        const variableDeclaratorData: VariableDeclaratorData = {
          path,
          couldBeDestructure: false,
          couldBeArrayAccess: false,
          varName: path.node.id.name,
          varStart: path.node.id.start,
        };

        if (isCallExpression(path.node.init) && isIdentifier(path.node.init.callee)
          && path.node.init.arguments.length === 2 && isIdentifier(path.node.init.arguments[0]) && isNumericLiteral(path.node.init.arguments[1])) {
          variableDeclaratorData.couldBeDestructure = true;
          variableDeclaratorData.destructureBindingStart = path.scope.getBindingIdentifier(path.node.init.callee.name)?.start ?? undefined;
          variableDeclaratorData.destructureArrayBindingStart = path.scope.getBindingIdentifier(path.node.init.arguments[0].name)?.start ?? undefined;
        }
        if (isMemberExpression(path.node.init) && isIdentifier(path.node.init.object) && isNumericLiteral(path.node.init.property)) {
          variableDeclaratorData.couldBeArrayAccess = true;
          variableDeclaratorData.arrayAccessBindingStart = path.scope.getBindingIdentifier(path.node.init.object.name)?.start ?? undefined;
          variableDeclaratorData.arrayAccessVal = path.node.init.property.value;
        }

        this.variableDeclarators.push(variableDeclaratorData);

        const callExpression = path.get('init');
        if (!callExpression.isCallExpression()) return;

        const moduleDependency = this.getModuleDependency(callExpression);
        if (moduleDependency?.moduleName === '@babel/runtime/helpers/slicedToArray') {
          this.destructureFunction = path;
          this.destructureFunctionStart = path.node.id.start;
        }
      },
    };
  }
Example #15
Source File: voidZeroToUndefined.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
getVisitor(): Visitor {
    return {
      UnaryExpression(path) {
        if (path.node.operator !== 'void' || !isNumericLiteral(path.node.argument) || path.node.argument.value !== 0) return;
        path.replaceWith(identifier('undefined'));
      },
    };
  }
Example #16
Source File: reactNativeFolderParser.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
async parse(args: CmdArgs): Promise<Module[]> {
    const fileNames = (await fs.readdir(args.in)).filter((fileName) => fileName.endsWith('.js'));

    console.log('Parsing folder...');
    this.startTimer('parse');
    this.progressBar.start(0, fileNames.length);

    const modules: Module[] = [];

    await Promise.all(fileNames.map(async (fileName) => {
      const file = await fs.readFile(path.join(args.in, fileName), 'utf8');
      const ast = babylon.parse(file);

      traverse(ast, {
        CallExpression: (nodePath) => {
          if (isIdentifier(nodePath.node.callee) && nodePath.node.callee.name === '__d') {
            const functionArg = nodePath.get('arguments')[0];
            const moduleId = nodePath.get('arguments')[1];
            const dependencies = nodePath.get('arguments')[2];
            if (functionArg.isFunctionExpression() && moduleId.isNumericLiteral() && dependencies.isArrayExpression() && functionArg.node.body.body.length) {
              const dependencyValues = dependencies.node.elements.map((e) => {
                if (!isNumericLiteral(e)) throw new Error('Not numeric literal');
                return e.value;
              });
              const newModule = new Module(ast, functionArg, moduleId.node.value, dependencyValues, this.SEVEN_PARAM_MAPPING);
              newModule.calculateFields();
              modules[newModule.moduleId] = newModule;
            }
          }
          nodePath.skip();
        },
      });

      this.progressBar.increment();
    }));

    this.progressBar.stop();
    this.stopAndPrintTime('parse');

    return modules;
  }