Skip to content

feat: use origin based </x/y/> relativisation rather than <../../../x/y/> when result is a shorter relative IRI. #529

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jeswr
Copy link
Collaborator

@jeswr jeswr commented May 26, 2025

Partially resolves #528 but does not cover the following test cases (where there are 2 or more sub paths after the relativisation):

     relativizes('an IRI directly relative to the base with two sub paths', 'http://example.org/a/b/c/d/e/f/',
       'http://example.org/a/b/c/d/h/i/', '../../h/i/');

     relativizes('an IRI where it is better to use a / path [/x/y/z]', 'http://example.org/x/q/r/n/m/',
       'http://example.org/x/y/z', '/x/y/z');

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced the path replacement logic to better handle parent path resolution based on relative length comparisons.
  • Tests
    • Added new test cases to verify correct behavior of relative path generation for a range of IRI structures.

Copy link
Contributor

coderabbitai bot commented May 26, 2025

Walkthrough

The changes update the _getBaseMatcher() method in BaseIRI.js to implement conditional logic for path replacements based on segment lengths. Additional tests are added to verify the improved relative IRI generation for various URL structures.

Changes

File(s) Change Summary
src/BaseIRI.js Modified _getBaseMatcher() to include conditional path replacement logic based on segment lengths.
test/BaseIRI-test.js Added two new test cases to verify the correctness of relative IRI outputs with different URL structures.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant BaseIRI

    Caller->>BaseIRI: toRelative(targetIRI)
    BaseIRI->>BaseIRI: _getBaseMatcher()
    Note right of BaseIRI: Conditional path replacement<br>based on segment lengths
    BaseIRI-->>Caller: Returns relative IRI
Loading

Assessment against linked issues

Objective Addressed Explanation
Cleaner relative IRI writing for base IRIs (e.g., prefer over ../../../../Jesse) (#528) The tests verify that toRelative() produces cleaner relative IRIs, aligning with the objective.

Possibly related PRs

Suggested labels

semver.minor

Poem

In the warren of code, a path is made neat,
With slashes and dots, no more tangled feet.
Now Jesse appears with a hop and a cheer—
No more ../../, just clarity here!
🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

npm warn config production Use --omit=dev instead.
npm error Exit handler never called!
npm error This is an error with npm itself. Please report this error at:
npm error https://github.com/npm/cli/issues
npm error A complete log of this run can be found in: /.npm/_logs/2025-05-26T13_41_49_709Z-debug-0.log


📜 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 eb1030a and 1cc2acc.

📒 Files selected for processing (2)
  • src/BaseIRI.js (2 hunks)
  • test/BaseIRI-test.js (1 hunks)
🔇 Additional comments (2)
test/BaseIRI-test.js (1)

141-145: LGTM! Test cases validate the origin-based path optimization.

The new test cases effectively verify that origin-based paths (/foo/bar, /x/y) are correctly chosen over longer parent paths (../../../../bar, ../../../../y) when they result in shorter relative IRIs. This aligns perfectly with the PR objectives.

src/BaseIRI.js (1)

9-9: LGTM! Origin constant properly defined.

The ORIGIN constant is well-defined and follows the existing naming pattern in the codebase.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.

@jeswr jeswr requested a review from RubenVerborgh May 26, 2025 13:10
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: 2

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5a58e93 and eb1030a.

📒 Files selected for processing (2)
  • src/BaseIRI.js (2 hunks)
  • test/BaseIRI-test.js (1 hunks)
🔇 Additional comments (2)
test/BaseIRI-test.js (1)

144-148: LGTM! Good test coverage for origin-based path optimization.

These test cases effectively validate the new logic that prefers shorter origin-based paths over longer parent-based paths.

src/BaseIRI.js (1)

9-9: LGTM! Good use of a named constant.

Using a named constant for the origin character improves code readability and maintainability.

@RubenVerborgh
Copy link
Member

Do we want this though? Because a big reason for relative support in the writer is the ability to move documents without changing syntax. CC @smessie

@smessie
Copy link
Contributor

smessie commented May 26, 2025

Although </a/b/c> appears more aesthetically pleasing than <../../../a/b/c>, it negates the advantages of using relative IRIs. Even though the IRI doesn’t include the base IRI, using an "absolute" IRI like </a/b/c> renders it no longer relative to the current RDF document. Instead, it becomes relativized against the base IRI. As @RubenVerborgh rightly points out, this approach eliminates the ability to relocate the documents, as the base IRI will change, which is not an issue when the IRIs are relative to each other.

Therefore, I strongly recommend maintaining the current state and refraining from merging this PR.

@jeswr
Copy link
Collaborator Author

jeswr commented May 26, 2025

Right, that makes sense.

The context for opening this PR is that I use the BaseIRI class to relativise IRIs in https://github.com/jeswr/pretty-turtle which has an explicit goal of maximal readability (or compactness depending on settings).

Given that I think this functionality would still be useful opt-in functionality in pretty-turtle. Would you be open to this being a separate opt-in here as well. If not I can just create a custom BaseIRI class downstream.

@RubenVerborgh
Copy link
Member

My two cents: pretty printing is a very different business from N3Writer. I'd architect the thing very differently for pretty printing (especially if the streaming constraint can be dropped).

It could be an option on BaseIRI.js; wondering if it makes sense to forward base IRI options from N3Writer to BaseIRI. I'm mainly concerned about the slippery slope, in which N3Writer becomes no-actually-a-pretty-printer-but-some-features-are-getting-close-so-why-not, whereas the design goals are different. N3Writer is about writing Turtle fast (so any abbreviations are for speed more than readability), and features tend to serve a functional need (relative URIs are to facilitate moving). Pretty printers are inherently different beasts.

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.

feat: cleaner relative IRI writing
3 participants