-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
173 lines (156 loc) · 5.42 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
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 = ['onError',
'disableTestGeneration',
'testFailingFiles',
'testPassingFiles' ,
'testGenerator',
'linterConfig',
'log',
'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:{}};
if(!options.linterConfig){
options.linterConfig = {};
}
this.log = true;
if(typeof options['disableConsoleLogging'] !== "undefined"){
console.warn('"disableConsoleLogging" propety has been deprecated in favour of "log"');
this.log = !options['disableConsoleLogging'];
}
for(var i = 0; i < this.availableOptions.length; i++){
var option = this.availableOptions[i];
if(typeof options[option] === "undefined" || options[option] === null){
continue;
}
this[option] = options[option];
delete options[option];
}
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.syntax);
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(syntax) {
var extensions = [];
var targetExtension;
if(!syntax)
syntax = 'scss';
this.linterConfig.syntax = syntax;
if(syntax === 'sugarss') {
targetExtension = 'sss';
} else {
targetExtension = 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
* - Logs to console
* - Generate tests
* @override
*/
StyleLinter.prototype.processString = function(content, relativePath) {
var _this = 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.results[0].source = relativePath;
if(results.errored){
if(_this.onError)
_this.onError(results.results[0]);
if(_this.log )
_this.console.log(results.output);
if(_this.testFailingFiles){
return _this.testGenerator(relativePath, results.results[0]);
} else {
return '';
}
} else {
if(_this.testPassingFiles){
return _this.testGenerator(relativePath);
}else {
return '';
}
}
})
.catch(function(err) {
// do things with err e.g.
console.error(err.stack);
});
};
/**
* @method testGenerator
*
* Alias of escapeString for hooks
*/
StyleLinter.prototype.escapeErrorString = escapeString;
/**
* @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;