-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.eslintrc.js
176 lines (148 loc) · 4.28 KB
/
.eslintrc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* eslint-disable */
const { readFileSync } = require("fs");
const generalRules = {
"default-case": "off",
"no-plusplus": "off",
"no-continue": "off",
"prefer-template": "off",
"prefer-destructuring": "off",
// Allow leading underscores in identifiers (e.g. _id in MongoDB).
"no-underscore-dangle": "off",
// Some APIs use snake_case identifiers.
camelcase: "off",
// Depending on the context, using bracket notation might be clearer.
"dot-notation": "off",
/**
* Reassigning parameters can be useful to avoid creating another variable,
* and to modify objects by reference.
*/
"no-param-reassign": "off",
/**
* Unused variables and arguments should be removed in most cases, but it's
* convenient to allow them when the code is still being implemented.
*
* Prefix variable names with an underscore to suppress the warning.
*/
"no-unused-vars": [
"warn",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
},
],
// Not necessary for some APIs (consistency reasons)
"import/prefer-default-export": "off",
// Stylistic rules.
"lines-between-class-members": "off",
};
const reactRules = {
"react/jsx-filename-extension": "off",
"react/prop-types": "off",
"react/destructuring-assignment": "off",
// Allow prop spreading, but require explicit spreading for HTML tags.
"react/jsx-props-no-spreading": [
2,
{ html: "enforce", custom: "ignore", explicitSpread: "ignore" },
],
// Use warnings instead of errors for issues that aren't deal-breakers.
"react/sort-comp": "warn",
"react/prefer-stateless-function": "warn",
"react/no-array-index-key": "warn",
};
/**
* Override rules from eslint-plugin-jsx-a11y, if present.
*/
function getAccessibilityOverrideRules() {
let a11yRules;
try {
a11yRules = require("eslint-plugin-jsx-a11y").rules;
} catch (e) {
return {};
}
const newRules = {};
// Produce warnings instead of errors for accessibility problems.
Object.keys(a11yRules).forEach((name) => {
newRules[`jsx-a11y/${name}`] = "warn";
});
return newRules;
}
/**
* Override rules from eslint-config-airbnb-base, if present.
*/
function getAirbnbOverrideRules() {
let airbnbRules;
try {
airbnbRules = require("eslint-config-airbnb-base/rules/style.js").rules;
} catch (e) {
return {};
}
return {
// Allow the use of for...of statements.
"no-restricted-syntax": airbnbRules["no-restricted-syntax"].filter(
(item) => item.selector !== "ForOfStatement"
),
};
}
/**
* Load the .eslintrc.json file, which contains frontend/backend-specific configuration.
*/
function loadEslintrcJson() {
const path = ".eslintrc.json";
let data;
try {
data = readFileSync(path, "utf8");
} catch (e) {
throw new Error(`File '${path}' does not exist.`);
}
try {
return JSON.parse(data);
} catch (e) {
throw new Error(`Syntax error in '${path}': ${e.message}`);
}
}
/**
* Generate the complete rules object.
*/
function generateRules(usingReact, usingNode) {
const rules = { ...generalRules };
Object.assign(rules, getAirbnbOverrideRules());
if (usingReact) {
Object.assign(rules, reactRules);
Object.assign(rules, getAccessibilityOverrideRules());
}
if (usingNode) {
// Allow console.log and friends in the backend.
rules["no-console"] = "off";
}
return rules;
}
/**
* Generate the ESLint configuration.
*/
function generateConfig() {
if (process.env.NODE_ENV === "production") {
// Disable linting in production, since it is unnecessary and can cause
// errors if dev dependencies are not installed.
return {};
}
const eslintrcJson = loadEslintrcJson();
const usingReact = eslintrcJson.plugins !== undefined && eslintrcJson.plugins.includes("react");
const usingNode = eslintrcJson.env.node;
const config = {
settings: {
react: {
version: "detect",
},
},
...eslintrcJson,
extends: ["eslint:recommended", usingReact ? "airbnb" : "airbnb-base", "prettier"],
rules: generateRules(usingReact, usingNode),
};
if (usingReact) {
config.parser = "@babel/eslint-parser";
config.parserOptions.requireConfigFile = false;
config.parserOptions.babelOptions = { plugins: ["@babel/plugin-syntax-jsx"] };
}
return config;
}
module.exports = generateConfig();