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

es-pattern-match

v0.7.4

Published

ECMAScript AST pattern match library that finds code fragments inside source code.

Downloads

3

Readme

ECMAScript AST pattern match: es-pattern-match

es-pattern-match makes you happy to play with AST of ECMAScript code. It helps you to find AST nodes by using regular ECMAScript code fragments. You don't have to traverse deep AST tree structure by yourself anymore.

Demo

const StatementPattern = require('es-pattern-match').StatementPattern;

// patterns is a dictionary of pattern codes.
// Its name (the following code uses 'array') is stored in result.
const pattern = new StatementPattern({
    array: `[1, 2, 3, 4, 5]`
});

// match() method checks code AST and returns result
// src text is just JS code
const src = `
    const a = [1, 2, 3, 4, 5];
`;

const results = pattern.match(src);
/*
   results = [
       {
           name: 'array',          // pattern name
           stack: [acorn.Node...], // matched 
           node: acorn.Node
       }
   ];
*/

Requirement

  • acorn

Reference

class StatementPattern;

constructor(patterns : Map.<string, string>, acornOption : Object)

Analyze patterns' AST and cache.

match(src : string) : {name: string|acorn.Node|acorn.Node[], node: acorn.Node, stack: acorn.Node[]}[]

Analyze src AST and find matched part in src with patterns.

function patternMatch(src : string|acorn.Node|acorn.Node[], patterns : Map.<string, string>, acornOption : Object) : {name: string, node: acorn.Node, stack: acorn.Node[]}[]

It is a shortcut function to call StatementPattern#match.

Wildcard Reference

__decl__

It matches with var, const, let.

const result = patternMatch(`const v = 'string'`, {
    'string-variable-declearation': `__decl__ v = __string__`
});

__any__

It matches with any node.

const result = patternMatch(`const v = ['array', {object: "also matched"}]`, {
    'variable-declearation': `__decl__ v = __any__`
});

__anybody__

It matches with expressions in function body.

const result = patternMatch(`
    (function () {
        console.log("hello world");
    })();`, {
    "immediate-function": `(function () {
        __anybody__;
    })`
});

__anyname__

It matches with any identifier.

const result = patternMatch(`const v = 'string'`, {
    'string-variable-declearation': `const __anyname__ = 'string'`
});

__number__

const result = patternMatch(`var v = 12345`, {
    'number-variable-declearation': `var v = __number__`
});

__string__

const result = patternMatch(`let v = 'string'`, {
    'string-variable-declearation': `let v = __string__`
});

__boolean__

It matches with true/false

const result = patternMatch(`const v = true`, {
    'boolean-variable-declearation': `const v = __boolean__`
});

__array__

It matches with any arrays

const result = patternMatch(`const v = ['array', 'is', 'matched', 'with', '__array__']`, {
    'array-variable-declearation': `const v = __array__`
});

__object__

It matches with object expression

const result = patternMatch(`const v = {'object': 'expression'}`, {
    'object-variable-declearation': `const v = __object__`
});

__extra__

It matches with extra function arguments or extra elements of Array

const result = patternMatch(`function(1, 2, 3, 4, 'debug-flag');`, {
    'array': `function(1, 2, 3, 4, __extra);`
});
const result = patternMatch(`const v = [1, 2, 3, 4, 5];`, {
    'array': `[1, 2, 3, __extra__]`
});

Install

$ npm install es-pattern-match

Licence

MIT

Author

Yoshiki Shibukawa