diff options
| author | Divy Srivastava <dj.srivastava23@gmail.com> | 2022-08-19 15:54:54 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-19 15:54:54 +0530 |
| commit | 25a109d9ea27ad3a76fdce14bba283e953af9bce (patch) | |
| tree | 68f0280065c9df4be8fa325ba82693879b4b46cd /cli/bench/testdata/npm/hono/dist/middleware/jsx | |
| parent | 9e576dff7c39cfd510c60ba92aa0d1c15fd24a6b (diff) | |
chore(bench): add flash router benchmarks (#15495)
Diffstat (limited to 'cli/bench/testdata/npm/hono/dist/middleware/jsx')
6 files changed, 240 insertions, 0 deletions
diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jsx/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/jsx/index.d.ts new file mode 100644 index 000000000..3222b2b36 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jsx/index.d.ts @@ -0,0 +1,26 @@ +import type { StringBuffer, HtmlEscaped, HtmlEscapedString } from '../../utils/html'; +declare global { + namespace jsx.JSX { + interface IntrinsicElements { + [tagName: string]: Record<string, any>; + } + } +} +declare type Child = string | number | JSXNode | Child[]; +export declare class JSXNode implements HtmlEscaped { + tag: string | Function; + props: Record<string, any>; + children: Child[]; + isEscaped: true; + constructor(tag: string | Function, props: Record<string, any>, children: Child[]); + toString(): string; + toStringToBuffer(buffer: StringBuffer): void; +} +export { jsxFn as jsx }; +declare const jsxFn: (tag: string | Function, props: Record<string, any>, ...children: (string | HtmlEscapedString)[]) => JSXNode; +declare type FC<T = Record<string, any>> = (props: T) => HtmlEscapedString; +export declare const memo: <T>(component: FC<T>, propsAreEqual?: (prevProps: Readonly<T>, nextProps: Readonly<T>) => boolean) => FC<T>; +export declare const Fragment: (props: { + key?: string; + children?: any; +}) => JSXNode; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jsx/index.js b/cli/bench/testdata/npm/hono/dist/middleware/jsx/index.js new file mode 100644 index 000000000..f72a7ecb9 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jsx/index.js @@ -0,0 +1,193 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Fragment = exports.memo = exports.jsx = exports.JSXNode = void 0; +const html_1 = require("../../utils/html"); +const emptyTags = [ + 'area', + 'base', + 'br', + 'col', + 'embed', + 'hr', + 'img', + 'input', + 'keygen', + 'link', + 'meta', + 'param', + 'source', + 'track', + 'wbr', +]; +const booleanAttributes = [ + 'allowfullscreen', + 'async', + 'autofocus', + 'autoplay', + 'checked', + 'controls', + 'default', + 'defer', + 'disabled', + 'formnovalidate', + 'hidden', + 'inert', + 'ismap', + 'itemscope', + 'loop', + 'multiple', + 'muted', + 'nomodule', + 'novalidate', + 'open', + 'playsinline', + 'readonly', + 'required', + 'reversed', + 'selected', +]; +const childrenToStringToBuffer = (children, buffer) => { + for (let i = 0, len = children.length; i < len; i++) { + const child = children[i]; + if (typeof child === 'string') { + (0, html_1.escapeToBuffer)(child, buffer); + } + else if (typeof child === 'boolean' || child === null || child === undefined) { + continue; + } + else if (child instanceof JSXNode) { + child.toStringToBuffer(buffer); + } + else if (typeof child === 'number' || child.isEscaped) { + buffer[0] += child; + } + else { + // `child` type is `Child[]`, so stringify recursively + childrenToStringToBuffer(child, buffer); + } + } +}; +class JSXNode { + constructor(tag, props, children) { + this.isEscaped = true; + this.tag = tag; + this.props = props; + this.children = children; + } + toString() { + const buffer = ['']; + this.toStringToBuffer(buffer); + return buffer[0]; + } + toStringToBuffer(buffer) { + const tag = this.tag; + const props = this.props; + let { children } = this; + buffer[0] += `<${tag}`; + const propsKeys = Object.keys(props || {}); + for (let i = 0, len = propsKeys.length; i < len; i++) { + const v = props[propsKeys[i]]; + if (typeof v === 'string') { + buffer[0] += ` ${propsKeys[i]}="`; + (0, html_1.escapeToBuffer)(v, buffer); + buffer[0] += '"'; + } + else if (typeof v === 'number') { + buffer[0] += ` ${propsKeys[i]}="${v}"`; + } + else if (v === null || v === undefined) { + // Do nothing + } + else if (typeof v === 'boolean' && booleanAttributes.includes(propsKeys[i])) { + if (v) { + buffer[0] += ` ${propsKeys[i]}=""`; + } + } + else if (propsKeys[i] === 'dangerouslySetInnerHTML') { + if (children.length > 0) { + throw 'Can only set one of `children` or `props.dangerouslySetInnerHTML`.'; + } + const escapedString = new String(v.__html); + escapedString.isEscaped = true; + children = [escapedString]; + } + else { + buffer[0] += ` ${propsKeys[i]}="`; + (0, html_1.escapeToBuffer)(v.toString(), buffer); + buffer[0] += '"'; + } + } + if (emptyTags.includes(tag)) { + buffer[0] += '/>'; + return; + } + buffer[0] += '>'; + childrenToStringToBuffer(children, buffer); + buffer[0] += `</${tag}>`; + } +} +exports.JSXNode = JSXNode; +class JSXFunctionNode extends JSXNode { + toStringToBuffer(buffer) { + const { children } = this; + const res = this.tag.call(null, { + ...this.props, + children: children.length <= 1 ? children[0] : children, + }); + if (res instanceof JSXNode) { + res.toStringToBuffer(buffer); + } + else if (typeof res === 'number' || res.isEscaped) { + buffer[0] += res; + } + else { + (0, html_1.escapeToBuffer)(res, buffer); + } + } +} +class JSXFragmentNode extends JSXNode { + toStringToBuffer(buffer) { + childrenToStringToBuffer(this.children, buffer); + } +} +const jsxFn = (tag, props, ...children) => { + if (typeof tag === 'function') { + return new JSXFunctionNode(tag, props, children); + } + else { + return new JSXNode(tag, props, children); + } +}; +exports.jsx = jsxFn; +const shallowEqual = (a, b) => { + if (a === b) { + return true; + } + const aKeys = Object.keys(a); + const bKeys = Object.keys(b); + if (aKeys.length !== bKeys.length) { + return false; + } + for (let i = 0, len = aKeys.length; i < len; i++) { + if (a[aKeys[i]] !== b[aKeys[i]]) { + return false; + } + } + return true; +}; +const memo = (component, propsAreEqual = shallowEqual) => { + let computed = undefined; + let prevProps = undefined; + return ((props) => { + if (prevProps && !propsAreEqual(prevProps, props)) { + computed = undefined; + } + prevProps = props; + return (computed || (computed = component(props))); + }); +}; +exports.memo = memo; +const Fragment = (props) => { + return new JSXFragmentNode('', {}, props.children || []); +}; +exports.Fragment = Fragment; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-dev-runtime.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-dev-runtime.d.ts new file mode 100644 index 000000000..d76fc7124 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-dev-runtime.d.ts @@ -0,0 +1,2 @@ +import type { JSXNode } from '.'; +export declare function jsxDEV(tag: string | Function, props: Record<string, any>): JSXNode; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-dev-runtime.js b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-dev-runtime.js new file mode 100644 index 000000000..6aa4bfa00 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-dev-runtime.js @@ -0,0 +1,10 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.jsxDEV = void 0; +const _1 = require("."); +function jsxDEV(tag, props) { + const children = props.children ?? []; + delete props['children']; + return (0, _1.jsx)(tag, props, children); +} +exports.jsxDEV = jsxDEV; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-runtime.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-runtime.d.ts new file mode 100644 index 000000000..4d505cdf8 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-runtime.d.ts @@ -0,0 +1,2 @@ +export { jsxDEV as jsx } from './jsx-dev-runtime'; +export { jsxDEV as jsxs } from './jsx-dev-runtime'; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-runtime.js b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-runtime.js new file mode 100644 index 000000000..ee8c3fe72 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-runtime.js @@ -0,0 +1,7 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.jsxs = exports.jsx = void 0; +var jsx_dev_runtime_1 = require("./jsx-dev-runtime"); +Object.defineProperty(exports, "jsx", { enumerable: true, get: function () { return jsx_dev_runtime_1.jsxDEV; } }); +var jsx_dev_runtime_2 = require("./jsx-dev-runtime"); +Object.defineProperty(exports, "jsxs", { enumerable: true, get: function () { return jsx_dev_runtime_2.jsxDEV; } }); |
