Repository navigation
fix(case-style): handle empty words from leading or repeated separators - #359
Open
KuljeetSinghXDA wants to merge 1 commit into
Open
KuljeetSinghXDA wants to merge 1 commit into
KuljeetSinghXDA wants to merge 1 commit into
Conversation
Leading, trailing and repeated separators split a name into empty words.
Those empty words leaked into the generated key, and capitalCase even
threw a RangeError on the empty string.
This made the builder crash for private fields with a case style, for
example:
@MappableClass(caseStyle: CaseStyle.pascalCase)
class Model { final String _name; }
It also produced keys like '-name' for paramCase.
Ignore empty words and guard TextTransform.capitalCase against empty
input, so '_name' maps to 'Name' / 'name' / 'name' / 'name' instead of
crashing or emitting stray separators.
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.
Problem
A private field combined with a case style breaks code generation.
The builder fails with
RangeError (index): Invalid value: Valid value range is empty: 0fromTextTransform.capitalCase. The name_nameis split into['', 'name']because the leading separator produces an empty word, andcapitalCase('')readstext[0].The same empty words leak into generated keys:
CaseStyle.camelCase.transform('_field')returnsFieldinstead offieldCaseStyle.paramCase.transform('_field')returns-fieldCaseStyle.pascalCase.transform('_')throwsFix
TextTransform.capitalCaseagainst empty input.After the fix,
_namemaps tonamefor camelCase, snakeCase, paramCase and lowerCase, and toNamefor pascalCase. Names that contain no separators are unchanged.Tests
packages/dart_mappable/test/core/case_style_test.dart: leading/trailing/repeated separators andcapitalCase('').packages/dart_mappable/test/serialization/private_field_test.dart: a private field withcamelCaseandpascalCaseencodes with the expected key.packages/dart_mappable_builder/test/case_style_test.dart: builds a model with a private field and asserts the generatedkey: r'Name'/key: r'name'. This test fails with the previousRangeError.Verification
dart testinpackages/dart_mappable: 159 tests passed.dart testinpackages/dart_mappable_builder: 10 tests passed.dart analyzein both packages: no issues.dart format --output=none --set-exit-if-changedon all touched files: clean.