Skip to content

Commit 25124c1

Browse files
authored
Merge pull request #170 from zardoy/develop
2 parents 7b2ba84 + 205aa50 commit 25124c1

File tree

9 files changed

+55
-17
lines changed

9 files changed

+55
-17
lines changed

src/configurationType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export type Configuration = {
6767
*/
6868
// volarLoadConfigPaths: string[]
6969
/**
70-
* Removes `Symbol`, `caller`, `prototype` everywhere
70+
* Removes annoying `Symbol`, `caller`, `prototype` everywhere
7171
* @default true
7272
* */
7373
'removeUselessFunctionProps.enable': boolean

src/specialCommands.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,12 @@ export default () => {
254254

255255
onCompletionAcceptedOverride.value = () => {}
256256
const { ranges, text } = await new Promise<{ text: string; ranges: vscode.Range[] }>(resolve => {
257-
vscode.workspace.onDidChangeTextDocument(({ document, contentChanges }) => {
257+
const { dispose } = vscode.workspace.onDidChangeTextDocument(({ document, contentChanges }) => {
258258
if (document !== editor.document || contentChanges.length === 0) return
259259
const ranges = contentChanges.map(
260260
change => new vscode.Range(change.range.start, offsetPosition(document, change.range.start, change.text.length)),
261261
)
262+
dispose()
262263
resolve({ ranges, text: contentChanges[0]!.text })
263264
})
264265
void vscode.commands.executeCommand('acceptSelectedSuggestion')

typescript/src/codeActions/extended/addMissingProperties.ts renamed to typescript/src/codeActions/extended/declareMissingProperties.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import { matchParents } from '../../utils'
12
import { ExtendedCodeAction } from '../getCodeActions'
23

34
export default {
45
codes: [2339],
56
kind: 'quickfix',
67
title: 'Declare missing property',
78
tryToApply({ sourceFile, node }) {
8-
if (node && ts.isIdentifier(node) && ts.isObjectBindingPattern(node.parent.parent) && ts.isParameter(node.parent.parent.parent)) {
9-
const param = node.parent.parent.parent
9+
const param = matchParents(node, ['Identifier', 'ObjectBindingPattern', 'Parameter'])
10+
if (param) {
1011
// special react pattern
1112
if (ts.isArrowFunction(param.parent) && ts.isVariableDeclaration(param.parent.parent)) {
1213
const variableDecl = param.parent.parent
@@ -19,7 +20,7 @@ export default {
1920
const hasMembers = param.type.members.length > 0
2021
const insertPos = param.type.members.at(-1)?.end ?? param.type.end - 1
2122
const insertComma = hasMembers && sourceFile.getFullText().slice(insertPos - 1, insertPos) !== ','
22-
let insertText = node.text
23+
let insertText = (node as ts.Identifier).text
2324
if (insertComma) insertText = `, ${insertText}`
2425
// alternatively only one snippetEdit could be used with tsFull.escapeSnippetText(insertText) + $0
2526
return {

typescript/src/codeActions/getCodeActions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ApplyExtendedCodeActionResult, IpcExtendedCodeAction } from '../ipcType
55
import objectSwapKeysAndValues from './custom/objectSwapKeysAndValues'
66
import changeStringReplaceToRegex from './custom/changeStringReplaceToRegex'
77
import splitDeclarationAndInitialization from './custom/splitDeclarationAndInitialization'
8-
import addMissingProperties from './extended/addMissingProperties'
8+
import declareMissingProperties from './extended/declareMissingProperties'
99
import { renameParameterToNameFromType, renameAllParametersToNameFromType } from './custom/renameParameterToNameFromType'
1010

1111
const codeActions: CodeAction[] = [
@@ -15,7 +15,7 @@ const codeActions: CodeAction[] = [
1515
renameParameterToNameFromType,
1616
renameAllParametersToNameFromType,
1717
]
18-
const extendedCodeActions: ExtendedCodeAction[] = [addMissingProperties]
18+
const extendedCodeActions: ExtendedCodeAction[] = [declareMissingProperties]
1919

2020
type SimplifiedRefactorInfo =
2121
| {

typescript/src/completions/functionCompletions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export default (entries: ts.CompletionEntry[]) => {
5050
const originalText = entry.insertText ?? entry.name
5151
const insertTextSnippetAdd = `(${methodSnippet.map((x, i) => `$\{${i + 1}:${x}}`).join(', ')})`
5252
// https://github.com/zardoy/typescript-vscode-plugins/issues/161
53-
const beforeDotWorkaround = prior.isMemberCompletion && prevChar === '.'
53+
// todo implement workaround for ?. as well
54+
const beforeDotWorkaround = !entry.replacementSpan && prior.isMemberCompletion && prevChar === '.'
5455
return {
5556
...entry,
5657
insertText: (beforeDotWorkaround ? '.' : '') + insertTextAfterEntry(originalText, insertTextSnippetAdd),
@@ -63,7 +64,7 @@ export default (entries: ts.CompletionEntry[]) => {
6364
start: position - 1,
6465
length: (c('editorSuggestInsertModeReplace') ? wordRangeAtPos(fullText, position).length : 0) + 1,
6566
}
66-
: undefined,
67+
: entry.replacementSpan,
6768
kind: ts.ScriptElementKind.functionElement,
6869
isSnippet: true,
6970
}

typescript/src/completions/functionPropsAndMethods.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import { oneOf } from '@zardoy/utils'
2+
import { matchParents } from '../utils'
23
import { sharedCompletionContext } from './sharedContext'
34

45
export default (entries: ts.CompletionEntry[]) => {
56
const { c, prevCompletionsMap } = sharedCompletionContext
67

78
if (c('removeUselessFunctionProps.enable')) {
89
entries = entries.filter(entry => {
9-
if (oneOf(entry.kind, ts.ScriptElementKind.warning)) return true
10-
return !['Symbol', 'caller', 'prototype'].includes(entry.name)
10+
const completionDeclaration = entry.symbol?.valueDeclaration
11+
if (
12+
['Symbol', 'caller', 'prototype'].includes(entry.name) &&
13+
!oneOf(entry.kind, ts.ScriptElementKind.warning) &&
14+
(entry.insertText === '[Symbol]' ||
15+
(completionDeclaration?.getSourceFile().fileName.includes('node_modules/typescript/lib/lib') &&
16+
matchParents(completionDeclaration.parent, ['InterfaceDeclaration'])?.name.text === 'Function'))
17+
) {
18+
return false
19+
}
20+
return true
1121
})
1222
}
1323

typescript/src/completionsAtPosition.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ export const getCompletionsAtPosition = (
7272
const sourceFile = program?.getSourceFile(fileName)
7373
if (!program || !sourceFile) return
7474
if (!scriptSnapshot || isInBannedPosition(position, scriptSnapshot, sourceFile)) return
75+
// just pick up random setting to check if we they are here
76+
if (c('correctSorting.enable') === undefined) {
77+
throw new Error(
78+
"Plugin didn't get required user settings (config). Most probably there is a problem with TypeScript. Either restart TS server, use stable TS version or disable typescript-essentials-plugin if you need to test this TS version",
79+
)
80+
}
7581
const exactNode = findChildContainingExactPosition(sourceFile, position)
7682
const isCheckedFile =
7783
!tsFull.isSourceFileJS(sourceFile as any) || !!tsFull.isCheckJsEnabledForFile(sourceFile as any, additionalData.compilerOptions as any)
@@ -226,11 +232,6 @@ export const getCompletionsAtPosition = (
226232
// if (c('completionHelpers') && node) prior.entries = objectLiteralHelpers(node, prior.entries) ?? prior.entries
227233

228234
if (c('patchToString.enable')) {
229-
// const indexToPatch = arrayMoveItemToFrom(
230-
// prior.entries,
231-
// ({ name }) => name === 'toExponential',
232-
// ({ name }) => name === 'toString',
233-
// )
234235
const indexToPatch = prior.entries.findIndex(({ name, kind }) => name === 'toString' && kind !== ts.ScriptElementKind.warning)
235236
if (indexToPatch !== -1) {
236237
const entryToPatch = prior.entries[indexToPatch]!

typescript/test/completions.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,28 @@ test('String template type completions', () => {
415415
})
416416
})
417417

418+
test('Remove Useless Function Props', () => {
419+
const tester = fourslashLikeTester(/* ts */ `
420+
const a = () => {}
421+
a./*1*/
422+
const b = {
423+
Symbol: 5,
424+
prototype: 5,
425+
caller: 5,
426+
}
427+
b./*2*/
428+
`)
429+
const badProps = ['Symbol', 'caller', 'prototype']
430+
tester.completion(1, {
431+
excludes: badProps,
432+
})
433+
tester.completion(2, {
434+
includes: {
435+
names: badProps,
436+
},
437+
})
438+
})
439+
418440
test('Switch Case Exclude Covered', () => {
419441
const [, _, numPositions] = fileContentsSpecialPositions(/*ts*/ `
420442
let test: 'foo' | 'bar'

typescript/test/testing.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ export const fourslashLikeTester = (contents: string, fileName = entrypoint) =>
130130
}
131131
}
132132
if (excludes) {
133-
expect(result?.entryNames, message).not.toContain(excludes)
133+
for (const exclude of excludes) {
134+
expect(result?.entryNames, message).not.toContain(exclude)
135+
}
134136
}
135137
}
136138
},

0 commit comments

Comments
 (0)