Skip to content

Commit deabaf6

Browse files
authored
Merge pull request #1133 from sasstools/feature/fix-output
Fix output
2 parents e3d0d9a + 64cec28 commit deabaf6

File tree

4 files changed

+58
-28
lines changed

4 files changed

+58
-28
lines changed

bin/sass-lint.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,13 @@ var tooManyWarnings = function (detects, userConfig) {
1616
return warningCount > 0 && warningCount > userConfig.options['max-warnings'];
1717
};
1818

19-
var detectPattern = function (pattern, userConfig) {
19+
var detectPattern = function (pattern) {
2020
var detects = lint.lintFiles(pattern, configOptions, configPath);
2121

22-
if (program.verbose) {
23-
lint.outputResults(detects, configOptions, configPath);
24-
}
25-
26-
if (lint.errorCount(detects).count || tooManyWarnings(detects, userConfig)) {
27-
exitCode = 1;
28-
}
29-
3022
if (program.exit) {
3123
lint.failOnError(detects, configOptions, configPath);
3224
}
25+
return detects;
3326
};
3427

3528
program
@@ -72,17 +65,28 @@ if (program.maxWarnings && program.maxWarnings !== true) {
7265
configOptions.options['max-warnings'] = program.maxWarnings;
7366
}
7467

75-
// load our config here so we only load it once for each file
76-
config = lint.getConfig(configOptions, configPath);
68+
(function () {
69+
// load our config here so we only load it once for each file
70+
config = lint.getConfig(configOptions, configPath);
71+
var results = [];
7772

78-
if (program.args.length === 0) {
79-
detectPattern(null, config);
80-
}
81-
else {
82-
program.args.forEach(function (path) {
83-
detectPattern(path, config);
84-
});
85-
}
73+
if (program.args.length === 0) {
74+
results = results.concat(detectPattern(null));
75+
}
76+
else {
77+
program.args.forEach(function (path) {
78+
results = results.concat(detectPattern(path));
79+
});
80+
}
81+
82+
if (program.verbose) {
83+
lint.outputResults(results, configOptions, configPath);
84+
}
85+
86+
if (lint.errorCount(results).count || tooManyWarnings(results, config)) {
87+
exitCode = 1;
88+
}
89+
}());
8690

8791
process.on('exit', function () {
8892
process.exit(exitCode); // eslint-disable-line no-process-exit

index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ sassLint.getConfig = function (config, configPath) {
3535
* Parses our results object to count errors and return
3636
* paths to files with detected errors.
3737
*
38-
* @param {object} results our results object
38+
* @param {Array} results our results Array
3939
* @returns {object} errors object containing the error count and paths for files incl. errors
4040
*/
4141
sassLint.errorCount = function (results) {
@@ -58,7 +58,7 @@ sassLint.errorCount = function (results) {
5858
* Parses our results object to count warnings and return
5959
* paths to files with detected warnings.
6060
*
61-
* @param {object} results our results object
61+
* @param {Array} results our results array
6262
* @returns {object} warnings object containing the error count and paths for files incl. warnings
6363
*/
6464
sassLint.warningCount = function (results) {
@@ -81,7 +81,7 @@ sassLint.warningCount = function (results) {
8181
* Parses our results object to count warnings and errors and return
8282
* a cumulative count of both
8383
*
84-
* @param {object} results our results object
84+
* @param {Array} results our results array
8585
* @returns {int} the cumulative count of errors and warnings detected
8686
*/
8787
sassLint.resultCount = function (results) {
@@ -188,7 +188,7 @@ sassLint.lintFileText = function (file, options, configPath) {
188188
* @param {string} files a glob pattern or single file path as a lint target
189189
* @param {object} options user specified rules/options passed in
190190
* @param {string} configPath path to a config file
191-
* @returns {object} results object containing all results
191+
* @returns {Array} results object containing all results
192192
*/
193193
sassLint.lintFiles = function (files, options, configPath) {
194194
var that = this,
@@ -245,10 +245,10 @@ sassLint.lintFiles = function (files, options, configPath) {
245245
/**
246246
* Handles formatting of results using EsLint formatters
247247
*
248-
* @param {object} results our results object
248+
* @param {Array} results our results array
249249
* @param {object} options user specified rules/options passed in
250250
* @param {string} configPath path to a config file
251-
* @returns {object} results our results object in the user specified format
251+
* @returns {string} results our results object in the user specified format
252252
*/
253253
sassLint.format = function (results, options, configPath) {
254254
var config = this.getConfig(options, configPath),
@@ -263,10 +263,10 @@ sassLint.format = function (results, options, configPath) {
263263
* Handles outputting results whether this be straight to the console/stdout or to a file.
264264
* Passes results to the format function to ensure results are output in the chosen format
265265
*
266-
* @param {object} results our results object
266+
* @param {Array} results our results array
267267
* @param {object} options user specified rules/options passed in
268268
* @param {string} configPath path to a config file
269-
* @returns {object} results our results object
269+
* @returns {string} the formatted results string
270270
*/
271271
sassLint.outputResults = function (results, options, configPath) {
272272
var config = this.getConfig(options, configPath);
@@ -295,7 +295,7 @@ sassLint.outputResults = function (results, options, configPath) {
295295
* Throws an error if there are any errors detected. The error includes a count of all errors
296296
* and a list of all files that include errors.
297297
*
298-
* @param {object} results - our results object
298+
* @param {Array} results - our results array
299299
* @param {object} [options] - extra options to use when running failOnError, e.g. max-warnings
300300
* @param {string} [configPath] - path to the config file
301301
* @returns {void}

tests/cli.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,29 @@ describe('cli', function () {
150150
});
151151
});
152152

153+
it('CLI format option should output valid JSON', function (done) {
154+
var command = 'node bin/sass-lint -c tests/yml/.stylish-output.yml tests/cli/*.scss --verbose --format json';
155+
156+
exec(command, function (err, stdout) {
157+
158+
if (err) {
159+
return done(err);
160+
}
161+
else {
162+
try {
163+
var result = JSON.parse(stdout);
164+
if (result && result.length && result.length > 1) {
165+
return done();
166+
}
167+
return done(new Error('Output is not combined'));
168+
}
169+
catch (e) {
170+
return done(new Error('Not JSON'));
171+
}
172+
}
173+
});
174+
});
175+
153176
it('CLI output option should write to test file', function (done) {
154177
var command = 'node bin/sass-lint -c tests/yml/.stylish-output.yml tests/cli/cli.scss --verbose --format json --output tests/cli-output.json',
155178
outputFile = path.resolve(process.cwd(), 'tests/cli-output.json');

tests/cli/cli-warn.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.cli {
2+
color: red;
3+
}

0 commit comments

Comments
 (0)