Skip to content

fix: avoid variable shadowing in generated operation code#3740

Merged
timovv merged 4 commits intoAzure:mainfrom
timovv:fix/variable-shadowing-in-generated-code
Feb 12, 2026
Merged

fix: avoid variable shadowing in generated operation code#3740
timovv merged 4 commits intoAzure:mainfrom
timovv:fix/variable-shadowing-in-generated-code

Conversation

@timovv
Copy link
Copy Markdown
Member

@timovv timovv commented Feb 10, 2026

Copilot PR

Problem

When a TypeSpec operation has a parameter whose name collides with a local variable in the generated code, the emitter produces code with a Cannot redeclare compilation error.

For example, a @path parameter named path causes:

export function _getResultFileSend(
  context: Client,
  operationId: string,
  path: string,                         // function parameter
  options: GetResultFileOptionalParams = { requestOptions: {} },
): StreamableMethod {
  const path = expandUrlTemplate(       // ERROR: Cannot redeclare 'path'
    ...
  );

The same class of bug could also occur with parameters named result, headers, payload, or streamableMethod in getOperationFunction.

Fix

  • Extract the private generateLocallyUniqueName() helper from Binder into namingHelpers.ts so it can be reused across the codebase, and update Binder to import it.
  • In getSendPrivateFunction: use generateLocallyUniqueName("path", paramNames) so the local variable is renamed to path_1 (etc.) only when a parameter named path exists.
  • In getOperationFunction: apply the same pattern for result, streamableMethod, headers, and payload.
  • When there is no conflict, the original variable name is preserved — no change to existing generated output.

Testing

  • Added a new modular unit test scenario (pathParameterNamedPath.md) that verifies a @path parameter named path generates const path_1 = ... instead of shadowing.
  • All 474 unit tests pass.
  • Build and lint pass.

When an operation parameter has a name that collides with a local
variable in the generated code (e.g. a path parameter named 'path'),
the generated code would produce a 'Cannot redeclare' compilation error.

Extract generateLocallyUniqueName() from Binder into namingHelpers so
it can be reused, and apply it in getSendPrivateFunction (for the
'path' variable) and getOperationFunction (for 'result',
'streamableMethod', 'headers', and 'payload' variables). When there is
no conflict the original name is kept; when there is a conflict a
suffix is appended (e.g. path_1).
@timovv timovv enabled auto-merge (squash) February 11, 2026 23:30
@timovv timovv merged commit 79af6fc into Azure:main Feb 12, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants