Skip to content

Allow negative %d counts in pathmap pattern replacements - #773

Open
afurm wants to merge 1 commit into
ruby:masterfrom
afurm:pathmap-negative-partial-with-pattern
Open

afurm wants to merge 1 commit into
ruby:masterfrom
afurm:pathmap-negative-partial-with-pattern

Conversation

@afurm

@afurm afurm commented Oct 20, 2026

Copy link
Copy Markdown
Member

String#pathmap documents that %d takes a numeric prefix (including negative ones, e.g. %-2d) and that %d also accepts a {pattern,replacement} spec. Combining the two only works for positive counts:

"this/is/a/really/long/path/ok.rb".pathmap("%{/really/,/}5d")  # => "this/is/a/long"
"this/is/a/really/long/path/ok.rb".pathmap("%{path,dir}-2d")
# => ArgumentError: Unknown pathmap specifier %{path,dir}-2d in '%{path,dir}-2d'

The scanner already accepts %{...}-2d as one fragment (-?\d* in the scan regexp), but the branch that applies the replacement only matched \d*[dpfnxX], so the minus sign made it fall through to the "unknown specifier" error.

The replacement branch now matches -?\d+d or a bare operator, so %{path,dir}-2d returns "long/dir". A count is still only accepted together with d, so something like %{a,x}-2f keeps raising ArgumentError instead of being silently mangled.

Tests: added cases to test/test_rake_path_map.rb; the -2d one fails without the change. bundle exec rake test passes (607 tests) and RuboCop is clean on the changed files.

This branch has not been deployed

No deployments
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.

1 participant