forked from sasstools/sass-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (118 loc) · 3.57 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'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'),
jsonFormatter = require('eslint/lib/formatters/json'),
fs = require('fs-extra');
var sassLint = function (config) {
config = require('./lib/config')(config);
return;
};
sassLint.getConfig = function (config, configPath) {
return slConfig(config, configPath);
};
sassLint.resultCount = function (results) {
var flagCount = 0,
jsonResults = JSON.parse(jsonFormatter(results));
for (var i = 0; i < jsonResults.length; i++) {
flagCount += (jsonResults[i].warningCount + jsonResults[i].errorCount);
}
return flagCount;
};
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;
if (ast.content.length > 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 = [],
ignores = '';
if (files) {
ignores = this.getConfig(options, configPath).files.ignore || '';
files = glob.sync(files, {ignore: ignores});
}
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': options.syntax ? options.syntax : path.extname(file).replace('.', ''),
'filename': file
}, options, configPath);
results.push(lint);
});
return results;
};
sassLint.format = function (results, options, configPath) {
var config = this.getConfig(options, configPath),
format = config.options.formatter.toLowerCase();
var formatted = require('eslint/lib/formatters/' + format);
return formatted(results);
};
sassLint.outputResults = function (results, options, configPath) {
var config = this.getConfig(options, configPath);
if (this.resultCount(results)) {
var formatted = this.format(results, options, configPath);
if (config.options['output-file']) {
try {
fs.outputFileSync(path.resolve(process.cwd(), config.options['output-file']), formatted);
console.log('Output successfully written to ' + path.resolve(process.cwd(), config.options['output-file']));
}
catch (e) {
console.log('Error: Output was unable to be written to ' + path.resolve(process.cwd(), config.options['output-file']));
}
}
else {
console.log(formatted);
}
}
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;