Skip to content

Commit

Permalink
Clean code + manages accesses/references from import with alias
Browse files Browse the repository at this point in the history
  • Loading branch information
jecisc committed Dec 12, 2024
1 parent 1812228 commit 6f30ae4
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/Famix-Python-Entities/FamixPythonImport.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ FamixPythonImport >> displayStringOn: aStream [
aStream nextPutAll: ')'
]

{ #category : 'testing' }
FamixPythonImport >> hasAlias [

^ alias isNotNil
]

{ #category : 'accessing' }
FamixPythonImport >> isFromImport [

Expand Down
45 changes: 45 additions & 0 deletions src/Famix-Python-Importer-Tests/FamixPythonProject1Test.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2210,6 +2210,51 @@ FamixPythonProject1Test >> testReadAccessFromImportedGlobalWithNamespace [
self assert: (module accesses anySatisfy: [ :anAccess | anAccess variable = global ])
]

{ #category : 'tests - accesses' }
FamixPythonProject1Test >> testReadAccessFromImportedGlobalWithNamespaceAlias [

| global module access |
global := self globalVariableNamed: 'subSubPackage1Variable'.
module := self moduleNamed: 'moduleWithImportsAndAliases'.

access := global incomingAccesses detect: [ :anAccess | anAccess accessor = module ].

self assert: access class equals: FamixPythonAccess.
self assert: access source equals: module.
self assert: access accessor equals: module.
self assert: access target equals: global.
self assert: access variable equals: global.
self deny: access isWrite.
self assert: access isRead.
self assert: (module accesses anySatisfy: [ :anAccess | anAccess variable = global ])
]

{ #category : 'tests - accesses' }
FamixPythonProject1Test >> testReadAccessFromImportedGlobalWithNamespaceAliasSourceAnchor [

| global module access |
global := self globalVariableNamed: 'subSubPackage1Variable'.
module := self moduleNamed: 'moduleWithImportsAndAliases'.

access := global incomingAccesses detect: [ :anAccess | anAccess accessor = module ].

self assert: access sourceAnchor isNotNil.
self assert: access sourceText equals: 'rss.subSubPackage1Variable'
]

{ #category : 'tests - accesses' }
FamixPythonProject1Test >> testReadAccessFromImportedGlobalWithNamespaceSourceAnchor [

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

access := global incomingAccesses detect: [ :anAccess | anAccess accessor = module ].

self assert: access sourceAnchor isNotNil.
self assert: access sourceText equals: 'root2.rootPackage2Variable'
]

{ #category : 'tests - accesses' }
FamixPythonProject1Test >> testReadAccessFromLambda [

Expand Down
29 changes: 19 additions & 10 deletions src/Famix-Python-Importer/FamixPythonImporterVisitor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -741,16 +741,25 @@ FamixPythonImporterVisitor >> visitClassDefinition: aClassDef [
{ #category : 'generated' }
FamixPythonImporterVisitor >> visitFieldAccessExpression: aFieldAccessExpression [

| source |
source := aFieldAccessExpression source.

self flag: #todo. "Manage invocations."
(aFieldAccessExpression parent isFunctionCallExpression and: [ aFieldAccessExpression parent receiver = aFieldAccessExpression ]) ifTrue: [
^ aFieldAccessExpression source ].
aFieldAccessExpression isReceiverOfFunctionCall ifTrue: [ ^ source ].

(self currentEntity withAllParents flatCollect: [ :entity | (entity query outgoing local dependenciesOfType: FamixPythonImport) reject: #isFromImport ])
"Symbol resolution of accesses and references to imported entities.
Example:
import moduleAtRoot
print(moduleAtRoot.moduleVariable)
"
self currentEntity allEffectiveSimpleImports
detect: [ :import |
| importPath |
importPath := import importPath , '.'.

(aFieldAccessExpression source beginsWith: importPath) and: [ ((aFieldAccessExpression source withoutPrefix: importPath) includes: $.) not ] ]
importPath := (import hasAlias
ifTrue: [ import alias ]
ifFalse: [ import importPath ]) , '.'.
(source beginsWith: importPath) and: [ ((source withoutPrefix: importPath) includes: $.) not ] ]
ifFound: [ :import |
self
resolve: ((FamixPythonImportedAccessOrReferenceResolvable identifier: aFieldAccessExpression name import: import)
Expand All @@ -760,11 +769,11 @@ FamixPythonImporterVisitor >> visitFieldAccessExpression: aFieldAccessExpression
| association |
association := entity createAccessOrReferenceFrom: currentEntity node: aFieldAccessExpression.
self setSourceAnchor: association from: aFieldAccessExpression ].
^ aFieldAccessExpression source ].
^ source ].

aFieldAccessExpression source traceCr.

" aFieldAccessExpression source traceCr.
"
^ aFieldAccessExpression source
^ source
]

{ #category : 'visiting' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ PyFieldAccessExpressionNode >> isInternalInstanceVariableAccess [
^ self receiver isSelf
]

{ #category : '*Famix-Python-Importer' }
PyFieldAccessExpressionNode >> isReceiverOfFunctionCall [

^ self parent isFunctionCallExpression and: [ self parent receiver = self ]
]

{ #category : '*Famix-Python-Importer' }
PyFieldAccessExpressionNode >> name [
^ self nameToken value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Extension { #name : 'TEntityMetaLevelDependency' }

{ #category : '*Famix-Python-Importer' }
TEntityMetaLevelDependency >> allEffectiveSimpleImports [
"Return all the imports effective in this entity, but not from imponts."

^ self withAllParents flatCollect: [ :entity | (entity query outgoing local dependenciesOfType: FamixPythonImport) reject: #isFromImport ]
]

0 comments on commit 6f30ae4

Please sign in to comment.