Skip to content

Commit 4430df4

Browse files
authored
fix: add destructure duplicated props (#196)
1 parent adee249 commit 4430df4

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

typescript/src/codeActions/custom/addDestructure/addSplittedDestructure.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { uniq } from 'rambda'
12
import { findChildContainingExactPosition, getChangesTracker, getPositionHighlights, isValidInitializerForDestructure, makeUniqueName } from '../../../utils'
23

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

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

59-
const bindings = propertyNames.map(({ initial, unique }) => {
60+
const bindings = uniq(propertyNames).map(({ initial, unique }) => {
6061
return ts.factory.createBindingElement(undefined, unique ? initial : undefined, unique ?? initial)
6162
})
6263

typescript/test/codeActions/addDestruct.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,37 @@ describe('Add destructure', () => {
2626

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

29+
codeAction(0, {
30+
refactorName: 'Add Destruct',
31+
newContent: expected,
32+
})
33+
})
34+
test('Multiple same prop extractions', () => {
35+
const initial = /* ts */ `
36+
const /*t*/props/*t*/ = {
37+
source: {
38+
type: Object,
39+
required: true,
40+
},
41+
};
42+
const test = props.source;
43+
const test2 = props.source;
44+
const test3 = props.source;
45+
`
46+
const expected = /* ts */ `
47+
const { source } = {
48+
source: {
49+
type: Object,
50+
required: true,
51+
},
52+
};
53+
const test = source;
54+
const test2 = source;
55+
const test3 = source;
56+
`
57+
58+
const { codeAction } = fourslashLikeTester(initial, undefined, { dedent: true })
59+
2960
codeAction(0, {
3061
refactorName: 'Add Destruct',
3162
newContent: expected,

typescript/test/codeActions/fromDestruct.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ describe('From destructure', () => {
5252
)
5353

5454
expect(content).toMatchInlineSnapshot(`
55-
"
56-
const something: number = anObject.something;
57-
"
58-
`)
55+
"
56+
const something: number = anObject.something;
57+
"
58+
`)
5959
})
6060
})
6161
test('Should convert nested', () => {
@@ -204,12 +204,12 @@ describe('From destructure', () => {
204204
{ compareContent: true },
205205
)
206206
expect(newContent).toMatchInlineSnapshot(`
207-
"
208-
const foo = {
209-
foo: 1,
210-
}.foo
211-
"
212-
`)
207+
"
208+
const foo = {
209+
foo: 1,
210+
}.foo
211+
"
212+
`)
213213
})
214214
test('Destructured two or more properties', () => {
215215
const initial = /* ts*/ `

0 commit comments

Comments
 (0)