Skip to content

Commit

Permalink
Merge ec7e78d into bb76a2d
Browse files Browse the repository at this point in the history
  • Loading branch information
forivall committed Aug 18, 2016
2 parents bb76a2d + ec7e78d commit dd8aaee
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ var optionsManager = require('./options-manager');

exports.lintText = function (str, opts) {
opts = optionsManager.preprocess(opts);

if (opts.overrides && opts.overrides.length) {
var overrides = opts.overrides;
delete opts.overrides;

var filename = path.relative(opts.cwd, opts.filename);
var foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
opts = optionsManager.mergeApplicableOverrides(opts, foundOverrides.applicable);
}

opts = optionsManager.buildConfig(opts);

var engine = new eslint.CLIEngine(opts);
Expand Down
7 changes: 6 additions & 1 deletion options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ function findApplicableOverrides(path, overrides) {
};
}

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) {
var map = {};
Expand All @@ -173,7 +177,7 @@ function groupConfigs(paths, baseOptions, overrides) {
var data = findApplicableOverrides(x, overrides);

if (!map[data.hash]) {
var mergedOpts = deepAssign.apply(null, [emptyOptions(), baseOptions].concat(data.applicable.map(normalizeOpts)));
var mergedOpts = mergeApplicableOverrides(baseOptions, data.applicable);
delete mergedOpts.files;

arr.push(map[data.hash] = {
Expand Down Expand Up @@ -201,6 +205,7 @@ exports.mergeWithPkgConf = mergeWithPkgConf;
exports.normalizeOpts = normalizeOpts;
exports.buildConfig = buildConfig;
exports.findApplicableOverrides = findApplicableOverrides;
exports.mergeApplicableOverrides = mergeApplicableOverrides;
exports.groupConfigs = groupConfigs;
exports.preprocess = preprocess;
exports.emptyOptions = emptyOptions;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"eslint-plugin-react": "^5.0.1",
"execa": "^0.4.0",
"nyc": "^6.1.1",
"pify": "^2.3.0",
"proxyquire": "^1.7.3",
"temp-write": "^2.0.1",
"xo": "file:."
Expand Down
19 changes: 19 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import fs from 'fs';
import path from 'path';
import test from 'ava';
import pify from 'pify';
import fn from '../';

const readFile = pify(fs.readFile, Promise);

function hasRule(results, ruleId) {
return results[0].messages.some(x => x.ruleId === ruleId);
}
Expand Down Expand Up @@ -63,3 +67,18 @@ test('.lintText() - regression test for #71', t => {
}).results;
t.is(results[0].errorCount, 0, results[0]);
});

test('lintText() - overrides support', async t => {
const cwd = path.join(__dirname, 'fixtures/overrides');
const bar = path.join(cwd, 'test/bar.js');
const barResults = fn.lintText(await readFile(bar, 'utf8'), {filename: bar, cwd}).results;
t.is(barResults[0].errorCount, 0, barResults[0]);

const foo = path.join(cwd, 'test/foo.js');
const fooResults = fn.lintText(await readFile(foo, 'utf8'), {filename: foo, cwd}).results;
t.is(fooResults[0].errorCount, 0, fooResults[0]);

const index = path.join(cwd, 'test/index.js');
const indexResults = fn.lintText(await readFile(bar, 'utf8'), {filename: index, cwd}).results;
t.is(indexResults[0].errorCount, 0, indexResults[0]);
});
3 changes: 2 additions & 1 deletion test/fixtures/overrides/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
}
],
"rules": {
"import/no-extraneous-dependencies": 0
"import/no-extraneous-dependencies": 0,
"ava/no-ignored-test-files": 0
}
}
}

0 comments on commit dd8aaee

Please sign in to comment.