Skip to content

Commit

Permalink
feat: apply .ignores to lintText
Browse files Browse the repository at this point in the history
- add multimatch-powered short-circuit to `lintText`
- add supporting test cases
- closes #168
  • Loading branch information
marionebl committed Nov 26, 2016
1 parent 6540a76 commit 3910206
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
19 changes: 19 additions & 0 deletions index.js
Expand Up @@ -2,16 +2,35 @@
const path = require('path');
const eslint = require('eslint');
const globby = require('globby');
const multimatch = require('multimatch');
const optionsManager = require('./options-manager');

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

if (opts.cwd && opts.filename) {
const filename = path.relative(opts.cwd, opts.filename);

if (multimatch([filename], opts.ignores)) {
return {
errorCount: 0,
warningCount: 0,
results: [{
errorCount: 0,
filePath: filename,
messages: [],
warningCount: 0
}]
};
}
}

if (opts.overrides && opts.overrides.length > 0) {
const overrides = opts.overrides;
delete opts.overrides;

const filename = path.relative(opts.cwd, opts.filename);

const foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
opts = optionsManager.mergeApplicableOverrides(opts, foundOverrides.applicable);
}
Expand Down
27 changes: 27 additions & 0 deletions test/api.js
Expand Up @@ -20,6 +20,33 @@ test('.lintText() - `esnext` option', t => {
t.true(hasRule(results, 'no-var'));
});

test('.lintText() - `ignores` option', t => {
const result = fn.lintText(`'use strict'\nconsole.log('unicorn');\n`, {
cwd: process.cwd(),
filename: 'ignored/index.js',
ignores: ['ignored/**/*.js']
});
t.is(result.errorCount, 0);
t.is(result.warningCount, 0);
});

test('.lintText() - `ignores` option without cwd', t => {
const result = fn.lintText(`'use strict'\nconsole.log('unicorn');\n`, {
filename: 'ignored/index.js',
ignores: ['ignored/**/*.js']
});
t.is(result.errorCount, 0);
t.is(result.warningCount, 0);
});

test('.lintText() - `ignores` option without filename', t => {
const result = fn.lintText(`'use strict'\nconsole.log('unicorn');\n`, {
ignores: ['ignored/**/*.js']
});
t.is(result.errorCount, 1);
t.is(result.warningCount, 0);
});

test('.lintText() - JSX support', t => {
const results = fn.lintText('var app = <div className="appClass">Hello, React!</div>;\n', {esnext: false}).results;
t.true(hasRule(results, 'no-unused-vars'));
Expand Down

0 comments on commit 3910206

Please sign in to comment.