Fix JSON strings with quotes causing escaping issues (fixes #39)#109
Open
konard wants to merge 3 commits into
Open
Fix JSON strings with quotes causing escaping issues (fixes #39)#109konard wants to merge 3 commits into
konard wants to merge 3 commits into
Conversation
Adding CLAUDE.md with task information for AI processing. This file will be removed when the task is complete. Issue: #39
This commit addresses the issue where JSON strings containing quotes and special
characters get corrupted when passed through command-stream's shell interpolation.
### Key Changes:
1. **Enhanced Shell Operator Detection** - Added detection for redirection operators
(`>`, `>>`, `<`, `2>`, etc.) in hasShellOperators to properly identify when
commands need real shell execution
2. **Improved needsRealShell Function** - Added basic redirection operators to the
unsupported features list, forcing JSON commands with redirection to use real
shell instead of virtual commands
3. **Virtual Command Bypass** - Added needsRealShell check to virtual command
decision logic to prevent JSON strings with redirection from being processed
by virtual echo command
4. **Version Bump** - Updated to 0.7.2
### Test Cases Added:
- Comprehensive JSON escaping test suite
- Examples demonstrating proper JSON usage with shell redirection
### Impact:
- ✅ JSON strings with nested quotes now work correctly with shell redirection
- ✅ Special characters (backticks, dollar signs) are properly preserved
- ✅ All existing functionality maintained (no regressions)
- ✅ Fixes configuration management, API integration, and build script use cases
Example working usage:
```javascript
const jsonData = { name: "test", description: "with 'quotes' and \"double quotes\"" };
await $`echo ${JSON.stringify(jsonData)} > config.json`; // Now works correctly!
```
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes the issue where JSON strings containing quotes and special characters get corrupted when passed through command-stream's shell interpolation, making it impossible to reliably work with JSON data using shell commands.
🐛 Problem
JSON strings inherently contain quotes which conflict with shell quoting mechanisms:
✅ Solution
1. Enhanced Shell Operator Detection
>,>>,<,2>, etc.)2. Improved needsRealShell Function
3. Virtual Command Bypass Logic
needsRealShellcheck to prevent JSON redirection commands from using virtual echo🧪 Test Plan
tests/json-escaping.test.mjs)📝 Usage Examples
Before (Broken):
After (Fixed):
📊 Impact
✅ Configuration management - Can now write JSON config files reliably
✅ Data processing - JSON data preserved during shell operations
✅ API integrations - JSON payloads can be passed through shell commands
✅ Build scripts - Package.json and similar files can be manipulated
🔄 Changes
src/$.mjsneedsRealShellfunction insrc/shell-parser.mjs🤖 Generated with Claude Code
Resolves #39