Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adds padding parameter to statements. #166

Open
wants to merge 1 commit into from

2 participants

@jameskeane

This change extends the Parser AST with an extra padding parameter that inserts newlines into the generated code.

Usage:

{
    type: 'Statement...',
    // will add 1 newline above, and 2 below this statement.
    padding: {top: 1, bottom: 2}
}
James Keane Adds padding parameter to statements so that nice output code can be
generated.

Usage:
{
    type: 'Statement...',
    padding: {top: 1, bottom: 2} // will add 1 newline above, and 2
                                 // below this statement.
}
5c29044
@michaelficarra

Firstly, we need to establish a use case for this. Then we can determine if this is the approach we'd want to take. Then we'll need you to provide tests. Then we can review this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 29, 2014
  1. Adds padding parameter to statements so that nice output code can be

    James Keane authored
    generated.
    
    Usage:
    {
        type: 'Statement...',
        padding: {top: 1, bottom: 2} // will add 1 newline above, and 2
                                     // below this statement.
    }
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 0 deletions.
  1. +24 −0 escodegen.js
View
24 escodegen.js
@@ -175,6 +175,7 @@
base: null,
parse: null,
comment: false,
+ padding: false,
format: {
indent: {
style: ' ',
@@ -700,6 +701,24 @@
return result;
}
+ function addPadding(stmt, result) {
+ if (stmt.padding) {
+ var save = result;
+ result = [];
+
+ if (stmt.padding.top) {
+ result.push((new Array(stmt.padding.top+1)).join(newline));
+ }
+
+ result.push(addIndent(save));
+
+ if (stmt.padding.bottom) {
+ result.push((new Array(stmt.padding.bottom+1)).join(newline));
+ }
+ }
+ return result;
+ }
+
function parenthesize(text, current, should) {
if (current < should) {
return ['(', text, ')'];
@@ -1981,6 +2000,11 @@
result = addCommentsToStatement(stmt, result);
}
+ // Attach newlines
+ if (extra.padding) {
+ result = addPadding(stmt, result);
+ }
+
fragment = toSourceNodeWhenNeeded(result).toString();
if (stmt.type === Syntax.Program && !safeConcatenation && newline === '' && fragment.charAt(fragment.length - 1) === '\n') {
result = sourceMap ? toSourceNodeWhenNeeded(result).replaceRight(/\s+$/, '') : fragment.replace(/\s+$/, '');
Something went wrong with that request. Please try again.