Skip to content

Commit

Permalink
Merge pull request #98 from Ubugeeei/97-template-binding-by-with
Browse files Browse the repository at this point in the history
chapter: #97 📜 binding by with statement
  • Loading branch information
Ubugeeei committed Aug 13, 2023
2 parents 1fd63dc + 87d0049 commit b047ff4
Show file tree
Hide file tree
Showing 116 changed files with 1,291 additions and 448 deletions.
Binary file added book/images/state_is_not_defined.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ export const generate = ({
children: TemplateChildNode[];
}): string => {
return `return function render(_ctx) {
const { h } = ChibiVue;
return ${genNode(children[0])};
with (_ctx) {
const { h } = ChibiVue;
return ${genNode(children[0])};
}
}`;
};

Expand Down Expand Up @@ -46,7 +48,7 @@ const genProp = (prop: AttributeNode | DirectiveNode): string => {
case NodeTypes.DIRECTIVE: {
switch (prop.name) {
case "on":
return `${toHandlerKey(prop.arg)}: _ctx.${prop.exp}`;
return `${toHandlerKey(prop.arg)}: ${prop.exp}`;
default:
// TODO: other directives
throw new Error(`unexpected directive name. got "${prop.name}"`);
Expand All @@ -62,5 +64,5 @@ const genText = (text: TextNode): string => {
};

const genInterpolation = (node: InterpolationNode): string => {
return `_ctx.${node.content}`;
return `${node.content}`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ export const generate = ({
children: TemplateChildNode[];
}): string => {
return `return function render(_ctx) {
const { h } = ChibiVue;
return ${genNode(children[0])};
with (_ctx) {
const { h } = ChibiVue;
return ${genNode(children[0])};
}
}`;
};

Expand Down Expand Up @@ -46,7 +48,7 @@ const genProp = (prop: AttributeNode | DirectiveNode): string => {
case NodeTypes.DIRECTIVE: {
switch (prop.name) {
case "on":
return `${toHandlerKey(prop.arg)}: _ctx.${prop.exp}`;
return `${toHandlerKey(prop.arg)}: ${prop.exp}`;
default:
// TODO: other directives
throw new Error(`unexpected directive name. got "${prop.name}"`);
Expand All @@ -62,5 +64,5 @@ const genText = (text: TextNode): string => {
};

const genInterpolation = (node: InterpolationNode): string => {
return `_ctx.${node.content}`;
return `${node.content}`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ export const generate = ({
children: TemplateChildNode[];
}): string => {
return `return function render(_ctx) {
const { h } = ChibiVue;
return ${genNode(children[0])};
with (_ctx) {
const { h } = ChibiVue;
return ${genNode(children[0])};
}
}`;
};

Expand Down Expand Up @@ -46,7 +48,7 @@ const genProp = (prop: AttributeNode | DirectiveNode): string => {
case NodeTypes.DIRECTIVE: {
switch (prop.name) {
case "on":
return `${toHandlerKey(prop.arg)}: _ctx.${prop.exp}`;
return `${toHandlerKey(prop.arg)}: ${prop.exp}`;
default:
// TODO: other directives
throw new Error(`unexpected directive name. got "${prop.name}"`);
Expand All @@ -62,5 +64,5 @@ const genText = (text: TextNode): string => {
};

const genInterpolation = (node: InterpolationNode): string => {
return `_ctx.${node.content}`;
return `${node.content}`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,53 @@ export const generate = (
option: Required<CompilerOptions>
): string => {
return `${option.isBrowser ? "return " : ""}function render(_ctx) {
const { h } = ChibiVue;
return ${genNode(children[0])};
${option.isBrowser ? "with (_ctx) {" : ""}
const { h } = ChibiVue;
return ${genNode(children[0], option)};
${option.isBrowser ? "}" : ""}
}`;
};

const genNode = (node: TemplateChildNode): string => {
const genNode = (
node: TemplateChildNode,
option: Required<CompilerOptions>
): string => {
switch (node.type) {
case NodeTypes.ELEMENT:
return genElement(node);
return genElement(node, option);
case NodeTypes.TEXT:
return genText(node);
case NodeTypes.INTERPOLATION:
return genInterpolation(node);
return genInterpolation(node, option);
default:
return "";
}
};

const genElement = (el: ElementNode): string => {
const genElement = (
el: ElementNode,
option: Required<CompilerOptions>
): string => {
return `h("${el.tag}", {${el.props
.map((prop) => genProp(prop))
.join(", ")}}, [${el.children.map((it) => genNode(it)).join(", ")}])`;
.map((prop) => genProp(prop, option))
.join(", ")}}, [${el.children
.map((it) => genNode(it, option))
.join(", ")}])`;
};

const genProp = (prop: AttributeNode | DirectiveNode): string => {
const genProp = (
prop: AttributeNode | DirectiveNode,
option: Required<CompilerOptions>
): string => {
switch (prop.type) {
case NodeTypes.ATTRIBUTE:
return `${prop.name}: "${prop.value?.content}"`;
case NodeTypes.DIRECTIVE: {
switch (prop.name) {
case "on":
return `${toHandlerKey(prop.arg)}: _ctx.${prop.exp}`;
return `${toHandlerKey(prop.arg)}: ${
option.isBrowser ? "" : "_ctx."
}${prop.exp}`;
default:
// TODO: other directives
throw new Error(`unexpected directive name. got "${prop.name}"`);
Expand All @@ -65,6 +80,9 @@ const genText = (text: TextNode): string => {
return `\`${text.content}\``;
};

const genInterpolation = (node: InterpolationNode): string => {
return `_ctx.${node.content}`;
const genInterpolation = (
node: InterpolationNode,
option: Required<CompilerOptions>
): string => {
return `${option.isBrowser ? "" : "_ctx."}${node.content}`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,53 @@ export const generate = (
option: Required<CompilerOptions>
): string => {
return `${option.isBrowser ? "return " : ""}function render(_ctx) {
const { h } = ChibiVue;
return ${genNode(children[0])};
${option.isBrowser ? "with (_ctx) {" : ""}
const { h } = ChibiVue;
return ${genNode(children[0], option)};
${option.isBrowser ? "}" : ""}
}`;
};

const genNode = (node: TemplateChildNode): string => {
const genNode = (
node: TemplateChildNode,
option: Required<CompilerOptions>
): string => {
switch (node.type) {
case NodeTypes.ELEMENT:
return genElement(node);
return genElement(node, option);
case NodeTypes.TEXT:
return genText(node);
case NodeTypes.INTERPOLATION:
return genInterpolation(node);
return genInterpolation(node, option);
default:
return "";
}
};

const genElement = (el: ElementNode): string => {
const genElement = (
el: ElementNode,
option: Required<CompilerOptions>
): string => {
return `h("${el.tag}", {${el.props
.map((prop) => genProp(prop))
.join(", ")}}, [${el.children.map((it) => genNode(it)).join(", ")}])`;
.map((prop) => genProp(prop, option))
.join(", ")}}, [${el.children
.map((it) => genNode(it, option))
.join(", ")}])`;
};

const genProp = (prop: AttributeNode | DirectiveNode): string => {
const genProp = (
prop: AttributeNode | DirectiveNode,
option: Required<CompilerOptions>
): string => {
switch (prop.type) {
case NodeTypes.ATTRIBUTE:
return `${prop.name}: "${prop.value?.content}"`;
case NodeTypes.DIRECTIVE: {
switch (prop.name) {
case "on":
return `${toHandlerKey(prop.arg)}: _ctx.${prop.exp}`;
return `${toHandlerKey(prop.arg)}: ${
option.isBrowser ? "" : "_ctx."
}${prop.exp}`;
default:
// TODO: other directives
throw new Error(`unexpected directive name. got "${prop.name}"`);
Expand All @@ -65,6 +80,9 @@ const genText = (text: TextNode): string => {
return `\`${text.content}\``;
};

const genInterpolation = (node: InterpolationNode): string => {
return `_ctx.${node.content}`;
const genInterpolation = (
node: InterpolationNode,
option: Required<CompilerOptions>
): string => {
return `${option.isBrowser ? "" : "_ctx."}${node.content}`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,53 @@ export const generate = (
option: Required<CompilerOptions>
): string => {
return `${option.isBrowser ? "return " : ""}function render(_ctx) {
const { h } = ChibiVue;
return ${genNode(children[0])};
${option.isBrowser ? "with (_ctx) {" : ""}
const { h } = ChibiVue;
return ${genNode(children[0], option)};
${option.isBrowser ? "}" : ""}
}`;
};

const genNode = (node: TemplateChildNode): string => {
const genNode = (
node: TemplateChildNode,
option: Required<CompilerOptions>
): string => {
switch (node.type) {
case NodeTypes.ELEMENT:
return genElement(node);
return genElement(node, option);
case NodeTypes.TEXT:
return genText(node);
case NodeTypes.INTERPOLATION:
return genInterpolation(node);
return genInterpolation(node, option);
default:
return "";
}
};

const genElement = (el: ElementNode): string => {
const genElement = (
el: ElementNode,
option: Required<CompilerOptions>
): string => {
return `h("${el.tag}", {${el.props
.map((prop) => genProp(prop))
.join(", ")}}, [${el.children.map((it) => genNode(it)).join(", ")}])`;
.map((prop) => genProp(prop, option))
.join(", ")}}, [${el.children
.map((it) => genNode(it, option))
.join(", ")}])`;
};

const genProp = (prop: AttributeNode | DirectiveNode): string => {
const genProp = (
prop: AttributeNode | DirectiveNode,
option: Required<CompilerOptions>
): string => {
switch (prop.type) {
case NodeTypes.ATTRIBUTE:
return `${prop.name}: "${prop.value?.content}"`;
case NodeTypes.DIRECTIVE: {
switch (prop.name) {
case "on":
return `${toHandlerKey(prop.arg)}: _ctx.${prop.exp}`;
return `${toHandlerKey(prop.arg)}: ${
option.isBrowser ? "" : "_ctx."
}${prop.exp}`;
default:
// TODO: other directives
throw new Error(`unexpected directive name. got "${prop.name}"`);
Expand All @@ -65,6 +80,9 @@ const genText = (text: TextNode): string => {
return `\`${text.content}\``;
};

const genInterpolation = (node: InterpolationNode): string => {
return `_ctx.${node.content}`;
const genInterpolation = (
node: InterpolationNode,
option: Required<CompilerOptions>
): string => {
return `${option.isBrowser ? "" : "_ctx."}${node.content}`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,53 @@ export const generate = (
option: Required<CompilerOptions>
): string => {
return `${option.isBrowser ? "return " : ""}function render(_ctx) {
const { h } = ChibiVue;
return ${genNode(children[0])};
${option.isBrowser ? "with (_ctx) {" : ""}
const { h } = ChibiVue;
return ${genNode(children[0], option)};
${option.isBrowser ? "}" : ""}
}`;
};

const genNode = (node: TemplateChildNode): string => {
const genNode = (
node: TemplateChildNode,
option: Required<CompilerOptions>
): string => {
switch (node.type) {
case NodeTypes.ELEMENT:
return genElement(node);
return genElement(node, option);
case NodeTypes.TEXT:
return genText(node);
case NodeTypes.INTERPOLATION:
return genInterpolation(node);
return genInterpolation(node, option);
default:
return "";
}
};

const genElement = (el: ElementNode): string => {
const genElement = (
el: ElementNode,
option: Required<CompilerOptions>
): string => {
return `h("${el.tag}", {${el.props
.map((prop) => genProp(prop))
.join(", ")}}, [${el.children.map((it) => genNode(it)).join(", ")}])`;
.map((prop) => genProp(prop, option))
.join(", ")}}, [${el.children
.map((it) => genNode(it, option))
.join(", ")}])`;
};

const genProp = (prop: AttributeNode | DirectiveNode): string => {
const genProp = (
prop: AttributeNode | DirectiveNode,
option: Required<CompilerOptions>
): string => {
switch (prop.type) {
case NodeTypes.ATTRIBUTE:
return `${prop.name}: "${prop.value?.content}"`;
case NodeTypes.DIRECTIVE: {
switch (prop.name) {
case "on":
return `${toHandlerKey(prop.arg)}: _ctx.${prop.exp}`;
return `${toHandlerKey(prop.arg)}: ${
option.isBrowser ? "" : "_ctx."
}${prop.exp}`;
default:
// TODO: other directives
throw new Error(`unexpected directive name. got "${prop.name}"`);
Expand All @@ -65,6 +80,9 @@ const genText = (text: TextNode): string => {
return `\`${text.content}\``;
};

const genInterpolation = (node: InterpolationNode): string => {
return `_ctx.${node.content}`;
const genInterpolation = (
node: InterpolationNode,
option: Required<CompilerOptions>
): string => {
return `${option.isBrowser ? "" : "_ctx."}${node.content}`;
};
Loading

0 comments on commit b047ff4

Please sign in to comment.