Skip to content

⬅️ Back to Table of Contents

📄 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:

  • configFile any: the path to the tsconfig.json file, relative to projectDirectory
  • projectDirectory any: the project directory to use as the CWD, defaults to process.cwd()
Raw JSDoc
/**
 * Parses a TSConfig file using the same logic as tsserver.
 *
 * @param configFile the path to the tsconfig.json file, relative to `projectDirectory`
 * @param projectDirectory the project directory to use as the CWD, defaults to `process.cwd()`
 */

Calls:

  • tsserver.getParsedCommandLineOfConfigFile
  • formatDiagnostics
  • fs.readFileSync
  • path.isAbsolute
  • path.join
  • getCurrentDirectory
  • [ "Unable to parse the specified 'tsconfig' file. Ensure it's correct and has valid syntax.", formatDiagnostics(parsed.errors), ].join
  • path.resolve
  • process.cwd
  • tsserver.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:

  • diag any

Returns: never

Calls:

  • formatDiagnostics
Code
diag => {
        throw new Error(formatDiagnostics([diag])); // ensures that `parsed` is defined.
      }

getCurrentDirectory(): string

Returns: string

Calls:

  • path.resolve
  • process.cwd
Code
function getCurrentDirectory(): string {
    return projectDirectory ? path.resolve(projectDirectory) : process.cwd();
  }

formatDiagnostics(diagnostics: ts.Diagnostic[]): string | undefined

Parameters:

  • diagnostics ts.Diagnostic[]

Returns: string | undefined

Calls:

  • tsserver.formatDiagnostics
Code
function formatDiagnostics(diagnostics: ts.Diagnostic[]): string | undefined {
    return tsserver.formatDiagnostics(diagnostics, {
      getCanonicalFileName: f => f,
      getCurrentDirectory,
      getNewLine: () => '\n',
    });
  }

Generated by Syntax Scribe