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

Weird behavior in presence of template literal #1157

Closed
qwelias opened this issue Aug 26, 2016 · 6 comments
Closed

Weird behavior in presence of template literal #1157

qwelias opened this issue Aug 26, 2016 · 6 comments

Comments

@qwelias
Copy link
Contributor

qwelias commented Aug 26, 2016

Weird behavior in presence of template literal.

const composeUrl = ( host, api, key, data ) => {
        switch ( api ) {
            case 'Init':
                return `${host}/vwapi/Init?VWID=${key}&DATA=${encodeURIComponent(
            Object.keys(data).map((k) => `${k}=${data[k]}`).join(';')
        )}`;
        case 'Pay':
            return `${host}/vwapi/Pay?SessionId=${par}`;
    };
};

The beautified code should have looked like:

const composeUrl = ( host, api, key, data ) => {
    switch ( api ) {
        case 'Init':
            return `${ host }/vwapi/Init?VWID=${ key }&DATA=${ encodeURIComponent(
                Object.keys( data ).map( ( k ) => `${ k }=${ data[ k ] }` ).join( ';' )
            ) }`;
        case 'Pay':
            return `${ host}/vwapi/Pay?SessionId=${ par }`;
    };
};

Here is a link to the debug.md Gist: https://gist.github.com/anonymous/d9766518dc9dceb14ea07fa3ee5c9a11

@prettydiff
Copy link
Collaborator

Just as an FYI only the whitespace not contained by the escape delimiters ${ and } must be preserved. The contents of the escape delimiters should be beautified as normal JavaScript.

@qwelias
Copy link
Contributor Author

qwelias commented Aug 29, 2016

There's actually two things:

  • extra indentation fore some reason at switch (it appears to happen only with JS Beautify and first template literal in place)
  • not beautified contents of ${ } (applies for JS Beautify and Prettydiff)

@prettydiff
Copy link
Collaborator

Pretty Diff appears to be beautifying code within the escape delimiters. If you mean to have the extra spaces padding the ( and ) or { and } characters try Pretty Diff's bracepadding option.

@qwelias
Copy link
Contributor Author

qwelias commented Aug 29, 2016

Looks like PrettyDiff's spaces in paren not supported in atom-beautify

And if I use Pretty Diff as default beautifier it does this

const composeUrl = (host, api, key, data) => {
    const path = `${host}/vwapi/${api}`;
    switch (api) {
        case 'Init':
            return `${path}?VWID=${key}&DATA=${encodeURIComponent(Object.keys(data).map((k) => `${k}=${data[k]}`).join(';'))}`;
        case 'Pay':
            return `${path}?SessionId=${data}`;
    };
};

All \n's and indentation inside template literals removed.

@prettydiff
Copy link
Collaborator

When I look at the original code sample I see two new lines in the template literal:

  • The first before Object.keys
  • The second after .join(';')

The first one is clearly within a template literal escape (immediately before encodeURIComponent) so there is no reason to preserve this white space as a part of the template literal unless there is a beautification rule about preserving as part of the escape.

The second one is in the same escape as the first one, but it is a bit more interesting because it follows a nested template literal.

@Glavin001
Copy link
Owner

#1193 has been merged and published.


Published to v0.29.12

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

No branches or pull requests

3 participants