Skip to content

Commit

Permalink
feat: apply .ignores to lintText (#169)
Browse files Browse the repository at this point in the history
* feat: apply `.ignores` to `lintText`

- add multimatch-powered short-circuit to `lintText`
- add supporting test cases
- closes #168

* fix: respect overrides before ignoring files

*  merge applicable overrides before ignoring files
*  throw for opts.ignores without opts.filename
*  use default ignores when opts.filename is given
*  add test case for default ignores

BREAKING CHANGES:
* lintText now uses the same default ignores as lintFiles with opts.filename
  * **/node_modules/**
  * **/bower_components/**
  * coverage/**
  * {tmp,temp}/**
  * **/*.min.js
  * **/bundle.js
  * fixtures{-*,}.{js,jsx}
  * fixture{s,}/**
  * {test,tests,spec,__tests__}/fixture{s,}/**
  * vendor/**
  * dist/**
* lintText now throws when providing opts.ignores without opts.filename

* style: use lodash.isequal, shorten error message

* test: add case with overriden ignores

* chore: remove array-equal
  • Loading branch information
marionebl authored and sindresorhus committed Dec 13, 2016
1 parent 3e1a318 commit 2e14d1b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
const path = require('path');
const eslint = require('eslint');
const globby = require('globby');
const isEqual = require('lodash.isequal');
const multimatch = require('multimatch');
const optionsManager = require('./options-manager');

exports.lintText = (str, opts) => {
Expand All @@ -12,11 +14,34 @@ exports.lintText = (str, opts) => {
delete opts.overrides;

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

const foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
opts = optionsManager.mergeApplicableOverrides(opts, foundOverrides.applicable);
}

opts = optionsManager.buildConfig(opts);
const defaultIgnores = optionsManager.getIgnores({}).ignores;

if (opts.ignores && !isEqual(defaultIgnores, opts.ignores) && typeof opts.filename !== 'string') {
throw new Error('The `ignores` option requires the `filename` option to be defined.');
}

if (opts.ignores && opts.ignores.length > 0 && 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
}]
};
}
}

const engine = new eslint.CLIEngine(opts);
const report = engine.executeOnText(str, opts.filename);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"get-stdin": "^5.0.0",
"globby": "^6.0.0",
"has-flag": "^2.0.0",
"lodash.isequal": "^4.4.0",
"meow": "^3.4.2",
"multimatch": "^2.1.0",
"parse-gitignore": "^0.3.1",
Expand Down
63 changes: 63 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,69 @@ test('.lintText() - `esnext` option', t => {
t.true(hasRule(results, 'no-var'));
});

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

test('.lintText() - `ignores` option', 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 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() - respect overrides', t => {
const result = fn.lintText(`'use strict'\nconsole.log('unicorn');\n`, {
filename: 'ignored/index.js',
ignores: ['ignored/**/*.js'],
overrides: [
{
files: ['ignored/**/*.js'],
ignores: []
}
]
});
t.is(result.errorCount, 1);
t.is(result.warningCount, 0);
});

test('.lintText() - overriden ignore', t => {
const result = fn.lintText(`'use strict'\nconsole.log('unicorn');\n`, {
filename: 'unignored.js',
overrides: [
{
files: ['unignored.js'],
ignores: ['unignored.js']
}
]
});
t.is(result.errorCount, 0);
t.is(result.warningCount, 0);
});

test('.lintText() - `ignores` option without filename', t => {
t.throws(() => {
fn.lintText(`'use strict'\nconsole.log('unicorn');\n`, {
ignores: ['ignored/**/*.js']
});
}, /The `ignores` option requires the `filename` option to be defined./);
});

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 2e14d1b

Please sign in to comment.