Skip to content

Make the sourceFile.commentDirectives property public #61809

Closed
@mrazauskas

Description

@mrazauskas

πŸ” Search Terms

"SourceFile", "commentDirectives", "public"

βœ… Viability Checklist

⭐ Suggestion

Please consider making the sourceFile.commentDirectives property public. Currently it is marked as internal:

/** @internal */ commentDirectives?: CommentDirective[];

πŸ“ƒ Motivating Example

The sourceFile.commentDirectives property currently is available at run time. Only the types are missing.

The community is actually using it. I am not really good in searching on GitHub, but here are few examples: https://github.com/search?q=Array.isArray%28commentDirectives%29&type=code

And one more here: https://github.com/Dionysusnu/roblox-ts/blob/dcf84ff4477830e47a21818315c71fb20d20e215/src/Project/preEmitDiagnostics/fileUsesCommentDirectives.ts#L12

The idea is simple: the // @ts-ignore and // @ts-expect-error comments are already collected and available just like any other nodes. The community projects could handle them as they like.

πŸ’» Use Cases

What do you want to use this for?

In my project, I want to check the text provided after // @ts-expect-error and to make sure it matches the diagnostic message. Somewhat similarly to typescript-eslint, that has a rule which requires providing a note after // @ts-expect-error, but one step forward.

What shortcomings exist with current approaches?

Based on current usage example, this is possible:

if ("commentDirectives" in sourceFile && Array.isArray(sourceFile.commentDirectives)) {
  // ...
}

, but the type of commentDirectives is any[]. Not a problem to cast it, of course.

What workarounds are you using in the meantime?

Instead of using the internals, a solution could be to call sourceFile.getText() and pass it through some logic which finds directives. That would work, but sounds like duplication, because the scanner did the same job already. (And did it better as well!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions