Skip to content

Commit b3c9ea7

Browse files
committedJun 10, 2019
Allow ESLint rules to override extensions and glob patterns
1 parent 7366a9d commit b3c9ea7

File tree

5 files changed

+139
-11
lines changed

5 files changed

+139
-11
lines changed
 

‎eslint-plugin-helper.js

+26-7
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,34 @@ const normalizeExtensions = require('./lib/extensions');
44
const {hasExtension, normalizeGlobs, classify} = require('./lib/globs');
55
const loadConfig = require('./lib/load-config');
66

7-
const cache = new Map();
7+
const configCache = new Map();
8+
const helperCache = new Map();
89

9-
function load(projectDir) {
10-
if (cache.has(projectDir)) {
11-
return cache.get(projectDir);
10+
function load(projectDir, overrides) {
11+
const cacheKey = `${JSON.stringify(overrides)}\n${projectDir}`;
12+
if (helperCache.has(cacheKey)) {
13+
return helperCache.get(cacheKey);
14+
}
15+
16+
let conf;
17+
let babelConfig;
18+
if (configCache.has(projectDir)) {
19+
({conf, babelConfig} = configCache.get(projectDir));
20+
} else {
21+
conf = loadConfig(projectDir);
22+
babelConfig = babelPipeline.validate(conf.babel);
23+
configCache.set(projectDir, {conf, babelConfig});
24+
}
25+
26+
if (overrides) {
27+
conf = {...conf, ...overrides};
28+
if (overrides.extensions) {
29+
// Ignore extensions from the Babel config. Assume all extensions are
30+
// provided in the override.
31+
babelConfig = null;
32+
}
1233
}
1334

14-
const conf = loadConfig(projectDir);
15-
const babelConfig = babelPipeline.validate(conf.babel);
1635
const extensions = normalizeExtensions(conf.extensions || [], babelConfig);
1736
const globs = {cwd: projectDir, ...normalizeGlobs(conf.files, conf.helpers, conf.sources, extensions.all)};
1837

@@ -30,7 +49,7 @@ function load(projectDir) {
3049
return classify(`${importPath}.${globs.extensions[0]}`, globs);
3150
}
3251
});
33-
cache.set(projectDir, helper);
52+
helperCache.set(cacheKey, helper);
3453
return helper;
3554
}
3655

‎test/eslint-plugin-helper.js

+102-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ const {test} = require('tap');
55
const {load} = require('../eslint-plugin-helper');
66

77
const projectDir = path.join(__dirname, 'fixture/eslint-plugin-helper');
8+
const overrideDir = path.join(__dirname, 'fixture/eslint-plugin-helper/for-overriding');
89

910
test('caches loaded configuration', t => {
1011
const expected = load(projectDir);
11-
const actual = load(projectDir);
12-
t.is(expected, actual);
12+
t.is(expected, load(projectDir));
13+
14+
const withOverride = load(projectDir, {});
15+
t.not(expected, withOverride);
16+
t.is(withOverride, load(projectDir, {}));
17+
1318
t.end();
1419
});
1520

@@ -43,6 +48,41 @@ test('classifies files according to the configuration', t => {
4348
t.end();
4449
});
4550

51+
test('classifies files according to configuration override', t => {
52+
const helper = load(overrideDir, {
53+
extensions: ['foo'],
54+
files: ['tests/**/*'],
55+
helpers: ['helpers/*'],
56+
sources: ['source.*']
57+
});
58+
t.deepEqual(helper.classifyFile(path.join(overrideDir, 'tests/test.foo')), {
59+
isHelper: false,
60+
isSource: false,
61+
isTest: true
62+
});
63+
t.deepEqual(helper.classifyFile(path.join(overrideDir, 'tests/_helper.foo')), {
64+
isHelper: true,
65+
isSource: false,
66+
isTest: false
67+
});
68+
t.deepEqual(helper.classifyFile(path.join(overrideDir, 'helpers/helper.foo')), {
69+
isHelper: true,
70+
isSource: false,
71+
isTest: false
72+
});
73+
t.deepEqual(helper.classifyFile(path.join(overrideDir, 'source.foo')), {
74+
isHelper: false,
75+
isSource: true,
76+
isTest: false
77+
});
78+
t.deepEqual(helper.classifyFile(path.join(overrideDir, 'tests/test.js')), {
79+
isHelper: false,
80+
isSource: false,
81+
isTest: false
82+
});
83+
t.end();
84+
});
85+
4686
test('classifies imports with extension according to the configuration', t => {
4787
const helper = load(projectDir);
4888
t.deepEqual(helper.classifyImport(path.join(projectDir, 'tests/test.foo')), {
@@ -68,6 +108,36 @@ test('classifies imports with extension according to the configuration', t => {
68108
t.end();
69109
});
70110

111+
test('classifies imports with extension according to the override', t => {
112+
const helper = load(overrideDir, {
113+
extensions: ['foo'],
114+
files: ['tests/**/*'],
115+
helpers: ['helpers/*'],
116+
sources: ['source.*']
117+
});
118+
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'tests/test.foo')), {
119+
isHelper: false,
120+
isSource: false,
121+
isTest: true
122+
});
123+
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'tests/_helper.foo')), {
124+
isHelper: true,
125+
isSource: false,
126+
isTest: false
127+
});
128+
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'helpers/helper.foo')), {
129+
isHelper: true,
130+
isSource: false,
131+
isTest: false
132+
});
133+
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'source.foo')), {
134+
isHelper: false,
135+
isSource: true,
136+
isTest: false
137+
});
138+
t.end();
139+
});
140+
71141
test('classifies imports without extension according to the configuration', t => {
72142
const helper = load(projectDir);
73143
t.deepEqual(helper.classifyImport(path.join(projectDir, 'tests/test')), {
@@ -92,3 +162,33 @@ test('classifies imports without extension according to the configuration', t =>
92162
});
93163
t.end();
94164
});
165+
166+
test('classifies imports without extension according to the override', t => {
167+
const helper = load(overrideDir, {
168+
extensions: ['foo'],
169+
files: ['tests/**/*'],
170+
helpers: ['helpers/*'],
171+
sources: ['source.*']
172+
});
173+
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'tests/test')), {
174+
isHelper: false,
175+
isSource: false,
176+
isTest: true
177+
});
178+
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'tests/_helper')), {
179+
isHelper: true,
180+
isSource: false,
181+
isTest: false
182+
});
183+
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'helpers/helper')), {
184+
isHelper: true,
185+
isSource: false,
186+
isTest: false
187+
});
188+
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'source')), {
189+
isHelper: false,
190+
isSource: true,
191+
isTest: false
192+
});
193+
t.end();
194+
});
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default {
22
babel: false,
3+
extensions: ['foo'],
34
files: ['tests/**/*'],
4-
helpers: ['helpers/*'],
5-
extensions: ['foo']
5+
helpers: ['helpers/*']
66
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
babel: {
3+
extensions: ['bar']
4+
},
5+
files: ['build/tests/**/*'],
6+
helpers: ['build/helpers/*'],
7+
sources: ['src/**/*']
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)
Please sign in to comment.