escaya-codegen
TypeScript icon, indicating that this package has built-in type declarations

0.0.15 • Public • Published

Escaya code generator

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


GitHub license Circle License

WIP!

Features

  • Generates JavaScript code up to ECMAScript® 2021
  • 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, and it has been implemented into the esbeautify library and uses the Escaya parser 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

export interface Options {
  // Specify the number of spaces per indentation-level
  tabWidth?: number;
  // Indent lines with tabs instead of spaces.
  tabsboolean;
  // Print spaces between brackets in object literals
  bracketSpacingboolean;
  // 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:

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) {} }'.
 

Dependents (0)

Package Sidebar

Install

npm i escaya-codegen

Weekly Downloads

4

Version

0.0.15

License

ISC

Unpacked Size

73.3 kB

Total Files

12

Last publish

Collaborators

  • escaya