fix(swift): honor --acronym-style for leading acronyms#3006
Merged
Conversation
The Swift name-styling helper hardcoded allUpperWordStyle/allLowerWordStyle for the first word of a name whenever it was recognized as an acronym, ignoring the acronymsStyle derived from the --acronym-style option. Later words in the same name already used acronymsStyle correctly, so an acronym appearing anywhere but the first position was styled correctly while the leading position always came out upper/lower-cased regardless of the option. Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.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.
Bug
With
--acronym-styleset to anything other than the default (pascal),Swift type names with a leading acronym word ignored the option. For
example, with
--acronym-style originalthe JSON keyFaqCoordinateincorrectly produced:
while
AFaqCoordinate(where the acronym "Faq" is not the first word)correctly produced:
Root cause
swiftNameStyleinpackages/quicktype-core/src/language/Swift/utils.tscalls
combineWordswith separate style functions for the first word vs.the rest of a name, and separately again for acronym vs. non-acronym words.
The style used for a first-word acronym was hardcoded to
isUpper ? allUpperWordStyle : allLowerWordStyle, instead of being derivedfrom the
acronymsStyleparameter that carries the user's--acronym-stylechoice. The style used for a later-word acronym(
restAcronymStyle) already correctly usedacronymsStyle. So acronymsanywhere but the first word honored the option; the first word did not.
Fix
Changed the hardcoded
firstWordAcronymStyleargument to useacronymsStyle(matching how Kotlin's equivalent
kotlinNameStylealready wires this up)when
isUpperis true:Non-upper (camelCase, e.g. property names) first words are left as
allLowerWordStyle, matching existing behavior and JS/Swift lowerCamelCaseidentifier conventions.
Test coverage
Added
test/unit/swift-acronym-names.test.ts, which generates Swift outputdirectly via
quicktype()for a type whose only property-bearing name isFaqCoordinateand asserts on the emittedstructdeclaration name across--acronym-stylevaluesoriginal,pascal,camel, andlowerCase.A unit test (rather than a JSON/JSON Schema fixture) is used because this
class of bug is invisible to the fixture harness: fixture tests round-trip
JSON through the generated code and diff the result, but the identifier
casing bug only affects the generated source identifier (the struct/type
name), not the JSON keys/values that get serialized and compared — the
mangled identifier is still self-consistent, compiles, and round-trips
correctly. This mirrors the precedent set by
test/unit/java-acronym-names.test.ts(added in #2851 for a related Java enum-constant acronym-casing bug), which
documents the same reasoning.
Verification
(3 of 4 acronym-style cases produced
FAQCoordinateinstead of theexpected casing).
npx vitest run test/unit): 167/167 passing, noregressions.
npm run buildpasses.(
node dist/index.js -l swift --acronym-style original input.json) andconfirmed
FaqCoordinatenow renders correctly, while re-checking--acronym-style pascal(the default) still rendersFAQCoordinateasbefore (no behavior change for the default style).
FIXTURE=swift script/test) were not runlocally — the
swiftctoolchain is not available in this environment.This will be validated by CI; note that fixture tests would not have
caught this bug in the first place (see above), so the unit test is the
operative coverage here.
Fixes #1599
🤖 Generated with Claude Code