Skip to content

Long line-length require statements are awkwardly formatted. #303

Closed

Description

@adrianmcli

It seems that there's no concept of keeping require statements and its params as one unit. It's not very often that you'll get an extremely long string to pass in to the require() parens. There should be a preference for keeping the require('./something') as one unit.

If I run prettier on this:

const { one, two, thee, four, five, six, seven, eight, nine, ten } = require('./my-utils');

I get this:

const { one, two, thee, four, five, six, seven, eight, nine, ten } = require(
  "./my-utils"
);

When it should be:

const { one, two, thee, four, five, six, seven, eight, nine, ten }
  = require('./my-utils');

or:

const { 
  one, 
  two, 
  thee, 
  four, 
  five, 
  six, 
  seven, 
  eight, 
  nine, 
  ten } = require('./my-utils');

Activity

  1. added
    status:needs discussion
    on Jan 18, 2017
  2. vjeux commented on Jan 18, 2017

    @vjeux
    Contributor

    This is tricky

    const { one, two, thee, four, five, six, seven, eight, nine, ten } = require('./my-utils');
    const { one, two, thee, four, five, six, seven, eight, nine, ten, eleven } = require('./my-utils');

    outputs

    const { one, two, thee, four, five, six, seven, eight, nine, ten } = require(
      "./my-utils"
    );
    const {
      one,
      two,
      thee,
      four,
      five,
      six,
      seven,
      eight,
      nine,
      ten,
      eleven
    } = require("./my-utils");

    It is unclear if we should prioritize breaking on the first part of the expression or on the second one.

    If I were to throw an heuristic in this case, it would be to break in the place where there's the biggest amount of code first.

    https://jlongster.github.io/prettier/#%7B%22content%22%3A%22const%20%7B%20one%2C%20two%2C%20thee%2C%20four%2C%20five%2C%20six%2C%20seven%2C%20eight%2C%20nine%2C%20ten%20%7D%20%3D%20require('.%2Fmy-utils')%3B%5Cnconst%20%7B%20one%2C%20two%2C%20thee%2C%20four%2C%20five%2C%20six%2C%20seven%2C%20eight%2C%20nine%2C%20ten%2C%20eleven%20%7D%20%3D%20require('.%2Fmy-utils')%3B%5Cn%22%2C%22options%22%3A%7B%22printWidth%22%3A80%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Afalse%2C%22trailingComma%22%3Afalse%2C%22bracketSpacing%22%3Atrue%7D%7D

  3. jlongster commented on Jan 18, 2017

    @jlongster
    Member

    I'd say this is the expected behavior for "normal" expressions. Trying to figure out which one is bigger could possibly done, but we need to be really careful how many combinations of styles we try to detect before performance becomes a serious issue.

    Also, I'd prefer for the format to be as consistent as possible. The above case, even if it's slightly awkward in rare cases, is consistent.

    What we could do is specialize the require pattern specifically. This is what I talked about in Respecting Patterns. It's a very common pattern to destructure a large amount of imported properties, so we could specifically break it differently.

  4. jlongster commented on Jan 18, 2017

    @jlongster
    Member

    Although, I'm actually a little confused. The destructuring param exists before the assigned expression... It should break first no matter what. Did we specifically break the right expression first?

  5. added 2 commits that reference this issue on Mar 1, 2017
    79749e9
    8d85aed
  6. vjeux commented on Mar 1, 2017

    @vjeux
    Contributor

    #841 fixes it.

  7. adrianmcli commented on Mar 1, 2017

    @adrianmcli
    Author

    Awesome! Can't wait to see it merged in.

  8. added a commit that references this issue on Mar 3, 2017
    705ac7d
  9. added
    locked-due-to-inactivity
    on Jul 8, 2018
  10. locked as resolved and limited conversation to collaborators on Jul 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    locked-due-to-inactivitystatus:needs discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions