forked from billybonks/broccoli-stylelint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
215 lines (194 loc) · 6.8 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
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
var Filter = require('broccoli-persistent-filter');
var escapeString = require('js-string-escape');
var stylelint = require('stylelint');
var merge = require('merge');
/* Setup class */
StyleLinter.prototype = Object.create(Filter.prototype);
StyleLinter.prototype.constructor = StyleLinter;
/* Used to extract and delete options from input hash */
StyleLinter.prototype.availableOptions = [{name: 'onError'},
{name: 'disableTestGeneration'},
{name: 'testFailingFiles'},
{name: 'testPassingFiles'},
{name: 'testGenerator', default: StyleLinter.prototype.testGenerator},
{name: 'consoleLogger', default: StyleLinter.prototype.consoleLogger},
{name: 'linterConfig', default: {}},
{name: 'log', default: true},
{name: 'console', default: console}];
/**
* Creates a new StyleLinter instance.
* Options
* - linterConfig (StyleLint options)
* - onError (Hook when error occurs)
* - testGenerator (Hook for custom test generation)
* - disableTestGeneration (Disable generatation tests for all files)
* - testFailingFiles (Generate tests for failing files)
* - testPassingFiles (Generate tests for passing files)
* - log (Disables error logging in console)
* - console (Custom console)
* @class
*/
function StyleLinter(inputNodes, options) {
this.options = options || {linterConfig:{}};
for(var i = 0; i < this.availableOptions.length; i++){
var option = this.availableOptions[i];
var name = option.name;
var defaultValue = option.default || this[name];
this[name] = typeof options[name] === "undefined" ? defaultValue : options[name];
delete options[name];
}
//TODO:remove this deprecation on v1 release
if(typeof options['disableConsoleLogging'] !== "undefined"){
console.warn('"disableConsoleLogging" propety has been deprecated in favour of "log"');
this.log = !options['disableConsoleLogging'];
}
merge(this.linterConfig, {
formatter: 'string'
});
if(typeof this.testFailingFiles === 'undefined' && typeof this.testPassingFiles === 'undefined' && typeof this.disableTestGeneration === 'undefined'){
this.testFailingFiles = true;
this.testPassingFiles = true;
}else if( typeof this.disableTestGeneration !== 'undefined' ){
this.testFailingFiles = typeof this.testFailingFiles === 'undefined' ? !this.disableTestGeneration : this.testFailingFiles;
this.testPassingFiles = typeof this.testPassingFiles === 'undefined' ? !this.disableTestGeneration : this.testPassingFiles;
}
this.linterConfig.files = null;
this.setSyntax(this.linterConfig);
Filter.call(this, inputNodes, options);
}
/**
* Sets the, file extensions that the broccoli plugin must parse
* @param {string} syntax sass|css|less|sugarss
*/
StyleLinter.prototype.setSyntax = function(config) {
var syntax = config.syntax;
var extensions = [];
var targetExtension;
if(!syntax)
syntax = 'scss';
this.linterConfig.syntax = syntax;
if(syntax === 'sugarss') {
targetExtension = 'sss';
} else {
targetExtension = syntax;
}
if(syntax === 'css'){
config.syntax = "";
}
extensions.push(targetExtension);
if(this.testPassingFiles || this.testFailingFiles)
targetExtension = 'stylelint-test.js';
this.extensions = extensions;
this.targetExtension = targetExtension;
};
/** Filter Class Overrides **/
/**
* Entry point for broccoli build
* @override
*/
StyleLinter.prototype.build = function() {
return Filter.prototype.build.call(this).finally(function() {
});
};
/**
* This method is executed for every scss file, it:
* - Calls onError
* @override
*/
StyleLinter.prototype.processString = function(content, relativePath) {
var self = this;
this.linterConfig.code = content;
this.linterConfig.codeFilename = relativePath;
return stylelint.lint(this.linterConfig).then(function(results){
//sets the value to relative path otherwise it would be absolute path
results = self.processResults(results, relativePath);
if(results.errored && self.testFailingFiles) {
results.output = self.testGenerator(relativePath, results);
} else if(!results.errored && self.testPassingFiles) {
results.output = self.testGenerator(relativePath);
}
return results;
}).catch(function(err) {
console.error(err.stack);
});
};
/**
* @method postProcess
* This method is called after, the file has been linted:
* - Logs to console
* - Generate tests
* @override
*/
StyleLinter.prototype.postProcess = function(results, relativePath) {
if(results.errored){
if(this.onError) {
this.onError(results);
}
if(this.log)
this.consoleLogger(results, relativePath);
}
return results;
};
/**
* @method processResults
*
* Reformats default results object
* {
* errored: boolean if file errored or not,
* output: String contains test if generate test is true,
* log: String default logging string,
* source: String relitivePath,
* deprecations: Array of errors,
* invalidOptionWarnings: Array,
* warnings: Array of errors,
* ignored: Array ignored files,
* _postcssResult: Object for postcss
* }
*/
StyleLinter.prototype.processResults = function(results, relativePath) {
var resultsInner = results.results[0];
resultsInner.errored = results.errored;
resultsInner.source = relativePath;
delete results.results;
results.log = results.output;
Object.assign(results, resultsInner);
results.source = relativePath;
results.output = '';
return results;
};
/**
* @method testGenerator
*
* Alias of escapeString for hooks
*/
StyleLinter.prototype.escapeErrorString = escapeString;
/**
* @method consoleLogger
*
* Geneartes tests.
*/
StyleLinter.prototype.consoleLogger = function(results, relativePath) {
this.console.log(results.log);
};
/**
* @method testGenerator
*
* Geneartes tests.
*/
StyleLinter.prototype.testGenerator = function(relativePath, errors) {
var assertions = [];
var module = "module('Style Lint');\n";
var test = "test('" + relativePath + " should pass stylelint', function() {\n";
if(!errors){
var assertion = " ok(\'true , "+relativePath+" passed stylelint\');";
return module+test+assertion+"\n});\n";
} else {
for(var i = 0; i < errors.warnings.length; i++){
var warning = errors.warnings[i];
var index = warning.line+':'+warning.column;
assertions.push(" ok(" + false + ", '"+index +" "+this.escapeErrorString(warning.text)+"');");
}
return module+test+assertions.join('\n')+"\n});\n";
}
};
module.exports = StyleLinter;