Skip to content

Commit

Permalink
fix(emitErrors): no console output when file(s) are clean (#71)
Browse files Browse the repository at this point in the history
* tests: add test for lint-free + emitErrors case

and use const for assured block scoping

* fix: use helper functions for results filtering

* fix: remove unneeded lodash.defaultTo dependency
  • Loading branch information
JaKXz authored Feb 22, 2017
1 parent 4d4d233 commit 154b405
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 285 deletions.
4 changes: 2 additions & 2 deletions lib/lint-dirty-modules-plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var assign = require('object-assign');
var defaultTo = require('lodash.defaultto');
var defaultTo = require('ramda').defaultTo;
var minimatch = require('minimatch');
var runCompilation = require('./run-compilation');

Expand Down Expand Up @@ -67,7 +67,7 @@ LintDirtyModulesPlugin.prototype.getChangedFiles = function getChangedFiles (fil
};

function hasFileChanged (filename, timestamp) {
return defaultTo(this.prevTimestamps[filename], this.startTime) < defaultTo(timestamp, Infinity);
return defaultTo(this.startTime)(this.prevTimestamps[filename]) < defaultTo(Infinity)(timestamp);
}

module.exports = LintDirtyModulesPlugin;
22 changes: 15 additions & 7 deletions lib/run-compilation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var chalk = require('chalk');
var R = require('ramda');
var linter = require('./linter');
var errorMessage = require('./constants').errorMessage;

Expand All @@ -19,13 +20,12 @@ module.exports = function runCompilation (options, compiler, done) {
.then(function linterSuccess (lint) {
var results = lint.results;

warnings = options.emitErrors === false ? results : results.filter(function (file) {
return !file.errored && file.warnings && file.warnings.length;
});

errors = options.emitErrors === false ? [] : results.filter(function (file) {
return file.errored;
});
if (options.emitErrors === false) {
warnings = results.filter(R.either(fileHasErrors, fileHasWarnings));
} else {
warnings = results.filter(R.both(R.complement(fileHasErrors), fileHasWarnings));
errors = results.filter(fileHasErrors);
}

if (options.quiet === false) {
console.warn(options.formatter(results));
Expand Down Expand Up @@ -53,3 +53,11 @@ module.exports = function runCompilation (options, compiler, done) {
callback();
});
};

function fileHasErrors (file) {
return file.errored;
}

function fileHasWarnings (file) {
return file.warnings && file.warnings.length;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"dependencies": {
"arrify": "^1.0.1",
"chalk": "^1.1.3",
"lodash.defaultto": "^4.14.0",
"minimatch": "^3.0.3",
"object-assign": "^4.1.0",
"ramda": "^0.23.0",
"stylelint": "^7.7.0"
},
"devDependencies": {
Expand Down
82 changes: 38 additions & 44 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

var assign = require('object-assign');
var td = require('testdouble');
const assign = require('object-assign');
const td = require('testdouble');

var StyleLintPlugin = require('../');
const StyleLintPlugin = require('../');

var pack = require('./helpers/pack');
var webpack = require('./helpers/webpack');
var baseConfig = require('./helpers/base-config');
var configFilePath = getPath('./.stylelintrc');
var errorMessage = require('../lib/constants').errorMessage;
const pack = require('./helpers/pack');
const webpack = require('./helpers/webpack');
const baseConfig = require('./helpers/base-config');
const configFilePath = getPath('./.stylelintrc');
const errorMessage = require('../lib/constants').errorMessage;

describe('stylelint-webpack-plugin', function () {
it('works with a simple file', function () {
Expand Down Expand Up @@ -46,7 +46,7 @@ describe('stylelint-webpack-plugin', function () {
});

it('fails on errors when asked to', function () {
var config = {
const config = {
context: './test/fixtures/single-error',
plugins: [
new StyleLintPlugin({
Expand All @@ -65,7 +65,7 @@ describe('stylelint-webpack-plugin', function () {
});

it('fails when .stylelintrc is not a proper format', function () {
var config = {
const config = {
context: './test/fixtures/single-error',
plugins: [
new StyleLintPlugin({
Expand All @@ -92,7 +92,7 @@ describe('stylelint-webpack-plugin', function () {
});

it('sends messages to the console', function () {
var config = {
const config = {
context: './test/fixtures/syntax-error',
plugins: [
new StyleLintPlugin({
Expand All @@ -111,15 +111,14 @@ describe('stylelint-webpack-plugin', function () {
});

context('without StyleLintPlugin configuration', function () {
var config = {
context: './test/fixtures/lint-free',
const config = {
plugins: [
new StyleLintPlugin()
]
};

it('works by using stylelint#cosmiconfig under the hood', function () {
return pack(assign({}, baseConfig, config))
return pack(assign({}, baseConfig, config, { context: './test/fixtures/lint-free' }))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(0);
expect(stats.compilation.warnings).to.have.length(0);
Expand All @@ -136,7 +135,7 @@ describe('stylelint-webpack-plugin', function () {

context('interop with NoErrorsPlugin', function () {
it('works when failOnError is false', function () {
var config = {
const config = {
context: './test/fixtures/single-error',
plugins: [
new StyleLintPlugin({
Expand All @@ -154,8 +153,7 @@ describe('stylelint-webpack-plugin', function () {
});

context('when failOnError is true', function () {
var config = {
context: './test/fixtures/single-error',
const config = {
plugins: [
new StyleLintPlugin({
configFile: configFilePath,
Expand All @@ -167,7 +165,7 @@ describe('stylelint-webpack-plugin', function () {
};

it('throws when there is an error', function () {
return pack(assign({}, baseConfig, config))
return pack(assign({}, baseConfig, config, { context: './test/fixtures/single-error' }))
.then(expect.fail)
.catch(function (err) {
expect(err).to.be.instanceof(Error);
Expand All @@ -184,19 +182,26 @@ describe('stylelint-webpack-plugin', function () {
});

context('when `emitErrors` is disabled', function () {
it('emits errors as warnings when asked to', function () {
var config = {
context: './test/fixtures/single-error',
plugins: [
new StyleLintPlugin({
configFile: configFilePath,
quiet: true,
emitErrors: false
})
]
};
const config = {
plugins: [
new StyleLintPlugin({
configFile: configFilePath,
quiet: true,
emitErrors: false
})
]
};

return pack(assign({}, baseConfig, config))
it('does not print warnings or errors when there are none', function () {
return pack(assign({}, baseConfig, config, { context: './test/fixtures/lint-free' }))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(0);
expect(stats.compilation.warnings).to.have.length(0);
});
});

it('emits errors as warnings when asked to', function () {
return pack(assign({}, baseConfig, config, { context: './test/fixtures/single-error' }))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(0);
expect(stats.compilation.warnings).to.have.length(1);
Expand All @@ -205,18 +210,7 @@ describe('stylelint-webpack-plugin', function () {
});

it('still indicates that warnings are warnings, even when emitting errors as warnings too', function () {
var config = {
context: './test/fixtures/rule-warning',
plugins: [
new StyleLintPlugin({
configFile: configFilePath,
quiet: true,
emitErrors: false
})
]
};

return pack(assign({}, baseConfig, config))
return pack(assign({}, baseConfig, config, { context: './test/fixtures/rule-warning' }))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(0);
expect(stats.compilation.warnings).to.have.length(1);
Expand All @@ -227,7 +221,7 @@ describe('stylelint-webpack-plugin', function () {

context('lintDirtyModulesOnly flag is enabled', function () {
it('skips linting on initial run', function () {
var config = {
const config = {
context: './test/fixtures/single-error',
plugins: [
new StyleLintPlugin({
Expand All @@ -246,7 +240,7 @@ describe('stylelint-webpack-plugin', function () {
});

it('still skips on initial run with `emitErrors` disabled', function () {
var config = {
const config = {
context: './test/fixtures/single-error',
plugins: [
new StyleLintPlugin({
Expand Down
Loading

0 comments on commit 154b405

Please sign in to comment.