# grater

> An ECMAScript parser

Latest version **0.0.1-alpha.1** (published 2021-06-04) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install grater
pnpm add grater
yarn add grater
bun add grater
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1-alpha.1 |
| Published | 2021-06-04 |
| First published | 2021-06-02 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=6.0.0 |
| Dependencies | 0 |
| Unpacked size | 783.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | NanYang |
| Maintainers | nanyang24 |

## Links

- npm: https://www.npmjs.com/package/grater
- Repository: https://github.com/nanyang24/grater
- Issues: https://github.com/nanyang24/grater/issues
- npm.io page: https://npm.io/package/grater

## Recent versions

- 0.0.1-alpha.1 (latest) — 2021-06-04
- 0.0.1-alpha.0 (init) — 2021-06-02

## README

<h1 align="center">Grater</h1>

<p align="center"><img width="60%" src="https://user-images.githubusercontent.com/17287124/120586137-8cecc400-c465-11eb-9557-7124f2eb628d.png"/></p>


Grater is an [ECMAScript](https://tc39.es/ecma262/index.html) abstract syntax trees (AST) Parser, according to [The ESTree Spec](https://github.com/estree/estree)


> What is an Abstract syntax tree (AST)?
> 
> Abstract syntax trees are [data structures](https://en.wikipedia.org/wiki/Data_structure) widely used in [compilers](https://en.wikipedia.org/wiki/Compiler), due to their property of representing the structure of program code. An AST is usually the result of the [syntax analysis](https://en.wikipedia.org/wiki/Parsing) phase of a compiler.


🚧  **Work in progress**

It's too early to release the source files to the public, but get a taste of it. [Here](https://nanyang24.github.io/Grater)

## 📝 Feature To-Do List
 - ES5
    - RegularExpressionLiteral
    - HoistableDeclaration
    - LabelStatement
 - ES6/ES2015 & More ES Feature
 - Scope
 - JSX Expressions
 - More test cases covered & running [Test262](https://github.com/tc39/test262)
 - Support TC39 proposals via options

## 💡 What the Grater can be used for?
1. Parse Javascript
2. Syntax checker
3. Lint
4. Convert the syntax of different ECMA versions
5. Global API for performing AST operations and tree walkers

## Install

```bash
# yarn
yarn add grater

# npm
npm install grater
```

## Usage

```js
import { parseScript } from 'grater';

parseScript(`

Array.prototype.customReduce = function (fn, initialValue) {
  // fn(acc, cur, ?index, ?originArray)

  var sourceArray = this;
  var acc, currentIndex

  if(initialValue) {
    acc = initialValue
    currentIndex = 0
  } else {
    acc = sourceArray[0]
    currentIndex = 1
  }

  while(currentIndex < sourceArray.length) {
    var currentValue = sourceArray[currentIndex]
    acc = fn(acc, currentValue, currentIndex, sourceArray)
    currentIndex ++
  }

  return acc;
};

`);

```

AST in JSON

```js
{
  "type": "Program",
  "sourceType": "script",
  "body": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "AssignmentExpression",
        "left": {
          "type": "MemberExpression",
          "object": {
            "type": "MemberExpression",
            "object": {
              "type": "Identifier",
              "name": "Array"
            },
            "computed": false,
            "property": {
              "type": "Identifier",
              "name": "prototype"
            }
          },
          "computed": false,
          "property": {
            "type": "Identifier",
            "name": "customReduce"
          }
        },
        "operator": "=",
        "right": {
          "type": "FunctionExpression",
          "id": null,
          "params": [
            {
              "type": "Identifier",
              "name": "fn"
            },
            {
              "type": "Identifier",
              "name": "initialValue"
            }
          ],
          "body": {
            "type": "BlockStatement",
            "body": [
              {
                "type": "VariableDeclaration",
                "kind": "var",
                "declarations": [
                  {
                    "type": "VariableDeclarator",
                    "id": {
                      "type": "Identifier",
                      "name": "sourceArray"
                    },
                    "init": {
                      "type": "ThisExpression"
                    }
                  }
                ]
              },
              {
                "type": "VariableDeclaration",
                "kind": "var",
                "declarations": [
                  {
                    "type": "VariableDeclarator",
                    "id": {
                      "type": "Identifier",
                      "name": "acc"
                    },
                    "init": null
                  },
                  {
                    "type": "VariableDeclarator",
                    "id": {
                      "type": "Identifier",
                      "name": "currentIndex"
                    },
                    "init": null
                  }
                ]
              },
              {
                "type": "IfStatement",
                "test": {
                  "type": "Identifier",
                  "name": "initialValue"
                },
                "consequent": {
                  "type": "BlockStatement",
                  "body": [
                    {
                      "type": "ExpressionStatement",
                      "expression": {
                        "type": "AssignmentExpression",
                        "left": {
                          "type": "Identifier",
                          "name": "acc"
                        },
                        "operator": "=",
                        "right": {
                          "type": "Identifier",
                          "name": "initialValue"
                        }
                      }
                    },
                    {
                      "type": "ExpressionStatement",
                      "expression": {
                        "type": "AssignmentExpression",
                        "left": {
                          "type": "Identifier",
                          "name": "currentIndex"
                        },
                        "operator": "=",
                        "right": {
                          "type": "Literal",
                          "value": 0
                        }
                      }
                    }
                  ]
                },
                "alternate": {
                  "type": "BlockStatement",
                  "body": [
                    {
                      "type": "ExpressionStatement",
                      "expression": {
                        "type": "AssignmentExpression",
                        "left": {
                          "type": "Identifier",
                          "name": "acc"
                        },
                        "operator": "=",
                        "right": {
                          "type": "MemberExpression",
                          "object": {
                            "type": "Identifier",
                            "name": "sourceArray"
                          },
                          "computed": true,
                          "property": {
                            "type": "Literal",
                            "value": 0
                          }
                        }
                      }
                    },
                    {
                      "type": "ExpressionStatement",
                      "expression": {
                        "type": "AssignmentExpression",
                        "left": {
                          "type": "Identifier",
                          "name": "currentIndex"
                        },
                        "operator": "=",
                        "right": {
                          "type": "Literal",
                          "value": 1
                        }
                      }
                    }
                  ]
                }
              },
              {
                "type": "WhileStatement",
                "test": {
                  "type": "BinaryExpression",
                  "left": {
                    "type": "Identifier",
                    "name": "currentIndex"
                  },
                  "right": {
                    "type": "MemberExpression",
                    "object": {
                      "type": "Identifier",
                      "name": "sourceArray"
                    },
                    "computed": false,
                    "property": {
                      "type": "Identifier",
                      "name": "length"
                    }
                  },
                  "operator": "<"
                },
                "body": {
                  "type": "BlockStatement",
                  "body": [
                    {
                      "type": "VariableDeclaration",
                      "kind": "var",
                      "declarations": [
                        {
                          "type": "VariableDeclarator",
                          "id": {
                            "type": "Identifier",
                            "name": "currentValue"
                          },
                          "init": {
                            "type": "MemberExpression",
                            "object": {
                              "type": "Identifier",
                              "name": "sourceArray"
                            },
                            "computed": true,
                            "property": {
                              "type": "Identifier",
                              "name": "currentIndex"
                            }
                          }
                        }
                      ]
                    },
                    {
                      "type": "ExpressionStatement",
                      "expression": {
                        "type": "AssignmentExpression",
                        "left": {
                          "type": "Identifier",
                          "name": "acc"
                        },
                        "operator": "=",
                        "right": {
                          "type": "CallExpression",
                          "callee": {
                            "type": "Identifier",
                            "name": "fn"
                          },
                          "arguments": [
                            {
                              "type": "Identifier",
                              "name": "acc"
                            },
                            {
                              "type": "Identifier",
                              "name": "currentValue"
                            },
                            {
                              "type": "Identifier",
                              "name": "currentIndex"
                            },
                            {
                              "type": "Identifier",
                              "name": "sourceArray"
                            }
                          ]
                        }
                      }
                    },
                    {
                      "type": "ExpressionStatement",
                      "expression": {
                        "type": "UpdateExpression",
                        "argument": {
                          "type": "Identifier",
                          "name": "currentIndex"
                        },
                        "operator": "++",
                        "prefix": false
                      }
                    }
                  ]
                }
              },
              {
                "type": "ReturnStatement",
                "argument": {
                  "type": "Identifier",
                  "name": "acc"
                }
              }
            ]
          },
          "async": false,
          "generator": false
        }
      }
    }
  ]
}
```


## License
[Apache License 2.0](https://github.com/nanyang24/Grater/blob/master/LICENSE)

---
_Source: https://npm.io/package/grater · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
