Ts-morph: Question: what is the difference between "diagnostics" and "preEmitDiagnostics?"

Created on 8 Aug 2018  路  3Comments  路  Source: dsherret/ts-morph

Couldn't find the answer here: https://dsherret.github.io/ts-simple-ast/setup/diagnostics

In the link there are project.getDiagnostics and project.getPreEmitDiagnostics. They seem identical. Are there any hidden difference e.g. in performance?

question

Most helpful comment

Hey @anurbol, excellent question! This was something I added a little while back and was mostly just trying to mirror functions available in the compiler api. I didn't realize the actual behaviour of getPreEmitDiagnostics, but after looking into it, you're right that these methods are almost identical. I recommend using .getPreEmitDiagnostics() as that will do the following:

export function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[] {
    const diagnostics = [
        ...program.getConfigFileParsingDiagnostics(),
        ...program.getOptionsDiagnostics(cancellationToken),
        ...program.getSyntacticDiagnostics(sourceFile, cancellationToken),
        ...program.getGlobalDiagnostics(cancellationToken),
        ...program.getSemanticDiagnostics(sourceFile, cancellationToken)
    ];

    if (program.getCompilerOptions().declaration) {
        addRange(diagnostics, program.getDeclarationDiagnostics(sourceFile, cancellationToken));
    }

    return sortAndDeduplicateDiagnostics(diagnostics);
}

The current .getDiagnostics() isn't as good as that and I'd rather leave as much functionality with the compiler api. So I'm going to deprecate .getDiagnostics() in #386.

All 3 comments

BTW, thanks @dsherret this library saved my life. Default TS compiler API was pain.

Hey @anurbol, excellent question! This was something I added a little while back and was mostly just trying to mirror functions available in the compiler api. I didn't realize the actual behaviour of getPreEmitDiagnostics, but after looking into it, you're right that these methods are almost identical. I recommend using .getPreEmitDiagnostics() as that will do the following:

export function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[] {
    const diagnostics = [
        ...program.getConfigFileParsingDiagnostics(),
        ...program.getOptionsDiagnostics(cancellationToken),
        ...program.getSyntacticDiagnostics(sourceFile, cancellationToken),
        ...program.getGlobalDiagnostics(cancellationToken),
        ...program.getSemanticDiagnostics(sourceFile, cancellationToken)
    ];

    if (program.getCompilerOptions().declaration) {
        addRange(diagnostics, program.getDeclarationDiagnostics(sourceFile, cancellationToken));
    }

    return sortAndDeduplicateDiagnostics(diagnostics);
}

The current .getDiagnostics() isn't as good as that and I'd rather leave as much functionality with the compiler api. So I'm going to deprecate .getDiagnostics() in #386.

Thank you for the response! Cool, I am glad I gave you idea on cleaning the library up. I ended up using
getPreEmitDiagnostics(). If I understand correctly, you will just deprecate the other method and add new methods to Diagnostic object item (of Diagnostic[], that is returned from getPreEmitDiagnostics()) so there would be no breaking change to API of getPreEmitDiagnostics() (I hope it would not be broke). Thank you again! Good luck!

Was this page helpful?
0 / 5 - 0 ratings