Socket
Socket
Sign inDemoInstall

babeliser

Package Overview
Dependencies
12
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    babeliser

A blazingly brilliant code parser for testing JavaScript code


Version published
Maintainers
1
Install size
630 kB
Created

Readme

Source

Babeliser

A helper library to parse JavaScript code. Uses @babel/parser under the hood.

API

Code Used:
import z from "z";
import { y } from "y";

const a = 1;
let b = 2;
var c = 3;
console.log(a + b + c);

function add(param1, param2) {
  const tot = param1 + param2;
  let currentWeapon;
  return tot;
}

add(a, b);

async function sub(param1, param2) {
  (() => {
    b++;
  })();
}

const complexType = {
  a: 1,
  b: [1, 2, "3"],
  c: {
    d: true,
  },
  e: () => {
    const inner = 24;
  },
};
import { Babeliser } from "babeliser";

const babelisedCode = new Babeliser(codeString);

assert.equal(t.parsedCode.program.body.length, 7);
getArrowFunctionExpressions
assert.equal(t.getArrowFunctionExpressions().length, 2);

const iIFEArrowFunctionExpression = t
  .getArrowFunctionExpressions()
  .find((a) => {
    return a.scope.join() === "global,sub";
  });
assert.exists(iIFEArrowFunctionExpression);
getExpressionStatements
assert.equal(t.getExpressionStatements().length, 4);

const consoleExpression = t.getExpressionStatements().find((e) => {
  const callExpression = e.expression;
  const memberExpression = callExpression.callee;
  const object = memberExpression.object;
  const property = memberExpression.property;
  return object.name === "console" && property.name === "log";
});

const consoleCallExpression = consoleExpression?.expression;
const binaryExpression = consoleCallExpression.arguments[0];
const binaryExpressionLeft = binaryExpression.left;
const binaryExpressionLeftLeft = binaryExpressionLeft.left;
const binaryExpressionLeftRight = binaryExpressionLeft.right;
const binaryExpressionRight = binaryExpression.right;
assert.equal(binaryExpressionLeftLeft.name, "a");
assert.equal(binaryExpressionLeftRight.name, "b");
assert.equal(binaryExpressionRight.name, "c");

const addExpression = t.getExpressionStatements().find((e) => {
  const callExpression = e.expression;
  const calleeIdentifier = callExpression.callee;
  if (calleeIdentifier.type === "Identifier") {
    return calleeIdentifier.name === "add";
  }
  return false;
});
const addCallExpression = addExpression?.expression;
const addCalleeIdentifier = addCallExpression.callee;
assert.equal(addCalleeIdentifier.name, "add");
const addArguments = addCallExpression.arguments;
const addArgOneIdentifier = addArguments[0];
assert.equal(addArgOneIdentifier.name, "a");
const addArgTwoIdentifier = addArguments[1];
assert.equal(addArgTwoIdentifier.name, "b");
getFunctionDeclarations
assert.equal(t.getFunctionDeclarations().length, 2);

const addFunction = t.getFunctionDeclarations().find((f) => {
  return f.id?.name === "add";
});
assert.exists(addFunction);
const addFunctionParams = addFunction.params;
assert.equal(addFunctionParams.length, 2);
const addFunctionParamOne = addFunctionParams[0];
assert.equal(addFunctionParamOne.name, "param1");
const addFunctionParamTwo = addFunctionParams[1];
assert.equal(addFunctionParamTwo.name, "param2");

const totVariable = addFunction.body.body[0];
const totVariableDeclarator = totVariable.declarations[0];
const totIdentifier = totVariableDeclarator.id;
assert.equal(totIdentifier.name, "tot");
const totBinaryExpression = totVariableDeclarator.init;
const totBinaryLeftIdentifier = totBinaryExpression.left;
assert.equal(totBinaryLeftIdentifier.name, "param1");
const totBinaryRightIdentifier = totBinaryExpression.right;
assert.equal(totBinaryRightIdentifier.name, "param2");

const returnStatement = addFunction.body.body.find((b) => {
  return b.type === "ReturnStatement";
});
const returnStatementArgument = returnStatement.argument;
assert.equal(returnStatementArgument.name, "tot");

const subFunctionDeclaration = t.getFunctionDeclarations().find((f) => {
  return f.id?.name === "sub";
});
assert.equal(subFunctionDeclaration.async, true);
getImportDeclarations
assert.equal(t.getImportDeclarations().length, 2);

const zImportDeclaration = t.getImportDeclarations().find((i) => {
  return i.specifiers[0].local.name === "z";
});
const zImportDefaultSpecifier = zImportDeclaration.specifiers[0];
const zSource = zImportDeclaration.source;
assert.equal(zSource.value, "z");

const yImportDeclaration = t.getImportDeclarations().find((i) => {
  return i.specifiers[0].local.name === "y";
});
const yImportSpecifier = yImportDeclaration.specifiers[0];
const yIdentifierLocal = yImportSpecifier.local;
const yIdentifierImported = yImportSpecifier.imported;
assert.equal(yIdentifierLocal.name, "y");
assert.equal(yIdentifierImported.name, "y");
const ySource = yImportDeclaration.source;
assert.equal(ySource.value, "y");
getType
const bUpdateExpression = t.getType("UpdateExpression").pop();
assert.equal(bUpdateExpression.operator, "++");
assert.equal(bUpdateExpression.scope.join(), "global,sub");
const bUpdateExpressionArgument = bUpdateExpression.argument;
assert.equal(bUpdateExpressionArgument.name, "b");
getVariableDeclarations
assert.equal(t.getVariableDeclarations().length, 7);
assert.equal(
  t
    .getVariableDeclarations()
    .filter((v) => v.scope.join() === "global,complexType,e").length,
  1
);

const aVariableDeclaration = t.getVariableDeclarations().find((v) => {
  const variableDeclarator = v.declarations[0];
  const identifier = variableDeclarator.id;
  return identifier.name === "a";
});
assert.equal(aVariableDeclaration.kind, "const");
assert.equal(aVariableDeclaration.scope.join(), "global");
const aNumericLiteral = aVariableDeclaration.declarations[0].init;
assert.equal(aNumericLiteral.value, 1);

const bVariableDeclaration = t.getVariableDeclarations().find((v) => {
  const variableDeclarator = v.declarations[0];
  const identifier = variableDeclarator.id;
  return identifier.name === "b";
});
assert.equal(bVariableDeclaration.kind, "let");
assert.equal(bVariableDeclaration.scope.join(), "global");
const bNumericLiteral = bVariableDeclaration.declarations[0].init;
assert.equal(bNumericLiteral.value, 2);

const cVariableDeclaration = t.getVariableDeclarations().find((v) => {
  const variableDeclarator = v.declarations[0];
  const identifier = variableDeclarator.id;
  return identifier.name === "c";
});
assert.equal(cVariableDeclaration.kind, "var");
assert.equal(cVariableDeclaration.scope.join(), "global");
const cNumericLiteral = cVariableDeclaration.declarations[0].init;
assert.equal(cNumericLiteral.value, 3);

const complexTypeVariableDeclaration = t.getVariableDeclarations().find((v) => {
  const variableDeclarator = v.declarations[0];
  const identifier = variableDeclarator.id;
  return identifier.name === "complexType";
});
assert.equal(complexTypeVariableDeclaration.kind, "const");
assert.equal(complexTypeVariableDeclaration.scope.join(), "global");

const innerVariableDeclaration = t.getVariableDeclarations().find((v) => {
  const variableDeclarator = v.declarations[0];
  const identifier = variableDeclarator.id;
  return identifier.name === "inner";
});
assert.equal(innerVariableDeclaration.kind, "const");
assert.equal(innerVariableDeclaration.scope.join(), "global,complexType,e");
const innerNumericLiteral = innerVariableDeclaration.declarations[0].init;
assert.equal(innerNumericLiteral.value, 24);

Keywords

FAQs

Last updated on 27 Oct 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc