google / google/source_helper.dart

[Feature]: support escaping reserved dart keywords `escapeDartName`

Open
#22 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Dart
Stars
18
Forks
6
PR merge metrics
No merged PRs in 30d

Description

Usecase:
We are maintaining a package that generates dart code from OpenAPI specs. Fields can have arbitrary names so we'd like to have a method to catch reserved dart keywords like `assert, try, final`, field names starting with a number or just having them kebab-case (the latter being quite common).

Implementation:
We are currently just parsing the field names to camelCase eliminating special characters and cases like snake_case and kebab-case. When the result is a reserved keyword or starting with a number we just prepend them with a `$`
```
'assert' => '$assert'
'10' => '$10'
'helloassert' => 'helloassert'
```

Other solutions:
As fields starting with a `$` signal generated code we could also escape them in other ways. For the reserved words we could add a trailing `_` although that wouldn't help with the leading numbers. I also don't really like this that much.

---

Would you consider such a method being in scope of this package? If yes I'll happily upstream our current implementation in the current or an altered form.

Thanks for considering this :)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.