Skip to content

Commit 24cf762

Browse files
committed
fix: Fix annoying JSX component filtering bug in some cases
fixes jsxImproveElementsSuggestions stops code completions #205
1 parent a952c9f commit 24cf762

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

Diff for: typescript/src/completions/filterJsxComponents.ts

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export default (entries: ts.CompletionEntry[], node: ts.Node, position: number,
1414
) {
1515
return
1616
}
17+
// const startLineNode = ts.getLineAndCharacterOfPosition(node.getSourceFile(), node.pos).line
18+
// const startLinePosition = ts.getLineAndCharacterOfPosition(node.getSourceFile(), position).line
19+
// if (startLineNode !== startLinePosition) return
20+
const identifier = ts.isJsxSelfClosingElement(node) || ts.isJsxOpeningElement(node) ? node.tagName : null
21+
// if already got name and we are not typing it
22+
if (identifier && identifier.end < position) return
1723

1824
const nodeText = node.getText().slice(0, position - (node.pos + node.getLeadingTriviaWidth()))
1925
// workaround for <div test |></div>

Diff for: typescript/test/completions.spec.ts

+35-5
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,10 @@ test('Fix properties sorting', () => {
529529
530530
declare function MyComponent(props: { b?; c? } & { a? }): JSX.Element
531531
<MyComponent /*4*/ />;
532+
<MyComponent
533+
c=''
534+
/*41*/
535+
/>;
532536
533537
let a: { b:{}, a() } = {
534538
/*5*/
@@ -542,6 +546,7 @@ test('Fix properties sorting', () => {
542546
assertSorted(2, ['c', 'b'])
543547
assertSorted(3, ['c', 'b'])
544548
assertSorted(4, ['b', 'c', 'a'])
549+
assertSorted(41, ['b', 'a'])
545550
assertSorted(5, ['b', 'b', 'a', 'a'])
546551
settingsOverride.fixSuggestionsSorting = false
547552
})
@@ -569,12 +574,37 @@ testTs5('Change to function kind', () => {
569574
settingsOverride['experiments.changeKindToFunction'] = false
570575
})
571576

572-
testTs5('Filter JSX Components', () => {
573-
const tester = fourslashLikeTester(/* ts */ `
574-
const a = () => {}
575-
a/*1*/
577+
testTs5.only('Filter JSX Components', () => {
578+
overrideSettings({
579+
// improveJsxCompletions: false,
580+
'experiments.excludeNonJsxCompletions': true,
581+
})
582+
const tester = fourslashLikeTester(/* tsx */ `
583+
const someFunction = () => {}
584+
declare namespace JSX {
585+
interface IntrinsicElements {
586+
superSpan: any;
587+
}
588+
}
589+
// return < // TODO
590+
return <s/*1*/
576591
`)
577-
// TODO
592+
tester.completion(1, {
593+
excludes: ['someFunction'],
594+
includes: {
595+
names: ['superSpan'],
596+
},
597+
})
598+
// https://github.com/zardoy/typescript-vscode-plugins/issues/205
599+
const tester2 = fourslashLikeTester(/* tsx */ `
600+
const Img = ({ alt }) => {}
601+
<Img\n\t/*1*/\n/>
602+
`)
603+
tester2.completion(1, {
604+
includes: {
605+
names: ['alt'],
606+
},
607+
})
578608
})
579609

580610
test('Omit<..., ""> suggestions', () => {

0 commit comments

Comments
 (0)