namespace ts { export function preProcessFile ( sourceText , readImportFiles = true , detectJavaScriptImports = false ) { const pragmaContext = { languageVersion : ScriptTarget . ES5 , pragmas : undefined , checkJsDirective : undefined , referencedFiles : [ ] , typeReferenceDirectives : [ ] , libReferenceDirectives : [ ] , amdDependencies : [ ] , hasNoDefaultLib : undefined , moduleName : undefined } ; const importedFiles = [ ] ; let ambientExternalModules ; let lastToken ; let currentToken ; let braceNesting = 0 ; let externalModule = false ; function nextToken ( ) { lastToken = currentToken ; currentToken = scanner . scan ( ) ; if ( currentToken === SyntaxKind . OpenBraceToken ) { braceNesting ++ ; } else if ( currentToken === SyntaxKind . CloseBraceToken ) { braceNesting -- ; } return currentToken ; } function getFileReference ( ) { const fileName = scanner . getTokenValue ( ) ; const pos = scanner . getTokenPos ( ) ; return { fileName , pos , end : pos + fileName . length } ; } function recordAmbientExternalModule ( ) { if ( ! ambientExternalModules ) { ambientExternalModules = [ ] ; } ambientExternalModules . push ( { ref : getFileReference ( ) , depth : braceNesting } ) ; } function recordModuleName ( ) { importedFiles . push ( getFileReference ( ) ) ; markAsExternalModuleIfTopLevel ( ) ; } function markAsExternalModuleIfTopLevel ( ) { if ( braceNesting === 0 ) { externalModule = true ; } } /**
         * Returns true if at least one token was consumed from the stream
         */ function tryConsumeDeclare ( ) { let token = scanner . getToken ( ) ; if ( token === SyntaxKind . DeclareKeyword ) { token = nextToken ( ) ; if ( token === SyntaxKind . ModuleKeyword ) { token = nextToken ( ) ; if ( token === SyntaxKind . StringLiteral ) { recordAmbientExternalModule ( ) ; } } return true ; } return false ; } /**
         * Returns true if at least one token was consumed from the stream
         */ function tryConsumeImport ( ) { if ( lastToken === SyntaxKind . DotToken ) { return false ; } let token = scanner . getToken ( ) ; if ( token === SyntaxKind . ImportKeyword ) { token = nextToken ( ) ; if ( token === SyntaxKind . OpenParenToken ) { token = nextToken ( ) ; if ( token === SyntaxKind . StringLiteral || token === SyntaxKind . NoSubstitutionTemplateLiteral ) { recordModuleName ( ) ; return true ; } } else if ( token === SyntaxKind . StringLiteral ) { recordModuleName ( ) ; return true ; } else { if ( token === SyntaxKind . TypeKeyword ) { const skipTypeKeyword = scanner . lookAhead ( ( ) => { const token = scanner . scan ( ) ; return token !== SyntaxKind . FromKeyword && ( token === SyntaxKind . AsteriskToken || token === SyntaxKind . OpenBraceToken || token === SyntaxKind . Identifier || isKeyword ( token ) ) ; } ) ; if ( skipTypeKeyword ) { token = nextToken ( ) ; } } if ( token === SyntaxKind . Identifier || isKeyword ( token ) ) { token = nextToken ( ) ; if ( token === SyntaxKind . FromKeyword ) { token = nextToken ( ) ; if ( token === SyntaxKind . StringLiteral ) { recordModuleName ( ) ; return true ; } } else if ( token === SyntaxKind . EqualsToken ) { if ( tryConsumeRequireCall ( true ) ) { return true ; } } else if ( token === SyntaxKind . CommaToken ) { token = nextToken ( ) ; } else { return true ; } } if ( token === SyntaxKind . OpenBraceToken ) { token = nextToken ( ) ; while ( token !== SyntaxKind . CloseBraceToken && token !== SyntaxKind . EndOfFileToken ) { token = nextToken ( ) ; } if ( token === SyntaxKind . CloseBraceToken ) { token = nextToken ( ) ; if ( token === SyntaxKind . FromKeyword ) { token = nextToken ( ) ; if ( token === SyntaxKind . StringLiteral ) { recordModuleName ( ) ; } } } } else if ( token === SyntaxKind . AsteriskToken ) { token = nextToken ( ) ; if ( token === SyntaxKind . AsKeyword ) { token = nextToken ( ) ; if ( token === SyntaxKind . Identifier || isKeyword ( token ) ) { token = nextToken ( ) ; if ( token === SyntaxKind . FromKeyword ) { token = nextToken ( ) ; if ( token === SyntaxKind . StringLiteral ) { recordModuleName ( ) ; } } } } } } return true ; } return false ; } function tryConsumeExport ( ) { let token = scanner . getToken ( ) ; if ( token === SyntaxKind . ExportKeyword ) { markAsExternalModuleIfTopLevel ( ) ; token = nextToken ( ) ; if ( token === SyntaxKind . TypeKeyword ) { const skipTypeKeyword = scanner . lookAhead ( ( ) => { const token = scanner . scan ( ) ; return token === SyntaxKind . AsteriskToken || token === SyntaxKind . OpenBraceToken ; } ) ; if ( skipTypeKeyword ) { token = nextToken ( ) ; } } if ( token === SyntaxKind . OpenBraceToken ) { token = nextToken ( ) ; while ( token !== SyntaxKind . CloseBraceToken && token !== SyntaxKind . EndOfFileToken ) { token = nextToken ( ) ; } if ( token === SyntaxKind . CloseBraceToken ) { token = nextToken ( ) ; if ( token === SyntaxKind . FromKeyword ) { token = nextToken ( ) ; if ( token === SyntaxKind . StringLiteral ) { recordModuleName ( ) ; } } } } else if ( token === SyntaxKind . AsteriskToken ) { token = nextToken ( ) ; if ( token === SyntaxKind . FromKeyword ) { token = nextToken ( ) ; if ( token === SyntaxKind . StringLiteral ) { recordModuleName ( ) ; } } } else if ( token === SyntaxKind . ImportKeyword ) { token = nextToken ( ) ; if ( token === SyntaxKind . TypeKeyword ) { const skipTypeKeyword = scanner . lookAhead ( ( ) => { const token = scanner . scan ( ) ; return token === SyntaxKind . Identifier || isKeyword ( token ) ; } ) ; if ( skipTypeKeyword ) { token = nextToken ( ) ; } } if ( token === SyntaxKind . Identifier || isKeyword ( token ) ) { token = nextToken ( ) ; if ( token === SyntaxKind . EqualsToken ) { if ( tryConsumeRequireCall ( true ) ) { return true ; } } } } return true ; } return false ; } function tryConsumeRequireCall ( skipCurrentToken , allowTemplateLiterals = false ) { let token = skipCurrentToken ? nextToken ( ) : scanner . getToken ( ) ; if ( token === SyntaxKind . RequireKeyword ) { token = nextToken ( ) ; if ( token === SyntaxKind . OpenParenToken ) { token = nextToken ( ) ; if ( token === SyntaxKind . StringLiteral || allowTemplateLiterals && token === SyntaxKind . NoSubstitutionTemplateLiteral ) { recordModuleName ( ) ; } } return true ; } return false ; } function tryConsumeDefine ( ) { let token = scanner . getToken ( ) ; if ( token === SyntaxKind . Identifier && scanner . getTokenValue ( ) === "define" ) { token = nextToken ( ) ; if ( token !== SyntaxKind . OpenParenToken ) { return true ; } token = nextToken ( ) ; if ( token === SyntaxKind . StringLiteral || token === SyntaxKind . NoSubstitutionTemplateLiteral ) { token = nextToken ( ) ; if ( token === SyntaxKind . CommaToken ) { token = nextToken ( ) ; } else { return true ; } } if ( token !== SyntaxKind . OpenBracketToken ) { return true ; } token = nextToken ( ) ; while ( token !== SyntaxKind . CloseBracketToken && token !== SyntaxKind . EndOfFileToken ) { if ( token === SyntaxKind . StringLiteral || token === SyntaxKind . NoSubstitutionTemplateLiteral ) { recordModuleName ( ) ; } token = nextToken ( ) ; } return true ; } return false ; } function processImports ( ) { scanner . setText ( sourceText ) ; nextToken ( ) ; while ( true ) { if ( scanner . getToken ( ) === SyntaxKind . EndOfFileToken ) { break ; } if ( tryConsumeDeclare ( ) || tryConsumeImport ( ) || tryConsumeExport ( ) || ( detectJavaScriptImports && ( tryConsumeRequireCall ( false , true ) || tryConsumeDefine ( ) ) ) ) { continue ; } else { nextToken ( ) ; } } scanner . setText ( undefined ) ; } if ( readImportFiles ) { processImports ( ) ; } processCommentPragmas ( pragmaContext , sourceText ) ; processPragmasIntoFields ( pragmaContext , noop ) ; if ( externalModule ) { if ( ambientExternalModules ) { for ( const decl of ambientExternalModules ) { importedFiles . push ( decl . ref ) ; } } return { referencedFiles : pragmaContext . referencedFiles , typeReferenceDirectives : pragmaContext . typeReferenceDirectives , libReferenceDirectives : pragmaContext . libReferenceDirectives , importedFiles , isLibFile : ! ! pragmaContext . hasNoDefaultLib , ambientExternalModules : undefined } ; } else { let ambientModuleNames ; if ( ambientExternalModules ) { for ( const decl of ambientExternalModules ) { if ( decl . depth === 0 ) { if ( ! ambientModuleNames ) { ambientModuleNames = [ ] ; } ambientModuleNames . push ( decl . ref . fileName ) ; } else { importedFiles . push ( decl . ref ) ; } } } return { referencedFiles : pragmaContext . referencedFiles , typeReferenceDirectives : pragmaContext . typeReferenceDirectives , libReferenceDirectives : pragmaContext . libReferenceDirectives , importedFiles , isLibFile : ! ! pragmaContext . hasNoDefaultLib , ambientExternalModules : ambientModuleNames } ; } } }