@@ -1635,6 +1635,86 @@ module ts {
16351635 sourceFile . scriptSnapshot = scriptSnapshot ;
16361636 }
16371637
1638+ export function transpile ( input : string , compilerOptions ?: CompilerOptions , fileName ?: string , syntaxErrors ?: Diagnostic [ ] ) : string {
1639+ let options = compilerOptions ? ts . clone ( compilerOptions ) : getDefaultCompilerOptions ( ) ;
1640+
1641+ // Single file transformation is only guranteed to be correct if inside an external module.
1642+ // External modules have thier own scope and can not contribute to internal modules outside their
1643+ // scope.
1644+ if ( options . target !== ScriptTarget . ES6 && ( ! options . module || options . module === ModuleKind . None ) ) {
1645+ // add errors
1646+ }
1647+
1648+ // In sigle file transformation the compiler does not have access to declaration sites.
1649+ // Const enum property access will not be detected if they are not in the same file as the enum.
1650+ // thus they are goign to be emitted as normal propety access. To ensure correct behaviour at runtime,
1651+ // we need to generare the actual enum object so that the proprty accesses do not fail.
1652+ options . preserveConstEnums = true ;
1653+
1654+ // No reason to get declarations, we are only returning javascript
1655+ options . declaration = false ;
1656+
1657+ // Filename can be non-ts file. We are not locating any modules as well, so allow
1658+ // non-ts extensions
1659+ options . allowNonTsExtensions = true ;
1660+
1661+ // enable relaxed emit rules
1662+ options . separateCompilation = true ;
1663+
1664+ // We are not resolving references or modules, or even including the library. Disabling
1665+ // emit on error will block generating the output and has no meaningful use here.
1666+ options . noEmitOnError = false ;
1667+
1668+ // No resolution requests will be honered anyways. So do not do it
1669+ options . noResolve = true ;
1670+
1671+ // TODO (vladima): add inlineSourceMap once it is checked in
1672+ //if (options.sourceMap) {
1673+ // // We need to return a single string, so inline the sourceMap in the output
1674+ // options.inlineSourceMap = true;;
1675+ //}
1676+
1677+ // Parse
1678+ var inputFileName = fileName || "module.ts" ;
1679+ var sourceFile = ts . createSourceFile ( inputFileName , input , options . target ) ;
1680+
1681+ // Store syntactic diagnostics
1682+ if ( syntaxErrors && sourceFile . parseDiagnostics ) {
1683+ syntaxErrors . push ( ...sourceFile . parseDiagnostics ) ;
1684+ }
1685+
1686+ // Output
1687+ let outputText : string ;
1688+
1689+ // Create a compilerHost object to allow the compiler to read and write files
1690+ var compilerHost : CompilerHost = {
1691+ getSourceFile : ( fileName , target ) => fileName === inputFileName ? sourceFile : undefined ,
1692+ writeFile : ( name , text , writeByteOrderMark ) => {
1693+ if ( ts . fileExtensionIs ( name , ".js" ) ) {
1694+ Debug . assert ( outputText === undefined , "Unexpected multiple outputs for the file: " + name ) ;
1695+ outputText = text ;
1696+ }
1697+ } ,
1698+ getDefaultLibFileName : ( ) => "lib.d.ts" ,
1699+ useCaseSensitiveFileNames : ( ) => false ,
1700+ getCanonicalFileName : fileName => fileName ,
1701+ getCurrentDirectory : ( ) => "" ,
1702+ getNewLine : ( ) => "\r\n"
1703+ } ;
1704+
1705+ // Note: The emitter needs a the checker to walk the file, so we will create a program for this
1706+ // though single file transformation does not really need this. First we need to drop the emitter
1707+ // dependcy on the checker and then implement a new resolver that does not do the full check.
1708+ var program = ts . createProgram ( [ inputFileName ] , options , compilerHost ) ;
1709+
1710+ // Emit
1711+ program . emit ( ) ;
1712+
1713+ Debug . assert ( outputText !== undefined , "Output generation failed" ) ;
1714+
1715+ return outputText ;
1716+ }
1717+
16381718 export function createLanguageServiceSourceFile ( fileName : string , scriptSnapshot : IScriptSnapshot , scriptTarget : ScriptTarget , version : string , setNodeParents : boolean ) : SourceFile {
16391719 let sourceFile = createSourceFile ( fileName , scriptSnapshot . getText ( 0 , scriptSnapshot . getLength ( ) ) , scriptTarget , setNodeParents ) ;
16401720 setSourceFileFields ( sourceFile , scriptSnapshot , version ) ;
0 commit comments