Skip to content

Commit

Permalink
First working version of FieldAccessExpression resolution for accesse…
Browse files Browse the repository at this point in the history
…s and references when there is an import.
  • Loading branch information
jecisc committed Dec 12, 2024
1 parent 753d905 commit 1812228
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,9 @@ FamixPythonProject1Test >> testFunctionSourceAnchor [

self assert: function sourceAnchor isNotNil.
self assert: function sourceText equals: ('def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z' copyReplaceAll: String cr with: String lf)
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z' copyReplaceAll: String cr with: String lf)
]

{ #category : 'tests - functions' }
Expand Down Expand Up @@ -2195,7 +2195,6 @@ FamixPythonProject1Test >> testReadAccessFromFunction [
FamixPythonProject1Test >> testReadAccessFromImportedGlobalWithNamespace [

| global module access |
self skip.
global := self globalVariableNamed: 'rootPackage2Variable'.
module := self moduleNamed: 'moduleAtRoot'.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"
I am a resolvable to resolve accesses or references to entities that are imported using a simple `import` and not a `from import`. For example
```python
import moduleAtRoot
print(moduleAtRoot.moduleAtRootVariable)
```
"
Class {
#name : 'FamixPythonImportedAccessOrReferenceResolvable',
#superclass : 'SRResolvable',
#instVars : [
'import',
'identifier',
'entity'
],
#category : 'Famix-Python-Importer-SymbolResolution',
#package : 'Famix-Python-Importer',
#tag : 'SymbolResolution'
}

{ #category : 'instance creation' }
FamixPythonImportedAccessOrReferenceResolvable class >> identifier: aString import: anImport [

^ self new
identifier: aString;
import: anImport;
yourself
]

{ #category : 'hooks' }
FamixPythonImportedAccessOrReferenceResolvable >> applyReplacementStrategyWithCurrentEntity: aCurrentEntity [

self entity: (self notFoundReplacementEntity cull: self cull: aCurrentEntity)
]

{ #category : 'accessing' }
FamixPythonImportedAccessOrReferenceResolvable >> entity [

^ entity
]

{ #category : 'accessing' }
FamixPythonImportedAccessOrReferenceResolvable >> entity: anObject [

entity := anObject
]

{ #category : 'accessing' }
FamixPythonImportedAccessOrReferenceResolvable >> identifier [

^ identifier
]

{ #category : 'accessing' }
FamixPythonImportedAccessOrReferenceResolvable >> identifier: anObject [

identifier := anObject
]

{ #category : 'accessing' }
FamixPythonImportedAccessOrReferenceResolvable >> import [

^ import
]

{ #category : 'accessing' }
FamixPythonImportedAccessOrReferenceResolvable >> import: anObject [

import := anObject
]

{ #category : 'resolution' }
FamixPythonImportedAccessOrReferenceResolvable >> resolveInScope: aScope currentEntity: currentEntity [

^ (import importedEntity definedEntitiesNamed: identifier ofKinds: {
FamixPythonClass.
FamixPythonFunction.
FamixPythonLocalVariable.
FamixPythonGlobalVariable.
FamixPythonAttribute.
FamixPythonParameter }) ifEmpty: [ SRNoResolutionPossible signal ] ifNotEmpty: [ :entities | self entity: entities anyOne ]
]
25 changes: 22 additions & 3 deletions src/Famix-Python-Importer/FamixPythonImporterVisitor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,28 @@ FamixPythonImporterVisitor >> visitClassDefinition: aClassDef [
FamixPythonImporterVisitor >> visitFieldAccessExpression: aFieldAccessExpression [

self flag: #todo. "Manage invocations."
aFieldAccessExpression parent isFunctionCallExpression ifTrue: [ ^ aFieldAccessExpression source ].

aFieldAccessExpression source traceCr.
(aFieldAccessExpression parent isFunctionCallExpression and: [ aFieldAccessExpression parent receiver = aFieldAccessExpression ]) ifTrue: [
^ aFieldAccessExpression source ].

(self currentEntity withAllParents flatCollect: [ :entity | (entity query outgoing local dependenciesOfType: FamixPythonImport) reject: #isFromImport ])
detect: [ :import |
| importPath |
importPath := import importPath , '.'.

(aFieldAccessExpression source beginsWith: importPath) and: [ ((aFieldAccessExpression source withoutPrefix: importPath) includes: $.) not ] ]
ifFound: [ :import |
self
resolve: ((FamixPythonImportedAccessOrReferenceResolvable identifier: aFieldAccessExpression name import: import)
notFoundReplacementEntity: [ :unresolved :currentEntity | self ensureStubUnknownAccessedOrReferencedEntityNamed: unresolved identifier ];
yourself)
foundAction: [ :entity :currentEntity |
| association |
association := entity createAccessOrReferenceFrom: currentEntity node: aFieldAccessExpression.
self setSourceAnchor: association from: aFieldAccessExpression ].
^ aFieldAccessExpression source ].

" aFieldAccessExpression source traceCr.
"
^ aFieldAccessExpression source
]

Expand Down

0 comments on commit 1812228

Please sign in to comment.