nesquick 2.1.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NesquickComponent = void 0;
4
4
  const State_1 = require("./State");
5
+ const util_1 = require("./util");
5
6
  const SVGNamespaces = new Map([
6
7
  ["xlink", "http://www.w3.org/1999/xlink"],
7
8
  ["xml", "http://www.w3.org/XML/1998/namespace"]
@@ -21,21 +22,65 @@ function getAttributeNs(attributes, k) {
21
22
  }
22
23
  return null;
23
24
  }
24
- function getterFromFunctions(props) {
25
- const res = Object.create(null);
25
+ function getterFromFunctionsSingleChildren(props) {
26
+ const res = {};
26
27
  for (const k in props) {
27
- Object.defineProperty(res, k, {
28
- get() {
29
- return props[k]();
28
+ if (k === "children") {
29
+ const v = props.children;
30
+ if (typeof v === "function" && !(v instanceof NesquickComponent)) {
31
+ Object.defineProperty(res, "children", {
32
+ get() {
33
+ return v();
34
+ }
35
+ });
30
36
  }
31
- });
37
+ else {
38
+ res.children = v;
39
+ }
40
+ }
41
+ else {
42
+ const v = props[k];
43
+ Object.defineProperty(res, k, {
44
+ get() {
45
+ return v();
46
+ }
47
+ });
48
+ }
49
+ }
50
+ return res;
51
+ }
52
+ function getterFromFunctionsArrayChildren(props) {
53
+ const res = {};
54
+ for (const k in props) {
55
+ if (k === "children") {
56
+ res.children = props.children;
57
+ for (let i = 0; i < props.children.length; i++) {
58
+ const v = props.children[i];
59
+ if (typeof v === "function" && !(v instanceof NesquickComponent)) {
60
+ Object.defineProperty(res.children, i, {
61
+ get() {
62
+ return v();
63
+ }
64
+ });
65
+ }
66
+ }
67
+ }
68
+ else {
69
+ const v = props[k];
70
+ Object.defineProperty(res, k, {
71
+ get() {
72
+ return v();
73
+ }
74
+ });
75
+ }
32
76
  }
33
77
  return res;
34
78
  }
35
79
  class NesquickComponent {
36
- constructor(_render, props) {
80
+ constructor(_render, props, jsxs = false) {
37
81
  this._render = _render;
38
82
  this.props = props;
83
+ this.jsxs = jsxs;
39
84
  this._subscriptions = new State_1.Subscriptions();
40
85
  this._styleSubscriptions = null;
41
86
  this._xmlns = null;
@@ -45,7 +90,7 @@ class NesquickComponent {
45
90
  render(document) {
46
91
  State_1.subscriptions.set(this._subscriptions);
47
92
  if (typeof this._render === "function") {
48
- this.props = getterFromFunctions(this.props);
93
+ this.props = this.jsxs ? getterFromFunctionsArrayChildren(this.props) : getterFromFunctionsSingleChildren(this.props);
49
94
  const element = this._render(this.props);
50
95
  if (this._xmlns) {
51
96
  element.setXmlns(this._xmlns);
@@ -122,23 +167,23 @@ class NesquickComponent {
122
167
  this._renderStyle(element, props[k]);
123
168
  }
124
169
  else if (typeof props[k] === "function") {
125
- if (k.startsWith("on")) {
126
- element[k.toLowerCase()] = props[k];
170
+ const attribute = getAttributeNs(attributes, k);
171
+ if (attribute) {
172
+ (0, State_1.useRender)(props[k], v => {
173
+ element.setAttributeNS(attribute.namespace, attribute.name, String(v));
174
+ this._onUpdated();
175
+ });
176
+ }
177
+ else if ((0, util_1.isEvent)(k)) {
178
+ (0, State_1.useRender)(props[k], v => {
179
+ element[k.toLowerCase()] = v;
180
+ });
127
181
  }
128
182
  else {
129
- const attribute = getAttributeNs(attributes, k);
130
- if (attribute) {
131
- (0, State_1.useRender)(props[k], v => {
132
- element.setAttributeNS(attribute.namespace, attribute.name, String(v));
133
- this._onUpdated();
134
- });
135
- }
136
- else {
137
- (0, State_1.useRender)(props[k], v => {
138
- element.setAttribute(k, String(v));
139
- this._onUpdated();
140
- });
141
- }
183
+ (0, State_1.useRender)(props[k], v => {
184
+ element.setAttribute(k, String(v));
185
+ this._onUpdated();
186
+ });
142
187
  }
143
188
  }
144
189
  else {
@@ -161,8 +206,10 @@ class NesquickComponent {
161
206
  this._renderStyle(element, props[k]);
162
207
  }
163
208
  else if (typeof props[k] === "function") {
164
- if (k.startsWith("on")) {
165
- element[k.toLowerCase()] = props[k];
209
+ if ((0, util_1.isEvent)(k)) {
210
+ (0, State_1.useRender)(props[k], v => {
211
+ element[k.toLowerCase()] = v;
212
+ });
166
213
  }
167
214
  else {
168
215
  (0, State_1.useRender)(props[k], v => {
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NesquickFragment = void 0;
4
4
  const NesquickComponent_1 = require("./NesquickComponent");
5
5
  class NesquickFragment extends NesquickComponent_1.NesquickComponent {
6
- constructor(children) {
7
- super("", { children });
6
+ constructor(children, jsxs = false) {
7
+ super("", { children }, jsxs);
8
8
  this._lastNode = null;
9
9
  this._fragment = null;
10
10
  }
@@ -2,11 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.transformer = void 0;
4
4
  const TS = require("typescript");
5
+ const util_1 = require("../util");
5
6
  function getSingleIdentifier(node) {
6
7
  let identifier = null;
7
8
  node.forEachChild(node => {
8
9
  if (TS.isIdentifier(node)) {
9
10
  if (identifier != null) {
11
+ identifier = null;
10
12
  return node;
11
13
  }
12
14
  identifier = node;
@@ -53,6 +55,9 @@ function createSpreadCheckFunction(fName) {
53
55
  TS.factory.createReturnStatement(res)
54
56
  ], true));
55
57
  }
58
+ function arrowify(node) {
59
+ return TS.factory.createArrowFunction(void 0, void 0, [], void 0, TS.factory.createToken(TS.SyntaxKind.EqualsGreaterThanToken), node);
60
+ }
56
61
  const transformer = context => {
57
62
  return sourceFile => {
58
63
  let hasSpreadChecker = false;
@@ -67,7 +72,7 @@ const transformer = context => {
67
72
  return { node, hasCallExpression };
68
73
  };
69
74
  const visitGeneric = (node, options) => {
70
- let hasCallExpression = TS.isCallExpression(node);
75
+ let hasCallExpression = TS.isCallExpression(node) || TS.isElementAccessExpression(node) || TS.isPropertyAccessExpression(node);
71
76
  if (TS.isJsxSpreadAttribute(node)) {
72
77
  hasSpreadChecker = true;
73
78
  const expression = visitGeneric(node.expression, {}).node;
@@ -76,22 +81,16 @@ const transformer = context => {
76
81
  node = TS.factory.updateJsxSpreadAttribute(node, callExpression);
77
82
  }
78
83
  }
79
- else if (TS.isJsxElement(node)) {
80
- const firstLetter = node.openingElement.tagName.getText()[0];
81
- const userComponent = firstLetter !== firstLetter.toLowerCase();
82
- const res = processNode(node, { userComponent });
83
- node = res.node;
84
- }
85
- else if (TS.isJsxOpeningLikeElement(node)) {
86
- const firstLetter = node.tagName.getText()[0];
84
+ else if (TS.isJsxElement(node) || TS.isJsxSelfClosingElement(node)) {
85
+ const firstLetter = TS.isJsxSelfClosingElement(node) ? node.tagName.getText()[0] : node.openingElement.tagName.getText()[0];
87
86
  const userComponent = firstLetter !== firstLetter.toLowerCase();
88
87
  const res = processNode(node, { userComponent });
89
88
  node = res.node;
90
- hasCallExpression = hasCallExpression || res.hasCallExpression;
91
89
  }
92
90
  else if (TS.isJsxAttribute(node)) {
91
+ const propIsEvent = TS.isIdentifier(node.name) && (0, util_1.isEvent)(node.name.text);
93
92
  node = TS.visitEachChild(node, node => {
94
- const res = visitGeneric(node, { ...options, isJsxAttribute: true });
93
+ const res = visitGeneric(node, { ...options, isJsxAttribute: true, isEvent: propIsEvent });
95
94
  hasCallExpression = hasCallExpression || res.hasCallExpression;
96
95
  return res.node;
97
96
  }, context);
@@ -100,7 +99,7 @@ const transformer = context => {
100
99
  node = TS.visitEachChild(node, node => visitorExpression(node, { ...options, isJsxAttribute: false }), context);
101
100
  }
102
101
  else if (options.isJsxAttribute && TS.isStringLiteral(node)) {
103
- const returnNode = TS.visitNode(node, node => visitorExpression(node, { ...options, isJsxAttribute: false }), TS.isExpression);
102
+ const returnNode = TS.visitNode(node, node => visitorExpression(node, { ...options, isJsxAttribute: false, isEvent: false }), TS.isExpression);
104
103
  if (TS.isStringLiteral(returnNode)) {
105
104
  node = returnNode;
106
105
  }
@@ -108,13 +107,22 @@ const transformer = context => {
108
107
  node = TS.factory.createJsxExpression(void 0, returnNode);
109
108
  }
110
109
  }
110
+ else if (TS.isFunctionLike(node)) {
111
+ node = TS.visitEachChild(node, node => {
112
+ const res = visitGeneric(node, { ...options, isJsxAttribute: false, isEvent: false });
113
+ return res.node;
114
+ }, context);
115
+ }
111
116
  else {
112
117
  node = TS.visitEachChild(node, node => {
113
- const res = visitGeneric(node, { ...options, isJsxAttribute: false });
118
+ const res = visitGeneric(node, { ...options, isJsxAttribute: false, isEvent: false });
114
119
  hasCallExpression = hasCallExpression || res.hasCallExpression;
115
120
  return res.node;
116
121
  }, context);
117
122
  }
123
+ if (TS.isJsxChild(node) && !TS.isJsxText(node) && !TS.isJsxExpression(node) && options.userComponent) {
124
+ node = TS.factory.createJsxExpression(void 0, arrowify(node));
125
+ }
118
126
  return { node, hasCallExpression };
119
127
  };
120
128
  const visitorExpression = (node, options) => {
@@ -132,10 +140,8 @@ const transformer = context => {
132
140
  }
133
141
  const res = visitGeneric(node, {});
134
142
  node = res.node;
135
- if (TS.isExpression(node)) {
136
- if (options.userComponent || (res.hasCallExpression && !TS.isFunctionLike(node) && !TS.isJsxElement(node) && !TS.isJsxOpeningLikeElement(node))) {
137
- node = TS.factory.createArrowFunction(void 0, void 0, [], void 0, TS.factory.createToken(TS.SyntaxKind.EqualsGreaterThanToken), node);
138
- }
143
+ if (TS.isConciseBody(node) && (options.userComponent || res.hasCallExpression || options.isEvent)) {
144
+ node = arrowify(node);
139
145
  }
140
146
  return node;
141
147
  };
@@ -7,22 +7,16 @@ const NesquickComponent_1 = require("./NesquickComponent");
7
7
  const NesquickFragment_1 = require("./NesquickFragment");
8
8
  exports.Fragment = Symbol();
9
9
  function jsxs(type, props, key) {
10
- if (type === exports.Fragment) {
11
- return new NesquickFragment_1.NesquickFragment(props.children);
12
- }
13
- if (key !== undefined) {
14
- props.key = key;
15
- }
16
- return new NesquickComponent_1.NesquickComponent(type, props);
10
+ return jsx(type, props, key, true);
17
11
  }
18
- function jsx(type, props, key) {
12
+ function jsx(type, props, key, jsxs = false) {
19
13
  if (type === exports.Fragment) {
20
- return new NesquickFragment_1.NesquickFragment([props.children]);
14
+ return new NesquickFragment_1.NesquickFragment([props.children], jsxs);
21
15
  }
22
16
  if (key !== undefined) {
23
17
  props.key = key;
24
18
  }
25
- return new NesquickComponent_1.NesquickComponent(type, props);
19
+ return new NesquickComponent_1.NesquickComponent(type, props, jsxs);
26
20
  }
27
21
  var JSX;
28
22
  (function (JSX) {
@@ -2,8 +2,8 @@ export type Child = NesquickComponent<any> | NesquickFragment | string | boolean
2
2
  export type Children = Child | Child[];
3
3
  export type ChildFunc = () => Exclude<Child, ChildFunc> | Exclude<Child, ChildFunc>[];
4
4
  export type ComponentProps = Record<string, any>;
5
- export type FunctionComponent<P extends ComponentProps = {}> = (props: P) => NesquickComponent<P>;
6
- export type VeactDocument = Pick<Document, "createElement" | "createElementNS" | "createTextNode" | "createDocumentFragment" | "createComment">;
5
+ export type FunctionComponent<P extends ComponentProps = {}> = (props: P) => NesquickComponent<any>;
6
+ export type NesquickDocument = Pick<Document, "createElement" | "createElementNS" | "createTextNode" | "createDocumentFragment" | "createComment">;
7
7
  type NesquickChild = {
8
8
  node: Node | null;
9
9
  } & ({
@@ -25,25 +25,26 @@ type XmlNs = {
25
25
  export declare class NesquickComponent<P extends ComponentProps = {}> {
26
26
  private _render;
27
27
  protected props: P;
28
+ protected jsxs: boolean;
28
29
  private _subscriptions;
29
30
  private _styleSubscriptions;
30
31
  private _xmlns;
31
32
  private _onUpdate;
32
33
  protected _children: NesquickChild[];
33
- constructor(_render: string | FunctionComponent<P>, props: P);
34
- render(document: VeactDocument): Node;
34
+ constructor(_render: string | FunctionComponent<P>, props: P, jsxs?: boolean);
35
+ render(document: NesquickDocument): Node;
35
36
  setXmlns(xmlns: XmlNs | null): void;
36
37
  private _onUpdated;
37
38
  private _renderPropsNs;
38
39
  private _renderProps;
39
40
  private _renderStyles;
40
41
  private _renderStyle;
41
- protected _renderChildren(document: VeactDocument, parent: NesquickParent, children?: Children): void;
42
+ protected _renderChildren(document: NesquickDocument, parent: NesquickParent, children?: Children): void;
42
43
  protected _pushChild(): NesquickChild;
43
44
  protected _spliceChild(i: number): NesquickChild;
44
45
  protected _swapChilds(parent: NesquickParent, i1: number, i2: number): void;
45
46
  protected _removeChild(i: number): void;
46
- protected _renderChild(document: VeactDocument, parent: NesquickParent, nesquickChild: NesquickChild, child: Exclude<Child, ChildFunc> | Exclude<Child, ChildFunc>[]): void;
47
+ protected _renderChild(document: NesquickDocument, parent: NesquickParent, nesquickChild: NesquickChild, child: Exclude<Child, ChildFunc> | Exclude<Child, ChildFunc>[]): void;
47
48
  dispose(): void;
48
49
  }
49
50
  import { NesquickFragment } from "./NesquickFragment";
@@ -1,12 +1,12 @@
1
1
  import { JSX } from "./Nesquick";
2
- import { Child, NesquickComponent, NesquickParent, VeactDocument } from "./NesquickComponent";
2
+ import { Child, NesquickComponent, NesquickParent, NesquickDocument } from "./NesquickComponent";
3
3
  export declare class NesquickFragment extends NesquickComponent<{
4
4
  children: Child[];
5
5
  }> implements NesquickParent {
6
6
  private _lastNode;
7
7
  private _fragment;
8
- constructor(children: Child[]);
9
- render(document: VeactDocument): Node;
8
+ constructor(children: Child[], jsxs?: boolean);
9
+ render(document: NesquickDocument): Node;
10
10
  getDocument(): Document | null;
11
11
  getParent(): Node | null;
12
12
  appendChild(child: Node): void;
@@ -2,7 +2,7 @@ import { FunctionComponent, ComponentProps, NesquickComponent } from "./Nesquick
2
2
  import { NesquickFragment } from "./NesquickFragment";
3
3
  export declare const Fragment: unique symbol;
4
4
  export declare function jsxs<P extends ComponentProps>(type: string | FunctionComponent<P> | typeof Fragment, props: P, key?: string | number | null): NesquickFragment | NesquickComponent<P>;
5
- export declare function jsx<P extends ComponentProps>(type: string | FunctionComponent<P> | typeof Fragment, props: P, key?: string | number | null): NesquickFragment | NesquickComponent<P>;
5
+ export declare function jsx<P extends ComponentProps>(type: string | FunctionComponent<P> | typeof Fragment, props: P, key?: string | number | null, jsxs?: boolean): NesquickFragment | NesquickComponent<P>;
6
6
  export type Component<P extends Record<any, any> = {}> = (props: P) => JSX.Element;
7
7
  export declare namespace JSX {
8
8
  export type JSXEvent<T extends Event, T2 extends EventTarget> = T & {
@@ -0,0 +1 @@
1
+ export declare function isEvent(name: string): boolean;
package/lib/util.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isEvent = isEvent;
4
+ function isEvent(name) {
5
+ return name.startsWith("on") && name.length > 2 && name[2] === name[2].toUpperCase();
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nesquick",
3
- "version": "2.1.0",
3
+ "version": "2.2.1",
4
4
  "description": "React-like library with focus on drawing performance",
5
5
  "types": "./lib/types/index.d.ts",
6
6
  "main": "./lib/index.js",