Repository navigation
Resolve static replacement constants in no-dynamic-string-replacement - #75760
Conversation
Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>
|
🧠 Matt Pocock Skills Reviewer is reviewing this pull request using Matt Pocock's engineering skills... |
|
✅ PR Code Quality Reviewer completed the code quality review.
|
|
✅ Design Decision Gate 🏗️ completed the design decision gate check. See the comment below for the result and any generated ADR draft. Design Decision Gate 🏗️Outcome: The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible. Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.
|
|
✅ Ponytail Reviewer completed successfully!
|
|
✅ Test Quality Sentinel completed test quality analysis. Test Quality SentinelOutcome: The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible. Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.
|
Comment MemoryPeek at saved memory (pr-code-quality-reviewer)Note This comment is managed by comment memory.Expand the saved memory block to view or edit the persistent context for this thread.
|
There was a problem hiding this comment.
Review summary
Request changes: the new const-resolution path can miss replacement tokens that are assembled through static bindings and can incorrectly accept destructured bindings without resolving their actual value.
Details
- Static
$tokens split across const bindings can bypass the rule. - Destructured const values such as
{ replacement: "$1" }are not inspected precisely.
🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt50 · ~2 AIC · ⌖ ~1 AIC · ⊞ 1.6K
Comment /review to run again
| } | ||
| resolving.add(variable); | ||
| const result = isDynamic(initializer, resolving) || hasStaticReplacementToken(initializer); | ||
| resolving.delete(variable); |
There was a problem hiding this comment.
This resolution misses replacement tokens assembled from multiple static const bindings, so const a = "$"; const b = "1"; s.replace(/x/g, a + b) is accepted even though it evaluates to $1 and remains replacement syntax at runtime.
💡 Preserve token tracking across resolved constants
When analyzing a binary/template expression, resolve static identifier initializers while checking the resulting concatenated expression for replacement tokens, or conservatively retain the dynamic warning whenever the resolved value cannot be proven token-free. Add a regression test for the split-token example.
| return true; | ||
| } | ||
| const definition = variable.defs[0]; | ||
| if (definition?.type !== "Variable" || definition.parent.type !== "VariableDeclaration" || definition.parent.kind !== "const") { |
There was a problem hiding this comment.
This accepts destructured const bindings without proving the identifier refers to a static token-free value; for example, const { replacement } = { replacement: "$1" }; s.replace(/x/g, replacement) resolves through the declaration but the object initializer is treated as non-dynamic and its property value is never inspected. That silently regresses the rule's protection.
💡 Reject or precisely resolve destructured definitions
Require a direct Identifier variable declarator before applying this const-resolution path, or implement property-aware destructuring resolution. Otherwise return dynamic for destructured bindings and add object/array destructuring regression tests.
There was a problem hiding this comment.
What this change does: The rule now follows eligible const identifiers to their initializers, while still reporting dynamic values and static replacement tokens. The tests cover direct static constants, dynamic initializers, and reassigned bindings.
Should fix:
-
Binding resolution is duplicated (
eslint-factory/src/rules/no-dynamic-string-replacement.ts:L147-L161)- What this is: The new identifier branch repeats the scope, write, cycle, initializer, and cleanup logic already used by
isKnownFunctionandisDefinitelyNonFunction. - Problem: A reported alias such as
const R = get(); s.replace(/x/, R)can run through all three recursive classifiers, so one binding policy is implemented in three places. - Fix: Extract one helper that returns an eligible initializer, then keep the replacement-token check in
isDynamic. - If we skip it: Future changes to binding eligibility must be kept synchronized across three walkers.
- What this is: The new identifier branch repeats the scope, write, cycle, initializer, and cleanup logic already used by
Not checked: The targeted Vitest test could not run because the vitest binary is absent in this checkout.
Verdict: fix 1 first.
Lean: -8 lines possible.
Generated by ✂️ Ponytail Reviewer for #75760 · codex · routed: gpt56 max · ~4 AIC · ⌖ ~1 AIC · ⊞ 118
Comment /ponytail to run again
| return false; | ||
| } | ||
| const variable = resolveVariable(expression); | ||
| if (!variable || variable.defs.length !== 1 || resolving.has(variable) || hasNonInitializingWrite(variable)) { |
There was a problem hiding this comment.
Should fix: This repeats the variable-resolution and initializer lifecycle already implemented in isKnownFunction and isDefinitelyNonFunction; the rule now has three recursive copies of the scope, write, cycle, and cleanup policy. A reported binding such as const R = get(); s.replace(/x/, R) can pass through all three classifiers, so a later binding-policy change must be kept in sync in three places. Extract one helper that returns an eligible initializer, then keep the replacement-token check here; that removes the duplicated guard logic without adding another abstraction.
There was a problem hiding this comment.
🟡 Changes recommended
The constant exemption incorrectly suppresses warnings for destructured and runtime-dependent initializers and misses replacement tokens encoded with escaped dollar signs.
3 open issues
What changed in this PR
This PR addresses #75595 by resolving constant replacement identifiers in the ESLint rule, reducing false positives for fixed, dollar-free strings.
Changes:
- Adds recursive
constinitializer resolution with cycle and reassignment guards. - Carries resolution state through nested expressions.
- Adds tests for safe constants, token-containing constants, computed values, and reassigned bindings.
| File | Description |
|---|---|
eslint-factory/src/rules/no-dynamic-string-replacement.ts |
Resolves constant bindings when classifying replacement arguments. |
eslint-factory/src/rules/no-dynamic-string-replacement.test.ts |
Adds valid and invalid cases for replacement bindings. |
Review effort: Balanced (auto)
| return true; | ||
| } | ||
| const definition = variable.defs[0]; | ||
| if (definition?.type !== "Variable" || definition.parent.type !== "VariableDeclaration" || definition.parent.kind !== "const") { |
| const initializer = getVariableInitializer(variable); | ||
| if (!initializer) { | ||
| return true; | ||
| } |
| return true; | ||
| } | ||
| resolving.add(variable); | ||
| const result = isDynamic(initializer, resolving) || hasStaticReplacementToken(initializer); |
|
Copilot assignment requested for the following outstanding work:
Once you start work, post this work summary as a PR comment without the leading Copilot mention, including the Sous-chef head, work, and state lines below, to record that the assignment is running. Apply the necessary fixes on this PR's existing branch, reply to each listed review thread and resolve it when addressed. Ignore feedback already answered or resolved; stop when only human review or CI remains. Do not merge or trigger CI. Sous-chef requested-head: bec9695
|
Work is running: update this branch from Sous-chef requested-head: bec9695 |
|
🎉 This pull request is included in a new release. Release: |


The rule treated every replacement identifier as dynamic, even when it referred to a fixed string constant. It now resolves eligible
constbindings while preserving reports for dynamic values and constants containing replacement tokens.REDACTEDno longer trigger a warning.$-containing constants, computed initializers, and reassigned bindings remain flagged.