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.

Preventing chained calls on multiple lines doesn't work

See original GitHub issue
  • prettier-eslint-cli version: 4.4.0
  • prettier version: 1.7.4
  • eslint version: 4.8.0

Relevant code/config.

.eslintrc.json

{
  "env": {
    "browser": true,
    "commonjs": true,
    "es6": true,
    "jasmine": true,
    "node": true
  },
  "extends": [
    "eslint:recommended"
  ],
  "parserOptions": {
    "ecmaFeatures": {
      "experimentalObjectRestSpread": true,
      "jsx": true
    },
    "sourceType": "module"
  },
  "plugins": [
    "prettier",
    "react"
  ],
  "rules": {
    "brace-style": [
      "error",
      "1tbs"
    ],
    "comma-dangle": [
      "error",
      "always-multiline"
    ],
    "comma-spacing": [
      "error",
      {
        "after": true,
        "before": false
      }
    ],
    "comma-style": [
      "error",
      "last"
    ],
    "complexity": "error",
    "newline-per-chained-call": [
      "error",
      {
        "ignoreChainWithDepth": 10
      }
    ]
  }
}

.prettierrc.json

{
  "bracketSpacing": false,
  "jsxBracketSameLine": false,
  "printWidth": 120,
  "requirePragma": false,
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false
}

What you did:

I would like to stop Prettier from turning this:

exports.up = (knex, Promise) => {
  return Promise.all([
    knex.schema.alterTable('categories', table => {
      table.timestamp('created_at').notNullable().defaultTo('2013-12-31 12:17:10.0');
      table.timestamp('updated_at').notNullable().defaultTo('2013-12-31 12:17:10.0');
    }),
  ]);
};

Into:

exports.up = (knex, Promise) => {
  return Promise.all([
    knex.schema.alterTable('categories', table => {
      table
        .timestamp('created_at')
        .notNullable()
        .defaultTo('2013-12-31 12:17:10.0');
      table
        .timestamp('updated_at')
        .notNullable()
        .defaultTo('2013-12-31 12:17:10.0');
    }),
  ]);
};

Therefore I defined “newline-per-chained-call” in my ESLint config and as far as I understood, Prettier runs before ESLint, so my ESLint should override Prettier’s output but it doesn’t seem to happen.

What happened / Problem:

Prettier is still breaking up my chained method calls.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
beklievcommented, Jun 3, 2019

I’m having this issue too! And I’m using eslint. And cause of that I do not enable Prettier’s editor.formatOnSave option in VSCode.

In some old projects I’m using jquery code with so much chains so I wanna turn off this behavior in prettier but can’t. How to turn it off?

4reactions
zimmecommented, Jun 28, 2019

The way to solve this, at the moment, is to not use prettier and only use eslint as it’s prettier that breaks up the chained calls.

There’s an open issue about fixing this behaviour in prettier.

Today, eslint, doesn’t seem to be smart enough to fix the output from prettier when it comes to chained calls and there isn’t an option in prettier to change how it outputs chained calls.

I’m closing this as it’s out of our hands.

Edit: Here’s the link to the open issue. https://github.com/prettier/prettier/issues/3107

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chained method calls indentation style in Python [duplicate]
This is a case where a line continuation character is preferred to open parentheses. ShortName.objects.distinct() \ .filter().values() ...
Read more >
Chained Method Calls Not Aligning Properly when ... - YouTrack
It appears that the "Align when multiline" setting has stopped working for chained method calls (unless I messed up my code style settings)....
Read more >
Method chains with line breaks in Python - nkmk note
Method chaining is not a special syntax, as it is just repeating the process of calling a method directly from the return value....
Read more >
Ruby Style Guide
Adopt a consistent multi-line method chaining style. There are two popular styles in the Ruby community, both of which are considered good -...
Read more >
Method chaining in multiple lines - Google Groups
I am struggling with making my code work in a way that multiple methods called in ... Period should be at the end...
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