Skip to content

Commit 3f0abdb

Browse files
authored
Add ESLint and Prettier (#21)
Closes #5.
1 parent 2e59ba1 commit 3f0abdb

File tree

10 files changed

+1267
-97
lines changed

10 files changed

+1267
-97
lines changed

.circleci/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ jobs:
2626
paths:
2727
- ~/.cache/yarn
2828

29+
- run: yarn lint
30+
2931
- run: yarn test
3032

3133
- run: yarn build

.eslintrc.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
'use strict';
2+
3+
const restrictedGlobals = require('confusing-browser-globals');
4+
5+
const OFF = 'off';
6+
const ERROR = 'error';
7+
8+
// Files that are transformed and can use ES6/JSX.
9+
const esNextPaths = [
10+
// Internal forwarding modules
11+
'./index.js',
12+
// Source files
13+
'src/**/*.js',
14+
// Jest
15+
'scripts/jest/setupTests.js',
16+
];
17+
18+
// Files that we distribute on npm that should be ES5-only.
19+
const es5Paths = ['npm/**/*.js'];
20+
21+
module.exports = {
22+
env: {
23+
browser: true,
24+
es6: true,
25+
node: true,
26+
},
27+
extends: [
28+
'eslint:recommended',
29+
'plugin:react/recommended',
30+
'plugin:prettier/recommended',
31+
'prettier/react',
32+
],
33+
globals: {
34+
Atomics: 'readonly',
35+
SharedArrayBuffer: 'readonly',
36+
},
37+
parser: 'babel-eslint',
38+
parserOptions: {
39+
ecmaVersion: 2018,
40+
sourceType: 'script',
41+
},
42+
plugins: ['react'],
43+
settings: {
44+
react: {
45+
version: 'detect',
46+
},
47+
},
48+
rules: {
49+
'no-console': ERROR,
50+
'no-empty': OFF,
51+
'no-restricted-globals': [ERROR, ...restrictedGlobals],
52+
'no-unsafe-finally': OFF,
53+
'no-unused-vars': [ERROR, {args: 'none'}],
54+
'no-useless-escape': OFF,
55+
56+
// We apply these settings to files that should run on Node.
57+
// They can't use JSX or ES6 modules, and must be in strict mode.
58+
// They can, however, use other ES6 features.
59+
// (Note these rules are overridden later for source files.)
60+
'no-var': ERROR,
61+
strict: ERROR,
62+
},
63+
overrides: [
64+
{
65+
// We apply these settings to files that we ship through npm.
66+
// They must be ES5.
67+
files: es5Paths,
68+
parser: 'espree',
69+
parserOptions: {
70+
ecmaVersion: 5,
71+
sourceType: 'script',
72+
},
73+
rules: {
74+
'no-var': OFF,
75+
strict: ERROR,
76+
},
77+
overrides: [
78+
{
79+
// These files are ES5 but with ESM support.
80+
files: ['npm/esm/**/*.js'],
81+
parserOptions: {
82+
// Although this is supposed to be 5, ESLint doesn't allow sourceType 'module' when ecmaVersion < 2015.
83+
// See https://github.com/eslint/eslint/issues/9687#issuecomment-508448526
84+
ecmaVersion: 2015,
85+
sourceType: 'module',
86+
},
87+
},
88+
],
89+
},
90+
{
91+
// We apply these settings to the source files that get compiled.
92+
// They can use all features including JSX (but shouldn't use `var`).
93+
files: esNextPaths,
94+
parserOptions: {
95+
ecmaVersion: 2018,
96+
sourceType: 'module',
97+
},
98+
rules: {
99+
'no-var': ERROR,
100+
strict: OFF,
101+
},
102+
},
103+
{
104+
// Rollup understands ESM
105+
files: ['rollup.config.js'],
106+
parserOptions: {
107+
ecmaVersion: 2018,
108+
sourceType: 'module',
109+
},
110+
},
111+
{
112+
files: ['**/__tests__/**/*.js', 'scripts/jest/setupTests.js'],
113+
env: {
114+
'jest/globals': true,
115+
},
116+
plugins: ['jest'],
117+
rules: {
118+
// https://github.com/jest-community/eslint-plugin-jest
119+
'jest/no-focused-tests': ERROR,
120+
'jest/valid-expect': ERROR,
121+
'jest/valid-expect-in-promise': ERROR,
122+
123+
// React & JSX
124+
// This isn't useful in our test code
125+
'react/display-name': OFF,
126+
'react/jsx-key': OFF,
127+
'react/no-deprecated': OFF,
128+
'react/no-string-refs': OFF,
129+
'react/prop-types': OFF,
130+
},
131+
},
132+
{
133+
files: ['scripts/**/*.js', 'npm/**/*.js'],
134+
rules: {
135+
'no-console': OFF,
136+
},
137+
},
138+
],
139+
};

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
build/
3+
.eslintcache

babel.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
module.exports = {
24
presets: [
35
[

jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
module.exports = {
24
modulePathIgnorePatterns: ['<rootDir>/build/'],
35
setupFilesAfterEnv: ['./scripts/jest/setupTests.js'],

package.json

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,31 @@
2121
},
2222
"devDependencies": {
2323
"@babel/cli": "^7.8.4",
24-
"@babel/core": "^7.8.4",
24+
"@babel/core": "^7.8.6",
2525
"@babel/plugin-proposal-class-properties": "^7.8.3",
2626
"@babel/plugin-proposal-object-rest-spread": "^7.8.3",
27-
"@babel/plugin-transform-classes": "^7.8.3",
27+
"@babel/plugin-transform-classes": "^7.8.6",
2828
"@babel/plugin-transform-react-jsx-source": "^7.8.3",
2929
"@babel/plugin-transform-template-literals": "^7.8.3",
30-
"@babel/preset-env": "^7.8.4",
30+
"@babel/preset-env": "^7.8.6",
3131
"@babel/preset-react": "^7.8.3",
3232
"@rollup/plugin-commonjs": "^11.0.1",
3333
"@rollup/plugin-node-resolve": "^7.0.0",
3434
"@rollup/plugin-replace": "^2.3.0",
35+
"babel-eslint": "^10.1.0",
3536
"babel-jest": "^25.1.0",
37+
"confusing-browser-globals": "^1.0.9",
38+
"eslint": "^6.8.0",
39+
"eslint-config-prettier": "^6.10.0",
40+
"eslint-plugin-jest": "^23.8.0",
41+
"eslint-plugin-prettier": "^3.1.2",
42+
"eslint-plugin-react": "^7.18.3",
3643
"fs-extra": "^8.1.0",
44+
"husky": "^4.2.3",
3745
"jest": "^25.1.0",
3846
"jest-diff": "^25.1.0",
47+
"lint-staged": "^10.0.8",
48+
"prettier": "1.19.1",
3949
"react": "^16.12.0",
4050
"rimraf": "^3.0.1",
4151
"rollup": "^1.30.1",
@@ -57,7 +67,16 @@
5767
"prebuild": "rimraf build",
5868
"build": "rollup --config",
5969
"postbuild": "node ./scripts/copyFiles.js",
70+
"lint": "eslint --ignore-path .gitignore .",
6071
"test": "jest",
6172
"test:debug": "node --inspect-brk node_modules/jest/bin/jest.js --runInBand --no-cache"
73+
},
74+
"husky": {
75+
"hooks": {
76+
"pre-commit": "lint-staged"
77+
}
78+
},
79+
"lint-staged": {
80+
"*.js": "eslint --cache --fix"
6281
}
6382
}

src/ReactShallowRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ See https://fb.me/react-invalid-hook-call for tips about how to debug and fix th
596596
if (process.env.NODE_ENV !== 'production') {
597597
ReactDebugCurrentFrame.getCurrentStack = prevGetStack;
598598
}
599-
}
599+
}
600600

601601
this._rendering = false;
602602
this._updater._invokeCallbacks();

src/shared/ReactSharedInternals.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ import React from 'react';
1010
const ReactSharedInternals =
1111
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
1212

13+
const hasOwnProperty = Object.prototype.hasOwnProperty;
14+
1315
// Prevent newer renderers from RTE when used with older react package versions.
1416
// Current owner and dispatcher used to share the same ref,
1517
// but PR #14548 split them out to better support the react-debug-tools package.
16-
if (!ReactSharedInternals.hasOwnProperty('ReactCurrentDispatcher')) {
18+
if (!hasOwnProperty.call(ReactSharedInternals, 'ReactCurrentDispatcher')) {
1719
ReactSharedInternals.ReactCurrentDispatcher = {
1820
current: null,
1921
};
2022
}
21-
if (!ReactSharedInternals.hasOwnProperty('ReactCurrentBatchConfig')) {
23+
if (!hasOwnProperty.call(ReactSharedInternals, 'ReactCurrentBatchConfig')) {
2224
ReactSharedInternals.ReactCurrentBatchConfig = {
2325
suspense: null,
2426
};

src/shared/consoleWithStackDev.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ function printWarning(level, format, args) {
4141
argsWithFormat.unshift('Warning: ' + format);
4242
// We intentionally don't use spread (or .apply) directly because it
4343
// breaks IE9: https://github.com/facebook/react/issues/13610
44+
// eslint-disable-next-line no-console
4445
Function.prototype.apply.call(console[level], console, argsWithFormat);
4546

4647
try {

0 commit comments

Comments
 (0)