Skip to content

Commit

Permalink
allow views in nested parens
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Jul 2, 2019
1 parent 392dc0d commit 4dc9630
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 93 deletions.
2 changes: 1 addition & 1 deletion build/test/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ const compile = require("../../packages/moon-compiler/dist/moon-compiler.min.js"

module.exports = {
process(src) {
return babel.transformSync(compile(src), {});
return babel.transformSync(compile(src), { sourceMaps: true });
}
};
53 changes: 22 additions & 31 deletions packages/moon-browser/dist/moon-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@
attributeValue += charAttribute;

for (j++; j < attributesText.length; j++) {
var _charString = attributesText[j]; // Add everything inside the string to the attribute
// Add everything inside the string to the attribute
// value.

var _charString = attributesText[j];
attributeValue += _charString;

if (isQuote(_charString, attributesText[j - 1]) && _charString === charAttribute) {
Expand Down Expand Up @@ -1045,12 +1045,12 @@
for (var i = 0; i < input.length;) {
var _char = input[i];

if (_char === "(") {
if (_char === "(" && input[i + 1] === "<") {
// Skip over the parenthesis.
output += _char;
i += 1; // Record the expression.
i += 1; // Record the view.

var expression = ""; // Store opened parentheses.
var view = ""; // Store opened parentheses.

var opened = 0;

Expand All @@ -1061,12 +1061,12 @@
break;
} else if (isQuote(_char2, input[i - 1])) {
// Skip over strings.
expression += _char2;
view += _char2;

for (i++; i < input.length; i++) {
var charString = input[i]; // Add the string contents to the output.

expression += charString;
view += charString;

if (isQuote(charString, input[i - 1]) && charString === _char2) {
// Skip over the closing quote.
Expand All @@ -1082,35 +1082,26 @@
opened -= 1;
}

expression += _char2;
view += _char2;
i += 1;
}
} // Remove surrounding whitespace.


expression = expression.trim();
}

if (expression[0] === "<") {
// If it is a Moon view, then lex, parse, and generate code for it.
var staticParts = [];
var staticPartsMap = {};
var result = generate(parse(lex(expression)), null, 0, variable, staticParts, staticPartsMap);
variable = result.variable;
var staticParts = [];
var staticPartsMap = {};
var result = generate(parse(lex(view)), null, 0, variable, staticParts, staticPartsMap);
variable = result.variable;

if (result.isStatic) {
// Generate a static output.
var staticPart = generateStaticPart(result.prelude, result.node, variable, staticParts, staticPartsMap);
variable = staticPart.variable;
output += "(function(){if(" + staticPart.variableStatic + "===undefined){" + staticParts[0].variablePart + "}return " + staticPart.variableStatic + ";})()";
} else {
// Add the prelude to the last seen block and the node in place of the expression.
output += "(function(){" + (staticParts.length === 0 ? "" : "if(" + staticParts[0].variableStatic + "===undefined){" + staticParts.map(function (staticPart) {
return staticPart.variablePart;
}).join("") + "}") + result.prelude + "return " + result.node + ";})()";
}
if (result.isStatic) {
// Generate a static output.
var staticPart = generateStaticPart(result.prelude, result.node, variable, staticParts, staticPartsMap);
variable = staticPart.variable;
output += "(function(){if(" + staticPart.variableStatic + "===undefined){" + staticParts[0].variablePart + "}return " + staticPart.variableStatic + ";})()";
} else {
// If not, then add it to the output as a normal expression.
output += expression;
// Add the prelude to the last seen block and the node in place of the expression.
output += "(function(){" + (staticParts.length === 0 ? "" : "if(" + staticParts[0].variableStatic + "===undefined){" + staticParts.map(function (staticPart) {
return staticPart.variablePart;
}).join("") + "}") + result.prelude + "return " + result.node + ";})()";
}
} else if (isQuote(_char, input[i - 1])) {
// If there is a string in the code, skip over it.
Expand Down
2 changes: 1 addition & 1 deletion packages/moon-browser/dist/moon-browser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4dc9630

Please sign in to comment.