-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinter.js
246 lines (215 loc) · 6.19 KB
/
linter.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
'use strict';
/**
* @module happiness-scss
* @author Oleg Dutchenko <[email protected]>
*/
// ----------------------------------------
// Imports
// ----------------------------------------
const path = require('path');
const sassLint = require('./libs/sass-lint');
// const wipStatusMessage = require('./test/wip-status-message');
// ----------------------------------------
// Info message
// ----------------------------------------
// wipStatusMessage();
// ----------------------------------------
// Private
// ----------------------------------------
/**
* Return correct path to `node_modules` folder in Current Working Directory (cwd)
* @returns {string}
*/
function ignoreNodeModules () {
return path.join(process.cwd(), './node_modules/**');
}
/**
* Transform user config for linter config
* @private
* @param {Object} [config]
* @returns {Object}
*/
function transformConfig (config) {
if (config === null || typeof config !== 'object') {
config = {};
}
if (!Object.keys(config).length) {
return {
options: {},
files: {
ignore: [ignoreNodeModules()]
}
};
}
let runConfig = {
options: {
showMaxStack: 0
}
};
if (config.noDisabling) {
runConfig.options.noDisabling = true;
}
if (config.formatter) {
runConfig.options.formatter = config.formatter;
}
if (config.showMaxStack >= 0) {
runConfig.options.showMaxStack = config.showMaxStack;
}
if (config.outputFile) {
runConfig.options['output-file'] = config.outputFile;
}
if (Array.isArray(config.ignore)) {
runConfig.files = {
ignore: config.ignore.concat(ignoreNodeModules())
};
} else {
runConfig.files = {
ignore: [ignoreNodeModules()]
};
}
return runConfig;
}
/**
* Creating linting methods with happiness-scss configPath
* @param {string} mtd
* @param {string} file
* @param {Object} [config]
* @param {function} [cb]
* @param {string} configPath
*/
function lintMethod (mtd, file, config, cb, configPath) {
let runConfig = transformConfig(config);
if (typeof cb !== 'function') {
cb = function () {};
}
try {
let results = sassLint[mtd](file, runConfig, configPath);
if (mtd !== 'lintFiles') {
if (!Array.isArray(results)) {
results = [results];
}
}
let data = {
results,
errorCount: sassLint.errorCount(results),
warningCount: sassLint.warningCount(results)
};
cb(null, data);
} catch (err) {
cb(err);
}
}
class Linter {
/**
* @param {string} configPath
*/
constructor (configPath) {
this.configPath = configPath;
}
/**
* Runs each rule against sass-lint AST tree.
* After linting - run callback for data processing
*
* Not usefull, duplicate .lintFileText()
* @deprecated
*
* @param {Object} file file object from fs.readFileSync
* @param {Object} config user specified rules/config passed in
* @param {function} cb
*/
// lintText (file, config, cb) {
// return lintMethod('lintText', file, config, cb, this.configPath);
// }
/**
* Handles ignored files for plugins such as the gulp plugin. Checks every file passed to it against
* the ignores as specified in users config or passed in default config.
* After linting - run callback for data processing
*
* @param {Object} file - The file/text to be linted
* @param {Object} config - The user defined config directly passed in
* @param {function} cb
*/
lintFileText (file, config, cb) {
return lintMethod('lintFileText', file, config, cb, this.configPath);
}
/**
* Takes a glob pattern or target string and creates an array of files as targets for
* linting taking into account any user specified ignores.
* After linting - run callback for data processing
*
* @param {string} files a glob pattern or single file path as a lint target
* @param {Object} config user specified rules/config passed in
* @param {function} cb
*/
lintFiles (files, config, cb) {
return lintMethod('lintFiles', files, config, cb, this.configPath);
}
/**
* Parses results object to count errors and return
* paths to files with detected errors.
*
* @param {Object} results results object
* @returns {Object} errors object containing the error count and paths for files incl. errors
*/
errorCount (results) {
sassLint.errorCount(results);
}
/**
* Parses results object to count warnings and return
* paths to files with detected warnings.
*
* @param {Object} results results object
* @returns {Object} warnings object containing the error count and paths for files incl. warnings
*/
warningCount (results) {
return sassLint.warningCount(results);
}
/**
* Parses results object to count warnings and errors and return
* a cumulative count of both
*
* @param {Object} results results object
* @returns {int} the cumulative count of errors and warnings detected
*/
resultCount (results) {
return sassLint.resultCount(results);
}
/**
* Handles formatting of results using EsLint formatters
*
* @param {Object} results our results object
* @param {Object} config user specified rules/config passed in
* @returns {Object} results results in the specified format as string
*/
format (results, config) {
return sassLint.format(results, transformConfig(config), this.configPath);
}
/**
* Handles outputting results whether this be straight to the console/stdout or to a file.
* Passes results to the format function to ensure results are output in the chosen format
*
* @param {Object} results our results object
* @param {Object} config user specified rules/config passed in
* @returns {Object} results our results object
*/
outputResults (results, config) {
config = transformConfig(config);
return sassLint.outputResults(results, config, this.configPath);
}
/**
* Throws an error if there are any errors detected. The error includes a count of all errors
* and a list of all files that include errors.
*
* @param {Object} results - our results object
* @param {Object} [config] - extra config to use when running failOnError
* @returns {void}
*/
failOnError (results, config) {
config = transformConfig(config);
return sassLint.failOnError(results, config, this.configPath);
}
}
// ----------------------------------------
// Exports
// ----------------------------------------
module.exports = Linter;