@@ -5,69 +5,127 @@ var tslint = require("tslint");
55var path = require ( "path" ) ;
66var Checker = ( function ( ) {
77 function Checker ( ) {
8+ this . END_LINE = '\n' ;
89 }
9- Checker . prototype . configure = function ( options ) {
10+ Checker . prototype . inspectCode = function ( options ) {
1011 var _this = this ;
11- this . tsConfig = options . tsConfigObj ;
1212 this . options = options ;
1313 var parseConfigHost = {
1414 fileExists : ts . sys . fileExists ,
1515 readDirectory : ts . sys . readDirectory ,
1616 readFile : ts . sys . readFile ,
1717 useCaseSensitiveFileNames : true
1818 } ;
19- var start = new Date ( ) . getTime ( ) ;
20- var parsed = ts . parseJsonConfigFileContent ( this . tsConfig , parseConfigHost , options . basePath || '.' , null ) ;
19+ var inspectionTimeStart = new Date ( ) . getTime ( ) ;
20+ var parsed = ts . parseJsonConfigFileContent ( this . options . tsConfigJsonContent , parseConfigHost , options . basePath || '.' , null ) ;
2121 this . program = ts . createProgram ( parsed . fileNames , parsed . options , null , this . program ) ;
22- this . diagnostics = [ ] ;
22+ this . tsDiagnostics = [ ] ;
2323 var optionsErrors = this . program . getOptionsDiagnostics ( ) . map ( function ( obj ) {
2424 obj . _type = 'options' ;
2525 return obj ;
2626 } ) ;
27- this . diagnostics = this . diagnostics . concat ( optionsErrors ) ;
27+ this . tsDiagnostics = this . tsDiagnostics . concat ( optionsErrors ) ;
2828 var globalErrors = this . program . getGlobalDiagnostics ( ) . map ( function ( obj ) {
2929 obj . _type = 'global' ;
3030 return obj ;
3131 } ) ;
32- this . diagnostics = this . diagnostics . concat ( globalErrors ) ;
32+ this . tsDiagnostics = this . tsDiagnostics . concat ( globalErrors ) ;
3333 var syntacticErrors = this . program . getSyntacticDiagnostics ( ) . map ( function ( obj ) {
3434 obj . _type = 'syntactic' ;
3535 return obj ;
3636 } ) ;
37- this . diagnostics = this . diagnostics . concat ( syntacticErrors ) ;
37+ this . tsDiagnostics = this . tsDiagnostics . concat ( syntacticErrors ) ;
3838 var semanticErrors = this . program . getSemanticDiagnostics ( ) . map ( function ( obj ) {
3939 obj . _type = 'semantic' ;
4040 return obj ;
4141 } ) ;
42- this . diagnostics = this . diagnostics . concat ( semanticErrors ) ;
43- this . lintResults = [ ] ;
42+ this . tsDiagnostics = this . tsDiagnostics . concat ( semanticErrors ) ;
43+ this . lintFileResult = [ ] ;
4444 if ( options . tsLint ) {
4545 var fullPath = path . resolve ( this . options . basePath , options . tsLint ) ;
46- this . files = tslint . Linter . getFileNames ( this . program ) ;
47- var config_1 = tslint . Configuration . findConfiguration ( fullPath , this . options . basePath ) . results ;
48- this . lintResults = this . files . map ( function ( file ) {
49- var fileContents = _this . program . getSourceFile ( file ) . getFullText ( ) ;
50- var linter = new tslint . Linter ( options . lintoptions , _this . program ) ;
51- linter . lint ( file , fileContents , config_1 ) ;
52- return linter . getResult ( ) ;
53- } ) . filter ( function ( result ) {
54- return result . errorCount ? true : false ;
55- } ) ;
46+ var files = tslint . Linter . getFileNames ( this . program ) ;
47+ var tsLintConfiguration_1 = tslint . Configuration . findConfiguration ( fullPath , this . options . basePath ) . results ;
48+ this . lintFileResult =
49+ files . map ( function ( file ) {
50+ var fileContents = _this . program . getSourceFile ( file ) . getFullText ( ) ;
51+ var linter = new tslint . Linter ( options . lintoptions , _this . program ) ;
52+ linter . lint ( file , fileContents , tsLintConfiguration_1 ) ;
53+ return linter . getResult ( ) ;
54+ } ) . filter ( function ( result ) {
55+ return result . errorCount ? true : false ;
56+ } ) ;
5657 }
57- this . elapsed = new Date ( ) . getTime ( ) - start ;
58+ this . elapsedInspectionTime = new Date ( ) . getTime ( ) - inspectionTimeStart ;
5859 } ;
59- Checker . prototype . typecheck = function ( ) {
60- var write = this . writeText ;
61- var diagnostics = this . diagnostics ;
60+ Checker . prototype . printResult = function ( isWorker ) {
61+ var print = this . writeText ;
6262 var program = this . program ;
6363 var options = this . options ;
64- var END_LINE = '\n' ;
65- write ( chalk . bgWhite ( chalk . black ( END_LINE + "Typechecker plugin(" + options . type + ") " + options . name ) ) +
64+ var END_LINE = this . END_LINE ;
65+ print ( chalk . bgWhite ( chalk . black ( END_LINE + "Typechecker plugin(" + options . type + ") " + options . name ) ) +
6666 chalk . white ( "." + END_LINE ) ) ;
67- write ( chalk . grey ( "Time:" + new Date ( ) . toString ( ) + " " + END_LINE ) ) ;
68- var lintResults = this . lintResults . map ( function ( errors ) {
69- if ( errors . failures ) {
70- var messages_1 = errors . failures . map ( function ( failure ) {
67+ print ( chalk . grey ( "Time:" + new Date ( ) . toString ( ) + " " + END_LINE ) ) ;
68+ var lintErrorMessages = this . processLintFiles ( ) ;
69+ var tsErrorMessages = this . processTsDiagnostics ( ) ;
70+ var allErrors = tsErrorMessages . concat ( lintErrorMessages ) ;
71+ if ( allErrors . length > 0 ) {
72+ allErrors . unshift ( chalk . underline ( END_LINE + "File errors" ) + chalk . white ( ':' ) ) ;
73+ print ( allErrors . join ( END_LINE ) ) ;
74+ }
75+ var optionsErrors = program . getOptionsDiagnostics ( ) . length ;
76+ var globalErrors = program . getGlobalDiagnostics ( ) . length ;
77+ var syntacticErrors = program . getSyntacticDiagnostics ( ) . length ;
78+ var semanticErrors = program . getSemanticDiagnostics ( ) . length ;
79+ var tsLintErrors = lintErrorMessages . length ;
80+ var totalsErrors = optionsErrors + globalErrors + syntacticErrors + semanticErrors + tsLintErrors ;
81+ if ( totalsErrors ) {
82+ print ( chalk . underline ( "" + END_LINE + END_LINE + "Errors" ) +
83+ chalk . white ( ":" + totalsErrors + END_LINE ) ) ;
84+ print ( chalk [ optionsErrors ? options . yellowOnOptions ? 'yellow' : 'red' : 'white' ] ( "\u2514\u2500\u2500 Options: " + optionsErrors + END_LINE ) ) ;
85+ print ( chalk [ globalErrors ? options . yellowOnGlobal ? 'yellow' : 'red' : 'white' ] ( "\u2514\u2500\u2500 Global: " + globalErrors + END_LINE ) ) ;
86+ print ( chalk [ syntacticErrors ? options . yellowOnSyntactic ? 'yellow' : 'red' : 'white' ] ( "\u2514\u2500\u2500 Syntactic: " + syntacticErrors + END_LINE ) ) ;
87+ print ( chalk [ semanticErrors ? options . yellowOnSemantic ? 'yellow' : 'red' : 'white' ] ( "\u2514\u2500\u2500 Semantic: " + semanticErrors + END_LINE ) ) ;
88+ print ( chalk [ tsLintErrors ? options . yellowOnLint ? 'yellow' : 'red' : 'white' ] ( "\u2514\u2500\u2500 TsLint: " + tsLintErrors + END_LINE + END_LINE ) ) ;
89+ }
90+ else {
91+ print ( chalk . grey ( "All good, no errors :-)" + END_LINE ) ) ;
92+ }
93+ print ( chalk . grey ( "Typechecking time: " + this . elapsedInspectionTime + "ms" + END_LINE ) ) ;
94+ switch ( true ) {
95+ case options . throwOnGlobal && globalErrors > 0 :
96+ case options . throwOnOptions && optionsErrors > 0 :
97+ case options . throwOnSemantic && semanticErrors > 0 :
98+ case options . throwOnTsLint && tsLintErrors > 0 :
99+ case options . throwOnSyntactic && syntacticErrors > 0 :
100+ if ( process . send ) {
101+ process . send ( 'error' ) ;
102+ }
103+ else {
104+ throw new Error ( 'Typechecker throwing error due to throw options set' ) ;
105+ }
106+ process . exit ( 1 ) ;
107+ break ;
108+ case options . quit && isWorker :
109+ print ( chalk . grey ( "Quiting typechecker" + END_LINE + END_LINE ) ) ;
110+ process . send ( 'done' ) ;
111+ break ;
112+ case options . quit && ! isWorker :
113+ print ( chalk . grey ( "Quiting typechecker" + END_LINE + END_LINE ) ) ;
114+ break ;
115+ default :
116+ print ( chalk . grey ( "Keeping typechecker alive" + END_LINE + END_LINE ) ) ;
117+ }
118+ return totalsErrors ;
119+ } ;
120+ Checker . prototype . writeText = function ( text ) {
121+ ts . sys . write ( text ) ;
122+ } ;
123+ Checker . prototype . processLintFiles = function ( ) {
124+ var options = this . options ;
125+ var lintResultsFilesMessages = this . lintFileResult . map ( function ( fileResult ) {
126+ var messages = [ ] ;
127+ if ( fileResult . failures ) {
128+ messages = fileResult . failures . map ( function ( failure ) {
71129 var r = {
72130 fileName : failure . fileName ,
73131 line : failure . startPosition . lineAndCharacter . line ,
@@ -83,22 +141,30 @@ var Checker = (function () {
83141 message += ' ' + r . failure ;
84142 return message ;
85143 } ) ;
86- return messages_1 ;
87144 }
145+ return messages ;
146+ } ) . filter ( function ( res ) {
147+ return res . length === 0 ? false : true ;
88148 } ) ;
149+ var lintErrorMessages = [ ] ;
89150 try {
90- if ( lintResults . length ) {
91- lintResults = lintResults . reduce ( function ( a , b ) {
151+ if ( lintResultsFilesMessages . length ) {
152+ lintErrorMessages = lintResultsFilesMessages . reduce ( function ( a , b ) {
92153 return a . concat ( b ) ;
93154 } ) ;
94155 }
95156 }
96157 catch ( err ) {
97158 console . log ( err ) ;
98159 }
99- var messages = [ ] ;
100- if ( diagnostics . length > 0 ) {
101- messages = diagnostics . map ( function ( diag ) {
160+ return lintErrorMessages ;
161+ } ;
162+ Checker . prototype . processTsDiagnostics = function ( ) {
163+ var options = this . options ;
164+ var END_LINE = this . END_LINE ;
165+ var tsErrorMessages = [ ] ;
166+ if ( this . tsDiagnostics . length > 0 ) {
167+ tsErrorMessages = this . tsDiagnostics . map ( function ( diag ) {
102168 var message = chalk . red ( '└── ' ) ;
103169 var color ;
104170 switch ( diag . _type ) {
@@ -126,60 +192,8 @@ var Checker = (function () {
126192 message += ' ' + ts . flattenDiagnosticMessageText ( diag . messageText , END_LINE ) ;
127193 return message ;
128194 } ) ;
129- messages . unshift ( chalk . underline ( END_LINE + "File errors" ) + chalk . white ( ':' ) ) ;
130- var x = messages . concat ( lintResults ) ;
131- write ( x . join ( '\n' ) ) ;
132195 }
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- }
139- var optionsErrors = program . getOptionsDiagnostics ( ) . length ;
140- var globalErrors = program . getGlobalDiagnostics ( ) . length ;
141- var syntacticErrors = program . getSyntacticDiagnostics ( ) . length ;
142- var semanticErrors = program . getSemanticDiagnostics ( ) . length ;
143- var tsLintErrors = lintResults . length ;
144- var totals = optionsErrors + globalErrors + syntacticErrors + semanticErrors + tsLintErrors ;
145- write ( chalk . underline ( "" + END_LINE + END_LINE + "Errors" ) +
146- chalk . white ( ":" + totals + END_LINE ) ) ;
147- if ( totals ) {
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 ) ) ;
153- }
154- write ( chalk . grey ( "Typechecking time: " + this . elapsed + "ms" + END_LINE ) ) ;
155- switch ( true ) {
156- case options . throwOnGlobal && globalErrors > 0 :
157- case options . throwOnOptions && optionsErrors > 0 :
158- case options . throwOnSemantic && semanticErrors > 0 :
159- case options . throwOnTsLint && tsLintErrors > 0 :
160- case options . throwOnSyntactic && syntacticErrors > 0 :
161- if ( process . send ) {
162- process . send ( 'error' ) ;
163- }
164- else {
165- throw new Error ( 'Typechecker throwing error due to throw options set' ) ;
166- }
167- process . exit ( 1 ) ;
168- break ;
169- case options . quit :
170- write ( chalk . grey ( "Quiting typechecker" + END_LINE + END_LINE ) ) ;
171- process . send ( 'done' ) ;
172- break ;
173- case options . finished :
174- write ( chalk . grey ( "Quiting typechecker" + END_LINE + END_LINE ) ) ;
175- break ;
176- default :
177- write ( chalk . grey ( "Keeping typechecker alive" + END_LINE + END_LINE ) ) ;
178- }
179- return totals ;
180- } ;
181- Checker . prototype . writeText = function ( text ) {
182- ts . sys . write ( text ) ;
196+ return tsErrorMessages ;
183197 } ;
184198 return Checker ;
185199} ( ) ) ;
0 commit comments