Skip to content

Commit 93b78b9

Browse files
authored
Merge pull request #22 from vegarringdal/master
V2.3.0
2 parents 97aa58e + 93b6239 commit 93b78b9

File tree

13 files changed

+229
-58
lines changed

13 files changed

+229
-58
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var TypeHelper = require('fuse-box-typechecker').TypeHelper
1313

1414

1515
// it checks entire program every time
16+
// see interface at bottom at readmefile for all options
1617

1718

1819
// Sync check
@@ -27,6 +28,7 @@ testSync.runSync();
2728

2829

2930

31+
3032
// Async check (worker)
3133
var testAsync = TypeHelper({
3234
tsConfig: './tsconfig.json',
@@ -47,6 +49,25 @@ var testWatch = TypeHelper({
4749
testWatch.runWatch('./src');
4850

4951

52+
53+
// as promise/async/await
54+
55+
var doTypeCheck = async() => {
56+
57+
var checker = TypeHelper({
58+
tsConfig: './tsconfig.json',
59+
basePath: './',
60+
name: 'Test Sync'
61+
})
62+
63+
let totalErrors = await checker.runPromise();
64+
Console.log(totalErrors)
65+
}
66+
67+
doTypeCheck();
68+
69+
70+
5071
```
5172

5273
### Output sample
@@ -61,10 +82,16 @@ interface OptionsInterface {
6182
throwOnSemantic?: boolean; // if you want it to throwe error
6283
throwOnGlobal?: boolean; // if you want it to throwe error
6384
throwOnOptions?: boolean; // if you want it to throwe error
85+
throwOnTsLint?: boolean; // trhow on lint errors
6486
basePath: string; //base path to use
6587
name?: string; // name, will be displayed when it runs, useful when you have more then 1
6688
tsLint:string; //config file (compared to basepath './tslint.json')
6789
lintoptions? Lintoptions; // se below, optional
90+
yellowLint?: boolean; // use yellow color instead of red on TSLint erros
91+
yellowOnOptions?: boolean; // use yellow color instead of red on Options erros
92+
yellowOnGlobal?: boolean; // use yellow color instead of red on Global erros
93+
yellowOnSemantic?: boolean; // use yellow color instead of red on Semantic erros
94+
yellowOnSyntactic?: boolean; // use yellow color instead of red on Syntactic erros
6895
}
6996

7097

dist/commonjs/checker.js

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,30 @@ var Checker = (function () {
1919
var start = new Date().getTime();
2020
var parsed = ts.parseJsonConfigFileContent(this.tsConfig, parseConfigHost, options.basePath || '.', null);
2121
this.program = ts.createProgram(parsed.fileNames, parsed.options, null, this.program);
22-
this.diagnostics = ts.getPreEmitDiagnostics(this.program);
22+
this.diagnostics = [];
23+
var optionsErrors = this.program.getOptionsDiagnostics().map(function (obj) {
24+
obj._type = 'options';
25+
return obj;
26+
});
27+
this.diagnostics = this.diagnostics.concat(optionsErrors);
28+
var globalErrors = this.program.getGlobalDiagnostics().map(function (obj) {
29+
obj._type = 'global';
30+
return obj;
31+
});
32+
this.diagnostics = this.diagnostics.concat(globalErrors);
33+
var syntacticErrors = this.program.getSyntacticDiagnostics().map(function (obj) {
34+
obj._type = 'syntactic';
35+
return obj;
36+
});
37+
this.diagnostics = this.diagnostics.concat(syntacticErrors);
38+
var semanticErrors = this.program.getSemanticDiagnostics().map(function (obj) {
39+
obj._type = 'semantic';
40+
return obj;
41+
});
42+
this.diagnostics = this.diagnostics.concat(semanticErrors);
2343
this.lintResults = [];
2444
if (options.tsLint) {
2545
var fullPath = path.resolve(this.options.basePath, options.tsLint);
26-
var fileName = path.basename(fullPath);
27-
var basePath = path.dirname(fullPath);
28-
console.log("f:" + fileName + " base:" + basePath);
2946
this.files = tslint.Linter.getFileNames(this.program);
3047
var config_1 = tslint.Configuration.findConfiguration(fullPath, this.options.basePath).results;
3148
this.lintResults = this.files.map(function (file) {
@@ -55,14 +72,14 @@ var Checker = (function () {
5572
fileName: failure.fileName,
5673
line: failure.startPosition.lineAndCharacter.line,
5774
char: failure.startPosition.lineAndCharacter.character,
58-
ruleSeverity: failure.ruleSeverity,
75+
ruleSeverity: failure.ruleSeverity.charAt(0).toUpperCase() + failure.ruleSeverity.slice(1),
5976
ruleName: failure.ruleName,
6077
failure: failure.failure
6178
};
6279
var message = chalk.red('└── ');
63-
message += chalk.red(r.fileName + ": (" + (r.line + 1) + ":" + (r.char + 1) + "):");
64-
message += chalk.white(r.ruleSeverity);
65-
message += chalk.white(" TSLint: \"" + r.ruleName + "\":");
80+
message += chalk[options.yellowOnLint ? 'yellow' : 'red'](r.fileName + " (" + (r.line + 1) + "," + (r.char + 1) + ") ");
81+
message += chalk.white("(" + r.ruleSeverity + ":");
82+
message += chalk.white(r.ruleName + ")");
6683
message += ' ' + r.failure;
6784
return message;
6885
});
@@ -83,11 +100,28 @@ var Checker = (function () {
83100
if (diagnostics.length > 0) {
84101
messages = diagnostics.map(function (diag) {
85102
var message = chalk.red('└── ');
103+
var color;
104+
switch (diag._type) {
105+
case 'options':
106+
color = options.yellowOnOptions ? 'yellow' : 'red';
107+
break;
108+
case 'global':
109+
color = options.yellowOnGlobal ? 'yellow' : 'red';
110+
break;
111+
case 'syntactic':
112+
color = options.yellowOnSyntactic ? 'yellow' : 'red';
113+
break;
114+
case 'semantic':
115+
color = options.yellowOnSemantic ? 'yellow' : 'red';
116+
break;
117+
default:
118+
color = 'red';
119+
}
86120
if (diag.file) {
87121
var _a = diag.file.getLineAndCharacterOfPosition(diag.start), line = _a.line, character = _a.character;
88-
message += chalk.red(diag.file.fileName + ": (" + (line + 1) + ":" + (character + 1) + "):");
89-
message += chalk.white(ts.DiagnosticCategory[diag.category]);
90-
message += chalk.white(" TS" + diag.code + ":");
122+
message += chalk[color](diag.file.fileName + " (" + (line + 1) + "," + (character + 1) + ") ");
123+
message += chalk.white("(" + ts.DiagnosticCategory[diag.category] + ":");
124+
message += chalk.white("TS" + diag.code + ")");
91125
}
92126
message += ' ' + ts.flattenDiagnosticMessageText(diag.messageText, END_LINE);
93127
return message;
@@ -96,6 +130,12 @@ var Checker = (function () {
96130
var x = messages.concat(lintResults);
97131
write(x.join('\n'));
98132
}
133+
else {
134+
if (lintResults.length > 0) {
135+
lintResults.unshift(chalk.underline(END_LINE + "File errors") + chalk.white(':'));
136+
write(lintResults.join('\n'));
137+
}
138+
}
99139
var optionsErrors = program.getOptionsDiagnostics().length;
100140
var globalErrors = program.getGlobalDiagnostics().length;
101141
var syntacticErrors = program.getSyntacticDiagnostics().length;
@@ -105,11 +145,11 @@ var Checker = (function () {
105145
write(chalk.underline("" + END_LINE + END_LINE + "Errors") +
106146
chalk.white(":" + totals + END_LINE));
107147
if (totals) {
108-
write(chalk[optionsErrors ? 'red' : 'white']("\u2514\u2500\u2500 Options: " + optionsErrors + END_LINE));
109-
write(chalk[globalErrors ? 'red' : 'white']("\u2514\u2500\u2500 Global: " + globalErrors + END_LINE));
110-
write(chalk[syntacticErrors ? 'red' : 'white']("\u2514\u2500\u2500 Syntactic: " + syntacticErrors + END_LINE));
111-
write(chalk[semanticErrors ? 'red' : 'white']("\u2514\u2500\u2500 Semantic: " + semanticErrors + END_LINE));
112-
write(chalk[tsLintErrors ? 'red' : 'white']("\u2514\u2500\u2500 TsLint: " + tsLintErrors + END_LINE + END_LINE));
148+
write(chalk[optionsErrors ? options.yellowOnOptions ? 'yellow' : 'red' : 'white']("\u2514\u2500\u2500 Options: " + optionsErrors + END_LINE));
149+
write(chalk[globalErrors ? options.yellowOnGlobal ? 'yellow' : 'red' : 'white']("\u2514\u2500\u2500 Global: " + globalErrors + END_LINE));
150+
write(chalk[syntacticErrors ? options.yellowOnSyntactic ? 'yellow' : 'red' : 'white']("\u2514\u2500\u2500 Syntactic: " + syntacticErrors + END_LINE));
151+
write(chalk[semanticErrors ? options.yellowOnSemantic ? 'yellow' : 'red' : 'white']("\u2514\u2500\u2500 Semantic: " + semanticErrors + END_LINE));
152+
write(chalk[tsLintErrors ? options.yellowOnLint ? 'yellow' : 'red' : 'white']("\u2514\u2500\u2500 TsLint: " + tsLintErrors + END_LINE + END_LINE));
113153
}
114154
write(chalk.grey("Typechecking time: " + this.elapsed + "ms" + END_LINE));
115155
switch (true) {

dist/commonjs/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export declare class TypeHelperClass {
66
private monitor;
77
constructor(options: OptionsInterface);
88
runAsync(): void;
9-
runSync(): void;
9+
runSync(): number;
10+
runPromise(): Promise<number>;
1011
runWatch(pathToWatch: string): void;
1112
killWorker(): void;
1213
private configureWorker(options);

dist/commonjs/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,21 @@ var TypeHelperClass = (function () {
3737
TypeHelperClass.prototype.runSync = function () {
3838
var options = Object.assign(this.options, { finished: true, type: 'sync' });
3939
this.checker.configure(options);
40-
this.checker.typecheck();
40+
return this.checker.typecheck();
41+
};
42+
TypeHelperClass.prototype.runPromise = function () {
43+
var _this = this;
44+
return new Promise(function (resolve, reject) {
45+
try {
46+
var options = Object.assign(_this.options, { finished: true, type: 'sync' });
47+
_this.checker.configure(options);
48+
var errors = _this.checker.typecheck();
49+
resolve(errors);
50+
}
51+
catch (err) {
52+
reject(err);
53+
}
54+
});
4155
};
4256
TypeHelperClass.prototype.runWatch = function (pathToWatch) {
4357
var _this = this;

dist/commonjs/interfaces.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ export interface OptionsInterface {
1212
throwOnTsLint?: boolean;
1313
throwOnGlobal?: boolean;
1414
throwOnOptions?: boolean;
15+
yellowOnLint?: boolean;
16+
yellowOnOptions?: boolean;
17+
yellowOnGlobal?: boolean;
18+
yellowOnSemantic?: boolean;
19+
yellowOnSyntactic?: boolean;
1520
tsLint?: string;
16-
tsConfigObj: any;
21+
tsConfigObj?: any;
1722
basePath: string;
1823
name?: string;
1924
type?: string;

dist/commonjs/test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
Object.defineProperty(exports, "__esModule", { value: true });

image/sampleNew2.png

63.4 KB
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fuse-box-typechecker",
3-
"version": "2.2.1",
3+
"version": "2.3.0",
44
"description": "Fuse-Box type checker plugin",
55
"keywords": [
66
"fuse-box-bundler",

src/checker.ts

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,32 @@ export class Checker {
4040
// get program and get diagnostics and store them diagnostics
4141
const parsed = ts.parseJsonConfigFileContent(this.tsConfig, parseConfigHost, options.basePath || '.', null);
4242
this.program = ts.createProgram(parsed.fileNames, parsed.options, null, this.program);
43-
this.diagnostics = ts.getPreEmitDiagnostics(this.program);
43+
44+
// get errors and tag them;
45+
this.diagnostics = [];
46+
let optionsErrors = this.program.getOptionsDiagnostics().map((obj) => {
47+
(<any>obj)._type = 'options';
48+
return obj;
49+
});
50+
this.diagnostics = this.diagnostics.concat(optionsErrors);
51+
52+
let globalErrors = this.program.getGlobalDiagnostics().map((obj) => {
53+
(<any>obj)._type = 'global';
54+
return obj;
55+
});
56+
this.diagnostics = this.diagnostics.concat(globalErrors);
57+
58+
let syntacticErrors = this.program.getSyntacticDiagnostics().map((obj) => {
59+
(<any>obj)._type = 'syntactic';
60+
return obj;
61+
});
62+
this.diagnostics = this.diagnostics.concat(syntacticErrors);
63+
64+
let semanticErrors = this.program.getSemanticDiagnostics().map((obj) => {
65+
(<any>obj)._type = 'semantic';
66+
return obj;
67+
});
68+
this.diagnostics = this.diagnostics.concat(semanticErrors);
4469

4570
// get tslint if json file is supplied
4671
this.lintResults = [];
@@ -95,15 +120,15 @@ export class Checker {
95120
fileName: failure.fileName,
96121
line: failure.startPosition.lineAndCharacter.line,
97122
char: failure.startPosition.lineAndCharacter.character,
98-
ruleSeverity: failure.ruleSeverity,
123+
ruleSeverity: failure.ruleSeverity.charAt(0).toUpperCase() + failure.ruleSeverity.slice(1),
99124
ruleName: failure.ruleName,
100125
failure: failure.failure
101126
};
102127

103128
let message = chalk.red('└── ');
104-
message += chalk.red(`${r.fileName}: (${r.line + 1}:${r.char + 1}):`);
105-
message += chalk.white(r.ruleSeverity);
106-
message += chalk.white(` TSLint: "${r.ruleName}":`);
129+
message += chalk[options.yellowOnLint ? 'yellow' : 'red'](`${r.fileName} (${r.line + 1},${r.char + 1}) `);
130+
message += chalk.white(`(${r.ruleSeverity}:`);
131+
message += chalk.white(`${r.ruleName})`);
107132
message += ' ' + r.failure;
108133
return message;
109134
});
@@ -130,16 +155,35 @@ export class Checker {
130155
// get message type error, warn, info
131156
let message = chalk.red('└── ');
132157

158+
// set color from options
159+
let color: string;
160+
switch (diag._type) {
161+
case 'options':
162+
color = options.yellowOnOptions ? 'yellow' : 'red';
163+
break;
164+
case 'global':
165+
color = options.yellowOnGlobal ? 'yellow' : 'red';
166+
break;
167+
case 'syntactic':
168+
color = options.yellowOnSyntactic ? 'yellow' : 'red';
169+
break;
170+
case 'semantic':
171+
color = options.yellowOnSemantic ? 'yellow' : 'red';
172+
break;
173+
default:
174+
color = 'red';
175+
}
176+
133177
// if file
134178
if (diag.file) {
135179
const {
136180
line,
137181
character
138182
} = diag.file.getLineAndCharacterOfPosition(diag.start);
139183

140-
message += chalk.red(`${diag.file.fileName}: (${line + 1}:${character + 1}):`);
141-
message += chalk.white(ts.DiagnosticCategory[diag.category]);
142-
message += chalk.white(` TS${diag.code}:`);
184+
message += chalk[color](`${diag.file.fileName} (${line + 1},${character + 1}) `);
185+
message += chalk.white(`(${ts.DiagnosticCategory[diag.category]}:`);
186+
message += chalk.white(`TS${diag.code})`);
143187
}
144188

145189
// flatten error message
@@ -156,6 +200,15 @@ export class Checker {
156200
let x = messages.concat(lintResults);
157201
write(x.join('\n'));
158202

203+
} else {
204+
205+
// no type errors, lets just print the lint errors if any
206+
if (lintResults.length > 0) {
207+
lintResults.unshift(
208+
chalk.underline(`${END_LINE}File errors`) + chalk.white(':') // fix windows
209+
);
210+
write(lintResults.join('\n'));
211+
}
159212
}
160213

161214
let optionsErrors = program.getOptionsDiagnostics().length;
@@ -173,27 +226,27 @@ export class Checker {
173226
if (totals) {
174227

175228
write(
176-
chalk[optionsErrors ? 'red' : 'white']
229+
chalk[optionsErrors ? options.yellowOnOptions ? 'yellow' : 'red' : 'white']
177230
(`└── Options: ${optionsErrors}${END_LINE}`)
178231
);
179232

180233
write(
181-
chalk[globalErrors ? 'red' : 'white']
234+
chalk[globalErrors ? options.yellowOnGlobal ? 'yellow' : 'red' : 'white']
182235
(`└── Global: ${globalErrors}${END_LINE}`)
183236
);
184237

185238
write(
186-
chalk[syntacticErrors ? 'red' : 'white']
239+
chalk[syntacticErrors ? options.yellowOnSyntactic ? 'yellow' : 'red' : 'white']
187240
(`└── Syntactic: ${syntacticErrors}${END_LINE}`)
188241
);
189242

190243
write(
191-
chalk[semanticErrors ? 'red' : 'white']
244+
chalk[semanticErrors ? options.yellowOnSemantic ? 'yellow' : 'red' : 'white']
192245
(`└── Semantic: ${semanticErrors}${END_LINE}`)
193246
);
194247

195248
write(
196-
chalk[tsLintErrors ? 'red' : 'white']
249+
chalk[tsLintErrors ? options.yellowOnLint ? 'yellow' : 'red' : 'white']
197250
(`└── TsLint: ${tsLintErrors}${END_LINE}${END_LINE}`)
198251
);
199252

0 commit comments

Comments
 (0)