Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
billyjanitsch committed Dec 12, 2016
0 parents commit 72c1f03
Show file tree
Hide file tree
Showing 14 changed files with 252 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2016 Kensho Technologies, Inc

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# eslint-config-kensho

This [eslint config](http://eslint.org/docs/developer-guide/shareable-configs) extends [Airbnb's config](https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb) (based on their [style guide](https://github.com/airbnb/javascript)).

Since the upstream config is well-maintained and justified, we try to stick as close to it as possible. All divergences are [annotated](index.js), and tend towards increased ES2016+ support and compatibility with our infrastructure rather than stylistic preferences.

## Usage

To add JS linting to a project, first install eslint and this config as development dependencies:

```sh
$ npm i -D eslint eslint-config-kensho
```

Add a [**.eslintrc.json**](http://eslint.org/docs/user-guide/configuring) which extends the config:

```json
{
"extends": "kensho"
}
```

Add a script in **package.json** to [run the linter](http://eslint.org/docs/user-guide/command-line-interface). Example:

```json
{
"scripts": {
"lint": "eslint src"
}
}
```

## Caveat

The eslint-plugin-X dependencies specified in [package.json](package.json) should actually be peer dependencies (see eslint/eslint#2518), and installed alongside eslint and this config. In practice, this is quite tedious, so we have instead specified them as dependencies and rely on npm@3's flattening to install them alongside the linter.

Note that this approach is technically incorrect, and if any plugin cannot be flattened this way (e.g. due to a conflicting version somewhere else in your dependency tree), **this config will break**. Since our projects use only this config, we feel that practicality outweighs absolute correctness in this case.
93 changes: 93 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
module.exports = {
extends: 'airbnb',
plugins: ['babel'],
parser: 'babel-eslint',
env: {
// allow browser globals
browser: true,
},
rules: {
// allow parens around arrow functions only when necessary (0 or >=2 args)
'arrow-parens': [2, 'as-needed'],

// require trailing commas in all contexts except function calls
'comma-dangle': [2, 'always-multiline'],

// allow require() calls
'global-require': 0,

// ensure that default, named, and namespaced imports have been exported by the target file
'import/default': 2,
'import/named': 2,
'import/namespace': 2,

// require all module imports to be declared as (dev-)dependencies in package.json
'import/no-extraneous-dependencies': [2, {devDependencies: true}],

// require import groups to be ordered by specificity and separated by linebreaks
'import/order': [2, {'newlines-between': 'always'}],

// allow files with a single named export (to allow in-progress util files)
'import/prefer-default-export': 0,

// allow non-ID-linked <label>s to accomodate those containing linked <input>s
'jsx-a11y/label-has-for': 0,

// require single quotes in JSX
'jsx-quotes': [2, 'prefer-single'],

// require multi-line operators to occur at the start of the line, except && and ||
'operator-linebreak': [2, 'before', {overrides: {'&&': 'after', '||': 'after'}}],

// allow supposedly-confusing arrows
'no-confusing-arrow': 0,

// do not allow more than one empty line anywhere
'no-multiple-empty-lines': [2, {max: 1, maxEOF: 1}],

// disallow confusing global browser variables
'no-restricted-globals': ['error', 'event', 'find'],

// allow variable shadowing, just for convenience
// TODO: enable?
'no-shadow': 0,

// allow dangling underscores in variable names (for "private" class methods)
'no-underscore-dangle': 0,

// disallow spaces in between curly braces
'babel/object-curly-spacing': [2, 'never'],
'object-curly-spacing': 0,

// disallow .jsx files for consistency
'react/jsx-filename-extension': 0,

// do not require a React import to use JSX (it's included automatically in our Babel preset)
'react/react-in-jsx-scope': 0,

// disallow semicolons
'semi': [2, 'never'],

// don't enforce JSX indentation, due to bugginess of the linter implementation :(
// TODO: https://github.com/yannickcr/eslint-plugin-react/issues/540
'react/jsx-indent': 0,
'react/jsx-indent-props': 0,
'react/jsx-closing-bracket-location': 0,

// TODO: enable when less buggy
'react/no-unused-prop-types': 0,

// allow event listeners on static elements (e.g. onClick on divs)
'jsx-a11y/no-static-element-interactions': 0,
},
settings: {
// do not attempt to parse npm modules or non-JS files for exports
'import/ignore': [
'node_modules',
'\.(png|svg|jpg|css|pdf)$'
],
'import/extensions': [
'.js'
],
},
}
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "eslint-config-kensho",
"version": "0.0.0",
"description": "Standard Kensho config to lint JS files.",
"main": "index.js",
"author": "Billy Janitsch <billyjanitsch@gmail.com> (http://billyjanitsch.com)",
"license": "MIT",
"repository": "kensho/eslint-config-kensho",
"scripts": {
"test": "ava"
},
"files": [
"index.js"
],
"dependencies": {
"babel-eslint": "^7.1.1",
"eslint-config-airbnb": "^13.0.0",
"eslint-plugin-babel": "^4.0.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^2.2.3",
"eslint-plugin-react": "^6.7.1"
},
"devDependencies": {
"ava": "^0.17.0",
"eslint": "^3.10.2"
}
}
1 change: 1 addition & 0 deletions test/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('..')
19 changes: 19 additions & 0 deletions test/fixtures/commas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const objGood = {
a: 1,
b: 2,
}

export const objBad = {
a: 1,
b: 2 // missing comma
}

export const arrGood = [
1,
2,
]

export const arrBad = [
1,
2 // missing comma
]
3 changes: 3 additions & 0 deletions test/fixtures/exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const foo = 5
// does not export bar
export default 6
4 changes: 4 additions & 0 deletions test/fixtures/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import x, {foo, bar} from './exports' // bar is not exported
import baz from './nonexistent' // file does not exist

export {foo, bar, baz, x}
4 changes: 4 additions & 0 deletions test/fixtures/linebreaks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const a = 1


export const b = 1 // extra newline
9 changes: 9 additions & 0 deletions test/fixtures/operator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const a = process.env.NODE_ENV === 'development' ? // ? ending line
10 : // : ending line
20

export const b = process.env.NODE_ENV === 'development'
&& 10 // && beginning line

export const c = process.env.NODE_ENV === 'development'
|| 10 // || beginning line
4 changes: 4 additions & 0 deletions test/fixtures/prop-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const MyComponent = props =>
<div>{props.myProp}</div> // missing prop type

export default MyComponent
10 changes: 10 additions & 0 deletions test/fixtures/variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const globalA = find([]) // confusing global var
export const globalB = implicitGlobal() // implicit global var

const unusedA = 1 // unused

export const functionA = (a) => a // unnecessary parens

export const functionB = a => { // unnecessary block
return a
}
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import test from 'ava'
import {CLIEngine} from 'eslint'

const cli = new CLIEngine()
const formatter = cli.getFormatter()

const invalid = async (t, input, count = 1) => {
const result = await cli.executeOnFiles([`${__dirname}/fixtures/${input}.js`])
t.is(result.errorCount, count, formatter(result.results))
t.is(result.warningCount, 0, formatter(result.results))
}

invalid.title = provided => `correctly lints issues with ${provided}`

test('operator placement', invalid, 'operator', 4)
test('line breaks', invalid, 'linebreaks')
test('imports', invalid, 'import', 3)
test('missing propTypes', invalid, 'prop-types')
test('variables', invalid, 'variables', 5)
test('commas', invalid, 'commas', 2)

0 comments on commit 72c1f03

Please sign in to comment.