@@ -32,6 +32,12 @@ npm install underscore
3232npm install esprima
3333npm install minimist
3434
35+ For TypeScript support:
36+
37+ npm install typescript
38+ sudo npm link typescript # to expose TypeScript compiler
39+ npm install source-map
40+
3541
3642From: https://code.google.com/p/v8-wiki/wiki/DebuggerProtocol
3743regarding the 'handle_' field of serialized objects ...
@@ -127,6 +133,59 @@ String.prototype.rtrim = function() {
127133 return this . replace ( / \s * $ / g, "" ) ;
128134} ;
129135
136+
137+ // Inspired by https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API
138+ var ts = require ( "typescript" ) ;
139+ var path = require ( "path" ) ;
140+
141+ function typescriptCompile ( contents ) {
142+ // default ts standard library
143+ var libSource = fs . readFileSync ( path . join ( path . dirname ( require . resolve ( 'typescript' ) ) , 'lib.d.ts' ) ) . toString ( ) ;
144+
145+ var compilerOptions = { sourceMap : true } ; // yes, create a source map!
146+ // Generated outputs
147+ var outputs = [ ] ;
148+ // Create a compilerHost object to allow the compiler to read and write files
149+ var compilerHost = {
150+ getSourceFile : function ( filename , languageVersion ) {
151+ if ( filename === "file.ts" )
152+ return ts . createSourceFile ( filename , contents , compilerOptions . target , "0" ) ;
153+ if ( filename === "lib.d.ts" )
154+ return ts . createSourceFile ( filename , libSource , compilerOptions . target , "0" ) ;
155+ return undefined ;
156+ } ,
157+ writeFile : function ( name , text , writeByteOrderMark ) {
158+ outputs . push ( { name : name , text : text , writeByteOrderMark : writeByteOrderMark } ) ;
159+ } ,
160+ getDefaultLibFilename : function ( ) { return "lib.d.ts" ; } ,
161+ useCaseSensitiveFileNames : function ( ) { return false ; } ,
162+ getCanonicalFileName : function ( filename ) { return filename ; } ,
163+ getCurrentDirectory : function ( ) { return "" ; } ,
164+ getNewLine : function ( ) { return "\n" ; }
165+ } ;
166+ // Create a program from inputs
167+ var program = ts . createProgram ( [ "file.ts" ] , compilerOptions , compilerHost ) ;
168+ // Query for early errors
169+ var errors = program . getDiagnostics ( ) ;
170+ // Do not generate code in the presence of early errors
171+ if ( ! errors . length ) {
172+ // Type check and get semantic errors
173+ var checker = program . getTypeChecker ( true ) ;
174+ errors = checker . getDiagnostics ( ) ;
175+ // Generate output
176+ checker . emitFiles ( ) ;
177+ }
178+ return {
179+ outputs : outputs ,
180+ //errors: errors.map(function (e) { return e.file.filename + "(" + e.file.getLineAndCharacterFromPosition(e.start).line + "): " + e.messageText; })
181+ errors : errors . map ( function ( e ) {
182+ var errPos = e . file . getLineAndCharacterFromPosition ( e . start ) ;
183+ return { line : errPos . line , msg : e . messageText } ;
184+ } )
185+ } ;
186+ }
187+
188+
130189// for some reason, stderr is borked when running under the node
131190// debugger, so we must print to stdout. the node 'assert' module fails
132191// silently :/
@@ -781,6 +840,34 @@ if (argv._.length === 1) {
781840 cod = argv . code . rtrim ( ) ;
782841}
783842
843+ var isTypescript = false ;
844+ var sm = require ( 'source-map' ) ;
845+
846+ if ( argv . typescript ) {
847+ isTypescript = true ;
848+ var tscCompilerOutput = typescriptCompile ( cod ) ;
849+ console . log ( tscCompilerOutput ) ;
850+
851+ var tsSourceMap , compiledJsCod ;
852+ tscCompilerOutput . outputs . forEach ( function ( e , i ) {
853+ if ( e . name === 'file.js.map' ) {
854+ tsSourceMap = new sm . SourceMapConsumer ( e . text ) ;
855+ } else if ( e . name === 'file.js' ) {
856+ compiledJsCod = e . text ;
857+ }
858+ } ) ;
859+
860+ // if there are any errors, then handle them here, create a trace, and
861+ // bail out before executing!
862+ if ( tscCompilerOutput . errors . length > 0 ) {
863+ console . log ( "HAS ERRORS!" ) ;
864+ // TODO: handle displaying multiple errors at once
865+ process . exit ( ) ;
866+ } else {
867+ cod = compiledJsCod ; // woohoo, this is just JavaScript, so proceed
868+ }
869+ }
870+
784871var wrappedCod = wrapUserscript ( cod ) ;
785872
786873try {
0 commit comments