Skip to content
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

Conversation

fbartho
Copy link
Collaborator

@fbartho fbartho commented May 15, 2023

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.

@fbartho fbartho requested a review from IanVS May 15, 2023 20:35
Copy link
Owner

@IanVS IanVS left a 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.

@@ -243,7 +244,7 @@ export const getCommentRegistryFromImportDeclarations = ({
/** Constructed Output Nodes */
outputNodes: ImportDeclaration[];
}) => {
if (outputNodes.length === 0 || !firstImport) {
if (outputNodes?.length === 0 || !firstImport) {
Copy link
Owner

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?

Copy link
Collaborator Author

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.

@@ -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)) {
Copy link
Owner

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?

Copy link
Collaborator Author

@fbartho fbartho May 15, 2023

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))

'Fatal Internal Error: Expected a list of commentEntriesFromRegistry',
);
}
if (outputNodes == null || outputNodes.length === 0) {
Copy link
Owner

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?

Copy link
Collaborator Author

@fbartho fbartho May 15, 2023

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))

@fbartho fbartho requested a review from IanVS May 15, 2023 23:44
@IanVS IanVS merged commit e9a9950 into fb/protect-the-top-of-file May 16, 2023
@IanVS IanVS deleted the fb/protect-the-top-of-file+clarify-patch-first-location-once branch May 16, 2023 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants