haxe icon indicating copy to clipboard operation
haxe copied to clipboard

Feature Request: Support for renaming methods, classes, variables, etc.

Open barisyild opened this issue 7 months ago • 0 comments

Currently in Haxe, we can use @:native or @:jsName to control how classes, methods, and fields are named in the generated output. However, these approaches are either target-specific or not fully consistent across all backends.

I propose introducing a new built-in metadata: @:rename.

The idea is simple:

  • @:rename would allow developers to rename classes, methods, and fields independently of the target platform.
  • It would be handled directly by the Haxe compiler, ensuring consistent behavior across all targets
  • It would only affect the output symbol names, without changing the runtime behavior (unless otherwise specified).

Example:

@:rename("CoolClass")
class MyClass {
  @:rename("doStuff")
  public function myMethod() {
    trace("Doing something!");
  }
}

Generated JavaScript (for example):

class CoolClass {
  doStuff() {
    console.log("Doing something!");
  }
}

Why this is useful:

  • Easier integration with external APIs expecting certain names.
  • Better control over output when targeting multiple platforms.
  • Supports code obfuscation and minification without losing readability at the source level.
  • Cleaner and more predictable output across targets.

Important notes:

This is different from @:native, which usually implies binding to an external object or API.

Reflection behavior (Type.resolveClass, Reflect.field, etc.) needs to be clearly defined: whether it refers to the original or renamed symbols (probably original for internal Haxe consistency).

Conclusion: A native @:rename metadata would provide developers with a clean, consistent way to control output symbol names without relying on target-specific hacks. It would make cross-platform development smoother and more flexible.

The original concept was implemented in hxobfuscator with some hacks.

barisyild avatar Apr 27 '25 17:04 barisyild