From b5b709db4bf95a384edb3876b2df816c7b25b065 Mon Sep 17 00:00:00 2001 From: jurcovicovam Date: Mon, 23 Jun 2014 17:07:43 +0200 Subject: [PATCH] Some debugging and tests. #186 --- .../core/compiler/stages/MixinsSolver.java | 41 +++++++++++-------- ....css => detached-rulesets-as-argument.css} | 0 ...ess => detached-rulesets-as-argument.less} | 20 ++++----- .../detached-rulesets-scoping.css | 4 +- src/test/resources/minitests/debug1.css | 27 +++++++++++- src/test/resources/minitests/debug1.less | 31 +++++++++++--- 6 files changed, 85 insertions(+), 38 deletions(-) rename src/test/resources/compile-basic-features/detached-rulesets/{detached-rulesets-incompatible.css => detached-rulesets-as-argument.css} (100%) rename src/test/resources/compile-basic-features/detached-rulesets/{detached-rulesets-incompatible.less => detached-rulesets-as-argument.less} (93%) diff --git a/src/main/java/com/github/sommeri/less4j/core/compiler/stages/MixinsSolver.java b/src/main/java/com/github/sommeri/less4j/core/compiler/stages/MixinsSolver.java index 6c26653a..36da88ef 100644 --- a/src/main/java/com/github/sommeri/less4j/core/compiler/stages/MixinsSolver.java +++ b/src/main/java/com/github/sommeri/less4j/core/compiler/stages/MixinsSolver.java @@ -70,11 +70,10 @@ public BodyCompilationResult run() { }); } - + private BodyCompilationResult resolveReferencedBody(final IScope callerScope, final BodyOwner mixin, final IScope mixinWorkingScope) { final ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator(mixinWorkingScope, problemsHandler, configuration); - final IScope referencedMixinScope = mixinWorkingScope; // ... and I'm starting to see the point of closures ... return InScopeSnapshotRunner.runInOriginalDataSnapshot(referencedMixinScope, new IFunction() { @@ -84,20 +83,25 @@ public BodyCompilationResult run() { // compile referenced mixin - keep the original copy unchanged List replacement = compileBody(mixin.getBody(), referencedMixinScope); + // collect variables and mixins to be imported + // IScope returnValues = expressionEvaluator.evaluateValues(referencedMixinScope); + // collect variables and mixins to be imported IScope returnValues = ScopeFactory.createDummyScope(); returnValues.addFilteredVariables(new ImportedScopeFilter(expressionEvaluator, callerScope), referencedMixinScope); + List unmodifiedMixinsToImport = referencedMixinScope.getAllMixins(); + + List allMixinsToImport = mixinsToImport(callerScope, referencedMixinScope, unmodifiedMixinsToImport); + returnValues.addAllMixins(allMixinsToImport); //FIXME: !!!!!!!!!! clean up return new BodyCompilationResult((ASTCssNode) mixin, replacement, returnValues); - } }); } - private List compileBody(Body body, IScope scopeSnapshot) { semiCompiledNodes.push(body.getParent()); try { @@ -109,7 +113,6 @@ private List compileBody(Body body, IScope scopeSnapshot) { } } - private List mixinsToImport(IScope referenceScope, IScope referencedMixinScope, List unmodifiedMixinsToImport) { List result = new ArrayList(); for (FullMixinDefinition mixinToImport : unmodifiedMixinsToImport) { @@ -168,7 +171,7 @@ public void run() { MixinsGuardsValidator guardsValidator = new MixinsGuardsValidator(mixinWorkingScope, problemsHandler, configuration); GuardValue guardValue = guardsValidator.evaluateGuards(mixin); - if (guardValue!=GuardValue.DO_NOT_USE) { + if (guardValue != GuardValue.DO_NOT_USE) { //OPTIMIZATION POSSIBLE: there is no need to compile mixins at this point, some of them are not going to be //used and create snapshot operation is cheap now. It should be done later on. BodyCompilationResult compiled = resolveMixinReference(callerScope, fullMixin, mixinWorkingScope); @@ -183,7 +186,7 @@ public void run() { // filter out mixins we do not want to use List mixinsToBeUsed = defaultGuardHelper.chooseMixinsToBeUsed(compiledMixins, reference); - + // update mixin replacements and update scope with imported variables and mixins for (BodyCompilationResult compiled : mixinsToBeUsed) { result.addMembers(compiled.getReplacement()); @@ -232,7 +235,8 @@ private void declarationsAreImportant(Body result) { } } - @Deprecated //FIXME: !!! evaluate need for this + @Deprecated + //FIXME: !!! evaluate need for this private static IScope calculateMixinsWorkingScope(IScope callerScope, IScope arguments, IScope mixinScope) { // add arguments IScope mixinDeclarationScope = mixinScope.getParent(); @@ -261,11 +265,12 @@ private static IScope calculateBodyWorkingScope(IScope callerScope, IScope argum // locally defined mixin does not require any other action boolean isLocallyDefined = bodyScope.seesLocalDataOf(callerScope); IScope parent = callerScope.getParent(); - while (isLocallyDefined && parent!=null) { - isLocallyDefined = bodyScope.seesLocalDataOf(parent);; + while (isLocallyDefined && parent != null) { + isLocallyDefined = bodyScope.seesLocalDataOf(parent); + ; parent = parent.getParent(); } - + if (isLocallyDefined) { return bodyScope; } @@ -274,10 +279,10 @@ private static IScope calculateBodyWorkingScope(IScope callerScope, IScope argum IScope result = ScopeFactory.createJoinedScopesView(callerScope, bodyScope); return result; } - + //FIXME !!!! refactor and clean, unify with references class ImportedScopeFilter implements ExpressionFilter { - + private final ExpressionEvaluator expressionEvaluator; private final IScope importTargetScope; @@ -296,9 +301,9 @@ public Expression apply(Expression input) { } private IScope apply(IScope input) { - if (input==null) + if (input == null) return importTargetScope; - + return constructImportedBodyScope(importTargetScope, input); } @@ -311,8 +316,9 @@ private ScopeView constructImportedBodyScope(IScope importTargetScope, IScope bo //FIXME !!!! unify with the above scope joiners // locally defined mixin does not require any other action IScope parent = importTargetScope.getParent(); - while (isLocalImport && parent!=null) { - isLocalImport = bodyToBeImportedScope.seesLocalDataOf(parent);; + while (isLocalImport && parent != null) { + isLocalImport = bodyToBeImportedScope.seesLocalDataOf(parent); + ; parent = parent.getParent(); } @@ -333,4 +339,3 @@ private ScopeView constructImportedBodyScope(IScope importTargetScope, IScope bo } } - diff --git a/src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-incompatible.css b/src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-as-argument.css similarity index 100% rename from src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-incompatible.css rename to src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-as-argument.css diff --git a/src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-incompatible.less b/src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-as-argument.less similarity index 93% rename from src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-incompatible.less rename to src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-as-argument.less index 9439f803..ab4efd87 100644 --- a/src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-incompatible.less +++ b/src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-as-argument.less @@ -1,11 +1,11 @@ -.mixin(@parameter: {default: default;}) { - @parameter(); -} - -.default-mixin-argument { - .mixin(); -} - -.custom-mixin-argument { - .mixin({custom: custom;}); +.mixin(@parameter: {default: default;}) { + @parameter(); +} + +.default-mixin-argument { + .mixin(); +} + +.custom-mixin-argument { + .mixin({custom: custom;}); } \ No newline at end of file diff --git a/src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-scoping.css b/src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-scoping.css index 2cf80e72..a4312624 100644 --- a/src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-scoping.css +++ b/src/test/resources/compile-basic-features/detached-rulesets/detached-rulesets-scoping.css @@ -1,8 +1,8 @@ .selector-direct { - color: #008000; + color: green; } .selector-indirect { - color: #ff0000; + color: red; } .multiple-imports .selector { scope: yes; diff --git a/src/test/resources/minitests/debug1.css b/src/test/resources/minitests/debug1.css index c196af3e..dba97a6f 100644 --- a/src/test/resources/minitests/debug1.css +++ b/src/test/resources/minitests/debug1.css @@ -1,3 +1,26 @@ -.wrap-selector { - visible-one: visible; +@media (orientation: portrait) { } +@media (orientation: portrait) and (min-size: 256) { + .my-selector { + background-color: black; + } +} +@media (orientation: portrait) and (max-size: 1028) { +} +@media (orientation: portrait) and (max-size: 1028) and (blah: 22) { +} +@media (orientation: portrait) and (max-size: 1028) and (blah: 22) and (min-size: 256) { + .triple-wrapped-mq { + triple: true; + } +} +@media (orientation: portrait) and (max-size: 1028) and (min-size: 256) { + .triple-wrapped-mq { + triple: true; + } +} +@media (orientation: portrait) and (min-size: 256) { + .triple-wrapped-mq { + triple: true; + } +} \ No newline at end of file diff --git a/src/test/resources/minitests/debug1.less b/src/test/resources/minitests/debug1.less index 247b4751..d35937b1 100644 --- a/src/test/resources/minitests/debug1.less +++ b/src/test/resources/minitests/debug1.less @@ -1,7 +1,26 @@ -@detached: { - property: mixin; -}; - -.img-responsive { - @detached(); +@my-ruleset: { + .my-selector { + @media (min-size:256) { //tv + background-color: black; + } + } + }; +@media (orientation:portrait) { + @my-ruleset(); + .wrap-media-mixin({ + @media (min-size:256) { //tv + .triple-wrapped-mq { + triple: true; + } + } + }); +} +.wrap-media-mixin(@ruleset) { + @media (max-size:1028) { //widescreen + @media (blah: 22) { //print + @ruleset(); + } + @ruleset(); + } + @ruleset(); }