Skip to content

Commit

Permalink
fix readonly property member access highlighting the entire expressio…
Browse files Browse the repository at this point in the history
…n as readonly instead of just the member name
  • Loading branch information
DetachHead committed Nov 24, 2024
1 parent 0c3cdcf commit 6d94c2a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
35 changes: 17 additions & 18 deletions packages/pyright-internal/src/analyzer/semanticTokensWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
ImportAsNode,
ImportFromAsNode,
ImportFromNode,
isExpressionNode,
LambdaNode,
MemberAccessNode,
NameNode,
ParameterNode,
ParseNodeType,
Expand Down Expand Up @@ -135,6 +135,21 @@ export class SemanticTokensWalker extends ParseTreeWalker {
return super.visitTypeAlias(node);
}

override visitMemberAccess(node: MemberAccessNode): boolean {
// check for properties without a setter
if (node.parent && this._evaluator) {
const declaredType = this._evaluator.getDeclaredTypeForExpression(node, {
method: 'set',
});
if (declaredType && isClass(declaredType) && declaredType.shared.flags & ClassTypeFlags.PropertyClass) {
this._addItem(node.d.member.start, node.d.member.length, SemanticTokenTypes.variable, [
SemanticTokenModifiers.readonly,
]);
}
}
return super.visitMemberAccess(node);
}

private _visitNameWithType(node: NameNode, type: Type | undefined) {
switch (type?.category) {
case TypeCategory.Function:
Expand Down Expand Up @@ -192,23 +207,7 @@ export class SemanticTokensWalker extends ParseTreeWalker {
break;
case TypeCategory.Class:
//type annotations handled by visitTypeAnnotation
if (type.flags & TypeFlags.Instance) {
if (node.parent && this._evaluator && isExpressionNode(node.parent)) {
const declaredType = this._evaluator.getDeclaredTypeForExpression(node.parent, {
method: 'set',
});
if (
declaredType &&
isClass(declaredType) &&
declaredType.shared.flags & ClassTypeFlags.PropertyClass
) {
this._addItem(node.start, node.length, SemanticTokenTypes.variable, [
SemanticTokenModifiers.readonly,
]);
return;
}
}
} else {
if (!(type.flags & TypeFlags.Instance)) {
// Exclude type aliases:
// PEP 613 > Name: TypeAlias = Types
// PEP 695 > type Name = Types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ def bar(self, value: int): ...


Foo().foo
Foo().bar
Foo().bar

baz = Foo()
_ = baz.foo
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,17 @@ if (process.platform !== 'win32' || !process.env['CI']) {
{ type: 'parameter', modifiers: ['definition'], start: 198, length: 4 },
{ type: 'parameter', modifiers: ['definition'], start: 204, length: 5 },
{ type: 'class', modifiers: ['defaultLibrary', 'builtin'], start: 211, length: 3 },
{ type: 'variable', modifiers: ['readonly'], start: 229, length: 3 },
{ type: 'class', modifiers: [], start: 223, length: 3 },
{ type: 'variable', modifiers: ['readonly'], start: 229, length: 3 },
{ type: 'class', modifiers: [], start: 233, length: 3 },
{ type: 'variable', modifiers: [], start: 239, length: 3 },
{ type: 'variable', modifiers: [], start: 244, length: 3 },
{ type: 'class', modifiers: [], start: 250, length: 3 },
{ type: 'variable', modifiers: [], start: 256, length: 1 },
{ type: 'variable', modifiers: ['readonly'], start: 264, length: 3 },
{ type: 'variable', modifiers: [], start: 260, length: 3 },
{ type: 'variable', modifiers: ['readonly'], start: 264, length: 3 },
]);
});

Expand Down

0 comments on commit 6d94c2a

Please sign in to comment.