Skip to content

rubocop-yard: Add support for string literals in TypesExplainer#1628

Merged
lsegal merged 4 commits into
lsegal:mainfrom
lekemula:parse-string-and-symbol-literals
May 25, 2026
Merged

rubocop-yard: Add support for string literals in TypesExplainer#1628
lsegal merged 4 commits into
lsegal:mainfrom
lekemula:parse-string-and-symbol-literals

Conversation

@lekemula

@lekemula lekemula commented Sep 13, 2025

Copy link
Copy Markdown
Contributor

Description

Resolves #1627

  • Add LITERALMATCH regex constant to match symbol (:symbol) and string ('string', "string") literals
  • Update TypesExplainer parser to recognize symbol and string literal types
  • Format literal values as "a literal value :symbol" for better readability
  • Add comprehensive test coverage for LITERALMATCH constant and literal type parsing
  • Fixes parsing inconsistency between YARD TypesExplainer and online parser

🤖 Generated with Claude Code

Completed Tasks

  • I have read the Contributing Guide.
  • The pull request is complete (implemented / written).
  • Git commits have been cleaned up (squash WIP / revert commits).
  • I wrote tests and ran bundle exec rake locally (if code is attached to PR).

Comment thread lib/yard/code_objects/base.rb Outdated
METHODMATCH = /(?:(?:#{NAMESPACEMATCH}|[a-z]\w*)\s*(?:#{CSEPQ}|#{NSEPQ})\s*)?#{METHODNAMEMATCH}/

# Regular expression to match symbol and string literals
LITERALMATCH = /:\w+|'[^']*'|"[^"]*"/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Does this constant make sense here?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This regex seems a little overgreedy / not fully accurate with respect to string tokens. I don't think this covers the full case of valid symbol / string syntaxes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved it inside the TypesExplainer class.

@lekemula lekemula force-pushed the parse-string-and-symbol-literals branch 3 times, most recently from 1250332 to 2d79fb8 Compare September 28, 2025 14:52
@lekemula lekemula changed the title Add support for symbol and string literals in TypesExplainer rubocop-yard: Add support for symbol and string literals in TypesExplainer Sep 28, 2025

@lsegal lsegal left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I appreciate the attempt but the LITERALMATCH that this is based on seems perilous if used elsewhere.

We have another competing implementation in #1622 that also doesn't quite get this right but at least is less invasive (it's really just a naming change).

I think it would make more sense to pull that identifier regex into the types_explainer code directly so that the "best case" logic doesn't leak into other more generalized YARD code paths. My worry is that someone would see LITERALMATCH and use it for general code parsing, which would fail quickly. It's fine in the context of type specifier text, but not for blocks of code.

tldr LITERALMATCH is a case of premature generalization and that regex should be specific to the types explainer code, maybe not even a constant at all.

Comment thread lib/yard/code_objects/base.rb Outdated
METHODMATCH = /(?:(?:#{NAMESPACEMATCH}|[a-z]\w*)\s*(?:#{CSEPQ}|#{NSEPQ})\s*)?#{METHODNAMEMATCH}/

# Regular expression to match symbol and string literals
LITERALMATCH = /:\w+|'[^']*'|"[^"]*"/

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This regex seems a little overgreedy / not fully accurate with respect to string tokens. I don't think this covers the full case of valid symbol / string syntaxes.

@lekemula lekemula force-pushed the parse-string-and-symbol-literals branch from 2d79fb8 to 861ea45 Compare February 17, 2026 08:10
@lekemula lekemula force-pushed the parse-string-and-symbol-literals branch 2 times, most recently from fd9eadd to 871c974 Compare February 24, 2026 21:59
@lekemula

lekemula commented Feb 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the elaboration @lsegal - makes total sense.

We have another competing implementation in #1622 that also doesn't quite get this right, but at least is less invasive (it's really just a naming change). We have another competing implementation in #1622 that also doesn't quite get this right, but at least is less invasive (it's really just a naming change).

While this PR does go a bit into refactoring, these changes make room for more complex changes from the following PR: #1630. I just wanted to extract the most out of it, to keep it as simple as possible. Let me know if you'd still want me to move the refactoring there.

Additionally, #1622 seems to only handle the symbol literals, but not the string ones. That said, I'm totally fine with having it merged first and later extending it for the strings.

@lekemula lekemula requested a review from lsegal February 24, 2026 22:06
@lsegal

lsegal commented Apr 16, 2026

Copy link
Copy Markdown
Owner

We went the route of pulling #1622 because there was less blast radius. If you want to roll your changes over that, that would be great, but I think this needs a rebase etc.

Thanks!

@lekemula

Copy link
Copy Markdown
Contributor Author

Thanks for the notice @lsegal - will do!

lekemula and others added 3 commits May 12, 2026 21:17
- Add LITERALMATCH regex constant to match symbol (:symbol) and string ('string', "string") literals
- Update TypesExplainer parser to recognize symbol and string literal types
- Format literal values as "a literal value :symbol" for better readability
- Add comprehensive test coverage for LITERALMATCH constant and literal type parsing
- Fixes parsing inconsistency between YARD TypesExplainer and online parser

Resolves lsegal#1627

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This could be useful for solargraph gem
@lekemula lekemula force-pushed the parse-string-and-symbol-literals branch from 871c974 to 6e204eb Compare May 12, 2026 19:27
@lekemula lekemula changed the title rubocop-yard: Add support for symbol and string literals in TypesExplainer rubocop-yard: Add support ~~for symbol and~~ string literals in TypesExplainer May 12, 2026
@lekemula lekemula changed the title rubocop-yard: Add support ~~for symbol and~~ string literals in TypesExplainer rubocop-yard: Add support for string literals in TypesExplainer May 12, 2026
@lekemula

Copy link
Copy Markdown
Contributor Author

Hi @lsegal, I rebased my changes, so this PR is ready for re-review 🙏

@lsegal lsegal merged commit d5b67c5 into lsegal:main May 25, 2026
25 checks passed
@lsegal

lsegal commented May 25, 2026

Copy link
Copy Markdown
Owner

Thank you for this!

lsegal added a commit that referenced this pull request May 25, 2026
…1630)

# Description

Follow-up on #1628 (omit first 2
commits when reviewing)

Adds support for multiple and nested hash keys - example from specs:

``` ruby
"Hash{:key_one, :key_two => String; :key_three => Symbol; :key_four => Hash{:sub_key_one => String}}" => "a Hash with keys made of (a literal value :key_one or a literal value :key_two) and values of (Strings) and keys made of (a literal value :key_three) and values of (Symbols) and keys made of (a literal value :key_four) and values of (a Hash with keys made of (a literal value :sub_key_one) and values of (Strings))",
"Hash{:key_one => String, Number; :key_two => String}" => "a Hash with keys made of (a literal value :key_one) and values of (Strings or Numbers) and keys made of (a literal value :key_two) and values of (Strings)"
```

NOTE: These changes probably only impact on
https://github.com/ksss/rubocop-yard and have no effect elsewhere.

PS. I think this alongside the base PR, will open up a lot of
opportunities for better `Hash` completion/typechecking for
[solargraph](https://github.com/castwide/solargraph) gem.

# Completed Tasks

- [x] I have read the [Contributing Guide][contrib].
- [x] The pull request is complete (implemented / written).
- [x] Git commits have been cleaned up (squash WIP / revert commits).
- [x] I wrote tests and ran `bundle exec rake` locally (if code is
attached to PR).

[contrib]: https://github.com/lsegal/yard/blob/main/CONTRIBUTING.md

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Loren Segal <lsegal@soen.ca>
lsegal added a commit that referenced this pull request May 25, 2026
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.

Support symbol and string literal values

2 participants