Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix readonly property member access highlighting the entire expression as reaonly instead of just the member name #902

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading