microsoft/TypeScript

transpileModule() does not generate diagnostics

Closed this issue · 3 comments

I would have expected the following console.log statement to have produced an array with at least 1 element. Running this on Typescript 1.6.2 does not produce anything.

var ts = require('typescript');

var content = 'var x: number = "string"';
var transpileOptions = {
  compilerOptions: {
    module: ts.ModuleKind.CommonJS
  },
  reportDiagnostics: true
};

var result = ts.transpileModule(content, transpileOptions);

console.log(result.diagnostics);

(Executed with node test.js)

transpileModule/transpile currently report only syntactic errors. Semantic errors are not reported since if it will yield tons of false positives i.e. we compile standalone file that has imports or uses something that should exists globally (even types from standard library). To summarize: current behavior is by design, in theory we can still collect semantic errors and expose then via different property, so user can differentiate between syntactic errors (which are real errors) and semantic errors (that are most likely appear because some name cannot be resolved in single file compilation scenario). If you think that having such functionality will be valuable - please file a suggestion issue for this.

In version 1.4.1 we were able to get these errors by calling ts.createProgram(...) and then getDiagnostics() on the object returned by createProgram(). How do we get that type of information in 1.6.2?

Program should expose a set of methods to query for diagnostics:

        getGlobalDiagnostics(cancellationToken?: CancellationToken): Diagnostic[];
        getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
        getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
        getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];