Skip to content

Commit 2a39444

Browse files
committed
fix: fix method snippets for classes with implicit constructor and functions within constructor arguments e.g. new A(foo())
1 parent c7f0618 commit 2a39444

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

typescript/src/constructMethodSnippet.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export default (
2626
if (ts.isIdentifier(containerNode)) containerNode = containerNode.parent
2727
if (ts.isPropertyAccessExpression(containerNode)) containerNode = containerNode.parent
2828

29-
const isNewExpression = ts.isNewExpression(containerNode)
29+
const isNewExpression =
30+
ts.isNewExpression(containerNode) &&
31+
ts.textSpanIntersectsWithPosition(ts.createTextSpanFromBounds(containerNode.expression.pos, containerNode.expression.end), position)
3032
if (!isNewExpression && (type.getProperties().length > 0 || type.getStringIndexType() || type.getNumberIndexType())) {
3133
resolveData.isAmbiguous = true
3234
}
@@ -36,7 +38,7 @@ export default (
3638
if (signatures.length === 0) return
3739
const signature = signatures[0]!
3840
// probably need to remove check as class can be instantiated inside another class, and don't really see a reason for this check
39-
if (isNewExpression && hasPrivateOrProtectedModifier((signature.getDeclaration() as ts.ConstructorDeclaration).modifiers)) return
41+
if (isNewExpression && hasPrivateOrProtectedModifier((signature.getDeclaration() as ts.ConstructorDeclaration | undefined)?.modifiers)) return
4042
if (signatures.length > 1 && c('methodSnippets.multipleSignatures') === 'empty') {
4143
return ['']
4244
}

typescript/test/completions.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ describe('Method snippets', () => {
137137
function foo(this: {}) {}
138138
foo/*3*/
139139
140+
// new class
141+
new Something(foo/*301*/)
142+
140143
// contextual type
141144
declare const bar: {
142145
b: (a) => {}
@@ -173,6 +176,7 @@ describe('Method snippets', () => {
173176
compareMethodSnippetAgainstMarker(markers, 1, null)
174177
compareMethodSnippetAgainstMarker(markers, 2, '()')
175178
compareMethodSnippetAgainstMarker(markers, 3, '(a)')
179+
compareMethodSnippetAgainstMarker(markers, 301, '(a)')
176180
compareMethodSnippetAgainstMarker(markers, 4, '($b)')
177181
compareMethodSnippetAgainstMarker(markers, 5, '(a, b, { d, e: {} }, ...c)')
178182
compareMethodSnippetAgainstMarker(markers, 6, '(a, b, c)')
@@ -188,13 +192,17 @@ describe('Method snippets', () => {
188192
protected constructor(a) {}
189193
}
190194
195+
class C {}
196+
191197
new A/*1*/
192198
// not sure...
193199
new B/*2*/
200+
new C/*3*/
194201
`)
195202

196203
compareMethodSnippetAgainstMarker(markers, 1, ['a'])
197204
compareMethodSnippetAgainstMarker(markers, 2, null)
205+
compareMethodSnippetAgainstMarker(markers, 3, [])
198206
})
199207

200208
test('Skip trailing void', () => {

0 commit comments

Comments
 (0)