forked from sasstools/sass-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (88 loc) · 2.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict';
var slConfig = require('./lib/config'),
groot = require('./lib/groot'),
helpers = require('./lib/helpers'),
slRules = require('./lib/rules'),
glob = require('glob'),
path = require('path'),
fs = require('fs');
var sassLint = function (config) {
config = require('./lib/config')(config);
return;
};
sassLint.getConfig = function (config, configPath) {
return slConfig(config, configPath);
};
sassLint.lintText = function (file, options, configPath) {
var rules = slRules(this.getConfig(options, configPath)),
ast = groot(file.text, file.format, file.filename),
detects,
results = [],
errors = 0,
warnings = 0;
rules.forEach(function (rule) {
detects = rule.rule.detect(ast, rule);
results = results.concat(detects);
if (detects.length) {
if (rule.severity === 1) {
warnings += detects.length;
}
else if (rule.severity === 2) {
errors += detects.length;
}
}
});
results.sort(helpers.sortDetects);
return {
'filePath': file.filename,
'warningCount': warnings,
'errorCount': errors,
'messages': results
};
};
sassLint.lintFiles = function (files, options, configPath) {
var that = this,
results = [];
if (files) {
files = glob.sync(files);
}
else {
files = this.getConfig(options, configPath).files;
if (typeof files === 'string') {
files = glob.sync(files);
}
else {
files = glob.sync(files.include, {
'ignore': files.ignore
});
}
}
files.forEach(function (file) {
var lint = that.lintText({
'text': fs.readFileSync(file),
'format': path.extname(file).replace('.', ''),
'filename': file
}, options);
results.push(lint);
});
return results;
};
sassLint.format = function (results) {
var stylish = require('eslint/lib/formatters/stylish');
return stylish(results);
};
sassLint.outputResults = function (results) {
console.log(results);
return results;
};
sassLint.failOnError = function (results) {
var result,
i;
for (i = 0; i < results.length; i++) {
result = results[i];
if (result.errorCount > 0) {
throw new Error(result.errorCount + ' errors detected in ' + result.filePath);
}
}
};
module.exports = sassLint;