-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
utils.js
412 lines (354 loc) · 10.2 KB
/
utils.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* eslint import/no-dynamic-require:0 */
import { oneLine } from 'common-tags';
import delve from 'dlv';
import getLogger from 'loglevel-colored-level-prefix';
import { Linter } from 'eslint';
const logger = getLogger({ prefix: 'prettier-eslint' });
const RULE_DISABLED = {};
const RULE_NOT_CONFIGURED = 'RULE_NOT_CONFIGURED';
const ruleValueExists = prettierRuleValue =>
prettierRuleValue !== RULE_NOT_CONFIGURED &&
prettierRuleValue !== RULE_DISABLED &&
typeof prettierRuleValue !== 'undefined';
const OPTION_GETTERS = {
printWidth: {
ruleValue: rules => getRuleValue(rules, 'max-len', 'code'),
ruleValueToPrettierOption: getPrintWidth
},
tabWidth: {
ruleValue: rules => {
let value = getRuleValue(rules, 'indent');
if (value === 'tab') {
value = getRuleValue(rules, 'max-len', 'tabWidth');
}
return value;
},
ruleValueToPrettierOption: getTabWidth
},
singleQuote: {
ruleValue: rules => getRuleValue(rules, 'quotes'),
ruleValueToPrettierOption: getSingleQuote
},
trailingComma: {
ruleValue: rules => getRuleValue(rules, 'comma-dangle', []),
ruleValueToPrettierOption: getTrailingComma
},
bracketSpacing: {
ruleValue: rules => getRuleValue(rules, 'object-curly-spacing'),
ruleValueToPrettierOption: getBracketSpacing
},
semi: {
ruleValue: rules => getRuleValue(rules, 'semi'),
ruleValueToPrettierOption: getSemi
},
useTabs: {
ruleValue: rules => getRuleValue(rules, 'indent'),
ruleValueToPrettierOption: getUseTabs
},
bracketSameLine: {
ruleValue: rules =>
getRuleValue(rules, 'react/jsx-closing-bracket-location', 'nonEmpty'),
ruleValueToPrettierOption: getBracketSameLine
},
arrowParens: {
ruleValue: rules => getRuleValue(rules, 'arrow-parens'),
ruleValueToPrettierOption: getArrowParens
}
};
/* eslint import/prefer-default-export:0 */
export { getESLint, getOptionsForFormatting, requireModule };
function getOptionsForFormatting(
eslintConfig,
prettierOptions = {},
fallbackPrettierOptions = {}
) {
const eslint = getRelevantESLintConfig(eslintConfig);
const prettier = getPrettierOptionsFromESLintRules(
eslintConfig,
prettierOptions,
fallbackPrettierOptions
);
return { eslint, prettier };
}
function getRelevantESLintConfig(eslintConfig) {
const linter = new Linter();
const rules = linter.getRules();
logger.debug('turning off unfixable rules');
const relevantRules = {};
rules.forEach((rule, name) => {
const {
meta: { fixable }
} = rule;
if (!fixable) {
logger.trace('turning off rule:', JSON.stringify({ [name]: rule }));
rule = ['off'];
relevantRules[name] = rule;
}
}, {});
return {
// defaults
useEslintrc: false,
...eslintConfig,
// overrides
rules: { ...eslintConfig.rules, ...relevantRules },
fix: true,
globals: eslintConfig.globals || {}
};
}
/**
* This accepts an eslintConfig object and converts
* it to the `prettier` options object
*/
function getPrettierOptionsFromESLintRules(
eslintConfig,
prettierOptions,
fallbackPrettierOptions
) {
const { rules } = eslintConfig;
const prettierPluginOptions = getRuleValue(rules, 'prettier/prettier', []);
if (ruleValueExists(prettierPluginOptions)) {
prettierOptions = { ...prettierPluginOptions, ...prettierOptions };
}
return Object.keys(OPTION_GETTERS).reduce(
(options, key) =>
configureOptions(
prettierOptions,
fallbackPrettierOptions,
key,
options,
rules
),
prettierOptions
);
}
// If an ESLint rule that prettier can be configured with is enabled create a
// prettier configuration object that reflects the ESLint rule configuration.
function configureOptions(
prettierOptions,
fallbackPrettierOptions,
key,
options,
rules
) {
const givenOption = prettierOptions[key];
const optionIsGiven = givenOption !== undefined;
if (optionIsGiven) {
options[key] = givenOption;
} else {
const { ruleValue, ruleValueToPrettierOption } = OPTION_GETTERS[key];
const eslintRuleValue = ruleValue(rules);
const option = ruleValueToPrettierOption(
eslintRuleValue,
fallbackPrettierOptions,
rules
);
if (option !== undefined) {
options[key] = option;
}
}
return options;
}
function getPrintWidth(eslintValue, fallbacks) {
return makePrettierOption('printWidth', eslintValue, fallbacks);
}
function getTabWidth(eslintValue, fallbacks) {
return makePrettierOption('tabWidth', eslintValue, fallbacks);
}
function getSingleQuote(eslintValue, fallbacks) {
let prettierValue;
if (eslintValue === 'single') {
prettierValue = true;
} else if (eslintValue === 'double') {
prettierValue = false;
} else if (eslintValue === 'backtick') {
prettierValue = false;
} else {
prettierValue = eslintValue;
}
return makePrettierOption('singleQuote', prettierValue, fallbacks);
}
function getTrailingComma(eslintValue, fallbacks) {
let prettierValue;
if (eslintValue === 'never') {
prettierValue = 'none';
} else if (
typeof eslintValue === 'string' &&
eslintValue.indexOf('always') === 0
) {
prettierValue = 'es5';
} else if (typeof eslintValue === 'object') {
prettierValue = getValFromTrailingCommaConfig(eslintValue);
} else {
prettierValue = RULE_NOT_CONFIGURED;
}
return makePrettierOption('trailingComma', prettierValue, fallbacks);
}
function getValFromTrailingCommaConfig(objectConfig) {
const { arrays = '', objects = '', functions = '' } = objectConfig;
const fns = isAlways(functions);
const es5 = [arrays, objects].some(isAlways);
if (fns) {
return 'all';
} else if (es5) {
return 'es5';
} else {
return 'none';
}
}
function getBracketSpacing(eslintValue, fallbacks) {
let prettierValue;
if (eslintValue === 'never') {
prettierValue = false;
} else if (eslintValue === 'always') {
prettierValue = true;
} else {
prettierValue = eslintValue;
}
return makePrettierOption('bracketSpacing', prettierValue, fallbacks);
}
function getSemi(eslintValue, fallbacks) {
let prettierValue;
if (eslintValue === 'never') {
prettierValue = false;
} else if (eslintValue === 'always') {
prettierValue = true;
} else {
prettierValue = eslintValue;
}
return makePrettierOption('semi', prettierValue, fallbacks);
}
function getUseTabs(eslintValue, fallbacks) {
let prettierValue;
if (eslintValue === 'tab') {
prettierValue = true;
} else {
prettierValue = RULE_NOT_CONFIGURED;
}
return makePrettierOption('useTabs', prettierValue, fallbacks);
}
function getBracketSameLine(eslintValue, fallbacks) {
let prettierValue;
if (eslintValue === 'after-props') {
prettierValue = true;
} else if (
eslintValue === 'tag-aligned' ||
eslintValue === 'line-aligned' ||
eslintValue === 'props-aligned'
) {
prettierValue = false;
} else {
prettierValue = eslintValue;
}
return makePrettierOption('bracketSameLine', prettierValue, fallbacks);
}
function getArrowParens(eslintValue, fallbacks) {
let prettierValue;
if (eslintValue === 'as-needed') {
prettierValue = 'avoid';
} else {
prettierValue = eslintValue;
}
return makePrettierOption('arrowParens', prettierValue, fallbacks);
}
function extractRuleValue(objPath, name, value) {
// XXX: Ignore code coverage for the following else case
// There are currently no eslint rules which we can infer prettier
// options from, that have an object option which we don't know how
// to infer from.
// istanbul ignore else
if (objPath) {
logger.trace(
oneLine`
Getting the value from object configuration of ${name}.
delving into ${JSON.stringify(value)} with path "${objPath}"
`
);
return delve(value, objPath, RULE_NOT_CONFIGURED);
}
// istanbul ignore next
logger.debug(
oneLine`
The ${name} rule is using an object configuration
of ${JSON.stringify(value)} but prettier-eslint is
not currently capable of getting the prettier value
based on an object configuration for ${name}.
Please file an issue (and make a pull request?)
`
);
// istanbul ignore next
return undefined;
}
function getRuleValue(rules, name, objPath) {
const ruleConfig = rules[name];
if (Array.isArray(ruleConfig)) {
const [ruleSetting, value] = ruleConfig;
// If `ruleSetting` is set to disable the ESLint rule don't use `value` as
// it might be a value provided by an overriden config package e.g. airbnb
// overriden by config-prettier. The airbnb values are provided even though
// config-prettier disables the rule. Instead use fallback or prettier
// default.
if (ruleSetting === 0 || ruleSetting === 'off') {
return RULE_DISABLED;
}
if (typeof value === 'object') {
return extractRuleValue(objPath, name, value);
} else {
logger.trace(
oneLine`
The ${name} rule is configured with a
non-object value of ${value}. Using that value.
`
);
return value;
}
}
return RULE_NOT_CONFIGURED;
}
function isAlways(val) {
return val.indexOf('always') === 0;
}
function makePrettierOption(prettierRuleName, prettierRuleValue, fallbacks) {
if (ruleValueExists(prettierRuleValue)) {
return prettierRuleValue;
}
const fallback = fallbacks[prettierRuleName];
if (typeof fallback !== 'undefined') {
logger.debug(
oneLine`
The ${prettierRuleName} rule is not configured,
using provided fallback of ${fallback}
`
);
return fallback;
}
logger.debug(
oneLine`
The ${prettierRuleName} rule is not configured,
let prettier decide
`
);
return undefined;
}
function requireModule(modulePath, name) {
try {
logger.trace(`requiring "${name}" module at "${modulePath}"`);
return require(modulePath);
} catch (error) {
logger.error(
oneLine`
There was trouble getting "${name}".
Is "${modulePath}" a correct path to the "${name}" module?
`
);
throw error;
}
}
function getESLint(eslintPath, eslintOptions) {
const { ESLint } = requireModule(eslintPath, 'eslint');
try {
return new ESLint(eslintOptions);
} catch (error) {
logger.error('There was trouble creating the ESLint CLIEngine.');
throw error;
}
}