-
Notifications
You must be signed in to change notification settings - Fork 93
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
[DO NOT MERGE] SONARPY-2295 Support relative imports in type inference V2 #2120
Conversation
…ted by the V2 type inference
9b74c04
to
3387928
Compare
3387928
to
7452ff9
Compare
7452ff9
to
8471fb4
Compare
@@ -96,13 +98,15 @@ public class TrivialTypeInferenceVisitor extends BaseTreeVisitor { | |||
|
|||
private final TypeTable projectLevelTypeTable; | |||
private final String fileId; | |||
private final String fullyQualifiedModuleName; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could store it as a list of strings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually use the string in my other PR to introduce FQNs for FunctionType
, so I'd rather keep the string here and split it when needed in the import resolution.
.ifPresent(fqn -> setTypeToImportFromStatement(importFrom, fqn)); | ||
.orElse(new ArrayList<>()); | ||
|
||
List<String> moduleFqnElements = new ArrayList<>(Arrays.stream(fullyQualifiedModuleName.split("\\.")).toList()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then this operation makes no sense, but if you prefer to store module name as a single string then this expression could be simpler:
var moduleFqnElements = List.of(fullyQualifiedModuleName.split("\\."))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, does it make sense to define this variable outside of the if
condition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also if you'll get rid of dotPrefixTokens
variable and simply assign the fromModuleFqn
- it will allow to get rid of the else
part
var fromModuleFqn = Optional.ofNullable(importFrom.module())
.map(TrivialTypeInferenceVisitor::dottedNameToPartFqn)
.orElse(new ArrayList<>());
var dotPrefixTokens = importFrom.dottedPrefixForModule();
if (!dotPrefixTokens.isEmpty()) {
var moduleFqnElements = List.of(fullyQualifiedModuleName.split("\\."));
// Relative import: we start from the current module FQN and go up as many levels as there are dots in the import statement
int sizeLimit = Math.max(0, moduleFqnElements.size() - dotPrefixTokens.size());
fromModuleFqn = Stream.concat(moduleFqnElements.stream().limit(sizeLimit), fromModuleFqn.stream()).toList();
}
setTypeToImportFromStatement(importFrom, fromModuleFqn);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just a couple of small changes could be applied
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Quality Gate failedFailed conditions See analysis details on SonarQube Catch issues before they fail your Quality Gate with our IDE extension SonarLint |
Note: I re-created #2122 to target master instead of the feature branch. |
fafc00f
to
1c837cc
Compare
Duplicate. |
SONARPY-2295