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

chiles

v0.1.3

Published

esprima wrapper to manipulate javascript files

Downloads

26

Readme

Chiles

Wrapper around esprima to modify javascript documents.

example

Modify inline comments and add content before each comment.

const _ = require('lodash');
const chiles = require('../dist/chiles.js');

const src = `
//i1
function abc(a, b) {
  //i2
}
//i3
`;

const doc = chiles(src);

console.log(doc.repr());
console.log(doc.toString());

const newNodes = [];
doc.walkPostOrder(n => {
  if (n.type === 'Line') {
    doc.update('\n', n.range[0], n.range[0], null);
    newNodes.push(doc.insert(`// ADDED: ${n.value}`, n.range[0] - 1));
    n.setSource(`// -> (${n.value})`);
  }
});

console.log(doc.repr());
console.log(doc.toString());

_.each(newNodes, n => {
  console.log([n.getSource()]);
});

output:

[0 - 41]: Document
  [0 - 41]: Program
    [1 - 5]: Line
    [6 - 35]: FunctionDeclaration
      [15 - 18]: Identifier
      [19 - 20]: Identifier
      [22 - 23]: Identifier
      [25 - 35]: BlockStatement
        [29 - 33]: Line
    [36 - 40]: Line

//i1
function abc(a, b) {
  //i2
}
//i3

[0 - 98]: Document
  [0 - 98]: Program
    [1 - 13]: _Unknown
    [14 - 24]: Line
    [25 - 73]: FunctionDeclaration
      [34 - 37]: Identifier
      [38 - 39]: Identifier
      [41 - 42]: Identifier
      [44 - 73]: BlockStatement
        [48 - 60]: _Unknown
        [61 - 71]: Line
    [74 - 86]: _Unknown
    [87 - 97]: Line

// ADDED: i1
// -> (i1)
function abc(a, b) {
  // ADDED: i2
// -> (i2)
}
// ADDED: i3
// -> (i3)

[ '// ADDED: i1' ]
[ '// ADDED: i2' ]
[ '// ADDED: i3' ]

methods

const chiles = require('chiles');

chiles(src)

Parse the string source src with esprima, returns a single Document object which can be used to modify the original source.

Document

A document object has the following methods:

str(start, end): returns a substring from the source with the specified endpoints.

repr(): Utility function displaying all the ranges and the node types for the document.

walkPostOrder(fn): Walks through the document and executes the function fn on a node only after all its children have been visited. The function fn takes in paramters node and level.

insert(text, start, type = 'Unknown'): Inserts the text text at the index spefied by start. We may optionally specified the type of node that corresponds to the inserted text.

update(text, start, end, type = 'Unknown'): Replace a substring from the source with the specified text. Usually useful when modifying a piece of text that has not yet been claimed by a node.

WARNING: At the moment there is no checks to make sure that the range we are modifying does not belong to another node.

toString(): Obtain the current document as a string.

nodes

Each node in a Document has the following properties:

_prev:

_next:

_child:

_parent:

_owner:

setSource:

getSource:

install

With npm:

npm install chiles