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.

Weird Indentation with chained calls

See original GitHub issue

Prettier 1.15.3 Playground link

--parser babylon

Input:

$.ajax({
    url: 'https://random.com'
}).done(function (data) {
    console.log('Done');
}).fail(function () {
    console.log('Error');
});

Output:

$.ajax({
  url: "https://random.com"
})
  .done(function(data) {
    console.log("Done");
  })
  .fail(function() {
    console.log("Error");
  });

Expected behavior:

$.ajax({
    url: 'https://random.com'
}).done(function (data) {
    console.log('Done');
}).fail(function () {
    console.log('Error');
});

If I remove the fail from the chained call list then prettier will format it like this -

Prettier 1.15.3 Playground link

--parser babylon

Input:

$.ajax({
    url: 'https://random.com'
}).done(function (data) {
    console.log('Done');
});

Output:

$.ajax({
  url: "https://random.com"
}).done(function(data) {
  console.log("Done");
});

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
duailibecommented, Jan 11, 2019

@bodrick Because with 2+ chains you’re most likely to want something similar to:

a
  .step(() => {
    //
  })
  .step(() => {
    //
  })
  .step(() => {
    //
  });

which is subjectively better than:

a.step(() => {
  //
}).step(() => {
  //
}).step(() => {
  //
});

We had to choose an heuristic and that was it.

As for the OP, I personally prefer the Prettier formatting than the your expected.

2reactions
pstantoncommented, May 1, 2019

agree to disagree, but for me double indentation is better than inconsistent indentation.

ie you have double indentation for all of the other functions…

Read more comments on GitHub >

github_iconTop Results From Across the Web

in chained calls causes strange formatting and indentation #230
seems to break the chain somehow. Parts of the chain may be merged onto the same line, while other parts are indented too...
Read more >
Auto-indent lines feature wrongly indents multiline chained ...
PY-33832 Incorrect indentation when chaining method calls within parentheses ... PyCharm seems to make some weird decisions, ruining the visual structure:
Read more >
c# - Indent chained method calls so they all start at the same ...
Consider the following code formatting, taken from this answer. var query = grades.GroupBy(student => student.Name) .Select(group => new ...
Read more >
bug#32496: 27.0.50; Strange indentation when ruby-align-chained ...
From: Bozhidar Batsov. Subject: bug#32496: 27.0.50; Strange indentation when ruby-align-chained-calls is t. Date: Wed, 01 Sep 2021 13:02:56 +0300.
Read more >
Indent Chained Methods - Unibeautify
Indent chained method calls. Configuration option for beautifiers JS-Beautify, ESLint and languages EJS, JSX, JavaScript.
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