https://github.com/hsiaosiyuan0/mole
Mole is a collection of parsers to parse the frontend stuffs.
https://github.com/hsiaosiyuan0/mole
ecmascript go lint parser
Last synced: 5 months ago
JSON representation
Mole is a collection of parsers to parse the frontend stuffs.
- Host: GitHub
- URL: https://github.com/hsiaosiyuan0/mole
- Owner: hsiaosiyuan0
- License: mit
- Created: 2021-08-10T13:29:03.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-10-31T07:11:45.000Z (over 3 years ago)
- Last Synced: 2024-06-21T05:02:46.526Z (almost 2 years ago)
- Topics: ecmascript, go, lint, parser
- Language: Go
- Homepage:
- Size: 4.87 MB
- Stars: 24
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Mole
Mole is a collection of parsers to parse the frontend stuffs.
Why Golang
~~A little bit explanation is good for why Golang is preferred in this project. Nowadays, a programming language is not only the grammar things, it's consist of runtime, stdlib, 3rd-party modules and a healthy community, all these are out-of-box by using Golang, more specifically:~~
- ~~Golang is productive, its simplicity philosophy(something like Grammar and Garbage-collection) saves more time to the functionalities themselves.~~
- ~~the functionalities like lint and bundle maybe needed to run as web services while Golang has been proved by many impressive projects such k8s that it's good at service things.~~
Fine, just all because I'm too fool to use a fancy language
## Features
- JavaScript Parser
- ECMAScript up to [ES2021](https://262.ecma-international.org/12.0/)
- [JSX](https://github.com/facebook/jsx)
- [ESTree](https://github.com/estree/estree) compatible outputs ([AST explorer on WASM](http://blog.thehardways.me/mole-is-more/#/))
- TypeScript Parser
- Type information retained
- [babel/typescript](https://babeljs.io/docs/en/babel-types#typescript) compatible outputs
### WIP
- [ ] CSS parser
- [ ] Less parser
- [ ] Scss parser
## Using the parser
For using the parser in Mole, the first step is using `go get` to add Mole as your project's dependency
```bash
go get github.com/hsiaosiyuan0/mole
# or using go get via a proxy if you have some network issues
https_proxy=127.0.0.1:1080 go get github.com/hsiaosiyuan0/mole
```
After the installation is succeeded, below code can be used as a demo:
Click to expand the demo
```go
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"github.com/hsiaosiyuan0/mole/ecma/estree"
"github.com/hsiaosiyuan0/mole/ecma/parser"
"github.com/hsiaosiyuan0/mole/span"
)
func main() {
// imitate the source code you want to parse
code := `console.log("hello world")`
// create a Source instance to handle to the source code
s := span.NewSource("", code)
// create a parser, here we use the default options
opts := parser.NewParserOpts()
p := parser.NewParser(s, opts)
// inform the parser do its parsing process
ast, err := p.Prog()
if err != nil {
log.Fatal(err)
}
// by default the parsed AST is not the ESTree form because the latter has a little redundancy,
// however Mole supports to convert its AST to ESTree by using the `estree.ConvertProg` function
b, err := json.Marshal(estree.ConvertProg(ast.(*parser.Prog), estree.NewConvertCtx(p)))
if err != nil {
log.Fatal(err)
}
// below is nothing new, we just print the ESTree in JSON form
var out bytes.Buffer
json.Indent(&out, b, "", " ")
fmt.Println(out.String())
}
```
The produced AST can be consumed by the ast-walker in Mole, more runnable demos see [mole-demo](https://github.com/hsiaosiyuan0/mole-demo)
## Development
See [dev.md](/docs/dev.md) to get more information about how to start development.