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

shift-shrink

v2.0.0

Published

Test case reducer for Shift-format JS ASTs

Downloads

21

Readme

Shift Shrink

A test case reducer for JavaScript ASTs in the Shift format.

Well suited for use with Shift Fuzzer.

Installation

npm install shift-shrink

Use

The primary interface for this tool takes as input an "interesting" AST (whatever that means for your use case) and a function which determines whether an AST is interesting, and returns the smallest interesting version of that AST it can find. For example, if you've generated a large AST that has different output when evaluated in V8 versus in SpiderMonkey, you can use this to find a smaller AST that has that property.

It only explores single-step simplifications, so this is not guaranteed to be the smallest possible interesting AST.

If you need to work with JavaScript programs rather than their ASTs, you can use shift-parser and shift-codegen to go back and forth between the two.

Because this can take some time, you can provide a log callback in the optional third options bag argument which will be called periodically. This parameter is intended only to allow humans to confirm it's making progress, not as a consistent part of the API. Additionally, the onImproved callback is called whenever a new best candidate is found, so you can see progress (and interrupt the shrinking without losing progress, as long as you persist the result).

let { shrink } = require('shift-shrink');
let { parseScript } = require('shift-parser');

let testCase = parseScript('let x = 0;');

// non-async functions also work, but `shrink` will still be async
let isStillGood = async tree => ...;

// a simpler AST, hopefully. returns `testCase` without modification if no reduction is possible
let shrunk = await shrink(testCase, isStillGood, { log: console.log, onImproved: tree => fs.writeFileSync('best.js', JSON.stringify(tree), 'utf8') });
console.log(shrunk);

Shrinking code within a harness

If you have some harness code which should be left untouched, you can provide a path option giving the path to the node you want to shrink.

let testCase = parseScript(`
  function run() {
    // code of interest goes here
  }
  run();
`);

// only attempt to shrink the body of `run`
let shrunk = await shrink(testCase, isStillGood, { path: ['statements', 0, 'body'] });

lookahead

By default, the runner will iterate over trees which are reachable by changing one of the top-level AST nodes, then trees reachable by changing one of the one-level-deep AST nodes, and so on.

The options bag takes a lookahead option which allows you to specify that it should eagerly generate the specified number of candidates and iterate over them in ascending order of an approximation of AST size. This can considerably improve performance when there are many reductions possible.

await shrink(testCase, isStillGood, { lookahead: 100 })

"smaller"

The goal of this project is to produce "interesting" trees which are smaller than the original "interesting" example. "Smaller" is mainly taken to mean having fewer nodes in the AST, but there are in addition some more subjective rules: for example, null is considered to be the smallest expression, plain function expressions are considered to be smaller than arrows or generators, etc. Currently it does not differentiate among literals except that it considers the empty string to be the smallest string.

Contributing

  • Open a Github issue with a description of your desired change. If one exists already, leave a message stating that you are working on it with the date you expect it to be complete.
  • Sign the CLA by submitting a PR as described in that repository.
  • Fork this repo, and clone the forked repo.
  • Install dependencies with npm install.
  • Build and test in your environment with npm run build && npm test.
  • Create a feature branch. Make your changes. Add tests.
  • Build and test in your environment with npm run build && npm test.
  • Make a commit that includes the text "fixes #XX" where XX is the Github issue.
  • Open a Pull Request on Github.