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

Beautifier breaks ES6 nested template strings #797

Closed
GuilhermeReda opened this issue Nov 4, 2015 · 14 comments
Closed

Beautifier breaks ES6 nested template strings #797

GuilhermeReda opened this issue Nov 4, 2015 · 14 comments

Comments

@GuilhermeReda
Copy link

Hi, I have a file with the following code.

`SELECT
  nextval('${this.options.schema ? `${this.options.schema}.` : ''}"${this.tableName}_${this.autoIncrementField}_seq"'::regclass
) nextval;`

When I format the file, that line becomes

`SELECT
  nextval('${this.options.schema ? `
  $ {
    this.options.schema
  }.
  ` : ''}"${this.tableName}_${this.autoIncrementField}_seq"'::regclass
) nextval;`

it is putting a space between $ and {, and that breaks my code.

@GuilhermeReda GuilhermeReda changed the title Preserver ${ on ES6 Preserve ${ on ES6 Nov 4, 2015
@IgorNovozhilov
Copy link

+1
I have similar problems.
Before:

      return `${err.name}${err.message?`: ${err.message}`:''}\n${stackLines}`;

After:

      return `${err.name}${err.message?`: $ {
         err.message
      }
      `:''}\n${stackLines}`;

@bitwiseman
Copy link
Member

Please point to documentation of ${ syntax.

@GuilhermeReda
Copy link
Author

@bitwiseman
Copy link
Member

@GuilhermeReda
Your original example is not valid javascript. The ${ notation must occur inside backtick pairs.

// Note the "`" after the first "?" 
`SELECT
  nextval('${this.options.schema ? `${this.options.schema}.` : ''}"${this.tableName}_${this.autoIncrementField}_seq"'::regclass
) nextval;`

I can't tell exactly what you want to do, but this is closer:

`SELECT
  nextval('${this.options.schema ? this.options.schema + '.' : ''} "${this.tableName}_${this.autoIncrementField}_seq"'::regclass
) nextval;`

@bitwiseman
Copy link
Member

@INovozhilov
I think you also need to review your code:
Yours:

      return `${err.name}${err.message?`: ${err.message}`:''}\n${stackLines}`;

What I think you meant:

    return `${err.name}${err.message ? err.message : ''}\n${stackLines}`;

@IgorNovozhilov
Copy link

No, @bitwiseman, my code is correct.
I used a nested string template:

`: ${err.message}`

in template:

`${err.name}${err.message?...<mySubTemplate>...:''}\n${stackLines}`

The ES6 specification is not prohibited to use nested templates describing them in a block of ${...}.
The same @GuilhermeReda shows in its example SQL.

@IgorNovozhilov
Copy link

Here is a simple example of correct template:

var a = '1';
var b = `a is ${ `${ a }` }`;
console.log(b);

output:

a is 1

@GuilhermeReda
Copy link
Author

Take a look at https://hacks.mozilla.org/2015/05/es6-in-depth-template-strings-2/

"The code in a template substitution can be any JavaScript expression, so function calls, arithmetic, and so on are allowed. (If you really want to, you can even nest a template string inside another template string, which I call template inception.)"

@bitwiseman bitwiseman changed the title Preserve ${ on ES6 Beautifier breaks ES6 nested template strings Nov 18, 2015
@bitwiseman bitwiseman added this to the v1.6.0 milestone Nov 18, 2015
@bitwiseman
Copy link
Member

Every time I think I've seen it all. Okay, I see what would be needed. Recursive Template string parsing. Considering this breaks the code in this case, I'll prioritize it. In the meanwhile, you'll need to use an annotation to disable beautification of that section.

@aleung
Copy link

aleung commented Dec 1, 2015

Can I ask how to "use an annotation to disable beautification of that section"? I can't find the answer from readme.

@bitwiseman
Copy link
Member

@midnight-wonderer
Copy link

I have another problem related to template string.
If js-beautify encounter some code like this.

let testTable = function() {
    return `CREATE TABLE IF NOT EXISTS some_table (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
label VARCHAR(64) NOT NULL,
PRIMARY KEY (id)
) ENGINE=Aria DEFAULT CHARSET ascii;`;
};

It will add extra linebreak between return and the template which make the function return undefined.

@bitwiseman
Copy link
Member

@MidnightWonderer -
"I have another problem" - that is indeed another problem. Please open a separate issue for it. 😄
In that new issue, please include the input, expected output (as you've done), and the actual output.

@IgorNovozhilov
Copy link

👍 good job

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

5 participants