Draft: convert file get-user-code-frame.js to TypeScript #983
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Continuing conversion to TypeScript per issue #494.
Checklist:
docs site
The types for the
get-user-code-frame.jsfile are raising enough issues that I'm making a PR just for this one file.Adding TypeScript types highlights potential issues in the code itself. In my opinion there are a few potential issues here where the TypeScript warnings are correct and should not be ignored. So there are some things that I'd like to change.
Don't Understand:
const nodeRequireThere are other parts that I don't understand fully, and I might be missing something. Like this:
I'm unsure of the reasons for:
module.requireto a variablemodule.requirevs.requirecallsyntaxIt's inside a
try/catchso it seems like it should be okay to just callmodule.require('fs')directly and let any errors be thrown.Can Improve: potentially undefined
firstClientCodeFrameconst firstClientCodeFramehas possibleundefinedissues in two places: TheErroris not guaranteed to have astackproperty andfindis not guaranteed to find a match.Can Improve: potentially invalid imports
The current code assumes that if
readFileSyncis non-null thenchalkmust be as well. Do we know that for certain? The call tochalk.dimingetCodeFrameis not located inside of atry/catchblock.Solution 1) Move the
try/catchhigher up so that it encloses the entire function body.Solution 2) Allow the
getCodeFramefunction to throw any Errors andcatchthem when it is called ingetUserCodeFrame. The only export from this file isgetUserCodeFrame, so changing the behaviors of other functions or variables is not a problem.Solution 3) Enforce that the initial
try/catchblock which assigns the variables must have all variables or none. We can do this by returning an object with three properties instead of using three independent variables.Example of solution 3 implementation using IIFE:
But my inclination is towards Solution 2, which is what I did in the PR.