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.js
file 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 nodeRequire
There 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.require
to a variablemodule.require
vs.require
call
syntaxIt's inside a
try
/catch
so it seems like it should be okay to just callmodule.require('fs')
directly and let any errors be thrown.Can Improve: potentially undefined
firstClientCodeFrame
const firstClientCodeFrame
has possibleundefined
issues in two places: TheError
is not guaranteed to have astack
property andfind
is not guaranteed to find a match.Can Improve: potentially invalid imports
The current code assumes that if
readFileSync
is non-null thenchalk
must be as well. Do we know that for certain? The call tochalk.dim
ingetCodeFrame
is not located inside of atry
/catch
block.Solution 1) Move the
try
/catch
higher up so that it encloses the entire function body.Solution 2) Allow the
getCodeFrame
function to throw any Errors andcatch
them 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
/catch
block 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.