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

Funky formatting when using template strings as arguments #2551

Open
pluma opened this issue Jul 27, 2017 · 2 comments
Open

Funky formatting when using template strings as arguments #2551

pluma opened this issue Jul 27, 2017 · 2 comments

Comments

@pluma
Copy link

pluma commented Jul 27, 2017

At ArangoDB (Apache 2.0 licensed NoSQL multi-model database with an embedded V8 environment) we're using an aql template tag to support queries without the risk of injection attacks (think SQL).

This means we have plenty of code that looks like this:

const result = db._query(aql`
  FOR x IN 1..5
  RETURN x
`).toArray();

However prettier seems to think that's unreadable and instead formats it as this:

const result = db
  ._query(
    aql`
  FOR x IN 1..5
  RETURN x
`
  )
  .toArray();

Curiously this seems to be triggered by the final .toArray(). Without it, prettier plays nice:

const result = db._query(aql`
  FOR x IN 1..5
  RETURN x
`);

The minimal test case seems to be this:

x.y(`
`).z();

Observations:

  • x.y needs to be a method, using a function (x(...).z();) doesn't trigger the behaviour
  • z() needs to be a method call, using a property access (x.y(...).z;) also doesn't trigger it (though something like x.y(...).z.a() triggers it again, as does x.y(...).z().a)
  • the template string needs to contain at least one linebreak
  • the length and content of the template string is otherwise irrelevant

So it seems to be doing the right thing UNLESS there's a second chained method call, even if the method takes no arguments.

@pluma
Copy link
Author

pluma commented Aug 16, 2017

Is there any more information I can provide?

Is this formatting intentional?

I'd submit a PR but I have no idea where to start. I'm not even sure how to read the test cases.

@azz
Copy link
Member

azz commented Aug 16, 2017

I think it is the intended (or at least expected) behaviour for the template string to be indented.

If you want to have a crack at inlining it (tagged template expessions and raw template literals), that would be awesome.

The tests inputs are tests/*/*.js and snapshots contain the input , ~~~, and output.

There's more info in CONTRIBUTING

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