Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

ESLint with Google style warning on properly formatted code #58

Open
janakdr opened this issue Aug 9, 2019 · 5 comments
Open

ESLint with Google style warning on properly formatted code #58

janakdr opened this issue Aug 9, 2019 · 5 comments

Comments

@janakdr
Copy link

janakdr commented Aug 9, 2019

The following code is unchanged by clang-format --style=Google:

/**
 * @param {number} param1
 * @param {number} param2
 * @return {number}
 */
function myFunction(param1, param2) {
  const aVeryLongArgumentThatDoesNotFitAsAParameter = 0;
  const resultOfCallingFunction =
      myFunction(myFunction(
                    aVeryLongArgumentThatDoesNotFitAsAParameter,
                    aVeryLongArgumentThatDoesNotFitAsAParameter))
          .value;
  return resultOfCallingFunction;
}

myFunction(0, 0);

However, running it with ESLint gives the errors:

test.js
  10:1  error  Expected indentation of 10 spaces but found 20  indent
  11:1  error  Expected indentation of 10 spaces but found 20  indent

ESLint apparently wants the lines above to be:

    myFunction(myFunction(
        aVeryLongArgumentThatDoesNotFitAsAParameter,
        aVeryLongArgumentThatDoesNotFitAsAParameter))

This seems inconsistent with clang-format's concept of ContinuationIndentWidth. Is there a workaround for this? Or is my setup wrong? The contents of my .eslintrc are:

module.exports = {
  'env': {
    'browser': true,
    'es6': true,
  },
  'extends': [
    'google',
  ],
  'globals': {
    'Atomics': 'readonly',
    'SharedArrayBuffer': 'readonly',
  },
  'parserOptions': {
    'ecmaVersion': 2018,
    'sourceType': 'module',
  },
  'rules': {
  },
};

(I was directed here from eslint/eslint#12083)

@janakdr
Copy link
Author

janakdr commented Aug 9, 2019

In particular, eslint's error here appears to violate the acceptable styles listed in https://google.github.io/styleguide/javascriptguide.xml#Code_formatting ("Function arguments' section).

@philipwalton
Copy link
Contributor

Yeah, there are a lot of cases where code that's valid according to Google style will show errors with this ESLint config. It's a trade-off between being overly strict and way too lax.

PR #50 add indentation rules based on the suggestion in #12 (comment). Previously there was no indentation enforcement (so your code wouldn't error), but that also meant someone using tabs wouldn't get an error either. This felt like a good compromise.

You can turn off the indent rule if you're already using clang-format. Unfortunately I don't think there's a set of ESLint config options that perfectly matches Google style for indentation.

@janakdr
Copy link
Author

janakdr commented Aug 10, 2019

Currently using:

    'indent': ['error', 2, {'CallExpression': {'arguments': 2}, 'ignoredNodes': ['CallExpression > CallExpression', 'CallExpression > MemberExpression']}]

which works for our code for now. I'm a little surprised that I need to put the 'CallExpression': {'arguments': 2} section in, though.

@Luca-Terrazzan
Copy link

Same issue here, it seems that google ESlint is requiring a 4 space indention when you go to a newline for a function parameter...really weird.

@ghost
Copy link

ghost commented Jan 10, 2021

You can fix it using:
rules: {
'indent': 'off',
}
It will turn off indention and it will help you though

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants