Skip to content

Commit

Permalink
[fix] handle null/undefined tag tokens (#1759)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip authored Nov 19, 2020
1 parent bc32222 commit c4d141d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/xarc-tag-renderer/src/render-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class RenderProcessor {

applyTokenModuleLoad(options, template: TagTemplate) {
for (const tokenModule of template._templateTags) {
if (tokenModule.load && typeof tokenModule.load === "function") {
if (typeof tokenModule?.load === "function") {
tokenModule.load({ ...this._renderer._handlerContext, ...options });
}
}
Expand Down Expand Up @@ -103,7 +103,9 @@ export class RenderProcessor {
const options = this._options;
const insertTokenIds = this._insertTokenIds;

if (tk[TAG_TYPE] === "function") {
if (!tk) {
opCode = null;
} else if (tk[TAG_TYPE] === "function") {
opCode = {
tk,
code: STEP_FUNC_HANDLER
Expand Down
4 changes: 2 additions & 2 deletions packages/xarc-tag-renderer/src/tag-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class TagTemplate {
processor: RenderProcessor;
}) {
this._templateTags = options.templateTags.map((tag, ix) => {
if (tag.hasOwnProperty(TAG_TYPE)) {
if (tag && tag.hasOwnProperty(TAG_TYPE)) {
tag.pos = ix;
} else if (typeof tag === "function") {
return { [TAG_TYPE]: "function", pos: ix, func: tag };
Expand Down Expand Up @@ -243,7 +243,7 @@ export class TagTemplate {

for (let index = 0; index < this._templateTags.length && found.length < count; index++) {
const token = this._templateTags[index];
if (token.id === id) {
if (token?.id === id) {
found.push({ index, token });
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/xarc-tag-renderer/test/data/template1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ export const templateTags = createTemplateTags`<html>
${Token("webapp-header-bundles")}
${Token("webapp-body-bundles")}
${Token("PAGE_TITLE")}
${""}
${Token("prefetch-bundles")}
${false}
${TokenInvoke(custom1)}
${undefined}
${context => {
return `hello world from function: ${Object.keys(context)}\n`;
}}
${null}
<script>
console.log("test");
</script>
Expand Down

0 comments on commit c4d141d

Please sign in to comment.