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 🙏

© 2026 – Pkg Stats / Ryan Hefner

lesx-parser

v0.0.7

Published

lesx DSL AST解析器

Readme

lesx-parser

lesx DSL AST解析器。

配置项

script

暂不支持 是否解析script标签里面的代码成AST,默认不解析。

style

暂不支持 是否解析style标签内部的样式代码,默认不解析。

TODO

  • 目前stylescript标签默认都是按照LesxText文本类型来解析的,后期可以在Compiler层面实现对内部内容进一步的AST解析实现;当然也可以考虑在parse阶段自动解析掉,但是考虑到style可以指定语言(css/sass/less),以及script标签内部js代码所使用到的js语法的不确定性(可能需要ES6/7/8的acorn特殊语法插件),所以到Compiler阶段traverse AST的时候实现解析会更合适一些,同时,也能保证parser的纯粹性。

example

const {
    acornParse,
    babylonParse,
} = require('lesx-parser');

require('colors');

const code = `
<style>
    a {
        color: #999;
    }
</style>

<template>
    <div>
        <a onClick={() => {
            alert(1);
        }}></a>
    </div>
</template>

<script>
    module.exports = {
        props: {},

        state: {},

        // React其他生命周期钩子函数
    };
</script>
`;

const acornTime = `acorn解析时长`.red;
console.time(acornTime);
const acornAst = acornParse(code);
console.timeEnd(acornTime);

const babylonTime = `babylon解析时长`.red;
console.time(babylonTime);
const babylonAst = babylonParse(code);
console.timeEnd(babylonTime);

console.log('acorn ast:'.blue, JSON.stringify(acornAst, null, 4));
console.log('babylon ast:'.blue, JSON.stringify(babylonAst, null, 4));

AST规范

Lesx AST扩展了ECMAScript 6th Edition (ECMA-262) 语法的PrimaryExpression部分:

PrimaryExpression:

LesxElement

Elements

LesxElement

  • LesxSelfClosingElement

  • LesxOpeningElement LesxChildren(可选) LesxClosingElement
    (LesxOpeningElement跟LesxClosingElement的名字需要匹配)

LesxSelfClosingElement

  • < LesxElementName LesxAttributes(可选) />

LesxOpeningElement

  • < LesxElementName LesxAttributes(可选) >

LesxClosingElement

  • < / LesxElementName >

LesxElementName

  • LesxIdentifier
  • LesxNamespacedName
  • LesxMemberExpression

LesxIdentifier

  • IdentifierStart
  • LesxIdentifier IdentifierPart
  • LesxIdentifier 没空格或注释 -

LesxNamespacedName

  • LesxIdentifier : LesxIdentifier

LesxMemberExpression

  • LesxIdentifier . LesxIdentifier
  • LesxMemberExpression . LesxIdentifier

Attributes

LesxAttributes

  • LesxSpreadAttribute LesxAttributes(可选)
  • LesxAttribute LesxAttributes(可选)

LesxSpreadAttribute

  • { ... AssignmentExpression }

LesxAttribute

  • LesxAttributeName LesxAttributeInitializer(可选)

LesxAttributeName

  • LesxIdentifier
  • LesxNamespacedName

LesxAttributeInitializer

  • = LesxAttributeValue

LesxAttributeValue

  • " LesxDoubleStringCharacters(可选) "
  • ' LesxSingleStringCharacters(可选) '
  • { AssignmentExpression }
  • LesxElement

LesxDoubleStringCharacters

  • LesxDoubleStringCharacter LesxDoubleStringCharacters(可选)

LesxDoubleStringCharacter

  • SourceCharacter but not "

LesxSingleStringCharacters

  • LesxSingleStringCharacter LesxSingleStringCharacters(可选)

LesxSingleStringCharacter

  • SourceCharacter but not '

Children

LesxChildren

  • LesxChild LesxChildren(可选)

LesxChild

  • LesxText
  • LesxElement
  • { LesxChildExpression(可选) }

LesxText

  • LesxTextCharacter LesxText(可选)

LesxTextCharacter

  • SourceCharacter 但不是以下几种 {, <, > or }

LesxChildExpression

  • AssignmentExpression
  • ... AssignmentExpression

Whitespace 以及 Comments

Lesx使用跟ECMAScript相同的标点符号以及括号空白符, 换行符以及注释可以放在任意的标点符号之间。