Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Commit

Permalink
Update linter with support for prettier, easier installation, new con…
Browse files Browse the repository at this point in the history
…figs

This new linter config integrates Prettier with ESLint and relies on ESLint's `--fix` option instead of running Prettier separately. ESLint delegates to Prettier, so `// prettier-ignore` and the `.prettierignore` file still work in addition to ESLint's inline-comment options.

There are four linter configs: default, native, web, node. You usually want to use one of the latter three. All three include support for React and Prettier.

This also bumps the line length to 100.

Going to publish this as 6.0.0-rc.0 and using it in a few projects.

Test Plan: Added Jest tests that (a) verify the configs are properly formatted, (b) can run on files as expected, and (c) don't have rules that overlap with Prettier's. (c) is a subjective choice -- it means that if you tell Prettier to ignore some code, ESLint won't enforce some stylistic rules that Prettier normally would take care of -- sometimes that's what you want and sometimes it isn't so I picked the option that gets out of your hair. Also some ESLint rules that support `--fix` ended up duplicating Prettier's work and would add two semicolons or two commas in some places.

fbshipit-source-id: cdda43f
  • Loading branch information
ide authored and expbot committed Aug 8, 2017
1 parent 8bc3070 commit ba2b782
Show file tree
Hide file tree
Showing 27 changed files with 1,051 additions and 349 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
extends: './react.js',
extends: './node.js',
};
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Shared ESLint configs for Expo's JS.
yarn add --dev eslint-config-expo
```

You will also need to install `eslint`, `babel-eslint`, `eslint-plugin-babel`, `eslint-plugin-import`, and `eslint-plugin-react` (if you want to lint React and JSX):
You will also need to install `eslint` and `prettier`:

```sh
yarn add --dev eslint babel-eslint eslint-plugin-babel eslint-plugin-import eslint-plugin-react
yarn add --dev eslint prettier
```

## Usage
Expand All @@ -33,16 +33,26 @@ module.exports = {
};
```

## React and JSX Support
## Support for Different Platforms

There are two configs: one for JavaScript and one for React. The React configuration extends the JavaScript one and adds support and linter rules for JSX.
There are several configs for different platforms. They are:
* `expo`: the basic config for JavaScript projects for which there isn't a more specific config
* `expo/native`: the config for React Native projects, including Expo projects, with support for React and JSX
* `expo/web`: the config for code that runs in web browsers, with support for React and JSX
* `expo/node`: the config for code that runs in Node

For an Expo project, your configuration might look like this:

###
```js
"eslintConfig": {
"extends": "expo"
// or
"extends": "expo/react"
"extends": "expo/native"
}
```

You also can extend multiple configs, which is useful for projects that span several platforms:

```js
"eslintConfig": {
"extends": ["expo/node", "expo/web"]
}
```
48 changes: 48 additions & 0 deletions __tests__/__snapshots__/default-test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`fixtures/all-00.js 1`] = `
Object {
"errorCount": 0,
"filePath": "/Users/ide/universe/libraries/eslint-config-expo/__tests__/fixtures/all-00.js",
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"output": "import 'hi';
import a from 'a';
import { b } from 'b';
import c, { c1 } from 'c';
import * as d from 'd';
import e from './e';
@d
export class Example {
static s = {
a,
b,
};
m = {};
hi() {
_fn();
}
async byeAsync() {
_fn(a, b, c, c1, d, e);
}
}
function _fn() {}
",
"warningCount": 0,
}
`;

exports[`message 1`] = `
"No rules that are unnecessary or conflict with Prettier were found.
"
`;

exports[`success 1`] = `true`;
160 changes: 160 additions & 0 deletions __tests__/__snapshots__/native-test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`fixtures/all-00.js 1`] = `
Object {
"errorCount": 0,
"filePath": "/Users/ide/universe/libraries/eslint-config-expo/__tests__/fixtures/all-00.js",
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"output": "import 'hi';
import a from 'a';
import { b } from 'b';
import c, { c1 } from 'c';
import * as d from 'd';
import e from './e';
@d
export class Example {
static s = {
a,
b,
};
m = {};
hi() {
_fn();
}
async byeAsync() {
_fn(a, b, c, c1, d, e);
}
}
function _fn() {}
",
"warningCount": 0,
}
`;

exports[`fixtures/web-native-00.js 1`] = `
Object {
"errorCount": 0,
"filePath": "/Users/ide/universe/libraries/eslint-config-expo/__tests__/fixtures/web-native-00.js",
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"warningCount": 0,
}
`;

exports[`fixtures/web-native-01.js 1`] = `
Object {
"errorCount": 0,
"filePath": "/Users/ide/universe/libraries/eslint-config-expo/__tests__/fixtures/web-native-01.js",
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [
Object {
"column": 3,
"endColumn": 22,
"endLine": 4,
"line": 4,
"message": "props should be placed after render",
"nodeType": "ClassProperty",
"ruleId": "react/sort-comp",
"severity": 1,
"source": " props = { x: 'x' };",
},
Object {
"column": 5,
"endColumn": 19,
"endLine": 7,
"line": 7,
"message": "Unexpected alert.",
"nodeType": "CallExpression",
"ruleId": "no-alert",
"severity": 1,
"source": " alert('uh oh');",
},
Object {
"column": 5,
"endColumn": 18,
"endLine": 8,
"line": 8,
"message": "Do not use setState in componentDidMount",
"nodeType": "MemberExpression",
"ruleId": "react/no-did-mount-set-state",
"severity": 1,
"source": " this.setState({});",
},
],
"output": "import React from 'react';
export default class Example extends React.Component {
props = { x: 'x' };
componentDidMount() {
alert('uh oh');
this.setState({});
}
render() {
return (
<div>
{this.props.x}
</div>
);
}
}
",
"warningCount": 3,
}
`;

exports[`fixtures/web-native-02.js 1`] = `
Object {
"errorCount": 0,
"filePath": "/Users/ide/universe/libraries/eslint-config-expo/__tests__/fixtures/web-native-02.js",
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [
Object {
"column": 3,
"endColumn": 25,
"endLine": 10,
"line": 10,
"message": "componentDidMount should be placed before componentDidUpdate",
"nodeType": "MethodDefinition",
"ruleId": "react/sort-comp",
"severity": 1,
"source": " componentDidMount() {}",
},
],
"source": "import React from 'react';
export default class Example extends React.Component {
componentDidUpdate() {}
render() {
return null;
}
componentDidMount() {}
static getInitialProps() {}
}
",
"warningCount": 1,
}
`;

exports[`message 1`] = `
"No rules that are unnecessary or conflict with Prettier were found.
"
`;

exports[`success 1`] = `true`;
48 changes: 48 additions & 0 deletions __tests__/__snapshots__/node-test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`fixtures/all-00.js 1`] = `
Object {
"errorCount": 0,
"filePath": "/Users/ide/universe/libraries/eslint-config-expo/__tests__/fixtures/all-00.js",
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"output": "import 'hi';
import a from 'a';
import { b } from 'b';
import c, { c1 } from 'c';
import * as d from 'd';
import e from './e';
@d
export class Example {
static s = {
a,
b,
};
m = {};
hi() {
_fn();
}
async byeAsync() {
_fn(a, b, c, c1, d, e);
}
}
function _fn() {}
",
"warningCount": 0,
}
`;

exports[`message 1`] = `
"No rules that are unnecessary or conflict with Prettier were found.
"
`;

exports[`success 1`] = `true`;
Loading

0 comments on commit ba2b782

Please sign in to comment.