-
Notifications
You must be signed in to change notification settings - Fork 21
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
Slight reorganization around top-of-file comments #84
Slight reorganization around top-of-file comments #84
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just have a few questions about some seemingly-impossible runtime checks.
src/utils/get-comment-registry.ts
Outdated
@@ -243,7 +244,7 @@ export const getCommentRegistryFromImportDeclarations = ({ | |||
/** Constructed Output Nodes */ | |||
outputNodes: ImportDeclaration[]; | |||
}) => { | |||
if (outputNodes.length === 0 || !firstImport) { | |||
if (outputNodes?.length === 0 || !firstImport) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When would outputNodes
be undefined? We have TS to guarantee it's an array just above, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I concur that TypeScript would prevent that.
I wasn’t sure how robust we wanted our src/utils/*
to be against misuse.
I wrote a unit test that crashed this, so I made the check verify that it was non-null. All of my Fatal Internal Error
checks could be removed if you wanted to do so. I found them helpful during the initial implementation phase.
src/utils/get-comment-registry.ts
Outdated
@@ -368,7 +369,12 @@ export function attachCommentsToOutputNodes( | |||
/** Original declaration, not the re-sorted output-node! */ | |||
firstImport: ImportDeclaration, | |||
) { | |||
if (outputNodes.length === 0) { | |||
if (!Array.isArray(commentEntriesFromRegistry)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would this happen without throwing a TypeScript compilation error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I concur that TypeScript would prevent that. (Responded further here: #84 (comment))
src/utils/get-comment-registry.ts
Outdated
'Fatal Internal Error: Expected a list of commentEntriesFromRegistry', | ||
); | ||
} | ||
if (outputNodes == null || outputNodes.length === 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why check for a null outputNodes
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I concur that TypeScript would prevent that. (Responded further here: #84 (comment))
I think this makes the code a little easier to follow.
I wasn't able to add an end-to-end integration test around "empty-statements-at-top-of-file" because you're right @IanVS, other layers prevent that from happening.
Instead I added a unit-test to assert the fatal-errors that we throw.