Skip to content

Commit

Permalink
Adding standard, xo support; improving linter detection (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdothtml authored and satazor committed Jun 19, 2018
1 parent dfcf03a commit f0ca912
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ At the moment the following linters are detected:
- [coffeelint](http://coffeelint.org)
- [tslint](https://palantir.github.io/tslint/)
- [prettier](https://prettier.io/)
- [standard](https://standardjs.com/)
- [xo](https://github.com/xojs/xo/)

Feel free to a PR to include other linters as part of the detection!

Expand Down
2 changes: 1 addition & 1 deletion lib/coffeelint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function detectCoffeeLint(dir) {

return Promise.all([
tryFiles(paths),
tryPackageJson(dir, 'coffeelintConfig'),
tryPackageJson(dir, ['coffeelintConfig', 'devDependencies.coffeelint']),
])
.then((booleans) => booleans.some((bool) => bool));
}
Expand Down
7 changes: 6 additions & 1 deletion lib/csslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

const path = require('path');
const tryFiles = require('./util/tryFiles');
const tryPackageJson = require('./util/tryPackageJson');

function detectCssLint(dir) {
const paths = ['.csslintrc']
.map((entry) => path.join(dir, entry));

return tryFiles(paths);
return Promise.all([
tryFiles(paths),
tryPackageJson(dir, ['devDependencies.csslint']),
])
.then((booleans) => booleans.some((bool) => bool));
}

module.exports = detectCssLint;
2 changes: 1 addition & 1 deletion lib/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function detectEslint(dir) {

return Promise.all([
tryFiles(paths),
tryPackageJson(dir, 'eslintConfig'),
tryPackageJson(dir, ['devDependencies.eslint', 'eslintConfig', 'eslintIgnore']),
])
.then((booleans) => booleans.some((bool) => bool));
}
Expand Down
7 changes: 6 additions & 1 deletion lib/htmlhint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

const path = require('path');
const tryFiles = require('./util/tryFiles');
const tryPackageJson = require('./util/tryPackageJson');

function detectHtmlHint(dir) {
const paths = ['.htmlhintrc']
.map((entry) => path.join(dir, entry));

return tryFiles(paths);
return Promise.all([
tryFiles(paths),
tryPackageJson(dir, ['devDependencies.htmlhint']),
])
.then((booleans) => booleans.some((bool) => bool));
}

module.exports = detectHtmlHint;
2 changes: 2 additions & 0 deletions lib/htmllint.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ function detectHtmlHint(dir) {
const paths = ['.htmllintrc']
.map((entry) => path.join(dir, entry));

// htmllint doesn't run without a config file, so no need to
// check dependencies
return tryFiles(paths);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/jscs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function detectJscs(dir) {

return Promise.all([
tryFiles(paths),
tryPackageJson(dir, 'jscsConfig'),
tryPackageJson(dir, ['jscsConfig']),
])
.then((booleans) => booleans.some((bool) => bool));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function detectJsHint(dir) {

return Promise.all([
tryFiles(paths),
tryPackageJson(dir, 'jshintConfig'),
tryPackageJson(dir, ['devDependencies.jshint', 'jshintConfig']),
])
.then((booleans) => booleans.some((bool) => bool));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function detectPrettierLint(dir) {

return Promise.all([
tryFiles(paths),
tryPackageJson(dir, 'prettier'),
tryPackageJson(dir, ['devDependencies.prettier', 'prettier']),
])
.then((booleans) => booleans.some((bool) => bool));
}
Expand Down
9 changes: 9 additions & 0 deletions lib/standard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const tryPackageJson = require('./util/tryPackageJson');

function detectStandardLint(dir) {
return tryPackageJson(dir, ['devDependencies.standard', 'standard']);
}

module.exports = detectStandardLint;
2 changes: 1 addition & 1 deletion lib/stylelint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function detectStyleLint(dir) {

return Promise.all([
tryFiles(paths),
tryPackageJson(dir, 'stylelint'),
tryPackageJson(dir, ['devDependencies.stylelint', 'stylelint']),
])
.then((booleans) => booleans.some((bool) => bool));
}
Expand Down
2 changes: 2 additions & 0 deletions lib/tslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

const path = require('path');
const tryFiles = require('./util/tryFiles');
const tryPackageJson = require('./util/tryPackageJson');

function detectTsLint(dir) {
const paths = ['tslint.json']
.map((entry) => path.join(dir, entry));

return Promise.all([
tryFiles(paths),
tryPackageJson(dir, ['devDependencies.tslint']),
])
.then((booleans) => booleans.some((bool) => bool));
}
Expand Down
7 changes: 5 additions & 2 deletions lib/util/tryPackageJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

const path = require('path');
const loadJsonFile = require('load-json-file');
const get = require('lodash.get');

function tryPackageJson(dir, prop) {
function tryPackageJson(dir, props) {
return loadJsonFile(path.join(dir, 'package.json'))
.then((json) => !!json[prop], (err) => {
.then((json) => {
return props.some((prop) => get(json, prop));
}, (err) => {
if (err.code === 'ENOENT') {
return false;
}
Expand Down
9 changes: 9 additions & 0 deletions lib/xo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const tryPackageJson = require('./util/tryPackageJson');

function detectXOLint(dir) {
return tryPackageJson(dir, ['devDependencies.xo', 'xo']);
}

module.exports = detectXOLint;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@
"jshint",
"jscs",
"jslint",
"standard",
"stylelint",
"csslint",
"htmlhint",
"htmllint"
"htmllint",
"xo"
],
"author": "IndigoUnited <[email protected]> (http://indigounited.com)",
"license": "MIT",
"dependencies": {
"load-json-file": "^3.0.0",
"lodash.get": "^4.4.2",
"require-directory": "^2.1.1"
},
"devDependencies": {
Expand Down
103 changes: 102 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ it('should detect eslint', () => {

return detectRepoLinters(tmpFolder);
})
.then(assert)
.then(() => {
cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ eslintIgnore: {} }));

return detectRepoLinters(tmpFolder);
})
.then(assert)
.then(() => {
cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ devDependencies: { eslint: '^x.x.x' } }));

return detectRepoLinters(tmpFolder);
})
.then(assert);
});

Expand Down Expand Up @@ -116,6 +130,13 @@ it('should detect jshint', () => {

return detectRepoLinters(tmpFolder);
})
.then(assert)
.then(() => {
cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ devDependencies: { jshint: '^x.x.x' } }));

return detectRepoLinters(tmpFolder);
})
.then(assert);
});

Expand Down Expand Up @@ -156,6 +177,13 @@ it('should detect stylelint', () => {

return detectRepoLinters(tmpFolder);
})
.then(assert)
.then(() => {
cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ devDependencies: { stylelint: '^x.x.x' } }));

return detectRepoLinters(tmpFolder);
})
.then(assert);
});

Expand All @@ -168,6 +196,13 @@ it('should detect csslint', () => {
fs.writeFileSync(`${tmpFolder}/.csslintrc`, '');

return detectRepoLinters(tmpFolder)
.then(assert)
.then(() => {
cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ devDependencies: { csslint: '^x.x.x' } }));

return detectRepoLinters(tmpFolder);
})
.then(assert);
});

Expand All @@ -180,6 +215,13 @@ it('should detect htmlhint', () => {
fs.writeFileSync(`${tmpFolder}/.htmlhintrc`, '');

return detectRepoLinters(tmpFolder)
.then(assert)
.then(() => {
cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ devDependencies: { htmlhint: '^x.x.x' } }));

return detectRepoLinters(tmpFolder);
})
.then(assert);
});

Expand Down Expand Up @@ -210,6 +252,13 @@ it('should detect coffeelint', () => {
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ coffeelintConfig: {} }));
return detectRepoLinters(tmpFolder);
})
.then(assert)
.then(() => {
cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ devDependencies: { coffeelint: '^x.x.x' } }));

return detectRepoLinters(tmpFolder);
})
.then(assert);
});

Expand All @@ -222,7 +271,14 @@ it('should detect tslint', () => {
fs.writeFileSync(`${tmpFolder}/tslint.json`, '');

return detectRepoLinters(tmpFolder)
.then(assert);
.then(assert)
.then(() => {
cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ devDependencies: { tslint: '^x.x.x' } }));

return detectRepoLinters(tmpFolder);
})
.then(assert);
});

it('should detect prettier', () => {
Expand All @@ -244,9 +300,54 @@ it('should detect prettier', () => {
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ prettier: {} }));
return detectRepoLinters(tmpFolder);
})
.then(assert)
.then(() => {
cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ devDependencies: { prettier: '^x.x.x' } }));

return detectRepoLinters(tmpFolder);
})
.then(assert);
});

it('should detect standard', () => {
function assert(linters) {
expect(linters).to.eql(['standard']);
}

cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ devDependencies: { standard: '^x.x.x' } }));

return detectRepoLinters(tmpFolder)
.then(assert)
.then(() => {
cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ standard: {} }));

return detectRepoLinters(tmpFolder);
})
.then(assert);
});

it('should detect xo', () => {
function assert(linters) {
expect(linters).to.eql(['xo']);
}

cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ xo: {} }));

return detectRepoLinters(tmpFolder)
.then(assert)
.then(() => {
cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/package.json`, JSON.stringify({ devDependencies: { xo: '^x.x.x' } }));

return detectRepoLinters(tmpFolder);
})
.then(assert);
});

it('should detect several linters in a complex repository', () => {
cleanTmpFolder();
fs.writeFileSync(`${tmpFolder}/.editorconfig`, '');
Expand Down

0 comments on commit f0ca912

Please sign in to comment.