npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babeliser

v0.6.0

Published

A blazingly brilliant code parser for testing JavaScript code

Downloads

195

Readme

Babeliser

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

API

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);
assert.equal(t.getArrowFunctionExpressions().length, 2);

const iIFEArrowFunctionExpression = t
  .getArrowFunctionExpressions()
  .find((a) => {
    return a.scope.join() === "global,sub";
  });
assert.exists(iIFEArrowFunctionExpression);
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");
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);
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");
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");
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);