Skip to content

Commit

Permalink
Add extensions option (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
abread authored and sindresorhus committed Oct 7, 2016
1 parent 8829b33 commit 3c42847
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
2 changes: 2 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const cli = meow(`
--extend Extend defaults with a custom config [Can be set multiple times]
--open Open files with issues in your editor
--quiet Show only errors and no warnings
--extension Additional extension to lint [Can be set multiple times]
Examples
$ xo
Expand All @@ -51,6 +52,7 @@ const cli = meow(`
$ xo --env=node --env=mocha
$ xo --init --esnext
$ xo --plugin=react
$ xo --plugin=html --extension=html
Tips
Put options in package.json instead of using flags so other tools can read it.
Expand Down
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ exports.lintFiles = (patterns, opts) => {
opts = optionsManager.preprocess(opts);

if (patterns.length === 0) {
patterns = '**/*.{js,jsx}';
patterns = '**/*';
}

return globby(patterns, {ignore: opts.ignores}).then(paths => {
// when users are silly and don't specify an extension in the glob pattern
// filter out unwanted file extensions
// for silly users that don't specify an extension in the glob pattern
paths = paths.filter(x => {
const ext = path.extname(x);
return ext === '.js' || ext === '.jsx';
// Remove dot before the actual extension
const ext = path.extname(x).replace('.', '');
return opts.extensions.indexOf(ext) !== -1;
});

if (!(opts.overrides && opts.overrides.length > 0)) {
Expand Down
9 changes: 8 additions & 1 deletion options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const DEFAULT_IGNORE = [
'dist/**'
];

const DEFAULT_EXTENSION = [
'js',
'jsx'
];

const DEFAULT_CONFIG = {
useEslintrc: false,
cache: true,
Expand All @@ -45,7 +50,8 @@ function normalizeOpts(opts) {
'ignore',
'plugin',
'rule',
'extend'
'extend',
'extension'
].forEach(singular => {
const plural = singular + 's';
let value = opts[plural] || opts[singular];
Expand Down Expand Up @@ -201,6 +207,7 @@ function preprocess(opts) {
opts = mergeWithPkgConf(opts);
opts = normalizeOpts(opts);
opts.ignores = DEFAULT_IGNORE.concat(opts.ignores || []);
opts.extensions = DEFAULT_EXTENSION.concat(opts.extensions || []);
return opts;
}

Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ $ xo --help
--extend Extend defaults with a custom config [Can be set multiple times]
--open Open files with issues in your editor
--quiet Show only errors and no warnings
--extension Additional extension to lint [Can be set multiple times]
Examples
$ xo
Expand All @@ -58,6 +59,7 @@ $ xo --help
$ xo --env=node --env=mocha
$ xo --init --esnext
$ xo --plugin=react
$ xo --plugin=html --extension=html
Tips
Put options in package.json instead of using flags so other tools can read it.
Expand Down Expand Up @@ -197,6 +199,12 @@ Type: `Array`, `string`

Use one or more [shareable configs](http://eslint.org/docs/developer-guide/shareable-configs.html) to override any of the default rules (like `rules` above).

### extensions

Type: `Array`

Allow more extensions to be linted besides `.js` and `.jsx`. Make sure they're supported by ESLint or an ESLint plugin.


## Config Overrides

Expand Down
13 changes: 13 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,16 @@ test('lintText() - overrides support', async t => {
const indexResults = fn.lintText(await readFile(bar, 'utf8'), {filename: index, cwd}).results;
t.is(indexResults[0].errorCount, 0, indexResults[0]);
});

test('.lintFiles() - only accepts whitelisted extensions', async t => {
// Markdown files will always produce linter errors and will not be going away
const mdGlob = path.join(__dirname, '..', '*.md');

// No files should be linted = no errors
const noOptionsResults = await fn.lintFiles(mdGlob, {});
t.is(noOptionsResults.errorCount, 0);

// Markdown files linted (with no plugin for it) = errors
const moreExtensionsResults = await fn.lintFiles(mdGlob, {extensions: ['md']});
t.true(moreExtensionsResults.errorCount > 0);
});
6 changes: 4 additions & 2 deletions test/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ test('normalizeOpts: makes all the opts plural and arrays', t => {
ignore: 'test.js',
plugin: 'my-plugin',
rule: {'my-rule': 'foo'},
extend: 'foo'
extend: 'foo',
extension: 'html'
});

t.deepEqual(opts, {
Expand All @@ -21,7 +22,8 @@ test('normalizeOpts: makes all the opts plural and arrays', t => {
ignores: ['test.js'],
plugins: ['my-plugin'],
rules: {'my-rule': 'foo'},
extends: ['foo']
extends: ['foo'],
extensions: ['html']
});
});

Expand Down

0 comments on commit 3c42847

Please sign in to comment.