-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.js
201 lines (201 loc) · 7.23 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
/*jshint node:true */
/*jshint nomen: true */
"use strict";
// Requires
var TSLint = require("tslint");
// import * as vinyl from "vinyl";
var through = require("through");
var gutil = require("gulp-util");
var PluginError = gutil.PluginError;
var map = require("map-stream");
/**
* Helper function to check if a value is a function
* @param {any} value to check whether or not it is a function
* @returns {boolean} Returns true if the value is a function
*/
function isFunction(value) {
return Object.prototype.toString.call(value) === "[object Function]";
}
/**
* Helper function to check if a value is a string
* @param {any} value to check whether or not it is a string
* @returns {boolean} Returns true if the value is a string
*/
function isString(value) {
return Object.prototype.toString.call(value) === "[object String]";
}
/**
* Returns the TSLint from the options, or if not set, the default TSLint.
* @param {PluginOptions} options
* @returns {any} TSLint module
*/
function getTslint(options) {
if (options && options.tslint) {
return options.tslint;
}
return TSLint;
}
/**
* Log an event or error using gutil.log.
* @param {string} message the log message.
* @param {string} level can be "error". Optional.
* Leave empty for the default logging type.
*/
function log(message, level) {
var prefix = "[" + gutil.colors.cyan("gulp-tslint") + "]";
if (level === "error") {
gutil.log(prefix, gutil.colors.red("error"), message);
}
else {
gutil.log(prefix, message);
}
}
/*
* Convert a failure to the prose error format.
* @param {RuleFailure} failure
* @returns {string} The failure in the prose error formar.
*/
var proseErrorFormat = function (failure) {
var fileName = failure.getFileName();
var failureString = failure.getFailure();
var lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
var line = lineAndCharacter.line + 1;
var character = lineAndCharacter.character + 1;
return fileName + " [" + line + ", " + character + "]: " + failureString;
};
/**
* Main plugin function
* @param {PluginOptions} [pluginOptions] contains the options for gulp-tslint.
* Optional.
* @returns {any}
*/
var tslintPlugin = function (pluginOptions) {
var loader;
var tslint;
// If user options are undefined, set an empty options object
if (!pluginOptions) {
pluginOptions = {};
}
return map(function (file, cb) {
// Skip
if (file.isNull()) {
return cb(null, file);
}
// Stream is not supported
if (file.isStream()) {
return cb(new PluginError("gulp-tslint", "Streaming not supported"));
}
// TSLint default options
var options = {
fix: pluginOptions.fix || false,
formatter: pluginOptions.formatter || "prose",
formattersDirectory: pluginOptions.formattersDirectory || null,
rulesDirectory: pluginOptions.rulesDirectory || null
};
var linter = getTslint(pluginOptions);
if (pluginOptions.configuration === null ||
pluginOptions.configuration === undefined ||
isString(pluginOptions.configuration)) {
// configuration can be a file path or null, if it's unknown
pluginOptions.configuration = linter.Configuration.findConfiguration(pluginOptions.configuration || null, file.path).results;
}
tslint = new linter.Linter(options, pluginOptions.program);
tslint.lint(file.path, file.contents.toString("utf8"), pluginOptions.configuration);
file.tslint = tslint.getResult();
// Pass file
cb(null, file);
});
};
tslintPlugin.report = function (options) {
// Notify the user that the old interface is used, this can be removed at some point
if (isString(options)) {
throw new Error("Deprecated interface used! See 6.0.0 changelog " +
"https://github.com/panuhorsmalahti/gulp-tslint/blob/master/CHANGELOG.md");
}
// Default options
if (!options) {
options = {};
}
if (options.emitError === undefined) {
options.emitError = true;
}
if (options.reportLimit === undefined) {
// 0 or less is unlimited
options.reportLimit = 0;
}
if (options.summarizeFailureOutput === undefined) {
options.summarizeFailureOutput = false;
}
// Collect all files with errors
var errorFiles = [];
// Collect all failures
var allFailures = [];
// Track how many errors have been reported
var totalReported = 0;
// Log formatted output for each file individually
var reportFailures = function (file) {
var failureCount = file.tslint.failureCount;
if (failureCount > 0) {
errorFiles.push(file);
Array.prototype.push.apply(allFailures, file.tslint.failures);
if (options.reportLimit <= 0 || (options.reportLimit && options.reportLimit > totalReported)) {
if (file.tslint.output !== undefined) {
console.log(file.tslint.output);
}
totalReported += failureCount;
if (options.reportLimit > 0 &&
options.reportLimit <= totalReported) {
log("More than " + options.reportLimit
+ " failures reported. Turning off reporter.");
}
}
}
// Pass file
this.emit("data", file);
};
/**
* After reporting on all files, throw the error.
*/
var throwErrors = function () {
// Throw error
if (options && errorFiles.length > 0) {
var failuresToOutput = allFailures;
var ignoreFailureCount = 0;
// If error count is limited, calculate number of errors not shown and slice reportLimit
// number of errors to be included in the error.
if (options.reportLimit > 0) {
ignoreFailureCount = allFailures.length - options.reportLimit;
failuresToOutput = allFailures.slice(0, options.reportLimit);
}
// Always use the proseErrorFormat for the error.
var failureOutput = failuresToOutput.map(function (failure) {
return proseErrorFormat(failure);
}).join(", ");
var errorOutput = "Failed to lint: ";
if (options.summarizeFailureOutput) {
errorOutput += failuresToOutput.length + " errors.";
}
else {
errorOutput += failureOutput + ".";
}
if (ignoreFailureCount > 0) {
errorOutput += " (" + ignoreFailureCount
+ " other errors not shown.)";
}
if (options.emitError === true) {
return this.emit("error", new PluginError("gulp-tslint", errorOutput));
}
else if (options.summarizeFailureOutput) {
log(errorOutput);
}
}
// Notify through that we're done
this.emit("end");
};
return through(reportFailures, throwErrors);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = tslintPlugin;
// ES5/ES6 fallbacks
module.exports = tslintPlugin;
module.exports.default = tslintPlugin;