Giter Club home page Giter Club logo

eslint-config-google's Introduction

eslint-config-google Build Status

ESLint shareable config for the Google JavaScript style guide (ES2015+ version)

Installation

$ npm install --save-dev eslint eslint-config-google

Usage

Once the eslint-config-google package is installed, you can use it by specifying google in the extends section of your ESLint configuration.

{
  "extends": "google",
  "rules": {
    // Additional, per-project rules...
  }
}

Using the google config with eslint:recommended

There are several rules in the eslint:recommended ruleset that Google style is not opinionated about that you might want to enforce in your project.

To use Google style in conjunction with ESLint's recommended rule set, extend them both, making sure to list google last:

{
  "extends": ["eslint:recommended", "google"],
  "rules": {
    // Additional, per-project rules...
  }
}

To see how the google config compares with eslint:recommended, refer to the source code of index.js, which lists every ESLint rule along with whether (and how) it is enforced by the google config.

License

Apache-2 ยฉ Google

eslint-config-google's People

Contributors

addyosmani avatar appsforartists avatar chrisjshull avatar floftar avatar justinhelmer avatar mathiasbynens avatar nchavez324 avatar ntwb avatar philipwalton avatar posquit0 avatar pragmaticivan avatar shikchen avatar sindresorhus avatar stramel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eslint-config-google's Issues

eslint-index

I have created an ESLint utility module that I thought you might find useful.

The module is called eslint-index and you can read the full documentation on npm.

eslint-index provides a great deal of functionality including:

  • List all available rules declared by ESLint and any plugins you have included
  • Colour code rules depending on their status:
    • omitted (not declared anywhere in you ESLint config file)
    • 0|off (declared, but set to 0|off)
    • 1|warn
    • 2|error
  • Display links to the rule documentation page next to each rule
  • Filter/reject rules by their status and/or their group
    • status is as described above (omitted|off|warn|error)
    • group is eslint for the core ESLint rules or the name of any of your plugins like react|import|flowtype
  • Format the output as a number or a table to get an overview of your rule settings
  • Rules that have been marked as deprecated are removed from all outputs
  • All of the above filters and formatting can be combined, for example:
    • filter omitted and off rules, output them as a list and display the rule doc links alongside
    • filter eslint rules and display the rule setting counters in a table

I wrote this plugin to aid the development of my own ESLint config settings and found it incredibly useful for keeping track of everything. I hope you find this module useful and please do let me know if you have any ideas on how to improve it.

Work together on eslint-config-defaults

Hello! I wanted to propose a collaboration between this repo and https://github.com/walmartlabs/eslint-config-defaults.

I originally set out to build config-defaults because I was tired of copy pasting my eslint config around and trying to keep them up to date across lots of projects. Eslint's extends key fixes the distribution problem but there are still two issues:

  1. Managing the eslint config file is a pain in the ass even though it is centralized. New rules are added, removed, moved and deprecated all the time and organization is terrible.
  2. Just getting up and running for a new project is not straightforward anymore and I strongly believe that code quality tooling should be well suited to those who are just starting out as they're often the ones who need it most after all.

So with this project I centralized a few configs from major players, I committed to staying on top of the latest rules (and deprecations) and most importantly I made the config composable which allows them to be easily mixed and matched!


It's obvious that you all saw the same problems and it has been enormously helpful to a lot of people but I want to take your config one step further and I am asking for your help. With eslint-config-defaults I want to try to centralize the config from a few thought leaders around the web so that folks have fast, standard and up to date options from the start.

I've been maintaining a few "unofficial" configs for a while now but it's duplicate effort and my hope is to try to collaborate/consolidate. A while back I actually painstakingly went through the old Google Style Guide and converted it to ESLint as best I could. For my part, I am committed to staying on top of the latest rules as they change. I should mention that you all have quite a few missing rules already :).

Potential Objections(?):

  • We need quick access to the repo to make changes. - I'm happy to try to get your maintainers added to the project.
  • The project is under walmartlabs and I'm not too sure I like that. - It's possible we could spin this out into it's own org. No promises, I'd have to talk to legal and such about what we can do but we may be able to find a way.
  • This project is an important part of our OSS portfolio that we're not going to shut down. - You could easily take config-defaults as a dependency and keep this exactly how it is.

Anyway, I wanted to start a conversation and see if we can make it work for both of us.

@addyosmani @sindresorhus

Plan for replacing valid-jsdoc and require-jsdoc

Eslint rules for valid-jsdoc and require-jsdoc are deprecated.

The require-jsdoc and valid-jsdoc rules will be deprecated. These two rules will remain in ESLint but we will no longer add new features or fix bugs for them. These rules may be removed in a future major release of ESLint.
Desired behavior

The official documentation recommends using eslint-plugin-jsdoc for this purpose.

If you would like to continue checking JSDoc comments using ESLint, we suggest using the community-supported eslint-plugin-jsdoc plugin. This plugin contains several rules that are more specific than the core ESLint rules being deprecated and may actually be a better fit for most projects.

Both of these deprecated rules are still in use with this style guide. I am hoping that we can have a change in this project to use alternatives instead of these two rules, or just remove these rules. It is possible to specify the eslint-plugin-jsdoc as part of this configuration.

I have also observed that this project currently does not have any plugins and this might be an intentional decision to keep things simple. Do you plan removing these two rules from this configuration in future?

indent under goog.scope

The google guide says:

Similar to C++ namespaces, do not indent under goog.scope declarations. Instead, continue from the 0 column.

It's a bit similar to the newly introduced outerIIFEBody option of the indent rule.
Since the option is now being improved: eslint/eslint#6585
I'm wondering if it's needed (planned to use) by eslint-config-google?

Does this config support for eslint 6.1.0 (lastest version)?

I was update Eslint to 6.1.0 version
Screen Shot 2019-07-31 at 4 28 48 PM

my .eslintrc.js in $HOME dir, work fine at Eslint 5.0.0 version

module.exports = {
  env: {
    browser: true,
    commonjs: true,
    es6: true,
    node: true,
  },
  parser: 'babel-eslint',
  plugins: ['react'],
  extends: [
    'eslint:recommended',
    'google',
    'plugin:react/recommended',
    'prefer-object-spread',
  ],
  globals: {
    wx: null,
    App: null,
    Page: null,
    getApp: null,
    Component: null,
  },
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 2017,
    sourceType: 'module',
  },
  rules: {
    'prefer-object-spread/prefer-object-spread': 2,
  }
};

Minor issues in index.js

A few minor issues in index.js:

(1) Line 36: What is no-condition option/ I couldn't find it from eslint rules documentation.

(2) ESLint 3.8.0 has a new rule func-name-matching, you may wan to include it between lines 191 and 192 for completeness.

(3) Line 218: there is a typo in the inline comments: enfore should be enforce.

(4) I am surprised that Google's coding style guide doesn't recommend using === (instead of ==) and !== (instead of !=) operators. Any special reason?

Thanks.

Issues with eslint usage

I've pretty much always had trouble with extending eslint configs so this could easily just be me.

When attempting to use this config (either in eslintrc or in package.json I was getting this error:

$ eslint ./src 1 โ†ต
/home/matt/.npm-packages/lib/node_modules/eslint/lib/config/config-file.js:332
throw e;
^

Error: Cannot read config package: eslint-config-google
Error: Cannot find module 'eslint-config-google'
Referenced from: /home/matt/Projects/Code/sw-precache/app-shell-demo/package.json
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at loadPackage (/home/matt/.npm-packages/lib/node_modules/eslint/lib/config/config-file.js:168:16)
at loadConfigFile (/home/matt/.npm-packages/lib/node_modules/eslint/lib/config/config-file.js:212:18)
at load (/home/matt/.npm-packages/lib/node_modules/eslint/lib/config/config-file.js:385:18)
at /home/matt/.npm-packages/lib/node_modules/eslint/lib/config/config-file.js:326:36
at Array.reduceRight (native)
at applyExtends (/home/matt/.npm-packages/lib/node_modules/eslint/lib/config/config-file.js:309:28)

When I use the gulp task that uses eslint there is no issue. Could it be a problem with attempting to use global eslint?

cc @addyosmani @sindresorhus

Throwing Cannot read property 'range' of null

Node: 8.0.0
npm: 5.0.1
babel-eslint: 7.2.3
eslint: 3.19.0
eslint-config-google: 0.8.0

Just create a new js file and paste contents below

async function run() {
}
run();

It throws this error

TypeError: Cannot read property 'range' of null
    at TokenStore.getTokenBefore (/Users/erayh/Yazilim/js/uniqorm/node_modules/eslint/lib/token-store/index.js:318:17)
    at EventEmitter.checkFunction (/Users/erayh/Yazilim/js/uniqorm/node_modules/eslint/lib/rules/generator-star-spacing.js:131:42)
    at emitOne (events.js:120:20)
    at EventEmitter.emit (events.js:210:7)
    at NodeEventGenerator.applySelector (/Users/erayh/Yazilim/js/uniqorm/node_modules/eslint/lib/util/node-event-generator.js:265:26)
    at NodeEventGenerator.applySelectors (/Users/erayh/Yazilim/js/uniqorm/node_modules/eslint/lib/util/node-event-generator.js:294:22)
    at NodeEventGenerator.enterNode (/Users/erayh/Yazilim/js/uniqorm/node_modules/eslint/lib/util/node-event-generator.js:308:14)
    at CodePathAnalyzer.enterNode (/Users/erayh/Yazilim/js/uniqorm/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:602:23)
    at CommentEventGenerator.enterNode (/Users/erayh/Yazilim/js/uniqorm/node_modules/eslint/lib/util/comment-event-generator.js:98:23)
    at Traverser.enter (/Users/erayh/Yazilim/js/uniqorm/node_modules/eslint/lib/eslint.js:929:36)

Make it compatible with typescript-eslint

Make eslint-config-google rules compatible with the TSLint replacement: typescript-eslint.

This will be valuable for TypeScript projects that follow the Google JavaScript Style Guide, such as Angular.

Indentation in line-wrapped function arguments

According to the Google JavaScript Style:

When possible, all function arguments should be listed on the same line. If doing so would exceed the 80-column limit, the arguments must be line-wrapped in a readable way. To save space, you may wrap as close to 80 as possible, or put each argument on its own line to enhance readability. The indentation may be either four spaces, or aligned to the parenthesis.

This ESLint configuration (version 0.5.0) does not enforce that indentation. No warning is throw when I use less or more indentation in line-wrapped function arguments. By comparison the Closure Linter does this. It also throws indentation warnings on line-wrapped variable initializations and method chaining, which this ESLint configuration also does not enforce.

prefer-const should be enabled?

It seems like it isn't enabled and eslint:recommended doesn't enable it either.

However, in the style guide here:

Declare all local variables with either const or let. Use const by default, unless a variable needs to be reassigned. The var keyword must not be used.

So surely this means it should be an error to use let for a variable which is not reassigned?

edit:

seems this is fixed by #49 if it gets merged.

[error] Definition for rule 'switch-colon-spacing' was not found

I am using eslint-config-google, for my styles with eslint and I keep getting an error for all the files I am linting
1:1 error Definition for rule 'switch-colon-spacing' was not found

My .eslintrc this:

{
  "parser": "babel-eslint",
  "parserOptions": {
    "sourceType": "module",
    "allowImportExportEverywhere": false,
    "codeFrame": false,
    "ecmaFeatures": {
      "jsx": true,
      "modules": true,
      "spread" : true,
      "restParams" : true
    }
  },
  "extends": ["eslint:recommended", "plugin:react/recommended", "google"],
  "globals": {
  	"require": true,
  	"module": true
  },
  "rules": {
    "strict": 1,
    "comma-spacing":1,
    "no-console":0,
    "indent": ["error", 2],
    "switch-colon-spacing": [
      "error", 
      {
        "after": true, 
        "before": false
      }
    ]
  },
  "env":{
    "es6": true,        // We are writing ES6 code
    "browser": true,    // for the browser
    "commonjs": true    // and use require() for stylesheets
  },
  "plugins": [
    "react"
  ]
}

I am unable to see what's wrong here

Reflect latest eslint-config-xo

Extended xo config doesn't download the latest version of the main eslint-config-xo repo. Still includes deprecated rules in eslint ^2.0.0 Maybe it's not compatible? package.json reads "eslint-config-xo": "^0.7.0"

Support ESLint 8.x

ESLint v8.0.0 is released ๐ŸŽ‰

It would be awesome to have official ESLint 8 support. ๐Ÿ‘Š
I'm happy to help where I can of course ๐Ÿ™‚

ESLint with Google style warning on properly formatted code

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)

Error: Cannot find module 'eslint-config-xo'

/home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/config/config-file.js:398
throw e;
^

Error: Cannot find module 'eslint-config-xo'
Referenced from: google
Referenced from: /home/shav/IdeaProjects/ds-orders-web/.eslintrc.js
at Object.ModuleResolver.resolve (/home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/util/module-resolver.js:74:19)
at resolve (/home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/config/config-file.js:475:33)
at load (/home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/config/config-file.js:492:24)
at /home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/config/config-file.js:388:36
at Array.reduceRight (native)
at applyExtends (/home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/config/config-file.js:365:28)
at load (/home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/config/config-file.js:527:22)
at /home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/config/config-file.js:388:36
at Array.reduceRight (native)
at applyExtends (/home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/config/config-file.js:365:28)
at Object.load (/home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/config/config-file.js:527:22)
at loadConfig (/home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/config.js:67:33)
at getLocalConfig (/home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/config.js:129:23)
at Config.getConfig (/home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/config.js:230:22)
at processText (/home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/cli-engine.js:149:27)
at CLIEngine.executeOnText (/home/shav/IdeaProjects/ds-orders-web/node_modules/ds-web-gulp/node_modules/gulp-eslint/node_modules/eslint/lib/cli-engine.js:666:26)

Process finished with exit code 1

shav@shav-OptiPlex-3020:/IdeaProjects/ds-orders-web$ node -v
v4.4.1
shav@shav-OptiPlex-3020:
/IdeaProjects/ds-orders-web$ npm -v
2.14.20

Strongly Recommend Extending 'eslint:recommended'

Google eslint rules don't catch typos:

function foo(wasModified) {
  bar(wasModifed); // <- Typo!
}

So extending eslint:recommended is not just a recommendation like "Google is not opinionated blablabla", it is the best practice and strong recommendation.

Using const variable declaration shows as invalid

According to:
5.1.1
we should be using const by default.

But when I eslint against:

'use strict';
const stream = require('stream');

It outputs:

/Users/vanalsti/Projects/examvis-eclass/examvis/eslint-test.js
2:1 error Parsing error: The keyword 'const' is reserved

โœ– 1 problem (1 error, 0 warnings)

More prominent note that this is for the ES6 style guide?

Most searches for the Google JS style guide seem to point to this one: https://google.github.io/styleguide/javascriptguide.xml - but really this config is for the ES6 style guide here: https://google.github.io/styleguide/jsguide.html

It's not obvious from the readme which of the two style guides this config is for, and they disagree on some key issues (e.g. 'var' vs 'let' and 'const'). It'd be a good idea to clarify this.

I know there's a link at the top, but just rewording that to clarify it's the ES6 style guide would help since users who find the style guide first and then try to find a linter for it would otherwise be greatly surprised and are unlikely to check the link's target.

Missing rule to ensure file extension in module imports

According to 3.4.1.1.1 File extensions in import paths, "the .js file extension is not optional in import paths and must always be included."

I guess this rule shouldn't be limited to '.js' but to all imported modules. Nevertheless, eslint-config-google should be able to enforce 3.4.1.1.1.

Currently, I'm using import/extension from eslint-plugin-import with this config:

rules: {
   // ...
   "import/extensions": ["error", "always"],
  // ...
}

In order to enforce 3.4.1.1.1 one-to-one, "import/extensions": ["error", { js: "always" }] is the corresponding configuration.

I suggest to include (copy?) import/extension into eslint-config-google.

dot-location rule should be configured as ['error', 'object']

The Google Style Guide states that for dot operators in multiline expressions, the dot operator should be on the preceding line:

var x = foo.bar().
    doSomething().
    doSomethingElse();

However, xo configures the dot-location rule as:

'dot-location': ['error', 'property']

which mandates that the dot operator be on the following line. This should be overriden by eslint-config-google as follows:

'dot-location': ['error', 'object']

Remove dependency on xo

xo is much more opinionated than our Style Guide.

I'm using this eslint config to write Google code, and I keep running into xo's opinions on things that are either undefined or encouraged by our internal guide. One of these days, I'm going to just read our internal guide and write a proper eslint config for it that doesn't inherit from anybody else's. That gives us immunity, both now and as both configs evolve over time.

Rule 3.4.2.3 Mutability of exports

This is probably the wrong place to discuss the Style Guide. But are you Googlers really applying rule "3.4.2.3 Mutability of exports", or is this a rule from days gone by that is no longer lived? If it's still applied: What is the purpose of this rule?

are tabs allowed?

Google JavaScript Style Guide says:

2.3.1 Whitespace characters
...
2. Tab characters are not used for indentation.

but rule indent works incorrectly. As a result, I can write like this:
image
And it will not cause an error.

Update to eslint v3

eslint v3 was released a couple of months ago. It uses eslint-config-google as one of the predefined configurations, so it needs a compatible package for people to use.

Override some rules

Hi everyone!

How can I overide rules that i do not want in my JS project?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.