@@ -23,6 +23,9 @@ export function ClassBody(node, context) {
23
23
/** @type {Map<string, StateField> } */
24
24
const private_state = new Map ( ) ;
25
25
26
+ /** @type {Map<(MethodDefinition|PropertyDefinition)["key"], string> } */
27
+ const definition_names = new Map ( ) ;
28
+
26
29
/** @type {string[] } */
27
30
const private_ids = [ ] ;
28
31
@@ -34,9 +37,12 @@ export function ClassBody(node, context) {
34
37
definition . key . type === 'Literal' )
35
38
) {
36
39
const type = definition . key . type ;
37
- const name = get_name ( definition . key ) ;
40
+ const name = get_name ( definition . key , public_state ) ;
38
41
if ( ! name ) continue ;
39
42
43
+ // we store the deconflicted name in the map so that we can access it later
44
+ definition_names . set ( definition . key , name ) ;
45
+
40
46
const is_private = type === 'PrivateIdentifier' ;
41
47
if ( is_private ) private_ids . push ( name ) ;
42
48
@@ -96,7 +102,7 @@ export function ClassBody(node, context) {
96
102
definition . key . type === 'PrivateIdentifier' ||
97
103
definition . key . type === 'Literal' )
98
104
) {
99
- const name = get_name ( definition . key ) ;
105
+ const name = definition_names . get ( definition . key ) ;
100
106
if ( ! name ) continue ;
101
107
102
108
const is_private = definition . key . type === 'PrivateIdentifier' ;
@@ -210,10 +216,20 @@ export function ClassBody(node, context) {
210
216
211
217
/**
212
218
* @param {Identifier | PrivateIdentifier | Literal } node
219
+ * @param {Map<string, StateField> } public_state
213
220
*/
214
- function get_name ( node ) {
221
+ function get_name ( node , public_state ) {
215
222
if ( node . type === 'Literal' ) {
216
- return node . value ?. toString ( ) . replace ( regex_invalid_identifier_chars , '_' ) ;
223
+ let name = node . value ?. toString ( ) . replace ( regex_invalid_identifier_chars , '_' ) ;
224
+
225
+ // the above could generate conflicts because it has to generate a valid identifier
226
+ // so stuff like `0` and `1` or `state%` and `state^` will result in the same string
227
+ // so we have to de-conflict. We can only check `public_state` because private state
228
+ // can't have literal keys
229
+ while ( name && public_state . has ( name ) ) {
230
+ name = '_' + name ;
231
+ }
232
+ return name ;
217
233
} else {
218
234
return node . name ;
219
235
}
0 commit comments