escaya-codegen
v0.0.15
Published
lightweight and blazing fast JavaScript code generator from an EScaya-compliant AST
Downloads
2,752
Maintainers
Readme
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.
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:
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) {} }'.