Skip to content

Commit

Permalink
fix: Fix properties sorting in JSX attribute completions
Browse files Browse the repository at this point in the history
fixes #206
  • Loading branch information
zardoy committed Apr 21, 2024
1 parent 53d4bb2 commit 2b084c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion typescript/src/completions/fixPropertiesSorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default (entries: ts.CompletionEntry[]) => {
const typeChecker = program.getTypeChecker()
let sourceProps: string[]
if (isJsxElem) {
const type = typeChecker.getContextualType((node as ts.JsxOpeningElement).attributes)
const type = typeChecker.getContextualType((targetNode as ts.JsxOpeningElement).attributes)
if (!type) return
// usually component own props defined first like interface Props extends ... {} or type A = Props & ..., but this is not a case with mui...
sourceProps = (type.isIntersection() ? type.types.flatMap(type => type.getProperties()) : type.getProperties()).map(symbol => symbol.name)
Expand Down
24 changes: 18 additions & 6 deletions typescript/test/completions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,26 +527,38 @@ test('Fix properties sorting', () => {
a.b({/*2*/})./*3*/
}
let a: { b:{}, a() } = {
/*5*/
}
declare function MyComponent(props: { b?; c? } & { a? }): JSX.Element
<MyComponent /*4*/ />;
<MyComponent
c=''
/*41*/
/>;
let a: { b:{}, a() } = {
/*5*/
}
<MyComponent
test2=''
/*41*/
test=''
/>;
<MyComponent /*42*/
test2=''
/>;
`)
const assertSorted = (marker: number, expected: string[]) => {
const { entriesSorted } = getCompletionsAtPosition(currentTestingContext.markers[marker]!)!
expect(entriesSorted.map(x => x.name)).toEqual(expected)
expect(
entriesSorted.map(x => x.name),
`${marker}`,
).toEqual(expected)
}
assertSorted(1, ['c', 'b'])
assertSorted(2, ['c', 'b'])
assertSorted(3, ['c', 'b'])
assertSorted(4, ['b', 'c', 'a'])
assertSorted(41, ['b', 'a'])
assertSorted(41, ['b', 'c', 'a'])
assertSorted(42, ['b', 'c', 'a'])
assertSorted(5, ['b', 'b', 'a', 'a'])
settingsOverride.fixSuggestionsSorting = false
})
Expand Down

0 comments on commit 2b084c7

Please sign in to comment.