google / eslint-config-google

ESLint shareable config for the Google JavaScript style guide
Apache License 2.0
1.74k stars 140 forks source link

ESLint with Google style warning on properly formatted code #58

Open janakdr opened 5 years ago

janakdr commented 5 years ago

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 https://github.com/eslint/eslint/issues/12083)

janakdr commented 5 years ago

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 commented 5 years ago

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 https://github.com/google/eslint-config-google/issues/12#issuecomment-352134238. 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 commented 5 years ago

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 commented 5 years ago

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 commented 3 years ago

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