prettier: Hang formatting nested functions

Hey, I just found that prettier hangs when formatting this:

someObject.someFunction().then(function () {
    return someObject.someFunction().then(function () {
        return someObject.someFunction().then(function () {
            return someObject.someFunction().then(function () {
                return someObject.someFunction().then(function () {
                    return someObject.someFunction().then(function () {
                        return someObject.someFunction().then(function () {
                            return someObject.someFunction().then(function () {
                                return someObject.someFunction().then(function () {
                                    anotherFunction();
                                });
                            });
                        });
                    });
                });
            });
        });
    });
});

(I’m embarrassed to have this piece of code in a codebase, it’s because of a limitation of a third-party library 😄)

If I remove two of the innermost nested calls, prettier does end and format correctly (it takes a while, though). So it seems that it’s not a hang but rather just taking exponentially forever when dealing with this (messed up) code.

I’m running prettier 1.0.2 on macOS 10.12.4 through the CLI: prettier file.js

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 17 (13 by maintainers)

Commits related to this issue

Most upvoted comments

it’s not a bug for me. that code is not serious and is totally ridiculous. you shoul separate it into separate functions and can be banned with 2 eslint rules (im calling it no nested hell)

@tunnckoCore prettier doesn’t make assumptions in how you actually write your program, it is going to format it. The problem here is that there is some exponential aspect inside of prettier that triggers here, so we should ensure that it doesn’t happen.

@vjeux @jlongster thank you for being so receptive, responsive, and making prettier great!

(One day I’d ❤️ to have prettier format my HTML — following its two core principles: parse and re-print, acknowledge maximum line length — as well 😃).