From bc71c83da6f079162dd338d8088ae320e211f2ae Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Mon, 20 Jun 2022 14:02:18 +0300 Subject: [PATCH] feat: disable completions when object recieves zero props --- typescript/src/completionsAtPosition.ts | 4 ++-- typescript/src/isInBannedPosition.ts | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/typescript/src/completionsAtPosition.ts b/typescript/src/completionsAtPosition.ts index cb2448e8..1d914f8e 100644 --- a/typescript/src/completionsAtPosition.ts +++ b/typescript/src/completionsAtPosition.ts @@ -19,13 +19,13 @@ export const getCompletionsAtPosition = ( const program = languageService.getProgram() const sourceFile = program?.getSourceFile(fileName) if (!program || !sourceFile) return - if (!scriptSnapshot || isInBannedPosition(position, fileName, scriptSnapshot, sourceFile, languageService)) return + const node = findChildContainingPosition(ts, sourceFile, position) + if (!scriptSnapshot || isInBannedPosition(position, fileName, scriptSnapshot, sourceFile, languageService, ts, program, node)) return let prior = languageService.getCompletionsAtPosition(fileName, position, options) // console.log( // 'raw prior', // prior?.entries.map(entry => entry.name), // ) - const node = findChildContainingPosition(ts, sourceFile, position) if (['.jsx', '.tsx'].some(ext => fileName.endsWith(ext))) { // JSX Features if (node) { diff --git a/typescript/src/isInBannedPosition.ts b/typescript/src/isInBannedPosition.ts index 854e3d1f..ec2864cb 100644 --- a/typescript/src/isInBannedPosition.ts +++ b/typescript/src/isInBannedPosition.ts @@ -6,7 +6,20 @@ export default ( scriptSnapshot: tslib.IScriptSnapshot, sourceFile: tslib.SourceFile, languageService: tslib.LanguageService, + ts: typeof tslib, + program: tslib.Program, + node?: tslib.Node, ): boolean => { + if (node) { + const typeChecker = program.getTypeChecker() + // TODO check not any! + if (ts.isObjectLiteralExpression(node)) { + const type = typeChecker.getTypeAtLocation(node) + if (type.getProperties().length === 0) { + return true + } + } + } const { character } = languageService.toLineColumnOffset!(fileName, position) const textBeforePositionLine = scriptSnapshot?.getText(position - character, position) const textAfterPositionLine = scriptSnapshot?.getText(position, sourceFile.getLineEndOfPosition(position))