question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

preserveBlankLines does not work.

See original GitHub issue
var ast = esprima.parse(code, {
    loc: true,
    range: true,
    comment: true,
    tokens: true
});

gen.attachComments(ast, ast.comments, ast.tokens);

var output = gen.generate(ast, {
    format: {
        compact: false,
        preserveBlankLines: true
    },
    comment: true,
    sourceMap: 'script.js',
    sourceMapWithCode: true
});

Disappointingly, blank line is removed from code.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
dtothefpcommented, Apr 7, 2016

any additional info on this, would be nice to preserve newlines and seems like a simple feature that doesn’t work?

1reaction
ghostcommented, Feb 7, 2018

The way I preserved blank lines was like this:

const whitespaceMarker = "// {Guid goes here}"
let programLines = program.split('\n');
let replacedProgramLines = programLines.map(line => {
    // If the line is blank or nothing but whitespace, return the whitespace marker as a comment.
    if (line.length === 0 || /^\s+$/.test(line)) {
        return whitespaceMarker;
    }

    return line;
});

let replacedProgram = replacedProgramLines.join('\n');   
let syntax = esprima.parse(replacedProgram, {
    raw: true,
    tokens: true,
    range: true,
    comment: true
}); 
escodegen.attachComments(syntax, syntax.comments, syntax.tokens);

// ...

let output = escodegen.generate(syntax, {
    comment: true
});
let replacedOutput = output.split(whitespaceMarker).join('');

This mostly works. I noticed today that sometimes a newline is omitted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

using for /f to preserve blank lines - TechNet - Microsoft
I use the command below, which works but it doesn't save the empty lines. What do I need to add to preserve the...
Read more >
Why is it so hard to preserve blank lines? - MobileRead Forums
The purists will insist you use margins to create a blank line and although they are correct, your reading program need to honor...
Read more >
Ghostscript textwriter preserve blank lines - pdf - Stack Overflow
Typically, a PDF has no blank lines. Even the notion of a 'line' is fairly broad - the specifications allow for a sequence...
Read more >
ChangeLog - mandoc
This fixes a wide range of formatting issues: * Print named escapes inside ... "Exp", lines: +68 -26 Preserve blank lines inside literal...
Read more >
Getting Started - djLint
--quiet Do not print diff when reformatting. ... --preserve-blank-lines Attempt to preserve blank lines. ... Running with a path -
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found