Skip to content

feature request: public the TransformationContext.addDiagnostic and read them in the compiler and transpileModule #36733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
5 tasks done
Jack-Works opened this issue Feb 11, 2020 · 4 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@Jack-Works
Copy link
Contributor

Search Terms

custom transformer diagnostic

Suggestion

mark the method addDiagnostic in TransformationContext as public and read the diagnostic result in the compiler

Use Cases

I'm writing a custom transformer. In some cases, the transform will fail but I don't want to throw an Error. I want to add my error to the diagnostics.

Examples

I have tried to call the addDiagnostic by as any

function createDiagnosticForNodeInSourceFile(
    ts: ts,
    sourceFile: SourceFile,
    node: Node,
    message: DiagnosticMessage,
    arg0?: string | number,
    arg1?: string | number,
    arg2?: string | number,
    arg3?: string | number,
): DiagnosticWithLocation | undefined {
    return (ts as any).createDiagnosticForNodeInSourceFile?.(sourceFile, node, message, arg0, arg1, arg2, arg3)
}
function appendDiagnosticMessageForNode(context: TransformationContext, diag: DiagnosticWithLocation | undefined) {
    if (!diag) return false
    const _context = internalTransformationContext(context)
    if (!_context.addDiagnostic) return false
    _context.addDiagnostic(diag)
    return true

    function internalTransformationContext(
        context: TransformationContext,
    ): TransformationContext & { addDiagnostic?(diag: DiagnosticWithLocation): void } {
        return context as any
    }
}

But that doesn't work. After digging into the source code, I found that it seems like the diagnostics only read in the dts generating.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Mar 10, 2020
@RyanCavanaugh
Copy link
Member

@rbuckton what are your thoughts?

@timocov
Copy link
Contributor

timocov commented Jun 30, 2020

I'd like to see this in public as well. In my transformer there are cases where I'm 100% sure that the error is possible and I need to notify about that somehow.

Any updates on this?

@timocov
Copy link
Contributor

timocov commented Jul 1, 2020

@rbuckton @RyanCavanaugh just a friendly ping (it seems that there is no answer since last Ryan's question, soooo...)

@timocov
Copy link
Contributor

timocov commented May 16, 2023

Probably after #54276 it would worth to re-visit this ticket? 😂 cc @jakebailey (sorry for pining)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants