Skip to content

Commit e713e19

Browse files
committed
Also add a setter for the builder
1 parent b5e9678 commit e713e19

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

src/builder/builder.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,24 @@ describe("ScopeInfoBuilder", () => {
144144
});
145145
});
146146

147+
describe("setRangeDefinitionScope", () => {
148+
it("sets the definition scope when it's provided as a number", () => {
149+
const info = builder.startScope(0, 0).endScope(10, 0).startRange(0, 0)
150+
.setRangeDefinitionScope(0).endRange(0, 10).build();
151+
152+
assertStrictEquals(info.scopes[0], info.ranges[0].originalScope);
153+
});
154+
155+
it("sets the definition scope when it's provided directly", () => {
156+
const scope = builder.startScope(0, 0).endScope(10, 0).lastScope();
157+
const info = builder.startRange(0, 0).setRangeDefinitionScope(scope!)
158+
.endRange(0, 10).build();
159+
160+
assertStrictEquals(info.scopes[0], info.ranges[0].originalScope);
161+
assertStrictEquals(info.ranges[0].originalScope, scope);
162+
});
163+
});
164+
147165
describe("endRange", () => {
148166
it("does nothing when no range is open", () => {
149167
builder.endRange(0, 20);

src/builder/builder.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ export class ScopeInfoBuilder {
139139
return this;
140140
}
141141

142+
setRangeDefinitionScope(scope: number | OriginalScope): this {
143+
const range = this.#rangeStack.at(-1);
144+
if (!range) return this;
145+
146+
if (typeof scope === "number") {
147+
range.originalScope = this.#countToScope.get(scope);
148+
} else {
149+
range.originalScope = scope;
150+
}
151+
152+
return this;
153+
}
154+
142155
endRange(line: number, column: number): this {
143156
const range = this.#rangeStack.pop();
144157
if (!range) return this;

src/builder/safe_builder.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,34 @@ describe("SafeScopeInfoBuilder", () => {
161161
});
162162
});
163163

164+
describe("setRangeDefinitionScope", () => {
165+
it("throws when no range is open", () => {
166+
assertThrows(() => builder.setRangeDefinitionScope(0));
167+
});
168+
169+
it("throws while building a scope", () => {
170+
builder.startScope(0, 0);
171+
172+
assertThrows(() => builder.setRangeDefinitionScope(0));
173+
});
174+
175+
it("throws when the definition scope doesnt point to a valid scope", () => {
176+
assertThrows(() => builder.startRange(0, 0).setRangeDefinitionScope(0));
177+
});
178+
179+
it("throws when the definition scope is not known to the builder", () => {
180+
assertThrows(() =>
181+
builder.startRange(0, 0).setRangeDefinitionScope({
182+
start: { line: 0, column: 0 },
183+
end: { line: 10, column: 10 },
184+
isStackFrame: false,
185+
variables: [],
186+
children: [],
187+
})
188+
);
189+
});
190+
});
191+
164192
describe("endRange", () => {
165193
it("throws when the range stack is empty", () => {
166194
assertThrows(() => builder.endRange(5, 0));

src/builder/safe_builder.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,30 @@ export class SafeScopeInfoBuilder extends ScopeInfoBuilder {
138138
return this;
139139
}
140140

141+
override setRangeDefinitionScope(scope: number | OriginalScope): this {
142+
this.#verifyEmptyScopeStack("setRangeDefinitionScope");
143+
this.#verifyRangePresent("setRangeDefinitionScope");
144+
145+
if (
146+
typeof scope === "number" &&
147+
!this.isValidScopeNumber(scope)
148+
) {
149+
throw new Error(
150+
`${scope} does not reference a valid OriginalScope`,
151+
);
152+
}
153+
if (
154+
typeof scope === "object" && !this.isKnownScope(scope)
155+
) {
156+
throw new Error(
157+
"The provided definition scope was not produced by this builder!",
158+
);
159+
}
160+
161+
super.setRangeDefinitionScope(scope);
162+
return this;
163+
}
164+
141165
override endRange(line: number, column: number): this {
142166
this.#verifyEmptyScopeStack("endRange");
143167

@@ -184,4 +208,10 @@ export class SafeScopeInfoBuilder extends ScopeInfoBuilder {
184208
throw new Error(`Can't ${op} while no OriginalScope is on the stack.`);
185209
}
186210
}
211+
212+
#verifyRangePresent(op: string): void {
213+
if (this.rangeStack.length === 0) {
214+
throw new Error(`Can't ${op} while no GeneratedRange is on the stack.`);
215+
}
216+
}
187217
}

0 commit comments

Comments
 (0)