Skip to content

Commit 154b405

Browse files
authored
fix(emitErrors): no console output when file(s) are clean (#71)
* 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
1 parent 4d4d233 commit 154b405

File tree

5 files changed

+287
-285
lines changed

5 files changed

+287
-285
lines changed

lib/lint-dirty-modules-plugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var assign = require('object-assign');
4-
var defaultTo = require('lodash.defaultto');
4+
var defaultTo = require('ramda').defaultTo;
55
var minimatch = require('minimatch');
66
var runCompilation = require('./run-compilation');
77

@@ -67,7 +67,7 @@ LintDirtyModulesPlugin.prototype.getChangedFiles = function getChangedFiles (fil
6767
};
6868

6969
function hasFileChanged (filename, timestamp) {
70-
return defaultTo(this.prevTimestamps[filename], this.startTime) < defaultTo(timestamp, Infinity);
70+
return defaultTo(this.startTime)(this.prevTimestamps[filename]) < defaultTo(Infinity)(timestamp);
7171
}
7272

7373
module.exports = LintDirtyModulesPlugin;

lib/run-compilation.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var chalk = require('chalk');
4+
var R = require('ramda');
45
var linter = require('./linter');
56
var errorMessage = require('./constants').errorMessage;
67

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

22-
warnings = options.emitErrors === false ? results : results.filter(function (file) {
23-
return !file.errored && file.warnings && file.warnings.length;
24-
});
25-
26-
errors = options.emitErrors === false ? [] : results.filter(function (file) {
27-
return file.errored;
28-
});
23+
if (options.emitErrors === false) {
24+
warnings = results.filter(R.either(fileHasErrors, fileHasWarnings));
25+
} else {
26+
warnings = results.filter(R.both(R.complement(fileHasErrors), fileHasWarnings));
27+
errors = results.filter(fileHasErrors);
28+
}
2929

3030
if (options.quiet === false) {
3131
console.warn(options.formatter(results));
@@ -53,3 +53,11 @@ module.exports = function runCompilation (options, compiler, done) {
5353
callback();
5454
});
5555
};
56+
57+
function fileHasErrors (file) {
58+
return file.errored;
59+
}
60+
61+
function fileHasWarnings (file) {
62+
return file.warnings && file.warnings.length;
63+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
"dependencies": {
3737
"arrify": "^1.0.1",
3838
"chalk": "^1.1.3",
39-
"lodash.defaultto": "^4.14.0",
4039
"minimatch": "^3.0.3",
4140
"object-assign": "^4.1.0",
41+
"ramda": "^0.23.0",
4242
"stylelint": "^7.7.0"
4343
},
4444
"devDependencies": {

test/index.test.js

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use strict';
22

3-
var assign = require('object-assign');
4-
var td = require('testdouble');
3+
const assign = require('object-assign');
4+
const td = require('testdouble');
55

6-
var StyleLintPlugin = require('../');
6+
const StyleLintPlugin = require('../');
77

8-
var pack = require('./helpers/pack');
9-
var webpack = require('./helpers/webpack');
10-
var baseConfig = require('./helpers/base-config');
11-
var configFilePath = getPath('./.stylelintrc');
12-
var errorMessage = require('../lib/constants').errorMessage;
8+
const pack = require('./helpers/pack');
9+
const webpack = require('./helpers/webpack');
10+
const baseConfig = require('./helpers/base-config');
11+
const configFilePath = getPath('./.stylelintrc');
12+
const errorMessage = require('../lib/constants').errorMessage;
1313

1414
describe('stylelint-webpack-plugin', function () {
1515
it('works with a simple file', function () {
@@ -46,7 +46,7 @@ describe('stylelint-webpack-plugin', function () {
4646
});
4747

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

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

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

113113
context('without StyleLintPlugin configuration', function () {
114-
var config = {
115-
context: './test/fixtures/lint-free',
114+
const config = {
116115
plugins: [
117116
new StyleLintPlugin()
118117
]
119118
};
120119

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

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

156155
context('when failOnError is true', function () {
157-
var config = {
158-
context: './test/fixtures/single-error',
156+
const config = {
159157
plugins: [
160158
new StyleLintPlugin({
161159
configFile: configFilePath,
@@ -167,7 +165,7 @@ describe('stylelint-webpack-plugin', function () {
167165
};
168166

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

186184
context('when `emitErrors` is disabled', function () {
187-
it('emits errors as warnings when asked to', function () {
188-
var config = {
189-
context: './test/fixtures/single-error',
190-
plugins: [
191-
new StyleLintPlugin({
192-
configFile: configFilePath,
193-
quiet: true,
194-
emitErrors: false
195-
})
196-
]
197-
};
185+
const config = {
186+
plugins: [
187+
new StyleLintPlugin({
188+
configFile: configFilePath,
189+
quiet: true,
190+
emitErrors: false
191+
})
192+
]
193+
};
198194

199-
return pack(assign({}, baseConfig, config))
195+
it('does not print warnings or errors when there are none', function () {
196+
return pack(assign({}, baseConfig, config, { context: './test/fixtures/lint-free' }))
197+
.then(function (stats) {
198+
expect(stats.compilation.errors).to.have.length(0);
199+
expect(stats.compilation.warnings).to.have.length(0);
200+
});
201+
});
202+
203+
it('emits errors as warnings when asked to', function () {
204+
return pack(assign({}, baseConfig, config, { context: './test/fixtures/single-error' }))
200205
.then(function (stats) {
201206
expect(stats.compilation.errors).to.have.length(0);
202207
expect(stats.compilation.warnings).to.have.length(1);
@@ -205,18 +210,7 @@ describe('stylelint-webpack-plugin', function () {
205210
});
206211

207212
it('still indicates that warnings are warnings, even when emitting errors as warnings too', function () {
208-
var config = {
209-
context: './test/fixtures/rule-warning',
210-
plugins: [
211-
new StyleLintPlugin({
212-
configFile: configFilePath,
213-
quiet: true,
214-
emitErrors: false
215-
})
216-
]
217-
};
218-
219-
return pack(assign({}, baseConfig, config))
213+
return pack(assign({}, baseConfig, config, { context: './test/fixtures/rule-warning' }))
220214
.then(function (stats) {
221215
expect(stats.compilation.errors).to.have.length(0);
222216
expect(stats.compilation.warnings).to.have.length(1);
@@ -227,7 +221,7 @@ describe('stylelint-webpack-plugin', function () {
227221

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

248242
it('still skips on initial run with `emitErrors` disabled', function () {
249-
var config = {
243+
const config = {
250244
context: './test/fixtures/single-error',
251245
plugins: [
252246
new StyleLintPlugin({

0 commit comments

Comments
 (0)