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AK,CAAC;wBACX,KAAK,MAAM,CAAC;wBACZ,KAAK,KAAK,CAAC;wBACX,KAAK,MAAM;4BACT,gGAAgG;4BAChG,MAAM,WAAW,GAAW,GAAG,oBAAoB,OAAO,CAAC;4BAE3D,IAAI,aAAa,GAAwB,cAAc,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;4BACzE,IAAI,aAAa,KAAK,SAAS,EAAE;gCAC/B,aAAa,GAAG,mBAAmB,CAAC,UAAW,CAAC,WAAW,CAAC,CAAC;gCAC7D,cAAc,CAAC,GAAG,CAAC,WAAW,EAAE,aAAa,CAAC,CAAC;6BAChD;4BAED,IAAI,aAAa,EAAE;gCACjB,6EAA6E;gCAC7E,6FAA6F;gCAC7F,OAAO,KAAK,CAAC;6BACd;4BACD,MAAM;qBACT;iBACF;aACF;YAED,6CAA6C;YAC7C,OAAO,mBAAmB,CAAC,UAAW,CAAC,QAAQ,CAAC,CAAC;QACnD,CAAC,CAAC;QAEF,OAAO,YAAY,CAAC;IACtB,CAAC;CACF;AAxKD,sCAwKC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport * as path from 'path';\nimport * as ts from 'typescript';\nimport colors = require('colors');\n\nimport { JsonFile } from '@rushstack/node-core-library';\n\nimport { ExtractorConfig } from './ExtractorConfig';\nimport type { IExtractorInvokeOptions } from './Extractor';\n\n/**\n * Options for {@link CompilerState.create}\n * @public\n */\nexport interface ICompilerStateCreateOptions {\n /** {@inheritDoc IExtractorInvokeOptions.typescriptCompilerFolder} */\n typescriptCompilerFolder?: string;\n\n /**\n * Additional .d.ts files to include in the analysis.\n */\n additionalEntryPoints?: string[];\n}\n\n/**\n * This class represents the TypeScript compiler state. This allows an optimization where multiple invocations\n * of API Extractor can reuse the same TypeScript compiler analysis.\n *\n * @public\n */\nexport class CompilerState {\n /**\n * The TypeScript compiler's `Program` object, which represents a complete scope of analysis.\n */\n public readonly program: unknown;\n\n private constructor(properties: CompilerState) {\n this.program = properties.program;\n }\n\n /**\n * Create a compiler state for use with the specified `IExtractorInvokeOptions`.\n */\n public static create(\n extractorConfig: ExtractorConfig,\n options?: ICompilerStateCreateOptions\n ): CompilerState {\n let tsconfig: {} | undefined = extractorConfig.overrideTsconfig;\n let configBasePath: string = extractorConfig.projectFolder;\n if (!tsconfig) {\n // If it wasn't overridden, then load it from disk\n tsconfig = JsonFile.load(extractorConfig.tsconfigFilePath);\n configBasePath = path.resolve(path.dirname(extractorConfig.tsconfigFilePath));\n }\n\n const commandLine: ts.ParsedCommandLine = ts.parseJsonConfigFileContent(tsconfig, ts.sys, configBasePath);\n\n if (!commandLine.options.skipLibCheck && extractorConfig.skipLibCheck) {\n commandLine.options.skipLibCheck = true;\n console.log(\n colors.cyan(\n 'API Extractor was invoked with skipLibCheck. This is not recommended and may cause ' +\n 'incorrect type analysis.'\n )\n );\n }\n\n const inputFilePaths: string[] = commandLine.fileNames.concat(extractorConfig.mainEntryPointFilePath);\n if (options && options.additionalEntryPoints) {\n inputFilePaths.push(...options.additionalEntryPoints);\n }\n\n // Append the entry points and remove any non-declaration files from the list\n const analysisFilePaths: string[] = CompilerState._generateFilePathsForAnalysis(inputFilePaths);\n\n const compilerHost: ts.CompilerHost = CompilerState._createCompilerHost(commandLine, options);\n\n const program: ts.Program = ts.createProgram(analysisFilePaths, commandLine.options, compilerHost);\n\n if (commandLine.errors.length > 0) {\n const errorText: string = ts.flattenDiagnosticMessageText(commandLine.errors[0].messageText, '\\n');\n throw new Error(`Error parsing tsconfig.json content: ${errorText}`);\n }\n\n return new CompilerState({\n program\n });\n }\n\n /**\n * Given a list of absolute file paths, return a list containing only the declaration\n * files. Duplicates are also eliminated.\n *\n * @remarks\n * The tsconfig.json settings specify the compiler's input (a set of *.ts source files,\n * plus some *.d.ts declaration files used for legacy typings). However API Extractor\n * analyzes the compiler's output (a set of *.d.ts entry point files, plus any legacy\n * typings). This requires API Extractor to generate a special file list when it invokes\n * the compiler.\n *\n * Duplicates are removed so that entry points can be appended without worrying whether they\n * may already appear in the tsconfig.json file list.\n */\n private static _generateFilePathsForAnalysis(inputFilePaths: string[]): string[] {\n const analysisFilePaths: string[] = [];\n\n const seenFiles: Set = new Set();\n\n for (const inputFilePath of inputFilePaths) {\n const inputFileToUpper: string = inputFilePath.toUpperCase();\n if (!seenFiles.has(inputFileToUpper)) {\n seenFiles.add(inputFileToUpper);\n\n if (!path.isAbsolute(inputFilePath)) {\n throw new Error('Input file is not an absolute path: ' + inputFilePath);\n }\n\n if (ExtractorConfig.hasDtsFileExtension(inputFilePath)) {\n analysisFilePaths.push(inputFilePath);\n }\n }\n }\n\n return analysisFilePaths;\n }\n\n private static _createCompilerHost(\n commandLine: ts.ParsedCommandLine,\n options: IExtractorInvokeOptions | undefined\n ): ts.CompilerHost {\n // Create a default CompilerHost that we will override\n const compilerHost: ts.CompilerHost = ts.createCompilerHost(commandLine.options);\n\n // Save a copy of the original members. Note that \"compilerHost\" cannot be the copy, because\n // createCompilerHost() captures that instance in a closure that is used by the members.\n const defaultCompilerHost: ts.CompilerHost = { ...compilerHost };\n\n if (options && options.typescriptCompilerFolder) {\n // Prevent a closure parameter\n const typescriptCompilerLibFolder: string = path.join(options.typescriptCompilerFolder, 'lib');\n compilerHost.getDefaultLibLocation = () => typescriptCompilerLibFolder;\n }\n\n // Used by compilerHost.fileExists()\n // .d.ts file path --> whether the file exists\n const dtsExistsCache: Map = new Map();\n\n // Used by compilerHost.fileExists()\n // Example: \"c:/folder/file.part.ts\"\n const fileExtensionRegExp: RegExp = /^(.+)(\\.[a-z0-9_]+)$/i;\n\n compilerHost.fileExists = (fileName: string): boolean => {\n // In certain deprecated setups, the compiler may write its output files (.js and .d.ts)\n // in the same folder as the corresponding input file (.ts or .tsx). When following imports,\n // API Extractor wants to analyze the .d.ts file; however recent versions of the compiler engine\n // will instead choose the .ts file. To work around this, we hook fileExists() to hide the\n // existence of those files.\n\n // Is \"fileName\" a .d.ts file? The double extension \".d.ts\" needs to be matched specially.\n if (!ExtractorConfig.hasDtsFileExtension(fileName)) {\n // It's not a .d.ts file. Is the file extension a potential source file?\n const match: RegExpExecArray | null = fileExtensionRegExp.exec(fileName);\n if (match) {\n // Example: \"c:/folder/file.part\"\n const pathWithoutExtension: string = match[1];\n // Example: \".ts\"\n const fileExtension: string = match[2];\n\n switch (fileExtension.toLocaleLowerCase()) {\n case '.ts':\n case '.tsx':\n case '.js':\n case '.jsx':\n // Yes, this is a possible source file. Is there a corresponding .d.ts file in the same folder?\n const dtsFileName: string = `${pathWithoutExtension}.d.ts`;\n\n let dtsFileExists: boolean | undefined = dtsExistsCache.get(dtsFileName);\n if (dtsFileExists === undefined) {\n dtsFileExists = defaultCompilerHost.fileExists!(dtsFileName);\n dtsExistsCache.set(dtsFileName, dtsFileExists);\n }\n\n if (dtsFileExists) {\n // fileName is a potential source file and a corresponding .d.ts file exists.\n // Thus, API Extractor should ignore this file (so the .d.ts file will get analyzed instead).\n return false;\n }\n break;\n }\n }\n }\n\n // Fall through to the default implementation\n return defaultCompilerHost.fileExists!(fileName);\n };\n\n return compilerHost;\n }\n}\n"]}