1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const path = require("path"),
|
13 | ConfigOps = require("./config/config-ops"),
|
14 | ConfigFile = require("./config/config-file"),
|
15 | Plugins = require("./config/plugins"),
|
16 | FileFinder = require("./file-finder"),
|
17 | userHome = require("user-home"),
|
18 | isResolvable = require("is-resolvable"),
|
19 | pathIsInside = require("path-is-inside");
|
20 |
|
21 | const debug = require("debug")("eslint:config");
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const PERSONAL_CONFIG_DIR = userHome || null;
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function isObject(item) {
|
40 | return typeof item === "object" && !Array.isArray(item) && item !== null;
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function loadConfig(configToLoad) {
|
50 | let config = {},
|
51 | filePath = "";
|
52 |
|
53 | if (configToLoad) {
|
54 |
|
55 | if (isObject(configToLoad)) {
|
56 | config = configToLoad;
|
57 |
|
58 | if (config.extends) {
|
59 | config = ConfigFile.applyExtends(config, filePath);
|
60 | }
|
61 | } else {
|
62 | filePath = configToLoad;
|
63 | config = ConfigFile.load(filePath);
|
64 | }
|
65 |
|
66 | }
|
67 |
|
68 | return config;
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | function getPersonalConfig() {
|
77 | let config;
|
78 |
|
79 | if (PERSONAL_CONFIG_DIR) {
|
80 | const filename = ConfigFile.getFilenameForDirectory(PERSONAL_CONFIG_DIR);
|
81 |
|
82 | if (filename) {
|
83 | debug("Using personal config");
|
84 | config = loadConfig(filename);
|
85 | }
|
86 | }
|
87 |
|
88 | return config || null;
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | function hasRules(options) {
|
97 | return options.rules && Object.keys(options.rules).length > 0;
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | function getLocalConfig(thisConfig, directory) {
|
107 | const localConfigFiles = thisConfig.findLocalConfigFiles(directory),
|
108 | numFiles = localConfigFiles.length,
|
109 | projectConfigPath = ConfigFile.getFilenameForDirectory(thisConfig.options.cwd);
|
110 | let found,
|
111 | config = {},
|
112 | rootPath;
|
113 |
|
114 | for (let i = 0; i < numFiles; i++) {
|
115 |
|
116 | const localConfigFile = localConfigFiles[i];
|
117 |
|
118 |
|
119 |
|
120 | if (path.dirname(localConfigFile) === PERSONAL_CONFIG_DIR && localConfigFile !== projectConfigPath) {
|
121 | continue;
|
122 | }
|
123 |
|
124 |
|
125 | if (rootPath && !pathIsInside(path.dirname(localConfigFile), rootPath)) {
|
126 | continue;
|
127 | }
|
128 |
|
129 | debug("Loading " + localConfigFile);
|
130 | const localConfig = loadConfig(localConfigFile);
|
131 |
|
132 |
|
133 | if (!localConfig) {
|
134 | continue;
|
135 | }
|
136 |
|
137 |
|
138 | if (localConfig.root === true) {
|
139 | rootPath = path.dirname(localConfigFile);
|
140 | }
|
141 |
|
142 | found = true;
|
143 | debug("Using " + localConfigFile);
|
144 | config = ConfigOps.merge(localConfig, config);
|
145 | }
|
146 |
|
147 | if (!found && !thisConfig.useSpecificConfig) {
|
148 |
|
149 | |
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | const personalConfig = getPersonalConfig();
|
156 |
|
157 | if (personalConfig) {
|
158 | config = ConfigOps.merge(config, personalConfig);
|
159 | } else if (!hasRules(thisConfig.options) && !thisConfig.options.baseConfig) {
|
160 |
|
161 |
|
162 | const noConfigError = new Error("No ESLint configuration found.");
|
163 |
|
164 | noConfigError.messageTemplate = "no-config-found";
|
165 | noConfigError.messageData = {
|
166 | directory,
|
167 | filesExamined: localConfigFiles
|
168 | };
|
169 |
|
170 | throw noConfigError;
|
171 | }
|
172 | }
|
173 |
|
174 | return config;
|
175 | }
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | function Config(options) {
|
188 | options = options || {};
|
189 |
|
190 | this.ignore = options.ignore;
|
191 | this.ignorePath = options.ignorePath;
|
192 | this.cache = {};
|
193 | this.parser = options.parser;
|
194 | this.parserOptions = options.parserOptions || {};
|
195 |
|
196 | this.baseConfig = options.baseConfig ? loadConfig(options.baseConfig) : { rules: {} };
|
197 |
|
198 | this.useEslintrc = (options.useEslintrc !== false);
|
199 |
|
200 | this.env = (options.envs || []).reduce(function(envs, name) {
|
201 | envs[name] = true;
|
202 | return envs;
|
203 | }, {});
|
204 |
|
205 | |
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | this.globals = (options.globals || []).reduce(function(globals, def) {
|
212 | const parts = def.split(":");
|
213 |
|
214 | globals[parts[0]] = (parts.length > 1 && parts[1] === "true");
|
215 |
|
216 | return globals;
|
217 | }, {});
|
218 |
|
219 | const useConfig = options.configFile;
|
220 |
|
221 | this.options = options;
|
222 |
|
223 | if (useConfig) {
|
224 | debug("Using command line config " + useConfig);
|
225 | if (isResolvable(useConfig) || isResolvable("eslint-config-" + useConfig) || useConfig.charAt(0) === "@") {
|
226 | this.useSpecificConfig = loadConfig(useConfig);
|
227 | } else {
|
228 | this.useSpecificConfig = loadConfig(path.resolve(this.options.cwd, useConfig));
|
229 | }
|
230 | }
|
231 | }
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | Config.prototype.getConfig = function(filePath) {
|
240 | const directory = filePath ? path.dirname(filePath) : this.options.cwd;
|
241 | let config,
|
242 | userConfig;
|
243 |
|
244 | debug("Constructing config for " + (filePath ? filePath : "text"));
|
245 |
|
246 | config = this.cache[directory];
|
247 |
|
248 | if (config) {
|
249 | debug("Using config from cache");
|
250 | return config;
|
251 | }
|
252 |
|
253 |
|
254 | if (this.useEslintrc) {
|
255 | debug("Using .eslintrc and package.json files");
|
256 | userConfig = getLocalConfig(this, directory);
|
257 | } else {
|
258 | debug("Not using .eslintrc or package.json files");
|
259 | userConfig = {};
|
260 | }
|
261 |
|
262 |
|
263 | config = ConfigOps.merge({}, this.baseConfig);
|
264 |
|
265 |
|
266 | config = ConfigOps.merge(config, userConfig);
|
267 |
|
268 |
|
269 | if (this.useSpecificConfig) {
|
270 | debug("Merging command line config file");
|
271 |
|
272 | config = ConfigOps.merge(config, this.useSpecificConfig);
|
273 | }
|
274 |
|
275 |
|
276 | debug("Merging command line environment settings");
|
277 | config = ConfigOps.merge(config, { env: this.env });
|
278 |
|
279 |
|
280 | if (this.options.rules) {
|
281 | debug("Merging command line rules");
|
282 | config = ConfigOps.merge(config, { rules: this.options.rules });
|
283 | }
|
284 |
|
285 |
|
286 | config = ConfigOps.merge(config, { globals: this.globals });
|
287 |
|
288 |
|
289 |
|
290 | if (this.parser || !config.parser) {
|
291 | config = ConfigOps.merge(config, {
|
292 | parser: this.parser
|
293 | });
|
294 | }
|
295 |
|
296 | if (this.parserOptions) {
|
297 | config = ConfigOps.merge(config, {
|
298 | parserOptions: this.parserOptions
|
299 | });
|
300 | }
|
301 |
|
302 |
|
303 | if (this.options.plugins) {
|
304 | debug("Merging command line plugins");
|
305 | Plugins.loadAll(this.options.plugins);
|
306 | config = ConfigOps.merge(config, { plugins: this.options.plugins });
|
307 | }
|
308 |
|
309 |
|
310 | if (config.env) {
|
311 | config = ConfigOps.applyEnvironments(config);
|
312 | }
|
313 |
|
314 | this.cache[directory] = config;
|
315 |
|
316 | return config;
|
317 | };
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 | Config.prototype.findLocalConfigFiles = function(directory) {
|
325 |
|
326 | if (!this.localConfigFinder) {
|
327 | this.localConfigFinder = new FileFinder(ConfigFile.CONFIG_FILES, this.options.cwd);
|
328 | }
|
329 |
|
330 | return this.localConfigFinder.findAllInDirectoryAndParents(directory);
|
331 | };
|
332 |
|
333 | module.exports = Config;
|