1
- /// <reference path="typings/node/node.d.ts" />
2
1
/// <reference path="node_modules/typescript/bin/typescript.d.ts" />
3
2
4
3
import fs = require( 'fs' ) ;
@@ -41,11 +40,12 @@ module tss {
41
40
42
41
/**
43
42
* @param {string } code TypeScript source code to compile
44
- * @param {string= } filename Only needed if you plan to use sourceMaps. Provide the complete filePath relevant to you
43
+ * @param {string= } fileName Only needed if you plan to use sourceMaps.
44
+ * Provide the complete filePath relevant to you
45
45
* @return {string } The JavaScript with inline sourceMaps if sourceMaps were enabled
46
46
* @throw {Error } A syntactic error or a semantic error (if doSemanticChecks is true)
47
47
*/
48
- compile ( code : string , filename = FILENAME_TS ) : string {
48
+ compile ( code : string , fileName = FILENAME_TS ) : string {
49
49
if ( ! this . service ) {
50
50
this . service = this . createService ( ) ;
51
51
}
@@ -54,44 +54,57 @@ module tss {
54
54
file . text = code ;
55
55
file . version ++ ;
56
56
57
- return this . toJavaScript ( this . service , filename ) ;
57
+ return this . toJavaScript ( this . service , fileName ) ;
58
58
}
59
59
60
60
private createService ( ) : ts . LanguageService {
61
- var defaultLib = this . getDefaultLibFilename ( this . options ) ;
61
+ var defaultLib = this . getDefaultLibFileName ( this . options ) ;
62
62
var defaultLibPath = path . join ( this . getTypeScriptBinDir ( ) , defaultLib ) ;
63
63
this . files [ defaultLib ] = { version : 0 , text : fs . readFileSync ( defaultLibPath ) . toString ( ) } ;
64
64
this . files [ FILENAME_TS ] = { version : 0 , text : '' } ;
65
65
66
- var servicesHost : ts . LanguageServiceHost = {
67
- getScriptFileNames : ( ) => [ this . getDefaultLibFilename ( this . options ) , FILENAME_TS ] ,
68
- getScriptVersion : ( filename ) => this . files [ filename ] && this . files [ filename ] . version . toString ( ) ,
69
- getScriptSnapshot : ( filename ) => {
70
- var file = this . files [ filename ] ;
71
- return {
72
- getText : ( start , end ) => file . text . substring ( start , end ) ,
73
- getLength : ( ) => file . text . length ,
74
- getLineStartPositions : ( ) => [ ] ,
75
- getChangeRange : ( oldSnapshot ) => undefined
76
- } ;
66
+ var serviceHost : ts . LanguageServiceHost = {
67
+ getScriptFileNames : ( ) => [ this . getDefaultLibFileName ( this . options ) , FILENAME_TS ] ,
68
+ getScriptVersion : ( fileName ) => this . files [ fileName ] && this . files [ fileName ] . version . toString ( ) ,
69
+ getScriptSnapshot : ( fileName ) => {
70
+ var file = this . files [ fileName ] ;
71
+ if ( file ) {
72
+ return {
73
+ getText : ( start , end ) => file . text . substring ( start , end ) ,
74
+ getLength : ( ) => file . text . length ,
75
+ getLineStartPositions : ( ) : number [ ] => [ ] ,
76
+ getChangeRange : ( oldSnapshot ) => undefined
77
+ } ;
78
+ }
79
+ else { // This is some reference import
80
+ return {
81
+ getText : ( start , end ) => '' ,
82
+ getLength : ( ) => 0 ,
83
+ getLineStartPositions : ( ) : number [ ] => [ ] ,
84
+ getChangeRange : ( oldSnapshot ) => undefined
85
+ } ;
86
+ }
77
87
} ,
78
88
getCurrentDirectory : ( ) => process . cwd ( ) ,
79
89
getScriptIsOpen : ( ) => true ,
80
90
getCompilationSettings : ( ) => this . options ,
81
- getDefaultLibFilename : ( options : ts . CompilerOptions ) => {
82
- return this . getDefaultLibFilename ( options ) ;
91
+ getDefaultLibFileName : ( options : ts . CompilerOptions ) => {
92
+ return this . getDefaultLibFileName ( options ) ;
83
93
} ,
84
- log : ( message ) => console . log ( message )
94
+ getNewLine : ( ) => os . EOL ,
95
+ log : ( message : string ) => console . log ( message ) ,
96
+ trace : ( message : string ) => console . debug ( message ) ,
97
+ error : ( message : string ) => console . error ( message )
85
98
} ;
86
99
87
- return ts . createLanguageService ( servicesHost , ts . createDocumentRegistry ( ) )
100
+ return ts . createLanguageService ( serviceHost , ts . createDocumentRegistry ( ) )
88
101
}
89
102
90
103
private getTypeScriptBinDir ( ) : string {
91
104
return path . dirname ( require . resolve ( 'typescript' ) ) ;
92
105
}
93
106
94
- private getDefaultLibFilename ( options : ts . CompilerOptions ) : string {
107
+ private getDefaultLibFileName ( options : ts . CompilerOptions ) : string {
95
108
if ( options . target === ts . ScriptTarget . ES6 ) {
96
109
return 'lib.es6.d.ts' ;
97
110
} else {
@@ -100,56 +113,50 @@ module tss {
100
113
}
101
114
102
115
/**
103
- * converts {"version":3,"file":"file.js","sourceRoot":"","sources":["file.ts"],"names":[],"mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC"}
104
- * to {"version":3,"sources":["foo/test.ts"],"names":[],"mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC","file":"foo/test.ts","sourcesContent":["var x = 'test';"]}
116
+ * converts {"version":3,"file":"file.js","sourceRoot":"","sources":["file.ts"],"names":[],
117
+ * "mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC"}
118
+ * to {"version":3,"sources":["foo/test.ts"],"names":[],
119
+ * "mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC","file":"foo/test.ts","sourcesContent":["var x = 'test';"]}
105
120
* derived from : https://github.com/thlorenz/convert-source-map
106
121
*/
107
- private getInlineSourceMap ( mapText : string , filename : string ) : string {
122
+ private getInlineSourceMap ( mapText : string , fileName : string ) : string {
108
123
var sourceMap = JSON . parse ( mapText ) ;
109
- sourceMap . file = filename ;
110
- sourceMap . sources = [ filename ] ;
124
+ sourceMap . file = fileName ;
125
+ sourceMap . sources = [ fileName ] ;
111
126
sourceMap . sourcesContent = [ this . files [ FILENAME_TS ] . text ] ;
112
127
delete sourceMap . sourceRoot ;
113
128
return JSON . stringify ( sourceMap ) ;
114
129
}
115
130
116
- private toJavaScript ( service : ts . LanguageService , filename = FILENAME_TS ) : string {
131
+ private toJavaScript ( service : ts . LanguageService , fileName = FILENAME_TS ) : string {
117
132
var output = service . getEmitOutput ( FILENAME_TS ) ;
118
133
119
- // Meaning of succeeded is driven by whether we need to check for semantic errors or not
120
- var succeeded = output . emitOutputStatus === ts . EmitReturnStatus . Succeeded ;
121
- if ( ! this . doSemanticChecks && ! succeeded ) {
122
- // We have an output. It implies syntactic success
123
- succeeded = ! ! output . outputFiles . length ;
124
- }
125
-
126
- if ( ! succeeded ) {
127
- var allDiagnostics = service . getCompilerOptionsDiagnostics ( )
128
- . concat ( service . getSyntacticDiagnostics ( FILENAME_TS ) ) ;
134
+ var allDiagnostics = service . getCompilerOptionsDiagnostics ( )
135
+ . concat ( service . getSyntacticDiagnostics ( FILENAME_TS ) ) ;
129
136
130
- if ( this . doSemanticChecks ) {
131
- allDiagnostics = allDiagnostics . concat ( service . getSemanticDiagnostics ( FILENAME_TS ) ) ;
132
- }
137
+ if ( this . doSemanticChecks ) {
138
+ allDiagnostics = allDiagnostics . concat ( service . getSemanticDiagnostics ( FILENAME_TS ) ) ;
139
+ }
133
140
141
+ if ( allDiagnostics . length ) {
134
142
throw new Error ( this . formatDiagnostics ( allDiagnostics ) ) ;
135
143
}
136
144
137
- var outputFilename = FILENAME_TS . replace ( / t s $ / , 'js' ) ;
138
- var file = output . outputFiles . filter ( ( file ) => file . name === outputFilename ) [ 0 ] ;
139
- // TODO: Fixed in v1.5 https://github.com/Microsoft/TypeScript/issues/1653
140
- var text = file . text . replace ( / \r \n / g, os . EOL ) ;
145
+ var outputFileName = FILENAME_TS . replace ( / t s $ / , 'js' ) ;
146
+ var file = output . outputFiles . filter ( ( file ) => file . name === outputFileName ) [ 0 ] ;
147
+ var text = file . text ;
141
148
142
149
// If we have sourceMaps convert them to inline sourceMaps
143
150
if ( this . options . sourceMap ) {
144
- var sourceMapFilename = FILENAME_TS . replace ( / t s $ / , 'js.map' ) ;
145
- var sourceMapFile = output . outputFiles . filter ( ( file ) => file . name === sourceMapFilename ) [ 0 ] ;
151
+ var sourceMapFileName = FILENAME_TS . replace ( / t s $ / , 'js.map' ) ;
152
+ var sourceMapFile = output . outputFiles . filter ( ( file ) => file . name === sourceMapFileName ) [ 0 ] ;
146
153
147
154
// Transform sourcemap
148
155
var sourceMapText = sourceMapFile . text ;
149
- sourceMapText = this . getInlineSourceMap ( sourceMapText , filename ) ;
156
+ sourceMapText = this . getInlineSourceMap ( sourceMapText , fileName ) ;
150
157
var base64SourceMapText = new Buffer ( sourceMapText ) . toString ( 'base64' ) ;
151
158
var sourceMapComment = '//# sourceMappingURL=data:application/json;base64,' + base64SourceMapText ;
152
- text = text . replace ( '//# sourceMappingURL=' + sourceMapFilename , sourceMapComment ) ;
159
+ text = text . replace ( '//# sourceMappingURL=' + sourceMapFileName , sourceMapComment ) ;
153
160
}
154
161
155
162
return text ;
@@ -158,7 +165,7 @@ module tss {
158
165
private formatDiagnostics ( diagnostics : ts . Diagnostic [ ] ) : string {
159
166
return diagnostics . map ( ( d ) => {
160
167
if ( d . file ) {
161
- return 'L' + d . file . getLineAndCharacterFromPosition ( d . start ) . line + ': ' + d . messageText ;
168
+ return 'L' + d . file . getLineAndCharacterOfPosition ( d . start ) . line + ': ' + d . messageText ;
162
169
} else {
163
170
return d . messageText ;
164
171
}
0 commit comments