Skip to content

Commit

Permalink
Mark DecimalFloat as error in types
Browse files Browse the repository at this point in the history
Fixes #632

If in a Range, error "Floats aren't allowed in Ranges"; otherwise, error
"Float literals are not allowed in types.  Use float() instead.".
  • Loading branch information
KronicDeth committed Jul 23, 2017
1 parent 772516a commit 6baf9a0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
50 changes: 43 additions & 7 deletions src/org/elixir_lang/annonator/ModuleAttribute.java
Expand Up @@ -16,17 +16,15 @@
import org.elixir_lang.errorreport.Logger;
import org.elixir_lang.psi.*;
import org.elixir_lang.psi.call.Call;
import org.elixir_lang.psi.operation.Infix;
import org.elixir_lang.psi.operation.Match;
import org.elixir_lang.psi.operation.Type;
import org.elixir_lang.psi.operation.When;
import org.elixir_lang.psi.operation.*;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.elixir_lang.psi.ElixirTypes.RANGE_OPERATOR;
import static org.elixir_lang.psi.call.name.Function.UNQUOTE;
import static org.elixir_lang.psi.call.name.Module.KERNEL;
import static org.elixir_lang.psi.impl.ElixirPsiImplUtil.identifierName;
Expand Down Expand Up @@ -409,8 +407,10 @@ private void highlightType(@NotNull final AtUnqualifiedNoParenthesesCall atUnqua
}
}

private void highlightTypeError(@NotNull PsiElement element, @NotNull AnnotationHolder annotationHolder) {
annotationHolder.createErrorAnnotation(element, "Strings aren't allowed in types");
private void highlightTypeError(@NotNull PsiElement element,
@NotNull String message,
@NotNull AnnotationHolder annotationHolder) {
annotationHolder.createErrorAnnotation(element, message);
}

private Set<String> highlightTypeLeftOperand(@NotNull final ElixirMatchedUnqualifiedParenthesesCall call,
Expand Down Expand Up @@ -838,6 +838,37 @@ private void highlightTypesAndTypeParameterUsages(Arguments arguments,
);
}

private void highlightTypesAndTypeParameterUsages(@NotNull ElixirDecimalFloat decimalFloat,
@NotNull AnnotationHolder annotationHolder) {
PsiElement parent = decimalFloat.getParent();
String message = null;

if (parent instanceof ElixirAccessExpression) {
PsiElement grandParent = parent.getParent();

if (grandParent instanceof Two) {
Two two = (Two) grandParent;
Operator operator = two.operator();

if (operator != null) {
ASTNode[] rangeOperators = operator.getNode().getChildren(TokenSet.create(RANGE_OPERATOR));

if (rangeOperators.length > 0) {
message = "Floats aren't allowed in Ranges";
}
}
}
} else {
cannotHighlightTypes(decimalFloat);
}

if (message == null) {
message = "Float literals are not allowed in types: use float() instead";
}

highlightTypeError(decimalFloat, message, annotationHolder);
}

private void highlightTypesAndTypeParameterUsages(ElixirMapOperation mapOperation,
Set<String> typeParameterNameSet,
AnnotationHolder annotationHolder,
Expand Down Expand Up @@ -997,6 +1028,11 @@ private void highlightTypesAndTypeParameterUsages(@NotNull PsiElement psiElement
annotationHolder,
typeTextAttributesKey
);
} else if (psiElement instanceof ElixirDecimalFloat) {
highlightTypesAndTypeParameterUsages(
(ElixirDecimalFloat) psiElement,
annotationHolder
);
} else if (psiElement instanceof ElixirMapOperation) {
highlightTypesAndTypeParameterUsages(
(ElixirMapOperation) psiElement,
Expand Down Expand Up @@ -1046,7 +1082,7 @@ private void highlightTypesAndTypeParameterUsages(@NotNull PsiElement psiElement
typeTextAttributesKey
);
} else if (psiElement instanceof InterpolatedString) {
highlightTypeError(psiElement, annotationHolder);
highlightTypeError(psiElement, "Strings aren't allowed in types", annotationHolder);
} else if (psiElement instanceof Infix && !(psiElement instanceof When)) {
highlightTypesAndTypeParameterUsages(
(Infix) psiElement,
Expand Down
@@ -0,0 +1,2 @@
@type unity <error descr="Float literals are not allowed in types: use float() instead">1.0</error>
@type percentage_change -100.0..<error descr="Floats aren't allowed in Ranges">100.0</error>
5 changes: 5 additions & 0 deletions tests/org/elixir_lang/annotator/ModuleAttributeTest.java
Expand Up @@ -54,6 +54,11 @@ public void testIssue605() {
myFixture.checkHighlighting(false, false, true);
}

public void testIssue632() {
myFixture.configureByFile("issue_632.ex");
myFixture.checkHighlighting(false, false, true);
}

public void testMatch() {
myFixture.configureByFile("match.ex");
myFixture.checkHighlighting(false, false, true);
Expand Down

0 comments on commit 6baf9a0

Please sign in to comment.