-
Notifications
You must be signed in to change notification settings - Fork 110
/
gulp-reporters.js
125 lines (106 loc) · 3.56 KB
/
gulp-reporters.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
'use strict';
var fs = require('fs');
var gutil = require('gulp-util');
var replace = require('gulp-replace');
var reactTools = require('react-tools');
function flattenDiagnosticsVerbose(message, index) {
if (index == null) index = 0;
if (typeof message === 'undefined') {
return '';
} else if (typeof message === 'string') {
return message;
} else {
var result;
if (index === 0) {
result = message.messageText;
} else {
result = '\n> TS' + message.code + ' ' + message.messageText;
}
return result + flattenDiagnosticsVerbose(message.next, index + 1);
}
}
exports.writeErrors = function(path, errorLines) {
var data;
if (errorLines.length) {
data = errorLines.join('\n') + '\nTotal: ' + errorLines.length + ' error' + (errorLines.length > 1 ? 's' : '');
} else {
data = '';
}
fs.writeFileSync(path, data, 'utf8');
};
exports.tscLintReporterFactory = function(opt) {
var errorTexts = opt.errorTexts;
var fixPath = opt.fixPath || String;
return function(failures, file) {
failures.forEach(function (failure) {
// line + 1 because TSLint's first line and character is 0
var errorText = fixPath(file.path) +
'(' + (failure.startPosition.line + 1) + ',' + (failure.startPosition.character + 1) + '): ' +
failure.ruleName + ': ' + failure.failure;
errorTexts.push(errorText);
console.error(gutil.colors.red(errorText));
});
}
};
exports.tscReporterFactory = function(opt) {
var errorTexts = opt.errorTexts;
var fixPath = opt.fixPath || String;
var onFinish = opt.onFinish;
return {
error: function(error) {
if (error.tsFile) {
var errorText = fixPath(error.fullFilename) + '(' + error.startPosition.line + ',' + error.startPosition.character + '): error TS' + error.diagnostic.code + ' ' + flattenDiagnosticsVerbose(error.diagnostic.messageText);
errorTexts.push(errorText);
console.error(gutil.colors.red(errorText));
} else {
console.error(error.message);
}
},
finish: function(results) {
if (onFinish) onFinish();
}
}
};
exports.sassLintReporterFactory = function(opt) {
var errorTexts = opt.errorTexts;
return function(file, stream) {
if (!file.scsslint.success) {
file.scsslint.issues.forEach(function (issue) {
var linter = issue.linter ? (issue.linter + ': ') : '';
var logMsg = file.path + '(' + issue.line + ',0): ' + linter + issue.reason;
errorTexts.push(logMsg);
console.error(gutil.colors.red(logMsg));
});
}
}
};
exports.sassErrorFactory = function(opt) {
var errorTexts = opt.errorTexts;
return function(error) {
var message = error.message.replace(/\n\s\s/, '(').replace(/(\d+):(\d+)/, '$1,$2):');
errorTexts.push(message);
console.error(gutil.colors.red(message));
this.emit('end');
}
};
exports.jsxFixerFactory = function() {
return replace(/JSX\((`([^`]*)`)\)/gm, function (match, fullMatch, stringContents) {
var transformed;
if (stringContents.indexOf('\n\n') !== -1) {
return '$_can_not_have_double_new_line_in_JSX';
}
try {
transformed = reactTools.transform(stringContents);
transformed = transformed.replace(/\s+\n/g, '\n'); // Trim trailing whitespace
} catch (e) {
return e.message.replace(/\W/g, '_');
}
if (transformed.split('\n').length !== stringContents.split('\n').length) {
return '$_transformed_line_count_does_not_match';
}
if (transformed[0] === '\n') {
transformed = '(' + transformed + ')';
}
return transformed;
});
};