Skip to content

Commit 25d7f4f

Browse files
committed
refactor
1 parent 6aac430 commit 25d7f4f

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

src/group-specifiers.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,18 @@ export class GroupSpecifiersAsES2018 implements GroupSpecifiers {
8484
}
8585

8686
export class GroupSpecifiersAsES2025 implements GroupSpecifiers {
87-
private groupNamesInDisjunction = new Set<string>()
88-
private groupNamesInDisjunctionStack: Set<string>[] = []
89-
private groupNamesInAlternative = new Set<string>()
90-
private groupNamesInAlternativeStack: Set<string>[] = []
87+
private groupNamesAddedInDisjunction = new Set<string>()
88+
private groupNamesAddedInUpperDisjunctionStack: Set<string>[] = []
89+
private groupNamesInScope = new Set<string>()
90+
private groupNamesInUpperScopeStack: Set<string>[] = []
9191

9292
private groupNamesInPattern = new Set<string>()
9393

9494
public clear(): void {
95-
this.groupNamesInDisjunction.clear()
96-
this.groupNamesInDisjunctionStack.length = 0
97-
this.groupNamesInAlternative.clear()
98-
this.groupNamesInAlternativeStack.length = 0
95+
this.groupNamesAddedInDisjunction.clear()
96+
this.groupNamesAddedInUpperDisjunctionStack.length = 0
97+
this.groupNamesInScope.clear()
98+
this.groupNamesInUpperScopeStack.length = 0
9999
this.groupNamesInPattern.clear()
100100
}
101101

@@ -104,25 +104,31 @@ export class GroupSpecifiersAsES2025 implements GroupSpecifiers {
104104
}
105105

106106
public enterDisjunction(): void {
107-
this.groupNamesInDisjunctionStack.push(this.groupNamesInDisjunction)
108-
this.groupNamesInDisjunction = new Set()
107+
this.groupNamesAddedInUpperDisjunctionStack.push(
108+
this.groupNamesAddedInDisjunction,
109+
)
110+
// Clear groupNamesAddedInDisjunction to store the groupName added in this Disjunction.
111+
this.groupNamesAddedInDisjunction = new Set()
109112
}
110113

111114
public enterAlternative(): void {
112-
this.groupNamesInAlternativeStack.push(this.groupNamesInAlternative)
113-
this.groupNamesInAlternative = new Set(this.groupNamesInAlternative)
115+
this.groupNamesInUpperScopeStack.push(this.groupNamesInScope)
116+
this.groupNamesInScope = new Set(this.groupNamesInScope)
114117
}
115118

116119
public leaveAlternative(): void {
117-
this.groupNamesInAlternative = this.groupNamesInAlternativeStack.pop()!
120+
this.groupNamesInScope = this.groupNamesInUpperScopeStack.pop()!
118121
}
119122

120123
public leaveDisjunction(): void {
121-
const child = this.groupNamesInDisjunction
122-
this.groupNamesInDisjunction = this.groupNamesInDisjunctionStack.pop()!
123-
for (const groupName of child) {
124-
this.groupNamesInDisjunction.add(groupName)
125-
this.groupNamesInAlternative.add(groupName)
124+
const groupNamesAddedInDisjunction = this.groupNamesAddedInDisjunction
125+
this.groupNamesAddedInDisjunction =
126+
this.groupNamesAddedInUpperDisjunctionStack.pop()!
127+
for (const groupName of groupNamesAddedInDisjunction) {
128+
// Adds the groupName added in Disjunction to groupNamesInScope.
129+
this.groupNamesInScope.add(groupName)
130+
// Adds the groupName added in Disjunction to the upper Disjunction.
131+
this.groupNamesAddedInDisjunction.add(groupName)
126132
}
127133
}
128134

@@ -131,12 +137,12 @@ export class GroupSpecifiersAsES2025 implements GroupSpecifiers {
131137
}
132138

133139
public hasInScope(name: string): boolean {
134-
return this.groupNamesInAlternative.has(name)
140+
return this.groupNamesInScope.has(name)
135141
}
136142

137143
public addToScope(name: string): void {
138-
this.groupNamesInAlternative.add(name)
139-
this.groupNamesInDisjunction.add(name)
144+
this.groupNamesInScope.add(name)
145+
this.groupNamesAddedInDisjunction.add(name)
140146
this.groupNamesInPattern.add(name)
141147
}
142148
}

0 commit comments

Comments
 (0)