Skip to content

Convert some default exports to named ones #1717

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

Merged
merged 1 commit into from
Apr 10, 2025

Conversation

alexeyr-ci2
Copy link
Collaborator

@alexeyr-ci2 alexeyr-ci2 commented Mar 26, 2025

Summary

Simplifies code (mostly in preparation for larger similar future changes)

Pull Request checklist

  • [ ] Add/update test to cover these changes
  • [ ] Update documentation
  • [ ] Update CHANGELOG file

This change is Reviewable

Summary by CodeRabbit

  • Refactor

    • Improved internal organization by standardizing module exports and imports. Core functionalities—such as token handling, component and store registration, and script generation—have been streamlined for better modularity and consistency.
  • Tests

    • Updated test suites to align with the new export structure and to provide clearer error messaging for enhanced validation.

Copy link
Contributor

coderabbitai bot commented Mar 26, 2025

Walkthrough

Several modules have been refactored to enhance modularity. Default exports were replaced with named exports in multiple source files, and associated import statements were updated to namespace imports. Minor adjustments include changes to function signatures, error message strings, and a variable initialization. The overall functionality remains unchanged while the export/import patterns have been standardized.

Changes

File(s) Change Summary
node_package/src/{Authenticity.ts, ComponentRegistry.ts, RenderUtils.ts, StoreRegistry.ts} Converted default exports to named exports and updated method/function signatures.
node_package/src/{ReactOnRails.client.ts, buildConsoleReplay.ts, serverRenderReactComponent.ts, streamServerRenderedReactComponent.ts}
node_package/tests/{ComponentRegistry.test.js, streamServerRenderedReactComponent.test.jsx, StoreRegistry.test.js, serverRenderReactComponent.test.ts}
Updated import statements from default imports to namespace imports; adjusted usage patterns, including function calls, error messages, and variable initialization.

Suggested reviewers

  • Judahmeek

Poem

I'm a rabbit in the code field, hopping with glee,
Default exports turned named, as clear as can be.
Imports now wear a namespace cap, so neat,
In refactored lines, I find a rhythmic beat.
Carrots of code crunch through every change I see!
🥕🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6f0a2d7 and a62702e.

📒 Files selected for processing (12)
  • node_package/src/Authenticity.ts (1 hunks)
  • node_package/src/ComponentRegistry.ts (1 hunks)
  • node_package/src/ReactOnRails.client.ts (1 hunks)
  • node_package/src/RenderUtils.ts (1 hunks)
  • node_package/src/StoreRegistry.ts (1 hunks)
  • node_package/src/buildConsoleReplay.ts (2 hunks)
  • node_package/src/serverRenderReactComponent.ts (2 hunks)
  • node_package/src/streamServerRenderedReactComponent.ts (1 hunks)
  • node_package/tests/ComponentRegistry.test.js (1 hunks)
  • node_package/tests/StoreRegistry.test.js (2 hunks)
  • node_package/tests/serverRenderReactComponent.test.ts (1 hunks)
  • node_package/tests/streamServerRenderedReactComponent.test.jsx (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • node_package/tests/serverRenderReactComponent.test.ts
🚧 Files skipped from review as they are similar to previous changes (10)
  • node_package/src/streamServerRenderedReactComponent.ts
  • node_package/src/RenderUtils.ts
  • node_package/tests/streamServerRenderedReactComponent.test.jsx
  • node_package/src/buildConsoleReplay.ts
  • node_package/src/Authenticity.ts
  • node_package/tests/StoreRegistry.test.js
  • node_package/src/serverRenderReactComponent.ts
  • node_package/tests/ComponentRegistry.test.js
  • node_package/src/ReactOnRails.client.ts
  • node_package/src/StoreRegistry.ts
🧰 Additional context used
🧠 Learnings (1)
node_package/src/ComponentRegistry.ts (1)
Learnt from: AbanoubGhadban
PR: shakacode/react_on_rails#1644
File: node_package/src/ComponentRegistry.ts:59-61
Timestamp: 2025-04-09T21:46:33.449Z
Learning: In ComponentRegistry, callbacks for waiting on component registration are managed by the CallbackRegistry instance and don't need separate cleanup as they are only relevant while waiting for components to be registered.
🧬 Code Graph Analysis (1)
node_package/src/ComponentRegistry.ts (3)
node_package/src/ReactOnRails.client.ts (2)
  • register (45-47)
  • getOrWaitForComponent (155-157)
node_package/src/types/index.ts (2)
  • ReactComponentOrRenderFunction (108-108)
  • RegisteredComponent (125-138)
node_package/src/CallbackRegistry.ts (2)
  • get (77-84)
  • clear (90-93)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: dummy-app-integration-tests (newest)
  • GitHub Check: dummy-app-integration-tests (oldest)
  • GitHub Check: rspec-package-tests (newest)
  • GitHub Check: rspec-package-tests (oldest)
  • GitHub Check: build
🔇 Additional comments (4)
node_package/src/ComponentRegistry.ts (4)

7-31: Great conversion from default export to named export function.

The refactoring maintains the original functionality while making the export pattern more explicit. This approach will make imports clearer in consuming files, as they'll now use namespace imports like import * as ComponentRegistry from './ComponentRegistry'.


33-37: Good extraction of the get method as a named export.

The arrow function syntax is clean and the JSDoc comments help clarify the return value structure.


39-40: Clean implementation of getOrWaitForComponent as a named export.

This maintains functionality while simplifying the export pattern. Note that this function returns a Promise that resolves when the component is registered, which is useful for asynchronous loading scenarios.


42-47: Well-documented components function with detailed JSDoc.

The comprehensive JSDoc comment clearly explains the purpose and return value of this debugging utility.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

}
return null;
},
export function authenticityToken(): string | null {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexeyr-ci alexeyr-ci force-pushed the alexeyr/improve-tree-shakability branch from 3caa95c to 0ea105a Compare March 26, 2025 14:20
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
node_package/src/serverRenderReactComponent.ts (1)

139-139: Initialize renderState variable

The variable is now declared without initialization. Although it gets assigned a value in all code paths before use, it's generally a good practice to initialize variables to prevent potential issues.

Consider initializing the variable:

-  let renderState: RenderState;
+  let renderState: RenderState = { result: null, hasErrors: false };
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 99a2d71 and 0ea105a.

📒 Files selected for processing (11)
  • node_package/src/Authenticity.ts (1 hunks)
  • node_package/src/ComponentRegistry.ts (1 hunks)
  • node_package/src/ReactOnRails.client.ts (1 hunks)
  • node_package/src/RenderUtils.ts (1 hunks)
  • node_package/src/StoreRegistry.ts (1 hunks)
  • node_package/src/buildConsoleReplay.ts (2 hunks)
  • node_package/src/serverRenderReactComponent.ts (2 hunks)
  • node_package/src/streamServerRenderedReactComponent.ts (1 hunks)
  • node_package/tests/ComponentRegistry.test.js (1 hunks)
  • node_package/tests/serverRenderReactComponent.test.js (1 hunks)
  • node_package/tests/streamServerRenderedReactComponent.test.jsx (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
node_package/src/ComponentRegistry.ts (1)
Learnt from: AbanoubGhadban
PR: shakacode/react_on_rails#1644
File: node_package/src/ComponentRegistry.ts:59-61
Timestamp: 2025-03-21T05:13:50.847Z
Learning: In ComponentRegistry, callbacks for waiting on component registration are managed by the CallbackRegistry instance and don't need separate cleanup as they are only relevant while waiting for components to be registered.
🧬 Code Definitions (4)
node_package/src/Authenticity.ts (2)
node_package/src/ReactOnRails.client.ts (2)
  • authenticityToken (164-166)
  • authenticityHeaders (174-176)
node_package/src/types/index.ts (1)
  • AuthenticityHeaders (92-92)
node_package/src/buildConsoleReplay.ts (1)
node_package/src/RenderUtils.ts (1)
  • wrapInScriptTags (2-11)
node_package/src/serverRenderReactComponent.ts (1)
node_package/src/types/index.ts (1)
  • RenderState (206-210)
node_package/src/StoreRegistry.ts (2)
node_package/src/ReactOnRails.client.ts (8)
  • storeGenerators (327-329)
  • getStore (82-84)
  • getStoreGenerator (197-199)
  • setStore (205-207)
  • clearHydratedStores (213-215)
  • stores (335-337)
  • getOrWaitForStore (91-93)
  • getOrWaitForStoreGenerator (100-102)
node_package/src/types/index.ts (2)
  • StoreGenerator (96-96)
  • Store (95-95)
🪛 GitHub Actions: JS unit tests for Renderer package
node_package/tests/streamServerRenderedReactComponent.test.jsx

[warning] 13-13: Called register for component that is already registered TestComponentForStreaming

node_package/tests/serverRenderReactComponent.test.js

[error] 64-64: Exception in rendering! Detected a renderer while server rendering component 'X4'.

🪛 GitHub Actions: Lint JS and Ruby
node_package/src/ComponentRegistry.ts

[error] 49-49: Unused exports (1): clear function

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: dummy-app-integration-tests (oldest)
  • GitHub Check: dummy-app-integration-tests (newest)
🔇 Additional comments (19)
node_package/tests/streamServerRenderedReactComponent.test.jsx (1)

8-8: Import statement updated appropriately.

The change from default import to namespace import aligns with the PR's objective of converting default exports to named ones. This ensures all exported functions from ComponentRegistry are accessible through the ComponentRegistry namespace.

node_package/tests/serverRenderReactComponent.test.js (1)

7-7: Import statement updated appropriately.

The change from default import to namespace import aligns with the PR's objective of converting default exports to named ones. All ComponentRegistry methods are still accessed correctly throughout the test file.

node_package/src/streamServerRenderedReactComponent.ts (1)

5-5: Import statement updated appropriately.

The change from default import to namespace import aligns with the PR's objective of converting default exports to named ones. The ComponentRegistry.get() method on line 186 is still accessed correctly.

node_package/src/RenderUtils.ts (1)

1-11: Function export structure improved.

Good conversion from default export of an object to a named export of the function. This change:

  1. Makes the export structure clearer
  2. Allows for easier addition of more exports in the future
  3. Maintains the same functionality while improving maintainability

The ESLint comment clearly explains the reasoning behind this approach.

node_package/src/Authenticity.ts (2)

3-9: Nice refactoring to named exports

Converting the authenticityToken method to a standalone exported function aligns well with modern JavaScript practices and the PR objective of converting default exports to named ones.


11-15: Well-structured conversion to arrow function

Good job converting the authenticityHeaders method to an arrow function that directly calls authenticityToken. This simplifies the code and eliminates the need for this context binding.

node_package/src/buildConsoleReplay.ts (2)

1-1: Good use of named import

Replacing the default import with a specific named import improves code clarity and facilitates tree-shaking.


57-57: Clean direct function usage

The change from RenderUtils.wrapInScriptTags to directly using the imported wrapInScriptTags function simplifies the code while maintaining the same functionality.

node_package/tests/ComponentRegistry.test.js (1)

9-9: Good update to namespace import

Converting from default import to namespace import is consistent with the PR objective and ensures the test file works correctly with the refactored ComponentRegistry module.

node_package/src/serverRenderReactComponent.ts (1)

4-4: Good conversion to namespace import

Updating to namespace import aligns with the changes in other files and maintains consistency across the codebase.

node_package/src/ReactOnRails.client.ts (1)

4-5: Import changes look good!

The change from default imports to namespace imports is consistent with the PR goal of converting default exports to named exports. This change is necessary to match the updated export style in the imported modules.

Also applies to: 8-8

node_package/src/ComponentRegistry.ts (2)

7-31: Export restructuring looks good!

Converting the register method from being part of a default exported object to a standalone exported function maintains the same functionality while making the code more modular.


33-48: Named exports approach is cleaner!

Converting these methods to named exports improves the modularity of the code and makes it more aligned with modern JavaScript/TypeScript practices. The functionality remains unchanged while allowing for more flexible importing options.

node_package/src/StoreRegistry.ts (6)

7-28: Export refactoring looks good!

Converting the register method to a standalone exported function maintains the same functionality while making the code more modular and easier to import.


30-55: Named export approach is appropriate!

The getStore function's export refactoring maintains all the error handling and functionality while moving to a more modular export pattern.


57-62: Clean arrow function export!

The conversion to a named arrow function export is concise and maintains the same functionality.


64-78: Export restructuring maintains functionality!

Both setStore and clearHydratedStores functions have been properly converted to named exports while preserving their original implementation.


80-90: Accessor functions correctly exported!

The storeGenerators and stores functions have been converted to arrow function exports, which is a clean and concise way to export these utility functions.


92-106: Promise-returning functions properly exported!

The getOrWaitForStore and getOrWaitForStoreGenerator functions have been correctly converted to named arrow function exports while maintaining their functionality.

@alexeyr-ci alexeyr-ci force-pushed the alexeyr/improve-tree-shakability branch 5 times, most recently from 7de0704 to 6f0a2d7 Compare March 26, 2025 14:35
@alexeyr-ci alexeyr-ci force-pushed the alexeyr/improve-tree-shakability branch from 6f0a2d7 to a62702e Compare April 10, 2025 07:17
@alexeyr-ci2 alexeyr-ci2 merged commit 2b692cc into master Apr 10, 2025
11 checks passed
@alexeyr-ci2 alexeyr-ci2 deleted the alexeyr/improve-tree-shakability branch April 10, 2025 07:25
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.

3 participants