Skip to content

Commit

Permalink
improve code generator to remove special directives
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Feb 19, 2017
1 parent dd25830 commit d1cfdd7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
15 changes: 10 additions & 5 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,11 +789,13 @@
// Special directive found that generates code after initial generation, push it to its known special directives to run afterGenerate later
if (specialDirectives[prop].afterGenerate) {
if (!vnode.specialDirectivesAfter) {
vnode.specialDirectivesAfter = [prop];
vnode.specialDirectivesAfter = {};
vnode.specialDirectivesAfter[prop] = props[prop];
} else {
vnode.specialDirectivesAfter.push(prop);
vnode.specialDirectivesAfter[prop] = props[prop];
}
}
// Invoke any special directives that need to change values before code generation
if (specialDirectives[prop].beforeGenerate) {
specialDirectives[prop].beforeGenerate(props[prop], vnode);
delete props[prop];
Expand All @@ -805,11 +807,13 @@
return "{}";
}

// It's a normal prop, generate code normally
if (props[prop]) {
generatedObject += '"' + prop + '": ' + compileTemplate(JSON.stringify(props[prop]), true) + ', ';
}
}

// Remove ending comma and space, close the generated object
generatedObject = generatedObject.slice(0, -2) + "}";

return generatedObject;
Expand Down Expand Up @@ -904,9 +908,10 @@
}
var compiledCode = createCall(el, childrenCode);
if (el.specialDirectivesAfter) {
for (var i = 0; i < el.specialDirectivesAfter.length; i++) {
var specialDirective = el.specialDirectivesAfter[i];
compiledCode = specialDirectives[specialDirective].afterGenerate(el.props[specialDirective], compiledCode, el);
// There are special directives that need to change the value after code generation, so
// run them now
for (var specialDirectiveAfter in el.specialDirectivesAfter) {
compiledCode = specialDirectives[specialDirectiveAfter].afterGenerate(el.specialDirectivesAfter[specialDirectiveAfter], compiledCode, el);
}
}
code += compiledCode;
Expand Down
Loading

0 comments on commit d1cfdd7

Please sign in to comment.