Skip to content

Commit d0388d0

Browse files
authored
Merge pull request #172 from zardoy/develop
2 parents 25124c1 + 3fe103c commit d0388d0

File tree

5 files changed

+47
-22
lines changed

5 files changed

+47
-22
lines changed

typescript/src/codeActions/extended/declareMissingProperties.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ export default {
66
kind: 'quickfix',
77
title: 'Declare missing property',
88
tryToApply({ sourceFile, node }) {
9-
const param = matchParents(node, ['Identifier', 'ObjectBindingPattern', 'Parameter'])
9+
const param = matchParents(node, ['Identifier', 'BindingElement', 'ObjectBindingPattern', 'Parameter'])
1010
if (param) {
1111
// special react pattern
1212
if (ts.isArrowFunction(param.parent) && ts.isVariableDeclaration(param.parent.parent)) {
1313
const variableDecl = param.parent.parent
1414
if (variableDecl.type?.getText().match(/(React\.)?FC/)) {
15-
// handle interface
15+
// todo handle interface
1616
}
1717
}
1818
// general patterns
@@ -24,21 +24,12 @@ export default {
2424
if (insertComma) insertText = `, ${insertText}`
2525
// alternatively only one snippetEdit could be used with tsFull.escapeSnippetText(insertText) + $0
2626
return {
27-
edits: [
28-
{
29-
newText: insertText,
30-
span: {
31-
length: 0,
32-
start: insertPos,
33-
},
34-
},
35-
],
3627
snippetEdits: [
3728
{
38-
newText: '$0',
29+
newText: `${tsFull.escapeSnippetText(insertText)}$0`,
3930
span: {
4031
length: 0,
41-
start: insertPos + insertText.length - 1,
32+
start: insertPos,
4233
},
4334
},
4435
],
@@ -48,3 +39,16 @@ export default {
4839
return
4940
},
5041
} as ExtendedCodeAction
42+
43+
const testCode = () => {
44+
const tester = (code: string) => {
45+
// ^ - problem location in which quickfix needs to be tested (applied)
46+
// | - cursor position after quickfix is applied
47+
// [[...]] - applied part of the code
48+
/* TODO */
49+
}
50+
51+
tester(/* ts */ `
52+
const b = ({ b, ^a }: { b[[, a/*|*/]] }) => {}
53+
`)
54+
}

typescript/src/completions/staticHintSuggestions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const letterKeys = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
2828
)
2929
const someOtherKeys = [
3030
'Space',
31-
'Esc',
31+
'Escape',
3232
'Tab',
3333
'Enter',
3434
'Equal',

typescript/src/constructMethodSnippet.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default (
2828
const isNewExpression =
2929
ts.isNewExpression(containerNode) &&
3030
ts.textSpanIntersectsWithPosition(ts.createTextSpanFromBounds(containerNode.expression.pos, containerNode.expression.end), position)
31-
if (!isNewExpression && (type.getProperties().length > 0 || type.getStringIndexType() || type.getNumberIndexType())) {
31+
if (!isNewExpression && (getNonFunctionProperties(type).length > 0 || type.getStringIndexType() || type.getNumberIndexType())) {
3232
resolveData.isAmbiguous = true
3333
}
3434

@@ -152,3 +152,8 @@ function getPromiseLikeTypeArgument(type: ts.Type | undefined, checker: ts.TypeC
152152
function hasPrivateOrProtectedModifier(modifiers: ts.NodeArray<ts.ModifierLike> | ts.NodeArray<ts.Modifier> | undefined) {
153153
return modifiers?.some(modifier => oneOf(modifier.kind, ts.SyntaxKind.PrivateKeyword, ts.SyntaxKind.ProtectedKeyword))
154154
}
155+
156+
function getNonFunctionProperties(type: ts.Type) {
157+
const customSpecialFunctionProperties = ['__promisify__']
158+
return type.getProperties().filter(x => !customSpecialFunctionProperties.includes(x.name))
159+
}

typescript/src/references.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
import { GetConfig } from './types'
2-
import { findChildContainingPositionMaxDepth, approveCast } from './utils'
2+
import { findChildContainingPositionMaxDepth, approveCast, findChildContainingExactPosition, matchParents } from './utils'
33

44
export default (proxy: ts.LanguageService, languageService: ts.LanguageService, c: GetConfig) => {
55
proxy.findReferences = (fileName, position) => {
66
let prior = languageService.findReferences(fileName, position)
77
if (!prior) return
8+
const program = languageService.getProgram()!
89
if (c('removeDefinitionFromReferences')) {
9-
prior = prior.map(({ references, ...other }) => ({
10-
...other,
11-
references: references.filter(({ isDefinition }) => !isDefinition),
12-
}))
10+
const sourceFile = program.getSourceFile(fileName)
11+
const node = findChildContainingExactPosition(sourceFile!, position)
12+
let filterDefs = true
13+
if (
14+
node &&
15+
node.flags & ts.NodeFlags.JavaScriptFile &&
16+
matchParents(node, ['Identifier', 'PropertyAccessExpression'])?.expression.kind === ts.SyntaxKind.ThisKeyword
17+
) {
18+
// https://github.com/zardoy/typescript-vscode-plugins/issues/165
19+
filterDefs = false
20+
}
21+
22+
if (filterDefs) {
23+
prior = prior.map(({ references, ...other }) => ({
24+
...other,
25+
references: references.filter(({ isDefinition, textSpan, fileName }) => {
26+
return !isDefinition
27+
}),
28+
}))
29+
}
1330
}
1431
if (c('removeImportsFromReferences')) {
15-
const program = languageService.getProgram()!
1632
const refCountPerFileName: Record<
1733
string,
1834
{

typescript/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,12 @@ export const matchParents: MatchParentsType = (node, treeToCompare) => {
303303
for (const toCompare of treeToCompare) {
304304
if (!first) {
305305
node = node?.parent
306-
first = false
307306
}
308307
if (!node) return
309308
if (!(ts[`is${toCompare}` as keyof typeof ts] as (node) => boolean)(node)) {
310309
return
311310
}
311+
first = false
312312
}
313313
return node as any
314314
}

0 commit comments

Comments
 (0)