Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TemplateString transform ruins indentation of multi-line strings #88

Open
nene opened this issue Feb 5, 2016 · 1 comment
Open

TemplateString transform ruins indentation of multi-line strings #88

nene opened this issue Feb 5, 2016 · 1 comment
Labels

Comments

@nene
Copy link
Collaborator

nene commented Feb 5, 2016

When a string concatenation is used to split string to multiple lines for better readability like so:

var msgBox = $(
     "  <div class='" + cssClass + "'> " +
     "    <a class='icon-close popup-close-button' title='Close'></a> " +
     "    <div class='alertContent'> " +
     "      <h1 class='title'>&nbsp;</h1> " +
     "      <div class='message'>&nbsp;</div> " +
     "    </div> " +
     "  </div> "
);

then Lebab replaces it with an ugly template string:

var msgBox = $(
    `  <div class='${cssClass}'>     <a class='icon-close popup-close-button' title='Close'></a>     <div class='alertContent'>       <h1 class='title'>&nbsp;</h1>       <div class='message'>&nbsp;</div>     </div>   </div> `
);

...completely ruining the nice original indentation.

What should happen instead?

In theory it would be nice to replace this with something like:

var msgBox = $(`
   <div class='${cssClass}'>
     <a class='icon-close popup-close-button' title='Close'></a>
     <div class='alertContent'>
       <h1 class='title'>&nbsp;</h1>
       <div class='message'>&nbsp;</div>
     </div>
   </div>
`);

but we can't risk adding newlines to string, as that might change the meaning of the code. Even when the original string did contain newlines at the end of each line, we'll risk ruining the indentation, as we also can't just add arbitrary spaces to the string. A result could look quite ugly:

    function foo() {
        if (true) {
            var msgBox = $(
`   <div class='${cssClass}'>
     <a class='icon-close popup-close-button' title='Close'></a>
     <div class='alertContent'>
       <h1 class='title'>&nbsp;</h1>
       <div class='message'>&nbsp;</div>
     </div>
   </div>`);
            someFn();
        }
    }

I guess the only really safe way is to avoid the conversion of such multi-line concatenation altogether.

@nene nene changed the title TemplateString transform ruins multi-line strings TemplateString transform ruins indentation of multi-line strings Feb 5, 2016
@nene nene added the bug label Feb 28, 2016
nene added a commit that referenced this issue Apr 3, 2016
If one just concatenates strings, it's likely for a reason
like formatting it nicely on multiple lines.  Converting that
to template string will likely just mess things up.

Refs #88
@grumd
Copy link

grumd commented Jan 5, 2017

not a bad solution would be to replace this

var msgBox = $(
     "  <div class='" + cssClass + "'> " +
     "    <a class='icon-close popup-close-button' title='Close'></a> " +
     "    <div class='alertContent'> " +
     "      <h1 class='title'>&nbsp;</h1> " +
     "      <div class='message'>&nbsp;</div> " +
     "    </div> " +
     "  </div> "
);

with this

var msgBox = $(
     `  <div class='${cssClass}'> ` +
     `    <a class='icon-close popup-close-button' title='Close'></a> ` +
     `    <div class='alertContent'> ` +
     `      <h1 class='title'>&nbsp;</h1> ` +
     `      <div class='message'>&nbsp;</div> ` +
     `    </div> ` +
     `  </div> `
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants