-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARPY-1486 Support generic type alias declaration syntax (#1610)
- Loading branch information
1 parent
8f382be
commit b8423a1
Showing
18 changed files
with
266 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
python-frontend/src/main/java/org/sonar/plugins/python/api/tree/TypeAliasStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* SonarQube Python Plugin | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.plugins.python.api.tree; | ||
|
||
import javax.annotation.CheckForNull; | ||
|
||
/** | ||
* <pre> | ||
* type {@link #name()} {@link #typeParams()} = {@link #expression()} | ||
* </pre> | ||
* | ||
* See https://docs.python.org/3/reference/simple_stmts.html#the-type-statement | ||
*/ | ||
public interface TypeAliasStatement extends Statement { | ||
Token typeKeyword(); | ||
|
||
Name name(); | ||
|
||
@CheckForNull | ||
TypeParams typeParams(); | ||
|
||
Token equalToken(); | ||
|
||
Expression expression(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
python-frontend/src/main/java/org/sonar/python/tree/TypeAliasStatementImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* SonarQube Python Plugin | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.python.tree; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import javax.annotation.CheckForNull; | ||
import javax.annotation.Nullable; | ||
import org.sonar.plugins.python.api.tree.Expression; | ||
import org.sonar.plugins.python.api.tree.Name; | ||
import org.sonar.plugins.python.api.tree.Token; | ||
import org.sonar.plugins.python.api.tree.Tree; | ||
import org.sonar.plugins.python.api.tree.TreeVisitor; | ||
import org.sonar.plugins.python.api.tree.TypeAliasStatement; | ||
import org.sonar.plugins.python.api.tree.TypeParams; | ||
|
||
public class TypeAliasStatementImpl extends SimpleStatement implements TypeAliasStatement { | ||
private final Token typeKeyword; | ||
private final Name name; | ||
private final TypeParams typeParams; | ||
private final Token equalToken; | ||
private final Expression expression; | ||
private final Separators separator; | ||
|
||
public TypeAliasStatementImpl(Token typeKeyword, Name name, @Nullable TypeParams typeParams, | ||
Token equalToken, Expression expression, Separators separator) { | ||
this.typeKeyword = typeKeyword; | ||
this.name = name; | ||
this.typeParams = typeParams; | ||
this.equalToken = equalToken; | ||
this.expression = expression; | ||
this.separator = separator; | ||
} | ||
|
||
@Override | ||
public void accept(TreeVisitor visitor) { | ||
visitor.visitTypeAliasStatement(this); | ||
} | ||
|
||
@Override | ||
public Kind getKind() { | ||
return Kind.TYPE_ALIAS_STMT; | ||
} | ||
|
||
@Override | ||
public Token typeKeyword() { | ||
return typeKeyword; | ||
} | ||
|
||
@Override | ||
public Name name() { | ||
return name; | ||
} | ||
|
||
@CheckForNull | ||
@Override | ||
public TypeParams typeParams() { | ||
return typeParams; | ||
} | ||
|
||
@Override | ||
public Token equalToken() { | ||
return equalToken; | ||
} | ||
|
||
@Override | ||
public Expression expression() { | ||
return expression; | ||
} | ||
|
||
@Override | ||
List<Tree> computeChildren() { | ||
var builder = Stream.<Tree>builder() | ||
.add(typeKeyword()) | ||
.add(name()) | ||
.add(typeParams()) | ||
.add(equalToken()).add(expression()); | ||
|
||
separator.elements().forEach(builder::add); | ||
return builder.build().filter(Objects::nonNull).collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public Token separator() { | ||
return separator.last(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ | |
while i < 10: | ||
i+=1 | ||
pass | ||
type a = str |