Skip to content

Commit 883cee6

Browse files
committed
- better diagnostics treatment
1 parent 903bd3f commit 883cee6

File tree

7 files changed

+422
-131
lines changed

7 files changed

+422
-131
lines changed

README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,35 @@ Following compiler options are forced though:
3333
* `noResolve`: false
3434

3535
Plugin takes following options:
36+
3637
* `check`: true
37-
- set to false to avoid doing any diagnostic checks on the code
38+
39+
Set to false to avoid doing any diagnostic checks on the code.
40+
3841
* `verbosity`: 2
39-
- goes up to 3
42+
43+
Goes up to 3.
44+
4045
* `clean`: false
41-
- set to true for clean build (wipes out cache)
46+
47+
Set to true for clean build (wipes out cache on every build).
48+
4249
* `cacheRoot`: ".rts2_cache"
43-
- path to cache
50+
51+
Path to cache.
52+
4453
* `include`: `[ "*.ts+(|x)", "**/*.ts+(|x)" ]`
45-
- passes all .ts files through typescript compiler.
54+
55+
Passes all .ts files through typescript compiler.
56+
4657
* `exclude`: `[ "*.d.ts", "**/*.d.ts" ]`
47-
- but not types
58+
59+
But excludes types.
60+
61+
* `abortOnError`: true
62+
63+
Bail out on first syntactic error. Im most cases setting this to false will result in exception in rollup itself.
64+
4865

4966
### TypeScript version
5067
This plugin currently requires TypeScript 2.0+.

dist/rollup-plugin-typescript2.cjs.js

Lines changed: 118 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,40 @@ var ConsoleContext = (function () {
7171
return ConsoleContext;
7272
}());
7373

74+
var RollupContext = (function () {
75+
function RollupContext(verbosity, bail, context, prefix) {
76+
if (prefix === void 0) { prefix = ""; }
77+
this.verbosity = verbosity;
78+
this.bail = bail;
79+
this.context = context;
80+
this.prefix = prefix;
81+
}
82+
RollupContext.prototype.warn = function (message) {
83+
if (this.verbosity < VerbosityLevel.Warning)
84+
return;
85+
this.context.warn("" + this.prefix + message);
86+
};
87+
RollupContext.prototype.error = function (message) {
88+
if (this.verbosity < VerbosityLevel.Error)
89+
return;
90+
if (this.bail)
91+
this.context.error("" + this.prefix + message);
92+
else
93+
this.context.warn("" + this.prefix + message);
94+
};
95+
RollupContext.prototype.info = function (message) {
96+
if (this.verbosity < VerbosityLevel.Info)
97+
return;
98+
this.context.warn("" + this.prefix + message);
99+
};
100+
RollupContext.prototype.debug = function (message) {
101+
if (this.verbosity < VerbosityLevel.Debug)
102+
return;
103+
this.context.warn("" + this.prefix + message);
104+
};
105+
return RollupContext;
106+
}());
107+
74108
var LanguageServiceHost = (function () {
75109
function LanguageServiceHost(parsedConfig) {
76110
this.parsedConfig = parsedConfig;
@@ -175,15 +209,33 @@ var RollingCache = (function () {
175209
return RollingCache;
176210
}());
177211

212+
function convertDiagnostic(data) {
213+
return _.map(data, function (diagnostic) {
214+
var entry = {
215+
flatMessage: ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"),
216+
category: diagnostic.category,
217+
};
218+
if (diagnostic.file) {
219+
var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character;
220+
entry.fileLine = diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + ")";
221+
}
222+
return entry;
223+
});
224+
}
178225
var Cache = (function () {
179226
function Cache(host, cache, options, rootFilenames, context) {
180227
var _this = this;
181228
this.host = host;
182229
this.options = options;
183230
this.context = context;
184-
this.cacheVersion = "1";
231+
this.cacheVersion = "2";
185232
this.ambientTypesDirty = false;
186-
this.cacheDir = cache + "/" + hash.sha1({ version: this.cacheVersion, rootFilenames: rootFilenames, options: this.options });
233+
this.cacheDir = cache + "/" + hash.sha1({
234+
version: this.cacheVersion,
235+
rootFilenames: rootFilenames,
236+
options: this.options,
237+
tsVersion: ts.version,
238+
});
187239
this.dependencyTree = new graph.Graph({ directed: true });
188240
this.dependencyTree.setDefaultNodeLabel(function (_node) { return { dirty: false }; });
189241
this.ambientTypes = _.filter(rootFilenames, function (file) { return _.endsWith(file, ".d.ts"); })
@@ -221,7 +273,8 @@ var Cache = (function () {
221273
};
222274
Cache.prototype.diagnosticsDone = function () {
223275
this.codeCache.roll();
224-
this.diagnosticsCache.roll();
276+
this.semanticDiagnosticsCache.roll();
277+
this.syntacticDiagnosticsCache.roll();
225278
this.typesCache.roll();
226279
};
227280
Cache.prototype.getCompiled = function (id, snapshot, transform) {
@@ -238,24 +291,31 @@ var Cache = (function () {
238291
this.codeCache.write(name, data);
239292
return data;
240293
};
241-
Cache.prototype.getDiagnostics = function (id, snapshot, check) {
294+
Cache.prototype.getSyntacticDiagnostics = function (id, snapshot, check) {
295+
return this.getDiagnostics(this.syntacticDiagnosticsCache, id, snapshot, check);
296+
};
297+
Cache.prototype.getSemanticDiagnostics = function (id, snapshot, check) {
298+
return this.getDiagnostics(this.semanticDiagnosticsCache, id, snapshot, check);
299+
};
300+
Cache.prototype.getDiagnostics = function (cache, id, snapshot, check) {
242301
var name = this.makeName(id, snapshot);
243-
if (!this.diagnosticsCache.exists(name) || this.isDirty(id, snapshot, true)) {
302+
if (!cache.exists(name) || this.isDirty(id, snapshot, true)) {
244303
this.context.debug("fresh diagnostics for: " + id);
245-
var data_2 = this.convert(check());
246-
this.diagnosticsCache.write(name, data_2);
304+
var data_2 = convertDiagnostic(check());
305+
cache.write(name, data_2);
247306
this.markAsDirty(id, snapshot);
248307
return data_2;
249308
}
250309
this.context.debug("old diagnostics for: " + id);
251-
var data = this.diagnosticsCache.read(name);
252-
this.diagnosticsCache.write(name, data);
310+
var data = cache.read(name);
311+
cache.write(name, data);
253312
return data;
254313
};
255314
Cache.prototype.init = function () {
256315
this.codeCache = new RollingCache(this.cacheDir + "/code", true);
257316
this.typesCache = new RollingCache(this.cacheDir + "/types", false);
258-
this.diagnosticsCache = new RollingCache(this.cacheDir + "/diagnostics", false);
317+
this.syntacticDiagnosticsCache = new RollingCache(this.cacheDir + "/syntacticDiagnostics", false);
318+
this.semanticDiagnosticsCache = new RollingCache(this.cacheDir + "/semanticDiagnostics", false);
259319
};
260320
Cache.prototype.markAsDirty = function (id, _snapshot) {
261321
this.context.debug("changed: " + id);
@@ -286,18 +346,6 @@ var Cache = (function () {
286346
var data = snapshot.getText(0, snapshot.getLength());
287347
return hash.sha1({ data: data, id: id });
288348
};
289-
Cache.prototype.convert = function (data) {
290-
return _.map(data, function (diagnostic) {
291-
var entry = {
292-
flatMessage: ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"),
293-
};
294-
if (diagnostic.file) {
295-
var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character;
296-
entry.fileLine = diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + ")";
297-
}
298-
return entry;
299-
});
300-
};
301349
return Cache;
302350
}());
303351

@@ -352,10 +400,27 @@ function parseTsConfig() {
352400
}
353401
function printDiagnostics(context, diagnostics) {
354402
_.each(diagnostics, function (diagnostic) {
403+
var print;
404+
var color;
405+
switch (diagnostic.category) {
406+
case ts.DiagnosticCategory.Message:
407+
print = context.info;
408+
color = colors.white;
409+
break;
410+
case ts.DiagnosticCategory.Error:
411+
print = context.error;
412+
color = colors.red;
413+
break;
414+
case ts.DiagnosticCategory.Warning:
415+
default:
416+
print = context.warn;
417+
color = colors.yellow;
418+
break;
419+
}
355420
if (diagnostic.fileLine)
356-
context.warn(diagnostic.fileLine + ": " + colors.yellow(diagnostic.flatMessage));
421+
print.call(context, [diagnostic.fileLine + ": " + color(diagnostic.flatMessage)]);
357422
else
358-
context.warn(colors.yellow(diagnostic.flatMessage));
423+
print.call(context, [color(diagnostic.flatMessage)]);
359424
});
360425
}
361426

@@ -368,6 +433,7 @@ function typescript(options) {
368433
cacheRoot: process.cwd() + "/.rts2_cache",
369434
include: ["*.ts+(|x)", "**/*.ts+(|x)"],
370435
exclude: ["*.d.ts", "**/*.d.ts"],
436+
abortOnError: true,
371437
});
372438
var filter$$1 = createFilter(options.include, options.exclude);
373439
var parsedConfig = parseTsConfig();
@@ -403,17 +469,21 @@ function typescript(options) {
403469
var _this = this;
404470
if (!filter$$1(id))
405471
return undefined;
472+
var contextWrapper = new RollupContext(options.verbosity, options.abortOnError, this, "rollup-plugin-typescript2: ");
473+
contextWrapper.debug(id);
406474
var snapshot = servicesHost.setSnapshot(id, code);
475+
// getting compiled file from cache of from ts
407476
var result = cache.getCompiled(id, snapshot, function () {
408477
var output = services.getEmitOutput(id);
409478
if (output.emitSkipped) {
410-
var diagnostics = cache.getDiagnostics(id, snapshot, function () {
411-
return services
412-
.getCompilerOptionsDiagnostics()
413-
.concat(services.getSyntacticDiagnostics(id))
414-
.concat(services.getSemanticDiagnostics(id));
415-
});
416-
printDiagnostics(_this, diagnostics);
479+
if (options.check) {
480+
var diagnostics = cache.getSyntacticDiagnostics(id, snapshot, function () {
481+
return services.getSyntacticDiagnostics(id);
482+
});
483+
contextWrapper.debug("printDiagnostics");
484+
printDiagnostics(contextWrapper, diagnostics);
485+
}
486+
// if no output was generated, aborting compilation
417487
_this.error(colors.red("failed to transpile " + id));
418488
}
419489
var transpiled = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".js"); });
@@ -423,23 +493,35 @@ function typescript(options) {
423493
map: map$$1 ? JSON.parse(map$$1.text) : { mappings: "" },
424494
};
425495
});
496+
// printing syntactic errors
497+
if (options.check) {
498+
var diagnostics = cache.getSyntacticDiagnostics(id, snapshot, function () {
499+
return services.getSyntacticDiagnostics(id);
500+
});
501+
contextWrapper.debug("printDiagnostics");
502+
printDiagnostics(contextWrapper, diagnostics);
503+
}
426504
return result;
427505
},
506+
intro: function () {
507+
context.debug("intro");
508+
// printing compiler option errors
509+
if (options.check)
510+
printDiagnostics(context, convertDiagnostic(services.getCompilerOptionsDiagnostics()));
511+
},
428512
outro: function () {
429513
context.debug("outro");
430514
cache.compileDone();
515+
// printing semantic errors
431516
if (options.check) {
432517
cache.walkTree(function (id) {
433518
var snapshot = servicesHost.getScriptSnapshot(id);
434519
if (!snapshot) {
435520
context.error(colors.red("failed lo load snapshot for " + id));
436521
return;
437522
}
438-
var diagnostics = cache.getDiagnostics(id, snapshot, function () {
439-
return services
440-
.getCompilerOptionsDiagnostics()
441-
.concat(services.getSyntacticDiagnostics(id))
442-
.concat(services.getSemanticDiagnostics(id));
523+
var diagnostics = cache.getSemanticDiagnostics(id, snapshot, function () {
524+
return services.getSemanticDiagnostics(id);
443525
});
444526
printDiagnostics(context, diagnostics);
445527
});

0 commit comments

Comments
 (0)