@@ -9,7 +9,7 @@ import * as path from 'path';
99import * as fancyLog from 'fancy-log' ;
1010import * as ansiColors from 'ansi-colors' ;
1111
12- const dtsv = '2 ' ;
12+ const dtsv = '3 ' ;
1313
1414const tsfmt = require ( '../../tsfmt.json' ) ;
1515
@@ -138,7 +138,7 @@ function isDefaultExport(declaration: ts.InterfaceDeclaration | ts.ClassDeclarat
138138 ) ;
139139}
140140
141- function getMassagedTopLevelDeclarationText ( sourceFile : ts . SourceFile , declaration : TSTopLevelDeclare , importName : string , usage : string [ ] , enums : string [ ] ) : string {
141+ function getMassagedTopLevelDeclarationText ( sourceFile : ts . SourceFile , declaration : TSTopLevelDeclare , importName : string , usage : string [ ] , enums : IEnumEntry [ ] ) : string {
142142 let result = getNodeText ( sourceFile , declaration ) ;
143143 if ( declaration . kind === ts . SyntaxKind . InterfaceDeclaration || declaration . kind === ts . SyntaxKind . ClassDeclaration ) {
144144 let interfaceDeclaration = < ts . InterfaceDeclaration | ts . ClassDeclaration > declaration ;
@@ -177,14 +177,36 @@ function getMassagedTopLevelDeclarationText(sourceFile: ts.SourceFile, declarati
177177 // life..
178178 }
179179 } ) ;
180+ } else if ( declaration . kind === ts . SyntaxKind . VariableStatement ) {
181+ const jsDoc = result . substr ( 0 , declaration . getLeadingTriviaWidth ( sourceFile ) ) ;
182+ if ( jsDoc . indexOf ( '@monacodtsreplace' ) >= 0 ) {
183+ const jsDocLines = jsDoc . split ( / \r \n | \r | \n / ) ;
184+ let directives : [ RegExp , string ] [ ] = [ ] ;
185+ for ( const jsDocLine of jsDocLines ) {
186+ const m = jsDocLine . match ( / ^ \s * \* \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ) \/ $ / ) ;
187+ if ( m ) {
188+ directives . push ( [ new RegExp ( m [ 1 ] , 'g' ) , m [ 2 ] ] ) ;
189+ }
190+ }
191+ // remove the jsdoc
192+ result = result . substr ( jsDoc . length ) ;
193+ if ( directives . length > 0 ) {
194+ // apply replace directives
195+ const replacer = createReplacerFromDirectives ( directives ) ;
196+ result = replacer ( result ) ;
197+ }
198+ }
180199 }
181200 result = result . replace ( / e x p o r t d e f a u l t / g, 'export ' ) ;
182201 result = result . replace ( / e x p o r t d e c l a r e / g, 'export ' ) ;
183202 result = result . replace ( / d e c l a r e / g, '' ) ;
184203
185204 if ( declaration . kind === ts . SyntaxKind . EnumDeclaration ) {
186205 result = result . replace ( / c o n s t e n u m / , 'enum' ) ;
187- enums . push ( result ) ;
206+ enums . push ( {
207+ enumName : declaration . name . getText ( sourceFile ) ,
208+ text : result
209+ } ) ;
188210 }
189211
190212 return result ;
@@ -324,6 +346,15 @@ function format(text: string, endl: string): string {
324346 }
325347}
326348
349+ function createReplacerFromDirectives ( directives : [ RegExp , string ] [ ] ) : ( str : string ) => string {
350+ return ( str : string ) => {
351+ for ( let i = 0 ; i < directives . length ; i ++ ) {
352+ str = str . replace ( directives [ i ] [ 0 ] , directives [ i ] [ 1 ] ) ;
353+ }
354+ return str ;
355+ } ;
356+ }
357+
327358function createReplacer ( data : string ) : ( str : string ) => string {
328359 data = data || '' ;
329360 let rawDirectives = data . split ( ';' ) ;
@@ -341,12 +372,7 @@ function createReplacer(data: string): (str: string) => string {
341372 directives . push ( [ new RegExp ( findStr , 'g' ) , replaceStr ] ) ;
342373 } ) ;
343374
344- return ( str : string ) => {
345- for ( let i = 0 ; i < directives . length ; i ++ ) {
346- str = str . replace ( directives [ i ] [ 0 ] , directives [ i ] [ 1 ] ) ;
347- }
348- return str ;
349- } ;
375+ return createReplacerFromDirectives ( directives ) ;
350376}
351377
352378interface ITempResult {
@@ -355,6 +381,11 @@ interface ITempResult {
355381 enums : string ;
356382}
357383
384+ interface IEnumEntry {
385+ enumName : string ;
386+ text : string ;
387+ }
388+
358389function generateDeclarationFile ( recipe : string , sourceFileGetter : SourceFileGetter ) : ITempResult | null {
359390 const endl = / \r \n / . test ( recipe ) ? '\r\n' : '\n' ;
360391
@@ -376,7 +407,7 @@ function generateDeclarationFile(recipe: string, sourceFileGetter: SourceFileGet
376407 return importName ;
377408 } ;
378409
379- let enums : string [ ] = [ ] ;
410+ let enums : IEnumEntry [ ] = [ ] ;
380411 let version : string | null = null ;
381412
382413 lines . forEach ( line => {
@@ -492,6 +523,16 @@ function generateDeclarationFile(recipe: string, sourceFileGetter: SourceFileGet
492523 resultTxt = format ( resultTxt , endl ) ;
493524 resultTxt = resultTxt . split ( / \r \n | \n | \r / ) . join ( endl ) ;
494525
526+ enums . sort ( ( e1 , e2 ) => {
527+ if ( e1 . enumName < e2 . enumName ) {
528+ return - 1 ;
529+ }
530+ if ( e1 . enumName > e2 . enumName ) {
531+ return 1 ;
532+ }
533+ return 0 ;
534+ } ) ;
535+
495536 let resultEnums = [
496537 '/*---------------------------------------------------------------------------------------------' ,
497538 ' * Copyright (c) Microsoft Corporation. All rights reserved.' ,
@@ -500,7 +541,7 @@ function generateDeclarationFile(recipe: string, sourceFileGetter: SourceFileGet
500541 '' ,
501542 '// THIS IS A GENERATED FILE. DO NOT EDIT DIRECTLY.' ,
502543 ''
503- ] . concat ( enums ) . join ( endl ) ;
544+ ] . concat ( enums . map ( e => e . text ) ) . join ( endl ) ;
504545 resultEnums = resultEnums . split ( / \r \n | \n | \r / ) . join ( endl ) ;
505546 resultEnums = format ( resultEnums , endl ) ;
506547 resultEnums = resultEnums . split ( / \r \n | \n | \r / ) . join ( endl ) ;
0 commit comments