-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert imperative rendering inside scriptlets to dynamic tags
- Loading branch information
1 parent
3e6b954
commit 2f042ef
Showing
9 changed files
with
356 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"transformer": "./root-transformer", | ||
"<assign>": { | ||
"transformer": "./assign-tag", | ||
"open-tag-only": true, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"use strict"; | ||
|
||
const OUT_IDENTIFIER_REG = /[(,] *out *[,)]/; | ||
const renderCallToDynamicTag = require("./util/renderCallToDynamicTag"); | ||
|
||
module.exports = function transform(el, context) { | ||
const walker = context.createWalker({ | ||
enter(node) { | ||
if ( | ||
node.type !== "Scriptlet" || | ||
!OUT_IDENTIFIER_REG.test(node.code) | ||
) { | ||
return; | ||
} | ||
|
||
const replacement = replaceScriptlets( | ||
context.builder.parseStatement(node.code), | ||
context | ||
); | ||
|
||
node.replaceWith(replacement); | ||
} | ||
}); | ||
walker.walk(el); | ||
}; | ||
|
||
function replaceScriptlets(node, context) { | ||
const builder = context.builder; | ||
if (!node.type) { | ||
if (node.replaceChild) { | ||
node.forEach(child => { | ||
const replacement = replaceScriptlets(child, context); | ||
if (child !== replacement) { | ||
node.replaceChild(replacement, child); | ||
} | ||
}); | ||
} else if (node.body) { | ||
node.body.forEach(child => { | ||
const replacement = replaceScriptlets(child, context); | ||
if (child !== replacement) { | ||
node.body.replaceChild(replacement, child); | ||
} | ||
}); | ||
} | ||
|
||
return node; | ||
} | ||
|
||
switch (node.type) { | ||
case "LogicalExpression": | ||
node = builder.ifStatement( | ||
node.operator === "&&" ? node.left : builder.negate(node.left), | ||
[replaceScriptlets(node.right, context)] | ||
); | ||
break; | ||
case "FunctionCall": | ||
node = renderCallToDynamicTag(node, context) || node; | ||
break; | ||
case "If": | ||
case "ElseIf": | ||
node.body = replaceScriptlets(node.body, context); | ||
if (node.else) { | ||
replaceScriptlets(node.else, context); | ||
} | ||
break; | ||
case "Else": | ||
case "ForStatement": | ||
case "WhileStatement": | ||
node.body = replaceScriptlets(node.body, context); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return node; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
module.exports = function renderCallToDynamicTag(ast, context) { | ||
const builder = context.builder; | ||
const args = ast.args; | ||
const callee = ast.callee; | ||
const argsLength = args.length; | ||
const outIndex = args.findIndex(arg => arg.name === "out"); | ||
const calleeProperty = callee.property && callee.property.name; | ||
|
||
if (outIndex === -1) { | ||
return false; | ||
} | ||
|
||
let tagName; | ||
let tagAttrs; | ||
|
||
if (argsLength <= 2) { | ||
if (outIndex === 0) { | ||
// Handles cases for the following: | ||
// 1. input.renderBody(out) --> <${input}/> | ||
// 2. input.renderThing(out) --> <${input.renderThing}/> | ||
// 3. input.renderBody(out, attrs) --> <${input} ...attrs/> | ||
// 4. renderBody(out) --> <${renderBody}/> | ||
if (argsLength === 2) { | ||
tagName = callee; | ||
tagAttrs = toAttributesOrSpread(args[1]); | ||
} | ||
|
||
// Removes `.renderBody` which is optional. | ||
if (calleeProperty === "renderBody") { | ||
tagName = callee.object; | ||
} else { | ||
tagName = callee; | ||
} | ||
} else if (outIndex === 1) { | ||
// Handles cases for the following: | ||
// 1. input.template.render({}, out) --> <${input.template} ...{}/> | ||
// 2. input.template.renderer({}, out) --> <${input.template} ...{}/> | ||
// 3. input.barRenderer({}, out) --> <${{ render:input.barRenderer }} ...{}/> | ||
|
||
tagAttrs = toAttributesOrSpread(args[0]); | ||
|
||
// Removes `.render` or `.renderer` which are optional. | ||
if (calleeProperty === "render" || calleeProperty === "renderer") { | ||
tagName = callee.object; | ||
} else { | ||
tagName = builder.objectExpression({ | ||
render: callee | ||
}); | ||
} | ||
} | ||
} else { | ||
// Handles worst case scenario: | ||
// 1. input.barRenderer({}, true, out) --> <${(out) => input.barRenderer({}, true, out)}/> | ||
tagName = builder.functionDeclaration( | ||
null, | ||
[builder.identifier("out")], | ||
[ast] | ||
); | ||
} | ||
|
||
return context.createNodeForEl(tagName, tagAttrs, null, true, true); | ||
}; | ||
|
||
function toAttributesOrSpread(val) { | ||
if ( | ||
!val || | ||
(val.type === "Literal" && val.value === null) || | ||
(val.type === "Identifier" && val.name === "undefined") | ||
) { | ||
return []; | ||
} | ||
|
||
if ( | ||
val.type === "ObjectExpression" && | ||
val.properties.every(prop => !prop.computed) | ||
) { | ||
return val.properties.map(prop => ({ | ||
name: prop.literalKeyValue, | ||
value: prop.value | ||
})); | ||
} | ||
|
||
return [ | ||
{ | ||
value: val, | ||
spread: true | ||
} | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<invoke input.renderBody(out)/> | ||
<invoke input.renderBody(out) w-id="hi" x=1/> |
Oops, something went wrong.