From dd8d05551a510dd962db89aec84c4d1ab8f09716 Mon Sep 17 00:00:00 2001 From: detachhead Date: Wed, 4 Sep 2024 20:20:06 +1000 Subject: [PATCH] fix incorrect semantic highlighting of `Never` --- .../src/analyzer/semanticTokensWalker.ts | 8 +++----- .../src/tests/semanticTokensProvider.test.ts | 18 +++++++++--------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/pyright-internal/src/analyzer/semanticTokensWalker.ts b/packages/pyright-internal/src/analyzer/semanticTokensWalker.ts index eea3ed8f3d..631a0733b1 100644 --- a/packages/pyright-internal/src/analyzer/semanticTokensWalker.ts +++ b/packages/pyright-internal/src/analyzer/semanticTokensWalker.ts @@ -1,6 +1,6 @@ import { ParseTreeWalker } from './parseTreeWalker'; import { TypeEvaluator } from './typeEvaluatorTypes'; -import { ClassType, FunctionType, OverloadedType, Type, TypeCategory, TypeFlags } from './types'; +import { ClassType, FunctionType, getTypeAliasInfo, OverloadedType, Type, TypeCategory, TypeFlags } from './types'; import { ClassNode, DecoratorNode, @@ -218,10 +218,8 @@ export class SemanticTokensWalker extends ParseTreeWalker { // to differentiate between "instances" of `Never` and type aliases/annotations of Never. // this is probably extremely cringe since i have no idea what this is doing and i literally // just brute forced random shit until all the tests passed - (typeResult.type.category !== TypeCategory.Never && - typeResult.type.category !== TypeCategory.Unbound && - typeResult.type.flags & TypeFlags.Instantiable) || - (typeResult.type.category === TypeCategory.Unbound && !typeResult.includesIllegalTypeAliasDecl) + (typeResult.type.category === TypeCategory.Never && !typeResult.includesVariableDecl) || + (getTypeAliasInfo(type) && !typeResult.includesIllegalTypeAliasDecl) ) { this._addItem(node.start, node.length, SemanticTokenTypes.type, []); return; diff --git a/packages/pyright-internal/src/tests/semanticTokensProvider.test.ts b/packages/pyright-internal/src/tests/semanticTokensProvider.test.ts index 82351d2e2a..1ca5b228cf 100644 --- a/packages/pyright-internal/src/tests/semanticTokensProvider.test.ts +++ b/packages/pyright-internal/src/tests/semanticTokensProvider.test.ts @@ -22,8 +22,8 @@ if (process.platform !== 'win32' || !process.env['CI']) { expect(result).toStrictEqual([ //TODO: fix duplicates { type: 'namespace', modifiers: [], start: 5, length: 6 }, - { type: 'class', modifiers: [], start: 19, length: 5 }, - { type: 'class', modifiers: [], start: 19, length: 5 }, + { type: 'type', modifiers: [], start: 19, length: 5 }, + { type: 'type', modifiers: [], start: 19, length: 5 }, { type: 'class', modifiers: [], start: 26, length: 8 }, { type: 'class', modifiers: [], start: 38, length: 3 }, { type: 'namespace', modifiers: [], start: 47, length: 11 }, @@ -52,12 +52,12 @@ if (process.platform !== 'win32' || !process.env['CI']) { const result = semanticTokenizeSampleFile('never.py'); expect(result).toStrictEqual([ { type: 'namespace', modifiers: [], start: 5, length: 6 }, // typing - { type: 'class', modifiers: [], start: 19, length: 5 }, // Never - { type: 'class', modifiers: [], start: 19, length: 5 }, // Never + { type: 'type', modifiers: [], start: 19, length: 5 }, // Never + { type: 'type', modifiers: [], start: 19, length: 5 }, // Never { type: 'variable', modifiers: [], start: 26, length: 3 }, // foo { type: 'type', modifiers: [], start: 31, length: 5 }, // Never - { type: 'class', modifiers: [], start: 37, length: 3 }, // bar - { type: 'class', modifiers: [], start: 43, length: 5 }, // Never + { type: 'type', modifiers: [], start: 37, length: 3 }, // bar + { type: 'type', modifiers: [], start: 43, length: 5 }, // Never { type: 'function', modifiers: ['definition'], start: 54, length: 3 }, // baz { type: 'function', modifiers: [], start: 54, length: 3 }, // baz { type: 'type', modifiers: [], start: 63, length: 5 }, // Never @@ -69,10 +69,10 @@ if (process.platform !== 'win32' || !process.env['CI']) { { type: 'type', modifiers: [], start: 112, length: 5 }, // Never { type: 'parameter', modifiers: [], start: 120, length: 3 }, // foo { type: 'variable', modifiers: [], start: 128, length: 5 }, // value - { type: 'class', modifiers: [], start: 135, length: 4 }, // Type - { type: 'class', modifiers: [], start: 142, length: 5 }, // Never + { type: 'type', modifiers: [], start: 135, length: 4 }, // Type + { type: 'type', modifiers: [], start: 142, length: 5 }, // Never { type: 'variable', modifiers: [], start: 148, length: 5 }, // value - { type: 'variable', modifiers: [], start: 155, length: 4 }, // Type (should be type. https://github.com/DetachHead/basedpyright/issues/490) + { type: 'type', modifiers: [], start: 155, length: 4 }, // Type { type: 'function', modifiers: ['definition'], start: 169, length: 8 }, // inferred { type: 'function', modifiers: [], start: 169, length: 8 }, // inferred { type: 'variable', modifiers: [], start: 185, length: 5 }, // value