Skip to content

Commit

Permalink
fix: prevent manual blocks name collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Golovin committed Oct 29, 2023
1 parent f626f5e commit eeff709
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
13 changes: 11 additions & 2 deletions typescript/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,18 @@ export const isValidInitializerForDestructure = (match: ts.Expression) => {
}
export const isNameUniqueAtLocation = (name: string, location: ts.Node | undefined, typeChecker: ts.TypeChecker) => {
const checker = getFullTypeChecker(typeChecker)
let result: boolean | undefined

const newVariable = checker.resolveName(name, location as unknown as import('typescript-full').Node, ts.SymbolFlags.Value, true)
return !newVariable
const checkCollision = (childNode: ts.Node) => {
if (result) return
result = !!checker.resolveName(name, childNode as unknown as import('typescript-full').Node, ts.SymbolFlags.Value, true)

if (ts.isBlock(childNode)) {
childNode.forEachChild(checkCollision)
}
}
location?.forEachChild(checkCollision)
return !result
}
export const isNameUniqueAtNodeClosestScope = (name: string, node: ts.Node, typeChecker: ts.TypeChecker) => {
const closestScope = findClosestParent(
Expand Down
26 changes: 26 additions & 0 deletions typescript/test/codeActions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,30 @@ describe('From destructure', () => {
`,
})
})
test('Should work with name collisions in nested manual blocks', () => {
const { codeAction } = fourslashLikeTester(
/* ts */ `
function fn({ /*t*/bar/*t*/, foo }) {
{
const newVariable = 5
const something = bar + foo
}
};
`,
undefined,
{ dedent: true },
)

codeAction(0, {
refactorName: 'From Destruct',
newContent: /* ts */ `
function fn(newVariable_1) {
{
const newVariable = 5
const something = newVariable_1.bar + newVariable_1.foo
}
};
`,
})
})
})

0 comments on commit eeff709

Please sign in to comment.