forked from xojs/xo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions-manager.js
264 lines (216 loc) · 5.98 KB
/
options-manager.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
'use strict';
const os = require('os');
const path = require('path');
const arrify = require('arrify');
const pkgConf = require('pkg-conf');
const deepAssign = require('deep-assign');
const multimatch = require('multimatch');
const resolveFrom = require('resolve-from');
const pathExists = require('path-exists');
const parseGitignore = require('parse-gitignore');
const globby = require('globby');
const DEFAULT_IGNORE = [
'**/node_modules/**',
'**/bower_components/**',
'coverage/**',
'{tmp,temp}/**',
'**/*.min.js',
'**/bundle.js',
'fixture{-*,}.{js,jsx}',
'fixture{s,}/**',
'{test,tests,spec,__tests__}/fixture{s,}/**',
'vendor/**',
'dist/**'
];
const DEFAULT_EXTENSION = [
'js',
'jsx'
];
const DEFAULT_CONFIG = {
useEslintrc: false,
cache: true,
cacheLocation: path.join(os.homedir() || os.tmpdir(), '.xo-cache/'),
baseConfig: {
extends: [
'xo',
path.join(__dirname, 'config/overrides.js'),
path.join(__dirname, 'config/plugins.js')
]
}
};
function normalizeOpts(opts) {
opts = Object.assign({}, opts);
// alias to help humans
[
'env',
'global',
'ignore',
'plugin',
'rule',
'setting',
'extend',
'extension'
].forEach(singular => {
const plural = singular + 's';
let value = opts[plural] || opts[singular];
delete opts[singular];
if (value === undefined) {
return;
}
if (singular !== 'rule' && singular !== 'setting') {
value = arrify(value);
}
opts[plural] = value;
});
return opts;
}
function mergeWithPkgConf(opts) {
opts = Object.assign({cwd: process.cwd()}, opts);
const conf = pkgConf.sync('xo', {cwd: opts.cwd, skipOnFalse: true});
return Object.assign({}, conf, opts);
}
// define the shape of deep properties for deepAssign
function emptyOptions() {
return {
rules: {},
settings: {},
globals: [],
envs: [],
plugins: [],
extends: []
};
}
function buildConfig(opts) {
const config = deepAssign(
emptyOptions(),
DEFAULT_CONFIG,
opts
);
if (opts.space) {
const spaces = typeof opts.space === 'number' ? opts.space : 2;
config.rules.indent = ['error', spaces, {SwitchCase: 1}];
// only apply if the user has the React plugin
if (opts.cwd && resolveFrom(opts.cwd, 'eslint-plugin-react')) {
config.plugins = config.plugins.concat('react');
config.rules['react/jsx-indent-props'] = ['error', spaces];
config.rules['react/jsx-indent'] = ['error', spaces];
}
}
if (opts.semicolon === false) {
config.rules.semi = ['error', 'never'];
config.rules['semi-spacing'] = ['error', {
before: false,
after: true
}];
}
if (opts.esnext !== false) {
config.baseConfig.extends = ['xo/esnext', path.join(__dirname, 'config/plugins.js')];
}
if (opts.rules) {
Object.assign(config.rules, opts.rules);
}
if (opts.settings) {
config.baseConfig.settings = opts.settings;
}
if (opts.parser) {
config.baseConfig.parser = opts.parser;
}
if (opts.extends && opts.extends.length > 0) {
// TODO: this logic needs to be improved, preferably use the same code as ESLint
// user's configs must be resolved to their absolute paths
const configs = opts.extends.map(name => {
// don't do anything if it's a filepath
if (pathExists.sync(name)) {
return name;
}
// don't do anything if it's a config from a plugin
if (name.startsWith('plugin:')) {
return name;
}
if (!name.includes('eslint-config-')) {
name = `eslint-config-${name}`;
}
const ret = resolveFrom(opts.cwd, name);
if (!ret) {
throw new Error(`Couldn't find ESLint config: ${name}`);
}
return ret;
});
config.baseConfig.extends = config.baseConfig.extends.concat(configs);
}
return config;
}
// Builds a list of overrides for a particular path, and a hash value.
// The hash value is a binary representation of which elements in the `overrides` array apply to the path.
//
// If overrides.length === 4, and only the first and third elements apply, then our hash is: 1010 (in binary)
function findApplicableOverrides(path, overrides) {
let hash = 0;
const applicable = [];
overrides.forEach(override => {
hash <<= 1;
if (multimatch(path, override.files).length > 0) {
applicable.push(override);
hash |= 1;
}
});
return {
hash,
applicable
};
}
function mergeApplicableOverrides(baseOptions, applicableOverrides) {
return deepAssign.apply(null, [emptyOptions(), baseOptions].concat(applicableOverrides.map(normalizeOpts)));
}
// Creates grouped sets of merged options together with the paths they apply to.
function groupConfigs(paths, baseOptions, overrides) {
const map = {};
const arr = [];
paths.forEach(x => {
const data = findApplicableOverrides(x, overrides);
if (!map[data.hash]) {
const mergedOpts = mergeApplicableOverrides(baseOptions, data.applicable);
delete mergedOpts.files;
arr.push(map[data.hash] = {
opts: mergedOpts,
paths: []
});
}
map[data.hash].paths.push(x);
});
return arr;
}
function getIgnores(opts) {
opts.ignores = DEFAULT_IGNORE.concat(opts.ignores || []);
const gitignores = globby.sync('**/.gitignore', {
ignore: opts.ignores,
cwd: opts.cwd || process.cwd()
});
const ignores = gitignores
.map(pathToGitignore => {
const patterns = parseGitignore(pathToGitignore);
const base = path.dirname(pathToGitignore);
return patterns.map(file => path.join(base, file));
})
.reduce((a, b) => a.concat(b), []);
opts.ignores = opts.ignores.concat(ignores);
return opts;
}
function preprocess(opts) {
opts = mergeWithPkgConf(opts);
opts = normalizeOpts(opts);
opts = getIgnores(opts);
opts.extensions = DEFAULT_EXTENSION.concat(opts.extensions || []);
return opts;
}
exports.DEFAULT_IGNORE = DEFAULT_IGNORE;
exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
exports.mergeWithPkgConf = mergeWithPkgConf;
exports.normalizeOpts = normalizeOpts;
exports.buildConfig = buildConfig;
exports.findApplicableOverrides = findApplicableOverrides;
exports.mergeApplicableOverrides = mergeApplicableOverrides;
exports.groupConfigs = groupConfigs;
exports.preprocess = preprocess;
exports.emptyOptions = emptyOptions;
exports.getIgnores = getIgnores;