Description
π Search Terms
"SourceFile", "commentDirectives", "public"
β Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
Please consider making the sourceFile.commentDirectives
property public. Currently it is marked as internal:
TypeScript/src/compiler/types.ts
Line 4434 in b504a1e
π 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!)