fix: handle newline characters in specLiteralStringCode#89
Merged
Conversation
Single-line raw strings (r'...' and r"...") cannot contain literal newlines. When a spec value contains \n or \r, the function now skips single-line raw forms and uses triple-quoted raw strings or the escaped fallback path (which now also escapes \n and \r). Closes #88 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #89 +/- ##
=======================================
Coverage 89.75% 89.76%
=======================================
Files 157 157
Lines 14479 14483 +4
=======================================
+ Hits 12996 13000 +4
Misses 1483 1483
🚀 New features to boost your workflow:
|
Update the specLiteralString docstring to reflect the newline handling added in the previous commit. Add standalone unit tests for escapeForSingleQuotedDartString covering \n, \r, and combined escaping. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes Dart code generation for OpenAPI spec string values containing newline characters by preventing invalid single-line raw string literals and ensuring the escaped fallback path produces syntactically valid Dart.
Changes:
- Updated
specLiteralStringCode()to avoid single-line raw strings when the input contains\nor\r, preferring raw triple-quoted strings when viable. - Updated
escapeForSingleQuotedDartString()to escape\nand\rso fallback (non-raw) literals remain valid. - Added unit tests covering
\n,\r,\r\n, and newline interactions with quotes /"""edge cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/tonik_generate/lib/src/util/spec_literal_string.dart | Adjusts quoting/escaping strategy to handle newline characters safely in generated Dart string literals. |
| packages/tonik_generate/test/src/util/spec_literal_string_test.dart | Adds focused tests validating the new newline handling and escaping behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Use triple-quoted Dart strings instead of escaped \n to better represent how newline values arrive from YAML multiline blocks in OpenAPI specs.
Literal carriage-return bytes in raw triple-quoted strings cause issues with formatters, diffs, and editors. Values containing \r now always use the escaped fallback path where \r becomes the literal escape sequence.
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
specLiteralStringCode()now skips single-line raw strings (r'...',r"...") when the value contains\nor\r, using triple-quoted raw strings or the escaped fallback path insteadescapeForSingleQuotedDartString()now escapes\nand\rso the fallback path produces valid Dart\n,\r,\r\n, and combinations with quotesTest plan
fvm dart analyzecleanCloses #88
🤖 Generated with Claude Code