BridgeJS: Fix name collision for same-named nested structs#744
Conversation
adf1074 to
d0939f0
Compare
| // `swift package bridge-js`. | ||
|
|
||
| export interface Stats { | ||
| export interface User_Stats { |
There was a problem hiding this comment.
I wonder if UserStats would be more aesthetically pleasing? I guess this could conflict with a separate struct just called UserStats in Swift.
More work but I think we could use TS namespaces here as well:
export namespace User {
export interface Stats {
}
}
I believe that this will automatically merge with the interface Player, if it exists.
There was a problem hiding this comment.
Hey William, on the naming - yeah, UserStats would read nicer, but as you noted it could collide with an actual UserStats struct. The underscore keeps it unambiguous and mirrors the internal ABI name and aligns with current approach elsewhere in the code.
As for TS namespace idea - we do already emit namespace blocks in the declare global path (for exposeToGlobal: true), so the mechanism exists and could be reused. Current approach aligns with use of underscore in other places, but that could be considered, depending on @kateinoigakukun preference.
There was a problem hiding this comment.
I think it makes more sense to use TS-namespace to represent the hierarchical types for public user-facing APIs.
Overview
Follow-up to #735. When two different parent types both have a nested struct with the same short name (e.g.,
User.StatsandPlayer.Stats), the generated JavaScript produces duplicateconstdeclarations and overwrites the samestructHelpers.Statskey, causing a SyntaxError at parse time.The fix uses
abiName(which joins namespace + name with_, e.g.,User_Stats) instead of the shortnamefor all JS-side struct identifiers - helper keys, factory names, and TypeScript interface names.Non-nested structs are unaffected since
abiNamereturns the plain name when there is no namespace.