Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[compiler, state] fix state comparison

* Added prefixer example
  • Loading branch information...
commit 5a4b3ad3a2cb44b9d2fc4d39014b9eea41312a7a 1 parent e63d4b2
@indutny indutny authored
View
32 examples/prefixer/compiled.js
@@ -0,0 +1,32 @@
+(function(exports) {
+ var __r0, __r1, __r3, __r4, __r6, __r7, __r9, __r10;
+ exports.apply = apply;
+ function apply() {
+ var __this = this;
+ var __t = this["node"];
+ if (__t === "right") {
+ return [ "right", ("", __r9 = __this["node"], __this["node"] = null, __r10 = __this["tree"], __this["tree"] = this["tree"], __r11 = apply.call(__this), __this["node"] = __r9, __this["tree"] = __r10, "", __r11) ];
+ return;
+ } else if (__t === "left") {
+ return [ "left", ("", __r6 = __this["node"], __this["node"] = null, __r7 = __this["tree"], __this["tree"] = this["tree"], __r8 = apply.call(__this), __this["node"] = __r6, __this["tree"] = __r7, "", __r8) ];
+ return;
+ } else {
+ if (!Array.isArray(this["tree"]) === false) {
+ return [ ("", __r0 = __this["node"], __this["node"] = "left", __r1 = __this["tree"], __this["tree"] = this["tree"][0], __r2 = apply.call(__this), __this["node"] = __r0, __this["tree"] = __r1, "", __r2), ("", __r3 = __this["node"], __this["node"] = "right", __r4 = __this["tree"], __this["tree"] = this["tree"][1], __r5 = apply.call(__this), __this["node"] = __r3, __this["tree"] = __r4, "", __r5) ];
+ return;
+ } else {
+ if (!true === false) {
+ return this["tree"];
+ return;
+ } else {
+ return $e.call(this, []);
+ }
+ }
+ }
+ }
+ function $e() {
+ throw new Error;
+ return;
+ }
+ return exports;
+})(typeof exports === "undefined" ? {} : exports);
View
47 examples/prefixer/source.xjst
@@ -0,0 +1,47 @@
+// Input:
+// {
+// "tree": [
+// [1, 2],
+// [3, 4]
+// ]
+// }
+//
+// Output:
+// [
+// [
+// 'left',
+// [
+// ['left', 1],
+// ['right', 2]
+// ]
+// ],
+// [
+// 'right',
+// [
+// ['left', 3],
+// ['right', 4]
+// ]
+// ]
+// ]
+//
+
+// Default template has lowest priority and will be called only
+// if all other hasn't matched against incoming data
+template(true) {
+ return this.tree;
+}
+
+template(Array.isArray(this.tree)) {
+ return [
+ apply({ node: 'left', tree: this.tree[0] }),
+ apply({ node: 'right', tree: this.tree[1] })
+ ];
+}
+
+template(this.node === 'left') {
+ return ['left', apply({ node: null, tree: this.tree })];
+}
+
+template(this.node === 'right') {
+ return ['right', apply({ node: null, tree: this.tree })];
+}
View
2  lib/xjst/compiler.js
@@ -375,7 +375,7 @@ exports.generate = function generate(ast, options) {
// Go through all collected applies
applies.forEach(function(apply) {
// If nothing was changed before .apply call
- if (utils.stringify(apply.node.state) === utils.stringify(apply.state)) {
+ if (apply.node.state.equalTo(apply.state)) {
// Skip this apply as it can't be optimized
return;
}
View
13 lib/xjst/state.js
@@ -96,6 +96,19 @@ State.prototype.clone = function clone() {
};
//
+// ### function equalTo (state)
+// #### @state {State} target state
+// Returns true if states are equal
+//
+State.prototype.equalTo = function equalTo(state) {
+ var source = this.states,
+ target = state.states;
+
+ return utils.stringify(source.high) === utils.stringify(target.high) &&
+ utils.stringify(source.low) === utils.stringify(target.low);
+};
+
+//
// ### function merge (source)
// #### @source {State} State to merge with
// Updates state with information in @source
Please sign in to comment.
Something went wrong with that request. Please try again.