UNPKG

escaya-codegen

Version:

lightweight and blazing fast JavaScript code generator from an EScaya-compliant AST

121 lines (98 loc) 4.09 kB
<h1 align="center">Escaya code generator</h1> <p align="center"> Lightweight and blazing fast JavaScript code generator from an EScaya-compliant AST. </p> <br> <p align="center"> <a href="https://lgtm.com/projects/g/escaya/escaya-codegen/context:javascript"><img src="https://img.shields.io/lgtm/grade/javascript/g/escaya/escaya-codegen.svg?logo=lgtm&logoWidth=18" alt="GitHub license" /></a> <a href="https://circleci.com/gh/escaya/escaya-codegen"><img src="https://circleci.com/gh/escaya/escaya-codegen.svg?style=svg" alt="Circle" /></a> <a href="https://github.com/escaya/escaya-codegen/blob/master/LICENSE.md"><img src="https://img.shields.io/github/license/escaya/escaya-codegen.svg" alt="License" /></a> </p> **WIP!** ## Features * Generates JavaScript code up to [ECMAScript® 2021](https://tc39.es/ecma262/index.html) * Generated output is 98 % identical to the input. * Supports code compression (*minification*) * Supports source map * Supports incremental code generation * Performance * Low memory usage ## API The `Escaya-codegen` generates JavaScript code up to [ECMAScript® 2021](https://tc39.es/ecma262/index.html), and it has been implemented into the [esbeautify](https://github.com/escaya/esbeautify) library and uses the [Escaya parser](https://github.com/escaya/escaya) to parse the source code before printing the result. The code can be generated incrementally while writing it, and the output will be the same as the input if the AST is correct. Only a semicolon - `;` - will be inserted as an replacement for line breaks if minified, and line breaks will be ignored if the generated input doesn't trigger an ASI failure. Code formatting changes can be done if you set the options for it. ### Options ```js export interface Options { // Specify the number of spaces per indentation-level tabWidth?: number; // Indent lines with tabs instead of spaces. tabs: boolean; // Print spaces between brackets in object literals bracketSpacing: boolean; // Print semicolons at the ends of statements semicolons?: boolean; // Remove unnecessary whitespace removeWhiteSpace?: boolean; // Define file endings ('lf', 'crlf', or 'cr') endOfLine?: string; // Removes unnecessary whitespace, semicolons and line endings minify?: boolean; // Include parentheses around a sole arrow function parameter arrowParens?: boolean; // Enable the ability to omit superfluous parentheses parentheses?: boolean; // Use single quotes instead of single quotes doubleQuotes?: boolean; // Force use of JSON.stringify json?: boolean; // Disallowed string escape disallowStringEscape?: boolean; } ``` ### Example usage: ```js import { generate } from 'escayaCodegen'; generate({ "type": "Script", "directives": [], "leafs": [ { "type": "ClassDeclaration", "name": { "type": "BindingIdentifier", "name": "x" }, "heritage": null, "elements": [ { "type": "ClassElement", "static": true, "method": { "type": "MethodDefinition", "async": false, "generator": false, "getter": false, "setter": true, "propertySetParameterList": { "type": "BindingIdentifier", "name": "y" }, "uniqueFormalParameters": [], "name": { "type": "IdentifierName", "name": "x" }, "contents": { "type": "FunctionBody", "directives": [], "leafs": [] } } } ] } ] }); produces the string 'class x { static set x(y) {} }'. ```