📄 getParsedConfigFile¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 4 |
| 📦 Imports | 1 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/tsconfig-utils/src/getParsedConfigFile.ts
📦 Imports¶
| Name | Source |
|---|---|
CORE_COMPILER_OPTIONS |
./compilerOptions |
Functions¶
getParsedConfigFile(tsserver: typeof ts, configFile: string, projectDirectory: string): ts.ParsedCommandLine¶
Parses a TSConfig file using the same logic as tsserver.
Parameters:
configFileany: the path to the tsconfig.json file, relative toprojectDirectoryprojectDirectoryany: the project directory to use as the CWD, defaults toprocess.cwd()
Raw JSDoc
Calls:
tsserver.getParsedCommandLineOfConfigFileformatDiagnosticsfs.readFileSyncpath.isAbsolutepath.joingetCurrentDirectory[ "Unable to parse the specified 'tsconfig' file. Ensure it's correct and has valid syntax.", formatDiagnostics(parsed.errors), ].joinpath.resolveprocess.cwdtsserver.formatDiagnostics
Internal Comments:
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/internal/eqeq-nullish
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Code
export function getParsedConfigFile(
tsserver: typeof ts,
configFile: string,
projectDirectory?: string,
): ts.ParsedCommandLine {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/internal/eqeq-nullish
if (tsserver.sys === undefined) {
throw new Error(
'`getParsedConfigFile` is only supported in a Node-like environment.',
);
}
const parsed = tsserver.getParsedCommandLineOfConfigFile(
configFile,
CORE_COMPILER_OPTIONS,
{
fileExists: fs.existsSync,
getCurrentDirectory,
onUnRecoverableConfigFileDiagnostic: diag => {
throw new Error(formatDiagnostics([diag])); // ensures that `parsed` is defined.
},
readDirectory: tsserver.sys.readDirectory,
readFile: file =>
fs.readFileSync(
path.isAbsolute(file) ? file : path.join(getCurrentDirectory(), file),
'utf-8',
),
useCaseSensitiveFileNames: tsserver.sys.useCaseSensitiveFileNames,
},
);
if (parsed?.errors.length) {
throw new Error(
[
"Unable to parse the specified 'tsconfig' file. Ensure it's correct and has valid syntax.",
formatDiagnostics(parsed.errors),
].join('\n\n'),
);
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return parsed!;
function getCurrentDirectory(): string {
return projectDirectory ? path.resolve(projectDirectory) : process.cwd();
}
function formatDiagnostics(diagnostics: ts.Diagnostic[]): string | undefined {
return tsserver.formatDiagnostics(diagnostics, {
getCanonicalFileName: f => f,
getCurrentDirectory,
getNewLine: () => '\n',
});
}
}
Internal helpers¶
Declared inside another function in this file.
onUnRecoverableConfigFileDiagnostic(diag: any): never¶
Parameters:
diagany
Returns: never
Calls:
formatDiagnostics
getCurrentDirectory(): string¶
Returns: string
Calls:
path.resolveprocess.cwd
Code
formatDiagnostics(diagnostics: ts.Diagnostic[]): string | undefined¶
Parameters:
diagnosticsts.Diagnostic[]
Returns: string | undefined
Calls:
tsserver.formatDiagnostics
Code
Generated by Syntax Scribe