From 5e76e0197a6f267a334cdb1a7e0123bd4d865d94 Mon Sep 17 00:00:00 2001 From: divyakarippath Date: Fri, 31 Jul 2020 21:17:42 -0700 Subject: [PATCH] ensure the HTML jsx template used xarc's createElement (#1712) --- packages/subapp-server/lib/template-routing.js | 4 ++-- packages/xarc-jsx-renderer/package.json | 8 ++++---- packages/xarc-jsx-renderer/src/JsxRenderer.ts | 12 +++++++++--- packages/xarc-jsx-renderer/src/index.ts | 5 ++++- packages/xarc-jsx-renderer/src/symbols.ts | 1 + .../test/jsx-templates/test-register-token-ids.tsx | 4 +++- .../xarc-jsx-renderer/test/jsx-templates/test1.tsx | 4 +++- 7 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 packages/xarc-jsx-renderer/src/symbols.ts diff --git a/packages/subapp-server/lib/template-routing.js b/packages/subapp-server/lib/template-routing.js index c1919d4742..75ba5ea095 100644 --- a/packages/subapp-server/lib/template-routing.js +++ b/packages/subapp-server/lib/template-routing.js @@ -3,7 +3,7 @@ const _ = require("lodash"); const Path = require("path"); const { TagRenderer } = require("@xarc/tag-renderer"); -const { JsxRenderer } = require("@xarc/jsx-renderer"); +const { JsxRenderer, xarcJsxElement } = require("@xarc/jsx-renderer"); const { loadTokenModuleHandler } = require("@xarc/render-context"); const { utils: { @@ -56,7 +56,7 @@ function initializeTemplate( const templateModule = require(templateFullPath); // eslint-disable-line const template = _.get(templateModule, "default", templateModule); - if (template.children) { + if (template.$$typeof === xarcJsxElement || template.children) { // JSX asyncTemplate = new JsxRenderer({ templateFullPath: Path.dirname(templateFullPath), diff --git a/packages/xarc-jsx-renderer/package.json b/packages/xarc-jsx-renderer/package.json index ca14ff2e38..566510f88d 100644 --- a/packages/xarc-jsx-renderer/package.json +++ b/packages/xarc-jsx-renderer/package.json @@ -5,10 +5,10 @@ "main": "dist/index.js", "scripts": { "build": "tsc", - "test": "clap test-only", - "coverage": "clap check", - "prepublishOnly": "clap -n build docs && clap check", - "docs": "clap xarc/docs" + "test": "xrun xarc/test-only", + "coverage": "xrun xarc/check", + "prepublishOnly": "xrun [[build, docs], xarc/check]", + "docs": "xrun xarc/docs" }, "publishConfig": { "access": "public" diff --git a/packages/xarc-jsx-renderer/src/JsxRenderer.ts b/packages/xarc-jsx-renderer/src/JsxRenderer.ts index 7eec16056e..0235ffd749 100644 --- a/packages/xarc-jsx-renderer/src/JsxRenderer.ts +++ b/packages/xarc-jsx-renderer/src/JsxRenderer.ts @@ -18,6 +18,7 @@ import { } from "@xarc/render-context"; import { omittedCloseTags, expandProps } from "./utils"; import { makeDefer, each } from "xaa"; +import { xarcJsxElement } from "./symbols"; /** * The JSX renderer @@ -177,6 +178,11 @@ export class JsxRenderer { } }; + assert( + element.$$typeof === xarcJsxElement, + "Invalid xarc jsx element. Please make sure the JSX pragma /* @jsx createElement */ is added in the template file" + ); + if (element.memoize) { context.output.add(`${element.memoize}\n`); } else if (element.tag) { @@ -187,9 +193,9 @@ export class JsxRenderer { context.output.add(`<${element.tag}${expandProps(element.props, context)}/>`); } } else if (!element.type) { - return handleElementResult( - element(element.props, context, { element, depth, output: context.output }) - ); + return handleElementResult( + element(element.props, context, { element, depth, output: context.output }) + ); } else if (element.Construct) { const inst = new element.Construct(element.props, context); return handleElementResult( diff --git a/packages/xarc-jsx-renderer/src/index.ts b/packages/xarc-jsx-renderer/src/index.ts index 4383b561a9..e37ad005c0 100644 --- a/packages/xarc-jsx-renderer/src/index.ts +++ b/packages/xarc-jsx-renderer/src/index.ts @@ -11,13 +11,15 @@ import { IndexPage } from "./tags/IndexPage"; import { Require } from "./tags/Require"; import { Literal } from "./tags/Literal"; import { RegisterTokenIds } from "./tags/RegisterTokenIds"; +import { xarcJsxElement } from "./symbols"; /** @ignore */ -export { Component, Token, IndexPage, Require, Literal, JsxRenderer, RegisterTokenIds }; +export { Component, Token, IndexPage, Require, Literal, JsxRenderer, RegisterTokenIds, xarcJsxElement }; let ELEMENT_ID = 0; export type Element = { + $$typeof: symbol; id: number; type: any; children: any; @@ -41,6 +43,7 @@ export function createElement(type: any, props: any, ...children: any[]) { } const element: Element = { + $$typeof: xarcJsxElement, id: ELEMENT_ID++, type, children, diff --git a/packages/xarc-jsx-renderer/src/symbols.ts b/packages/xarc-jsx-renderer/src/symbols.ts new file mode 100644 index 0000000000..8b4d2e9ca1 --- /dev/null +++ b/packages/xarc-jsx-renderer/src/symbols.ts @@ -0,0 +1 @@ +export const xarcJsxElement = Symbol("xarc.jsx.element"); diff --git a/packages/xarc-jsx-renderer/test/jsx-templates/test-register-token-ids.tsx b/packages/xarc-jsx-renderer/test/jsx-templates/test-register-token-ids.tsx index b58311cd19..7eeb6fa902 100644 --- a/packages/xarc-jsx-renderer/test/jsx-templates/test-register-token-ids.tsx +++ b/packages/xarc-jsx-renderer/test/jsx-templates/test-register-token-ids.tsx @@ -7,7 +7,8 @@ import { Require, Literal, Component, - RegisterTokenIds + RegisterTokenIds, + xarcJsxElement } from "../../src"; const Template = () => { @@ -32,4 +33,5 @@ const Template = () => { ); }; +Template.$$typeof = xarcJsxElement; export default Template; diff --git a/packages/xarc-jsx-renderer/test/jsx-templates/test1.tsx b/packages/xarc-jsx-renderer/test/jsx-templates/test1.tsx index 19b37f0830..922b7b46a7 100644 --- a/packages/xarc-jsx-renderer/test/jsx-templates/test1.tsx +++ b/packages/xarc-jsx-renderer/test/jsx-templates/test1.tsx @@ -1,6 +1,6 @@ /* @jsx createElement */ -import { IndexPage, createElement, Token, Require, Literal, Component } from "../../src"; +import { IndexPage, createElement, Token, Require, Literal, Component, xarcJsxElement } from "../../src"; const getBogelFontUrl = () => { return "bogel"; @@ -137,4 +137,6 @@ const Template = () => ( ); +Template.$$typeof = xarcJsxElement; + export default Template;