Skip to content

Handle Invalid Java Identifiers #1169

Open
@scottschreckengaust

Description

@scottschreckengaust

Describe the feature

How might invalid java identifiers, https://docs.oracle.com/javase/specs/jls/se12/html/jls-3.html#jls-3.8, be handled?

Here is a reference for "Package Names and Module Names" https://docs.oracle.com/javase/specs/jls/se12/html/jls-6.html#jls-6.1

  • If the domain name contains a hyphen, or any other special character not allowed in an identifier (§3.8), convert it into an underscore.
  • If any of the resulting package name components are keywords (§3.9), append an underscore to them.
  • If any of the resulting package name components start with a digit, or any other character that is not allowed as an initial character of an identifier, have an underscore prefixed to the component.

Use Case

Here is a snippet I used to correct the issue within Projen because hyphens, -, aren't legal characters for java packages:

  publishToMaven: {
  ...
    javaPackage: `io.github.${"no-hyphens".replace(/-/g, '_')}`,
  ...
  },

Proposed Solution

Two options:

  1. A ERROR/FAIL message stating invalid java identifiers detected
  2. A WARN message stating invalid java identifiers converted

Other Information

When using hypens, -, for example, there are tons of compilation errors and somewhat difficult to trace:

error: '(' or '[' expected
error: ';' expected
error: '{' expected
error: <identifier> expected
...
private final io.github.no-hypens.Builder props;
...

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.148.0

Environment details (OS name and version, etc.)

macOS Sonoma 14.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions