Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added symbol signature #1712

Merged
merged 1 commit into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/subapp-server/lib/template-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 4 additions & 4 deletions packages/xarc-jsx-renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 9 additions & 3 deletions packages/xarc-jsx-renderer/src/JsxRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion packages/xarc-jsx-renderer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +43,7 @@ export function createElement(type: any, props: any, ...children: any[]) {
}

const element: Element = {
$$typeof: xarcJsxElement,
id: ELEMENT_ID++,
type,
children,
Expand Down
1 change: 1 addition & 0 deletions packages/xarc-jsx-renderer/src/symbols.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const xarcJsxElement = Symbol("xarc.jsx.element");
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
Require,
Literal,
Component,
RegisterTokenIds
RegisterTokenIds,
xarcJsxElement
} from "../../src";

const Template = () => {
Expand All @@ -32,4 +33,5 @@ const Template = () => {
);
};

Template.$$typeof = xarcJsxElement;
export default Template;
4 changes: 3 additions & 1 deletion packages/xarc-jsx-renderer/test/jsx-templates/test1.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -137,4 +137,6 @@ const Template = () => (
</IndexPage>
);

Template.$$typeof = xarcJsxElement;

export default Template;