Skip to content

⬅️ Back to Table of Contents

📄 getParsedConfigFile.ts

📊 Analysis Summary

Metric Count
🔧 Functions 15
📦 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

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(formatDiagnostics(parsed.errors));
  }

  // 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',
    });
  }
}
  • 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()`
     */
    

  • Parameters:

  • tsserver: typeof ts
  • configFile: string
  • projectDirectory: string
  • Return Type: ts.ParsedCommandLine
  • Calls:
  • tsserver.getParsedCommandLineOfConfigFile
  • formatDiagnostics
  • fs.readFileSync
  • path.isAbsolute
  • path.join
  • getCurrentDirectory
  • 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
    

onUnRecoverableConfigFileDiagnostic(diag: any): never

Code
diag => {
        throw new Error(formatDiagnostics([diag])); // ensures that `parsed` is defined.
      }
  • Parameters:
  • diag: any
  • Return Type: never
  • Calls:
  • formatDiagnostics

readFile(file: any): string

Code
file =>
        fs.readFileSync(
          path.isAbsolute(file) ? file : path.join(getCurrentDirectory(), file),
          'utf-8',
        )
  • Parameters:
  • file: any
  • Return Type: string
  • Calls:
  • fs.readFileSync

onUnRecoverableConfigFileDiagnostic(diag: any): never

Code
diag => {
        throw new Error(formatDiagnostics([diag])); // ensures that `parsed` is defined.
      }
  • Parameters:
  • diag: any
  • Return Type: never
  • Calls:
  • formatDiagnostics

readFile(file: any): string

Code
file =>
        fs.readFileSync(
          path.isAbsolute(file) ? file : path.join(getCurrentDirectory(), file),
          'utf-8',
        )
  • Parameters:
  • file: any
  • Return Type: string
  • Calls:
  • fs.readFileSync

onUnRecoverableConfigFileDiagnostic(diag: any): never

Code
diag => {
        throw new Error(formatDiagnostics([diag])); // ensures that `parsed` is defined.
      }
  • Parameters:
  • diag: any
  • Return Type: never
  • Calls:
  • formatDiagnostics

readFile(file: any): string

Code
file =>
        fs.readFileSync(
          path.isAbsolute(file) ? file : path.join(getCurrentDirectory(), file),
          'utf-8',
        )
  • Parameters:
  • file: any
  • Return Type: string
  • Calls:
  • fs.readFileSync

onUnRecoverableConfigFileDiagnostic(diag: any): never

Code
diag => {
        throw new Error(formatDiagnostics([diag])); // ensures that `parsed` is defined.
      }
  • Parameters:
  • diag: any
  • Return Type: never
  • Calls:
  • formatDiagnostics

readFile(file: any): string

Code
file =>
        fs.readFileSync(
          path.isAbsolute(file) ? file : path.join(getCurrentDirectory(), file),
          'utf-8',
        )
  • Parameters:
  • file: any
  • Return Type: string
  • Calls:
  • fs.readFileSync

getCurrentDirectory(): string

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

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

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

getCanonicalFileName(f: any): any

Code
f => f
  • Parameters:
  • f: any
  • Return Type: any

getNewLine(): string

Code
() => '\n'
  • Return Type: string

getCanonicalFileName(f: any): any

Code
f => f
  • Parameters:
  • f: any
  • Return Type: any

getNewLine(): string

Code
() => '\n'
  • Return Type: string