Skip to content

Commit

Permalink
Added tests and fixed some bugs. #186
Browse files Browse the repository at this point in the history
  • Loading branch information
jurcovicovam committed Jun 24, 2014
1 parent ddfb7d6 commit 2245272
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public EmbeddedLessGenerator() {
public String toScript(Expression value, LessProblems problemsHandler) {
InStringCssPrinter builder = new InStringCssPrinter();
builder.append(value);
if (!builder.getUnprintableNodes().isEmpty())
return null;

String replacement = builder.toString();
return replacement;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,29 +127,29 @@ public Expression evaluate(List<Expression> parameters, ProblemsHandler problems

Expression format = parameters.get(0);
if (format.getType()==ASTCssNodeType.STRING_EXPRESSION)
return evaluate((CssString) format, parameters.subList(1, parameters.size()), call.getUnderlyingStructure());
return evaluate((CssString) format, parameters.subList(1, parameters.size()), problemsHandler, call.getUnderlyingStructure());

if (format.getType()==ASTCssNodeType.ESCAPED_VALUE)
return evaluate((EscapedValue) format, parameters.subList(1, parameters.size()), call.getUnderlyingStructure());
return evaluate((EscapedValue) format, parameters.subList(1, parameters.size()), problemsHandler, call.getUnderlyingStructure());

if (!format.isFaulty())
problemsHandler.errFormatWrongFirstParameter(call.getParameter());

return new FaultyExpression(call);
}

private Expression evaluate(EscapedValue format, List<Expression> parameters, HiddenTokenAwareTree technicalUnderlying) {
String newValue = format(format.getValue(), parameters, technicalUnderlying);
private Expression evaluate(EscapedValue format, List<Expression> parameters, ProblemsHandler problemsHandler, HiddenTokenAwareTree technicalUnderlying) {
String newValue = format(format.getValue(), parameters, problemsHandler, technicalUnderlying);
return new CssString(format.getUnderlyingStructure(), newValue, "\"");
}

private Expression evaluate(CssString format, List<Expression> parameters, HiddenTokenAwareTree technicalUnderlying) {
String newValue = format(format.getValue(), parameters, technicalUnderlying);
private Expression evaluate(CssString format, List<Expression> parameters, ProblemsHandler problemsHandler, HiddenTokenAwareTree technicalUnderlying) {
String newValue = format(format.getValue(), parameters, problemsHandler, technicalUnderlying);
return new CssString(format.getUnderlyingStructure(), newValue, "\"");
}

private String format(String value, List<Expression> parameters, HiddenTokenAwareTree technicalUnderlying) {
StringFormatter formatter = new StringFormatter();
private String format(String value, List<Expression> parameters, ProblemsHandler problemsHandler, HiddenTokenAwareTree technicalUnderlying) {
StringFormatter formatter = new StringFormatter(problemsHandler);
return formatter.replaceIn(value, parameters.iterator(), technicalUnderlying);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@
import com.github.sommeri.less4j.core.ast.IdentifierExpression;
import com.github.sommeri.less4j.core.ast.NamedColorExpression;
import com.github.sommeri.less4j.core.parser.HiddenTokenAwareTree;
import com.github.sommeri.less4j.core.problems.ProblemsHandler;
import com.github.sommeri.less4j.utils.InStringCssPrinter;
import com.github.sommeri.less4j.utils.PrintUtils;
import com.github.sommeri.less4j.utils.QuotesKeepingInStringCssPrinter;

public class StringFormatter extends AbstractStringReplacer<Iterator<Expression>> {

private static final Pattern PLACEHOLDER = Pattern.compile("%.");
@SuppressWarnings("unused")
private final ProblemsHandler problemsHandler;

public StringFormatter(ProblemsHandler problemsHandler) {
super();
this.problemsHandler = problemsHandler;
}

@Override
protected Pattern getPattern() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.github.sommeri.less4j.core.compiler.expressions.IScopeAwareExpressionsEvaluator;
import com.github.sommeri.less4j.core.parser.HiddenTokenAwareTree;
import com.github.sommeri.less4j.core.problems.ProblemsHandler;
import com.github.sommeri.less4j.utils.CssPrinter;

public class StringInterpolator extends AbstractStringReplacer<IScopeAwareExpressionsEvaluator> {

Expand Down Expand Up @@ -42,7 +43,12 @@ protected String replacementValue(IScopeAwareExpressionsEvaluator expressionEval
return matchRange.getFullMatch();
}

return embeddedScriptEvaluator.toScript(value, problemsHandler);
String result = embeddedScriptEvaluator.toScript(value, problemsHandler);
if (result==null) {
problemsHandler.stringInterpolationNotSupported(technicalUnderlying, value);
result = CssPrinter.ERROR;
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.github.sommeri.less4j.LessSource;
import com.github.sommeri.less4j.core.AbstractProblem;
import com.github.sommeri.less4j.core.ast.ASTCssNode;
import com.github.sommeri.less4j.core.ast.FaultyExpression;
import com.github.sommeri.less4j.core.parser.HiddenTokenAwareTree;

public class CompilationError extends AbstractProblem {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.github.sommeri.less4j.core.ast.DetachedRulesetReference;
import com.github.sommeri.less4j.core.ast.EscapedSelector;
import com.github.sommeri.less4j.core.ast.Expression;
import com.github.sommeri.less4j.core.ast.FaultyExpression;
import com.github.sommeri.less4j.core.ast.FunctionExpression;
import com.github.sommeri.less4j.core.ast.Import;
import com.github.sommeri.less4j.core.ast.MediaQuery;
Expand All @@ -28,6 +29,7 @@
import com.github.sommeri.less4j.core.ast.SupportsLogicalOperator;
import com.github.sommeri.less4j.core.ast.UnknownAtRule;
import com.github.sommeri.less4j.core.ast.Variable;
import com.github.sommeri.less4j.core.parser.HiddenTokenAwareTree;
import com.github.sommeri.less4j.utils.LessPrinter;
import com.github.sommeri.less4j.utils.PrintUtils;

Expand Down Expand Up @@ -311,6 +313,10 @@ public void addError(ASTCssNode errorNode, String description) {
collector.addError(new CompilationError(errorNode, description));
}

public void addError(HiddenTokenAwareTree errorNode, String description) {
collector.addError(new CompilationError(new FaultyExpression(errorNode), description));
}

@Override
public void addWarning(ASTCssNode weirdNode, String description) {
collector.addWarning(new CompilationWarning(weirdNode, description));
Expand Down Expand Up @@ -340,4 +346,8 @@ public void wrongDetachedRulesetLocation(DetachedRuleset detachedRuleset) {
addError(detachedRuleset, "Detached ruleset is not allowed outside of variable declaration.");
}

public void stringInterpolationNotSupported(HiddenTokenAwareTree errorNode, Expression value) {
addError(errorNode, "String interpolation does not requeted expression type. Requested expression was defined at " + printer.toPosition(value));
}

}
11 changes: 9 additions & 2 deletions src/main/java/com/github/sommeri/less4j/utils/CssPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.github.sommeri.less4j.core.ast.CssClass;
import com.github.sommeri.less4j.core.ast.CssString;
import com.github.sommeri.less4j.core.ast.Declaration;
import com.github.sommeri.less4j.core.ast.DetachedRuleset;
import com.github.sommeri.less4j.core.ast.Document;
import com.github.sommeri.less4j.core.ast.ElementSubsequent;
import com.github.sommeri.less4j.core.ast.EmbeddedScript;
Expand Down Expand Up @@ -84,7 +85,7 @@

public class CssPrinter {

private static final String ERROR = "!#error#!";
public static final String ERROR = "!#error#!";
protected ExtendedStringBuilder cssOnly = new ExtendedStringBuilder();
protected SourceMapBuilder cssAndSM;

Expand Down Expand Up @@ -309,11 +310,13 @@ public boolean switchOnType(ASTCssNode node) {
case INLINE_CONTENT:
return appendInlineContent((InlineContent) node); // TODOsm: source map

case DETACHED_RULESET:
return appendDetachedRuleset((DetachedRuleset) node);

case ESCAPED_SELECTOR:
case PARENTHESES_EXPRESSION:
case SIGNED_EXPRESSION:
case VARIABLE:
case DETACHED_RULESET:
case DETACHED_RULESET_REFERENCE:
case INDIRECT_VARIABLE:
case VARIABLE_DECLARATION:
Expand All @@ -324,6 +327,10 @@ public boolean switchOnType(ASTCssNode node) {
}
}

public boolean appendDetachedRuleset(DetachedRuleset node) {
throw new NotACssException(node);
}

public boolean appendCommaSeparated(List<? extends ASTCssNode> values) {
boolean result = false;
Iterator<? extends ASTCssNode> names = values.iterator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.github.sommeri.less4j.utils;

import java.util.ArrayList;
import java.util.List;

import com.github.sommeri.less4j.core.ast.ASTCssNode;
import com.github.sommeri.less4j.core.ast.ColorExpression;
import com.github.sommeri.less4j.core.ast.Comment;
import com.github.sommeri.less4j.core.ast.CssString;
import com.github.sommeri.less4j.core.ast.DetachedRuleset;

public class InStringCssPrinter extends CssPrinter {

private List<ASTCssNode> unprintableNodes = new ArrayList<ASTCssNode>();

public InStringCssPrinter() {
}

@Override
protected void appendComments(List<Comment> comments, boolean ensureSeparator) {
}
Expand All @@ -23,4 +31,15 @@ protected boolean appendColorExpression(ColorExpression expression) {
cssOnly.append(expression.getValueInHexadecimal());
return true;
}

public boolean appendDetachedRuleset(DetachedRuleset node) {
cssOnly.append(ERROR);
unprintableNodes.add(node);
return true;
}

public List<ASTCssNode> getUnprintableNodes() {
return unprintableNodes;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,8 @@

import org.junit.runners.Parameterized.Parameters;

//FIXME: call on variable with wrong datatype in
//FIXME: test order detached mixin imports who sees who and who overwrites who

/*
* .wrap-mixin(@ruleset) {
.wrap-selector {
@d: invisible;
@ruleset();
}
};
.mixin() {
abusing: less;
}
.wrap-mixin({
two: @d;
.mixin();
});
.selector {
@nosemi: {};
@indirect: "nosemi";
@indirect();
}
// ******************* another **************************
@detached: { color: blue; };
.selector {
interpolation: "@{detached}";
}
//*** should this work? ****
.mixin() {
@detached: { extreme: simplicity; };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@
.selector-indirect {
color: #aabbcc;
}
#mixin-called-from-detached-ruleset .wrap-selector {
two: visible;
abusing: less;
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,23 @@
.selector-indirect {
.return-detached();
@detached();
}

//mixin called from detached ruleset
#mixin-called-from-detached-ruleset {
.wrap-mixin(@ruleset) {
.wrap-selector {
@d: visible;
@ruleset();
}
};

.mixin() {
abusing: less;
}

.wrap-mixin({
two: @d;
.mixin();
});
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
.does-not-exists {
!#error#!
}
.wrong-datatype {
!#error#!
}
.selector {
interpolation: "!#error#!";
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Errors produced by compilation of testCase
ERROR 3:2 Could not find detached ruleset for "@does-not-exists".
ERROR 3:2 Could not find detached ruleset for "@does-not-exists".
ERROR 8:3 Detached ruleset reference "@wrongDatatype" does not evaluate to detached ruleset. It resolved to expression defined at 6:17
ERROR 13:18 String interpolation does not requeted expression type. Requested expression was defined at 11:12
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
//does not exists
.does-not-exists {
@does-not-exists();
}
}
//wrong datatype
@wrongDatatype: "this is not ruleset";
.wrong-datatype {
@wrongDatatype();
}
//attempt to interpolate
@detached: { color: blue; };
.selector {
interpolation: "@{detached}";
}

0 comments on commit 2245272

Please sign in to comment.