Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: addDestructure code action duplicated object properties #196

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { uniq } from 'rambda'
import { findChildContainingExactPosition, getChangesTracker, getPositionHighlights, isValidInitializerForDestructure, makeUniqueName } from '../../../utils'

export default (node: ts.Node, sourceFile: ts.SourceFile, formatOptions: ts.FormatCodeSettings | undefined, languageService: ts.LanguageService) => {
Expand Down Expand Up @@ -56,7 +57,7 @@ export default (node: ts.Node, sourceFile: ts.SourceFile, formatOptions: ts.Form

if (!nodeToReplaceWithBindingPattern || propertyNames.length === 0) return

const bindings = propertyNames.map(({ initial, unique }) => {
const bindings = uniq(propertyNames).map(({ initial, unique }) => {
return ts.factory.createBindingElement(undefined, unique ? initial : undefined, unique ?? initial)
})

Expand Down
31 changes: 31 additions & 0 deletions typescript/test/codeActions/addDestruct.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@ describe('Add destructure', () => {

const { codeAction } = fourslashLikeTester(initial, undefined, { dedent: true })

codeAction(0, {
refactorName: 'Add Destruct',
newContent: expected,
})
})
test('Multiple same prop extractions', () => {
const initial = /* ts */ `
const /*t*/props/*t*/ = {
source: {
type: Object,
required: true,
},
};
const test = props.source;
const test2 = props.source;
const test3 = props.source;
`
const expected = /* ts */ `
const { source } = {
source: {
type: Object,
required: true,
},
};
const test = source;
const test2 = source;
const test3 = source;
`

const { codeAction } = fourslashLikeTester(initial, undefined, { dedent: true })

codeAction(0, {
refactorName: 'Add Destruct',
newContent: expected,
Expand Down
20 changes: 10 additions & 10 deletions typescript/test/codeActions/fromDestruct.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ describe('From destructure', () => {
)

expect(content).toMatchInlineSnapshot(`
"
const something: number = anObject.something;
"
`)
"
const something: number = anObject.something;
"
`)
})
})
test('Should convert nested', () => {
Expand Down Expand Up @@ -204,12 +204,12 @@ describe('From destructure', () => {
{ compareContent: true },
)
expect(newContent).toMatchInlineSnapshot(`
"
const foo = {
foo: 1,
}.foo
"
`)
"
const foo = {
foo: 1,
}.foo
"
`)
})
test('Destructured two or more properties', () => {
const initial = /* ts*/ `
Expand Down
Loading