Skip to content

Commit 040d8be

Browse files
forivallsindresorhus
authored andcommitted
find and use overrides opts in lintText (#130)
* find and use overrides opts in lintText fixes sindresorhus/atom-linter-xo#20 * test overrides support for lintText * remove unnecessary `|| process.cwd()` * refactor to directly calculate overrides instead of grouping
1 parent 04733f1 commit 040d8be

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ var optionsManager = require('./options-manager');
66

77
exports.lintText = function (str, opts) {
88
opts = optionsManager.preprocess(opts);
9+
10+
if (opts.overrides && opts.overrides.length) {
11+
var overrides = opts.overrides;
12+
delete opts.overrides;
13+
14+
var filename = path.relative(opts.cwd, opts.filename);
15+
var foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
16+
opts = optionsManager.mergeApplicableOverrides(opts, foundOverrides.applicable);
17+
}
18+
919
opts = optionsManager.buildConfig(opts);
1020

1121
var engine = new eslint.CLIEngine(opts);

options-manager.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ function findApplicableOverrides(path, overrides) {
164164
};
165165
}
166166

167+
function mergeApplicableOverrides(baseOptions, applicableOverrides) {
168+
return deepAssign.apply(null, [emptyOptions(), baseOptions].concat(applicableOverrides.map(normalizeOpts)));
169+
}
170+
167171
// Creates grouped sets of merged options together with the paths they apply to.
168172
function groupConfigs(paths, baseOptions, overrides) {
169173
var map = {};
@@ -173,7 +177,7 @@ function groupConfigs(paths, baseOptions, overrides) {
173177
var data = findApplicableOverrides(x, overrides);
174178

175179
if (!map[data.hash]) {
176-
var mergedOpts = deepAssign.apply(null, [emptyOptions(), baseOptions].concat(data.applicable.map(normalizeOpts)));
180+
var mergedOpts = mergeApplicableOverrides(baseOptions, data.applicable);
177181
delete mergedOpts.files;
178182

179183
arr.push(map[data.hash] = {
@@ -201,6 +205,7 @@ exports.mergeWithPkgConf = mergeWithPkgConf;
201205
exports.normalizeOpts = normalizeOpts;
202206
exports.buildConfig = buildConfig;
203207
exports.findApplicableOverrides = findApplicableOverrides;
208+
exports.mergeApplicableOverrides = mergeApplicableOverrides;
204209
exports.groupConfigs = groupConfigs;
205210
exports.preprocess = preprocess;
206211
exports.emptyOptions = emptyOptions;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"eslint-plugin-react": "^6.3.0",
9494
"execa": "^0.4.0",
9595
"nyc": "^8.3.0",
96+
"pify": "^2.3.0",
9697
"proxyquire": "^1.7.3",
9798
"temp-write": "^2.0.1",
9899
"xo": "sindresorhus/xo#v0.16.0"

test/api.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import fs from 'fs';
12
import path from 'path';
23
import test from 'ava';
4+
import pify from 'pify';
35
import fn from '../';
46

7+
const readFile = pify(fs.readFile, Promise);
8+
59
const hasRule = (results, ruleId) => results[0].messages.some(x => x.ruleId === ruleId);
610

711
test('.lintText()', t => {
@@ -58,3 +62,18 @@ test('.lintText() - regression test for #71', t => {
5862
}).results;
5963
t.is(results[0].errorCount, 0, results[0]);
6064
});
65+
66+
test('lintText() - overrides support', async t => {
67+
const cwd = path.join(__dirname, 'fixtures/overrides');
68+
const bar = path.join(cwd, 'test/bar.js');
69+
const barResults = fn.lintText(await readFile(bar, 'utf8'), {filename: bar, cwd}).results;
70+
t.is(barResults[0].errorCount, 0, barResults[0]);
71+
72+
const foo = path.join(cwd, 'test/foo.js');
73+
const fooResults = fn.lintText(await readFile(foo, 'utf8'), {filename: foo, cwd}).results;
74+
t.is(fooResults[0].errorCount, 0, fooResults[0]);
75+
76+
const index = path.join(cwd, 'test/index.js');
77+
const indexResults = fn.lintText(await readFile(bar, 'utf8'), {filename: index, cwd}).results;
78+
t.is(indexResults[0].errorCount, 0, indexResults[0]);
79+
});

test/fixtures/overrides/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
}
1515
],
1616
"rules": {
17-
"import/no-extraneous-dependencies": 0
17+
"import/no-extraneous-dependencies": 0,
18+
"ava/no-ignored-test-files": 0
1819
}
1920
}
2021
}

0 commit comments

Comments
 (0)