-
Notifications
You must be signed in to change notification settings - Fork 133
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
fix: Correctly handle custom data factories by binding functions #467
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe changes in the pull request focus on the Changes
Assessment against linked issues
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (1)
src/N3Parser.js (1)
1086-1091
: Consider documenting factory method requirements.
Since the correct binding of factory methods is crucial for custom data factories, consider adding documentation that explains:
- The requirement for factory methods to maintain their context
- How custom factory implementations should be structured
- Examples of correct factory method usage
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
⛔ Files ignored due to path filters (1)
package-lock.json
is excluded by!**/package-lock.json
📒 Files selected for processing (1)
- src/N3Parser.js (1 hunks)
🔇 Additional comments (1)
src/N3Parser.js (1)
1086-1091
: LGTM! The factory method bindings fix the context issue.
The changes correctly bind the factory methods to their factory object, ensuring that this
context is preserved. This fixes the issue where factory methods were being treated as standalone functions.
Let's verify the usage of these factory methods:
✅ Verification successful
Let me analyze the usage patterns found in the codebase to verify if the factory method bindings are correctly implemented and used:
Based on the analysis of the codebase usage patterns and the implementation of initDataFactory
, I can now provide the final response:
The factory method bindings are correctly implemented and used throughout the codebase
The verification shows that:
- The factory methods are properly bound in
initDataFactory
- All direct factory method usages across the codebase are safe:
- In tests (N3Parser-test.js, N3Store-test.js): Factory methods are used in test setup
- In N3Store.js: Methods are called on
this._factory
maintaining proper context - In N3DataFactory.js: Factory is passed as parameter and methods are called directly on it
- In N3Util.js: Factory methods are called with proper context
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Check for any direct usage of factory methods without proper binding
# Test: Search for factory method usage patterns. Expect: No unbound factory method calls.
rg -A 3 "factory\.(namedNode|blankNode|literal|variable|quad)"
Length of output: 3245
Script:
#!/bin/bash
# Let's check the implementation of initDataFactory to understand the full context
ast-grep --pattern 'function initDataFactory($_) {
$$$
}'
Length of output: 61
Script:
#!/bin/bash
# Let's try a different approach to find the initDataFactory function
rg -A 15 "function initDataFactory"
Length of output: 1018
That's @smessie ! Could you add some regression tests? |
Added a small test @jeswr |
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.
Instead of binding, can we call them on the factory
object?
These functions are called hundreds of thousands of times per second; the difference might matter in some engines.
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.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (2)
src/N3Parser.js (1)
1086-1091
: Consider adding regression tests as requested.As suggested in the PR comments by jeswr, we should add regression tests to ensure this fix works correctly with custom data factories.
Would you like me to help generate regression tests that verify:
- Custom factory methods maintain their context
- Blank nodes are correctly generated (to prevent
undefinedNaN
values)- Integration with
rdf-data-factory
test/N3Parser-test.js (1)
2619-2620
: Specify the exact expected number of quadsCurrently, the test asserts that
quads.length
is greater than zero. For greater precision and to ensure the test fails when the number of quads is incorrect, consider asserting the exact expected number of quads.Apply this diff to make the test more precise:
- expect(quads.length).toBeGreaterThan(0); + expect(quads.length).toBe(2);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
⛔ Files ignored due to path filters (1)
package-lock.json
is excluded by!**/package-lock.json
📒 Files selected for processing (2)
src/N3Parser.js
(1 hunks)test/N3Parser-test.js
(1 hunks)
🔇 Additional comments (2)
src/N3Parser.js (1)
1086-1091
: LGTM! Factory methods are now correctly bound to maintain context.
The changes properly bind all factory methods to their factory instance, which ensures that the this
context is preserved when these methods are called. This fix aligns with the PR objectives and should resolve the issues with custom data factories like rdf-data-factory
.
Let's verify that all factory methods are consistently bound:
✅ Verification successful
defaultGraph()
call doesn't require binding as it's used for a one-time constant initialization
The verification shows that factory.defaultGraph()
is used to initialize a constant DEFAULTGRAPH
on the parser, not as a method that needs to maintain context for future calls. All other factory methods that are stored for later use (_namedNode
, _blankNode
, _literal
, _variable
, _quad
) are correctly bound to the factory.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify that all factory methods are bound consistently
# Test: Check for any factory method assignments that might be missing .bind(factory)
rg -A 1 "factory\." src/N3Parser.js | rg -v "\.bind\(factory\)"
Length of output: 149
Script:
#!/bin/bash
# Let's check the full context around defaultGraph usage
rg -B 5 -A 5 "defaultGraph" src/N3Parser.js
Length of output: 537
test/N3Parser-test.js (1)
2596-2637
: Well-designed test verifying custom data factory with preserved this
context
The test accurately validates the handling of custom data factories that rely on the internal this
context, ensuring methods are correctly bound to their factory objects.
Closes #466.
Tested this and this seems to resolve rubensworks/rdf-dereference.js#55
Summary by CodeRabbit
New Features
Bug Fixes