-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheslint-plugin-helper.js
62 lines (52 loc) · 1.99 KB
/
eslint-plugin-helper.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
'use strict';
const babelManager = require('./lib/babel-manager');
const normalizeExtensions = require('./lib/extensions');
const {hasExtension, normalizeGlobs, classify} = require('./lib/globs');
const loadConfig = require('./lib/load-config');
const configCache = new Map();
const helperCache = new Map();
function load(projectDir, overrides) {
const cacheKey = `${JSON.stringify(overrides)}\n${projectDir}`;
if (helperCache.has(cacheKey)) {
return helperCache.get(cacheKey);
}
let conf;
let babelProvider;
if (configCache.has(projectDir)) {
({conf, babelProvider} = configCache.get(projectDir));
} else {
conf = loadConfig({resolveFrom: projectDir});
const {nonSemVerExperiments: experiments} = conf;
if (!experiments.noBabelOutOfTheBox || conf.babel !== undefined) {
babelProvider = babelManager({experiments, projectDir});
babelProvider.validateConfig(conf.babel, conf.compileEnhancements !== false);
}
configCache.set(projectDir, {conf, babelProvider});
}
if (overrides) {
conf = {...conf, ...overrides};
if (overrides.extensions) {
// Ignore extensions from the Babel config. Assume all extensions are
// provided in the override.
babelProvider = undefined;
}
}
const extensions = normalizeExtensions(conf.extensions, babelProvider, {experiments: conf.nonSemVerExperiments});
const globs = {cwd: projectDir, ...normalizeGlobs(conf.files, conf.helpers, conf.sources, extensions.all)};
const helper = Object.freeze({
classifyFile: file => classify(file, globs),
classifyImport: importPath => {
if (hasExtension(globs.extensions, importPath)) {
// The importPath has one of the test file extensions: we can classify
// it directly.
return classify(importPath, globs);
}
// Add the first extension. If multiple extensions are available, assume
// patterns are not biased to any particular extension.
return classify(`${importPath}.${globs.extensions[0]}`, globs);
}
});
helperCache.set(cacheKey, helper);
return helper;
}
exports.load = load;