Skip to content

Commit 574d4d3

Browse files
committed
fix(ts-dsl): scope for-loop variable to loop block
Move ctx.analyze(this._variableOrInit) after ctx.pushScope() so the iteration variable is registered in the for-loop's own scope rather than the parent scope. This matches JS semantics where `for (const x of ...)` scopes `x` to the loop block, preventing false conflicts between sibling loops. Reverts the plugin.symbol('key') workaround since the correct scoping makes it unnecessary — sibling for-loops no longer conflict.
1 parent 39bf645 commit 574d4d3

3 files changed

Lines changed: 7 additions & 8 deletions

File tree

  • packages

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/transformers-additional-properties/transformers.gen.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ const barSchemaResponseTransformer = (data: any) => {
1313
data.baz[key] = fooSchemaResponseTransformer(data.baz[key]);
1414
}
1515
if (data.qux) {
16-
for (const key2 of Object.keys(data.qux)) {
17-
if (!['quux'].includes(key2)) {
18-
data.qux[key2] = new Date(data.qux[key2]);
16+
for (const key of Object.keys(data.qux)) {
17+
if (!['quux'].includes(key)) {
18+
data.qux[key] = new Date(data.qux[key]);
1919
}
2020
}
2121
}

packages/openapi-ts/src/plugins/@hey-api/transformers/plugin.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,16 @@ function processSchemaType({
195195
}
196196

197197
if (schema.additionalProperties && dataExpression) {
198-
const key = plugin.symbol('key');
199198
const entryValueNodes = processSchemaType({
200-
dataExpression: $(dataExpression).attr(key).computed(),
199+
dataExpression: $(dataExpression).attr('key').computed(),
201200
plugin,
202201
schema: schema.additionalProperties,
203202
});
204203

205204
if (entryValueNodes.length) {
206205
const properties = Object.keys(schema.properties ?? {});
207206
nodes.push(
208-
$.for($.const(key))
207+
$.for($.const('key'))
209208
.of($('Object').attr('keys').call(dataExpression))
210209
.$if(
211210
properties.length,
@@ -215,7 +214,7 @@ function processSchemaType({
215214
$.not(
216215
$.array(...properties)
217216
.attr('includes')
218-
.call(key),
217+
.call('key'),
219218
),
220219
).do(...entryValueNodes),
221220
),

packages/openapi-ts/src/ts-dsl/stmt/for.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class ImplForTsDsl<M extends ForMode = 'for'> extends Mixed {
4242
override analyze(ctx: AnalysisContext): void {
4343
ctx.analyze(this._condition);
4444
ctx.analyze(this._iterableOrUpdate);
45-
ctx.analyze(this._variableOrInit);
4645
ctx.pushScope();
46+
ctx.analyze(this._variableOrInit);
4747
try {
4848
super.analyze(ctx);
4949
} finally {

0 commit comments

Comments
 (0)