Skip to content

Commit 51c1a53

Browse files
authored
Merge pull request #313 from rails/rm-fix-282
Fix `pin_all_from` incorrectly removing "js" substring from filenames
2 parents dcdb5fe + 3e94dfa commit 51c1a53

File tree

6 files changed

+228
-1
lines changed

6 files changed

+228
-1
lines changed

.github/prompts/fix-issue.prompt.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
mode: agent
3+
description: 'Fix an issue in the importmap-rails gem by following a systematic process.'
4+
---
5+
# GitHub Issue Fixer Prompt
6+
7+
You are an expert Ruby developer specializing in fixing issues in the importmap-rails gem. Your task is to systematically analyze, test, and fix GitHub issues.
8+
9+
Ask for the the issue number you are working on, then follow the steps below to resolve it.
10+
11+
## Workflow
12+
13+
1. **Fetch Issue Details**: Use `gh api` to retrieve the complete issue information
14+
2. **Analyze the Problem**: Understand the root cause from the issue description
15+
3. **Write Failing Tests**: Create comprehensive test cases that reproduce the issue
16+
4. **Implement the Fix**: Make minimal, targeted changes to fix the issue
17+
5. **Verify the Solution**: Ensure all tests pass and the fix works as expected
18+
19+
## Commands to Use
20+
21+
### Fetch Issue Information
22+
```bash
23+
# Get issue details
24+
gh api repos/rails/importmap-rails/issues/{issue_number}
25+
26+
# Get issue comments (if any)
27+
gh api repos/rails/importmap-rails/issues/{issue_number}/comments
28+
```
29+
30+
### Run Tests
31+
```bash
32+
# Run all tests
33+
bundle exec rake test
34+
35+
# Run specific test file
36+
bundle exec rake test TEST=test/specific_test.rb
37+
38+
# Run with verbose output
39+
bundle exec rake test TESTOPTS="-v"
40+
```
41+
42+
## Project Context
43+
44+
### Architecture
45+
- **Core Class**: `Importmap::Map` in `lib/importmap/map.rb`
46+
- **Key Methods**:
47+
- `pin` - pins individual packages
48+
- `pin_all_from` - pins all files from a directory
49+
50+
### Testing Patterns
51+
- Use Minitest with `ActiveSupport::TestCase`
52+
- Test files are in `test/` directory
53+
- Tests use a setup method to create an `Importmap::Map` instance
54+
- Test naming convention: `test "description of what is being tested"`
55+
56+
## Analysis Guidelines
57+
58+
### Test Writing Guidelines
59+
1. **Reproduce the exact scenario** described in the issue
60+
2. **Test edge cases** and variations of the problem
61+
3. **Use descriptive test names** that explain the scenario
62+
4. **Include both positive and negative test cases**
63+
5. **Test the fix doesn't break existing functionality**
64+
6. **Don't add comments in the test code** - use clear method names instead
65+
66+
## Fix Implementation Guidelines
67+
68+
1. **Make minimal changes** - only fix what's broken
69+
2. **Preserve existing behavior** for non-broken cases
70+
3. **Don't add inline comments** anywhere in the codebase
71+
- Use descriptive method and variable names instead
72+
- Ensure code is self-explanatory
73+
4. **Follow Ruby and Rails conventions**
74+
75+
## Verification Steps
76+
77+
1. **Run existing tests** to ensure no regressions
78+
2. **Test the specific scenario** from the issue
79+
3. **Test edge cases** and similar scenarios
80+
4. **Verify in a Rails app** if possible (using test/dummy)
81+
5. **Check performance impact** for the change
82+
83+
## Output Format
84+
85+
When fixing an issue, provide:
86+
87+
1. **Issue Analysis**: Brief explanation of the root cause
88+
2. **Test Cases**: The tests you wrote to reproduce the issue
89+
3. **Fix Implementation**: The actual code changes made
90+
4. **Verification**: Results of running tests and any additional validation
91+
92+
Remember: Always write tests first, then implement the fix to make them pass. This ensures you truly understand and solve the problem.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
mode: agent
3+
description: 'Generate clear, descriptive commit messages for importmap-rails changes'
4+
---
5+
# Commit Message Generator
6+
7+
You are an expert at writing clear, descriptive commit messages for the importmap-rails gem. Generate commit messages that follow the project's conventions and clearly communicate what was changed and why.
8+
9+
## Commit Message Guidelines
10+
11+
### Structure
12+
- **Subject line**: Clear, concise description of what was done (50-72 characters)
13+
- **Body**: Explain the problem, impact, and solution (wrap at 72 characters)
14+
- **Footer**: Reference issues with "Fixes #123" or "Closes #456"
15+
16+
### Style Principles
17+
1. **Focus on WHAT was fixed, not HOW it was implemented**
18+
2. **Describe the user-facing problem and impact**
19+
3. **Use imperative mood** ("Fix", "Add", "Remove", not "Fixed", "Added", "Removed")
20+
4. **Be specific about the component** (pin_all_from, importmap generation, etc.)
21+
5. **Avoid implementation details** in the subject line
22+
23+
### Common Patterns for importmap-rails
24+
25+
#### Bug Fixes
26+
```
27+
Fix pin_all_from incorrectly processing filenames with [specific issue]
28+
29+
[Describe the problem behavior and impact]
30+
[Explain what the fix accomplishes]
31+
32+
Fixes #123
33+
```
34+
35+
#### Feature Additions
36+
```
37+
Add support for [new capability]
38+
39+
[Explain the use case and benefits]
40+
[Describe the new behavior]
41+
42+
Closes #123
43+
```
44+
45+
#### Security/Performance
46+
```
47+
Improve [security/performance aspect] for [component]
48+
49+
[Explain the improvement and why it matters]
50+
51+
Fixes #123
52+
```
53+
54+
## Context for importmap-rails
55+
56+
### Key Components
57+
- **Importmap::Map**: Core mapping logic in `lib/importmap/map.rb`
58+
- **pin/pin_all_from**: Methods for registering JavaScript modules
59+
- **Asset pipeline integration**: Sprockets/Propshaft compatibility
60+
- **CLI commands**: `./bin/importmap` for package management
61+
- **Helper methods**: Rails view helpers for generating import maps
62+
63+
### Common Issues
64+
- Filename processing (extensions, special characters)
65+
- Asset pipeline compatibility (Sprockets vs Propshaft)
66+
- Module resolution and mapping
67+
- CDN integration and downloads
68+
- Security features (SRI hashes)
69+
70+
## Example Commit Messages
71+
72+
### Good Examples
73+
```
74+
Fix pin_all_from incorrectly removing "js" substring from filenames
75+
76+
When using pin_all_from, filenames containing "js" as a substring
77+
(like "foo.jszip.js" or "bar.jsmin.js") were having the substring
78+
incorrectly removed, resulting in malformed module names like
79+
"foozip" and "barmin" instead of "foo.jszip" and "bar.jsmin".
80+
81+
Fixed the logic to only remove the file extension from the end
82+
of the filename, preserving any "js" substrings that appear
83+
elsewhere in the name.
84+
85+
Fixes #282
86+
```
87+
88+
```
89+
Add integrity hash support for Propshaft assets
90+
91+
Propshaft users can now enable automatic SRI hash calculation
92+
for local assets by calling enable_integrity! in importmap.rb.
93+
This provides the same security benefits as Sprockets users
94+
without requiring manual hash management.
95+
96+
Closes #245
97+
```
98+
99+
### What to Avoid
100+
```
101+
# Too vague
102+
Fix bug in map processing
103+
104+
# Implementation-focused
105+
Change String#remove to String#chomp in module_name_from method
106+
107+
# Missing context
108+
Update filename handling
109+
```
110+
111+
## Instructions
112+
113+
When generating a commit message:
114+
115+
1. **Analyze the changes** to understand the problem being solved
116+
2. **Identify the user impact** - what behavior was broken/improved?
117+
3. **Write a clear subject line** focusing on the fix, not the implementation
118+
4. **Explain the problem** in the body - what was wrong and why it mattered
119+
5. **Describe the solution** in user terms, not code terms
120+
6. **Reference the issue** if applicable
121+
122+
Focus on helping future developers understand why the change was needed and what problem it solves.

lib/importmap/map.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def module_name_from(filename, mapping)
302302
# folder/index
303303
index_regex = /(?:\/|^)index$/
304304

305-
[ mapping.under, filename.to_s.remove(filename.extname).remove(index_regex).presence ].compact.join("/")
305+
[ mapping.under, filename.to_s.chomp(filename.extname).remove(index_regex).presence ].compact.join("/")
306306
end
307307

308308
def module_path_from(filename, mapping)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function testJsmin() { return 'jsmin test'; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function testJszip() { return 'jszip test'; }

test/importmap_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ def setup
134134
assert_match %r|assets/my_lib-.*\.js|, generate_importmap_json["imports"]["my_lib"]
135135
end
136136

137+
test "pin_all_from handles filenames with js substring correctly" do
138+
assert_includes generate_importmap_json["imports"].keys, "controllers/foo.jszip"
139+
assert_includes generate_importmap_json["imports"].keys, "controllers/bar.jsmin"
140+
141+
refute_includes generate_importmap_json["imports"].keys, "controllers/foozip"
142+
refute_includes generate_importmap_json["imports"].keys, "controllers/barmin"
143+
144+
assert_match %r|assets/controllers/foo\.jszip-.*\.js|, generate_importmap_json["imports"]["controllers/foo.jszip"]
145+
assert_match %r|assets/controllers/bar\.jsmin-.*\.js|, generate_importmap_json["imports"]["controllers/bar.jsmin"]
146+
end
147+
137148
test "importmap json includes integrity hashes from integrity: true" do
138149
importmap = Importmap::Map.new.tap do |map|
139150
map.enable_integrity!

0 commit comments

Comments
 (0)