@htmlplus/element 0.7.8 → 0.8.0

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.
@@ -1,6 +1,6 @@
1
1
  import { camelCase, paramCase } from 'change-case';
2
2
  import * as CONSTANTS from '../../constants/index.js';
3
- import { call, fromAttribute, getConfig, getMembers, getNamespace, getTag, isServer, request } from '../utils/index.js';
3
+ import { call, getConfig, getMembers, getNamespace, getTag, isServer, request, toProperty } from '../utils/index.js';
4
4
  export function Element() {
5
5
  return function (constructor) {
6
6
  if (isServer())
@@ -38,7 +38,7 @@ export function Element() {
38
38
  return;
39
39
  const name = camelCase(attribute);
40
40
  const type = (_a = members[name]) === null || _a === void 0 ? void 0 : _a.type;
41
- const value = fromAttribute(next, type);
41
+ const value = toProperty(next, type);
42
42
  if (instance[name] === value)
43
43
  return;
44
44
  instance[name] = value;
@@ -1,5 +1,5 @@
1
1
  import * as CONSTANTS from '../../constants/index.js';
2
- import { addMember, appendToMethod, defineProperty, host, request, updateAttribute } from '../utils/index.js';
2
+ import { addMember, appendToMethod, defineProperty, host, request, toProperty, updateAttribute } from '../utils/index.js';
3
3
  export function Property(options) {
4
4
  return function (target, propertyKey) {
5
5
  const name = String(propertyKey);
@@ -32,7 +32,7 @@ export function Property(options) {
32
32
  return this[propertyKey];
33
33
  };
34
34
  const set = (input) => {
35
- this[propertyKey] = input;
35
+ this[propertyKey] = toProperty(input, options === null || options === void 0 ? void 0 : options.type);
36
36
  };
37
37
  // TODO: configurable
38
38
  defineProperty(element, propertyKey, { get, set, configurable: true });
@@ -7,7 +7,6 @@ export * from './config.js';
7
7
  export * from './defineProperty.js';
8
8
  export * from './direction.js';
9
9
  export * from './event.js';
10
- export * from './fromAttribute.js';
11
10
  export * from './getFramework.js';
12
11
  export * from './getMembers.js';
13
12
  export * from './getNamespace.js';
@@ -25,6 +24,7 @@ export * from './styles.js';
25
24
  export * from './task.js';
26
25
  export * from './toBoolean.js';
27
26
  export * from './toEvent.js';
27
+ export * from './toProperty.js';
28
28
  export * from './toUnit.js';
29
29
  export * from './typeOf.js';
30
30
  export * from './uhtml.js';
@@ -7,7 +7,6 @@ export * from './config.js';
7
7
  export * from './defineProperty.js';
8
8
  export * from './direction.js';
9
9
  export * from './event.js';
10
- export * from './fromAttribute.js';
11
10
  export * from './getFramework.js';
12
11
  export * from './getMembers.js';
13
12
  export * from './getNamespace.js';
@@ -25,6 +24,7 @@ export * from './styles.js';
25
24
  export * from './task.js';
26
25
  export * from './toBoolean.js';
27
26
  export * from './toEvent.js';
27
+ export * from './toProperty.js';
28
28
  export * from './toUnit.js';
29
29
  export * from './typeOf.js';
30
30
  export * from './uhtml.js';
@@ -0,0 +1 @@
1
+ export declare const toProperty: (input: any, type: number | undefined) => any;
@@ -1,6 +1,8 @@
1
1
  import * as CONSTANTS from '../../constants/index.js';
2
2
  import { typeOf } from './typeOf.js';
3
- export const fromAttribute = (input, type) => {
3
+ export const toProperty = (input, type) => {
4
+ if (type === undefined)
5
+ return input;
4
6
  const string = `${input}`;
5
7
  if (CONSTANTS.TYPE_BOOLEAN & type) {
6
8
  if (string === '')
@@ -1,7 +1,8 @@
1
+ import template from '@babel/template';
1
2
  import t from '@babel/types';
2
- import { pascalCase } from 'change-case';
3
+ import { camelCase, paramCase, pascalCase } from 'change-case';
3
4
  import * as CONSTANTS from '../../constants/index.js';
4
- import { addDependency, getType, print, visitor } from '../utils/index.js';
5
+ import { addDependency, getType, print, toEventTypeAnnotation, toPropertySignature, visitor } from '../utils/index.js';
5
6
  export const CUSTOM_ELEMENT_OPTIONS = {
6
7
  prefix: undefined,
7
8
  typings: true
@@ -165,11 +166,11 @@ export const customElement = (options) => {
165
166
  expressions.push(parts[i]);
166
167
  i += 1;
167
168
  }
168
- const template = t.templateLiteral(quasis, expressions);
169
+ const templateLiteral = t.templateLiteral(quasis, expressions);
169
170
  // TODO
170
171
  // if (!expressions.length) return template;
171
172
  const { local } = addDependency(path, CONSTANTS.UTILS_PATH, CONSTANTS.UTILS_HTML_LOCAL, CONSTANTS.UTILS_HTML_IMPORTED, true);
172
- return t.taggedTemplateExpression(t.identifier(local), template);
173
+ return t.taggedTemplateExpression(t.identifier(local), templateLiteral);
173
174
  };
174
175
  path.replaceWith(transform(render(path.node)));
175
176
  }
@@ -275,65 +276,66 @@ export const customElement = (options) => {
275
276
  if (options === null || options === void 0 ? void 0 : options.typings) {
276
277
  visitor(ast, {
277
278
  Program(path) {
278
- const { body } = path.node;
279
- body.push(t.exportNamedDeclaration(t.tsInterfaceDeclaration(
280
- // TODO
281
- t.identifier(context.className + 'JSX'), null, [], t.tsInterfaceBody([
282
- ...context.classProperties.map((property) => Object.assign(t.tSPropertySignature(property.key, property.typeAnnotation), {
283
- optional: property.optional,
284
- leadingComments: t.cloneNode(property, true).leadingComments
285
- })),
279
+ const jsx = [
280
+ ...context.classProperties.map((property) => {
281
+ return toPropertySignature(property);
282
+ }),
283
+ ...context.classProperties.map((event) => {
284
+ return toPropertySignature(event, {
285
+ optional: true,
286
+ keyTransformer: (key) => camelCase('on-' + key),
287
+ typeAnnotationTransformer: toEventTypeAnnotation
288
+ });
289
+ })
290
+ ];
291
+ const attributeJSX = [
292
+ ...context.classProperties.map((property) => {
293
+ return toPropertySignature(property, {
294
+ keyTransformer: (key) => paramCase(key)
295
+ });
296
+ }),
286
297
  ...context.classEvents.map((event) => {
287
- var _a, _b;
288
- return Object.assign(t.tSPropertySignature(t.identifier('on' + pascalCase(event.key['name'])), t.tsTypeAnnotation(t.tsFunctionType(undefined, [
289
- Object.assign({}, t.identifier('event'), {
290
- typeAnnotation: t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CustomEvent'), (_b = (_a = event.typeAnnotation) === null || _a === void 0 ? void 0 : _a['typeAnnotation']) === null || _b === void 0 ? void 0 : _b.typeParameters))
291
- })
292
- ], t.tsTypeAnnotation(t.tsVoidKeyword())))), {
298
+ return toPropertySignature(event, {
293
299
  optional: true,
294
- leadingComments: t.cloneNode(event, true).leadingComments
300
+ keyTransformer: (key) => 'on' + paramCase(key),
301
+ typeAnnotationTransformer: toEventTypeAnnotation
295
302
  });
296
303
  })
297
- ]))));
298
- body.push(Object.assign(t.tsModuleDeclaration(t.identifier('global'), t.tsModuleBlock([
299
- t.tsInterfaceDeclaration(t.identifier(componentInterfaceName), null, [
300
- t.tSExpressionWithTypeArguments(t.identifier('HTMLElement')) // TODO
301
- ], t.tsInterfaceBody([
302
- ...context.classProperties.map((property) => Object.assign(t.tSPropertySignature(property.key, property.typeAnnotation), {
303
- optional: property.optional,
304
- leadingComments: t.cloneNode(property, true).leadingComments
305
- }))
306
- ])),
307
- t.variableDeclaration('var', [
308
- t.variableDeclarator(Object.assign(t.identifier(componentInterfaceName), {
309
- typeAnnotation: t.tSTypeAnnotation(t.tSTypeLiteral([
310
- t.tSPropertySignature(t.identifier('prototype'), t.tsTypeAnnotation(t.tSTypeReference(t.identifier(componentInterfaceName)))),
311
- t.tSConstructSignatureDeclaration(null, [], t.tSTypeAnnotation(t.tSTypeReference(t.identifier(componentInterfaceName))))
312
- ]))
313
- }))
314
- ]),
315
- t.tsInterfaceDeclaration(t.identifier('HTMLElementTagNameMap'), null, [], t.tsInterfaceBody([
316
- t.tSPropertySignature(t.stringLiteral(tag), t.tSTypeAnnotation(t.tSIntersectionType([t.tSTypeReference(t.identifier(componentInterfaceName))])))
317
- ])),
318
- t.tSModuleDeclaration(t.identifier('JSX'), t.tSModuleBlock([
319
- t.tSInterfaceDeclaration(t.identifier('IntrinsicElements'), undefined, undefined, t.tSInterfaceBody([
320
- t.tSPropertySignature(t.stringLiteral(tag), t.tSTypeAnnotation(t.tSIntersectionType([
321
- t.tSTypeReference(t.identifier(context.className + 'JSX')),
322
- t.tSTypeLiteral([
323
- t.tSIndexSignature([
324
- Object.assign({}, t.identifier('key'), {
325
- typeAnnotation: t.tSTypeAnnotation(t.tSStringKeyword())
326
- })
327
- ], t.tSTypeAnnotation(t.tSAnyKeyword()))
328
- ])
329
- ])))
330
- ]))
331
- ]))
332
- ])), {
333
- declare: true,
334
- global: true
335
- }));
336
- body.push(t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(`${context.className}Element`), undefined, t.tsTypeReference(t.tsQualifiedName(t.identifier('globalThis'), t.identifier(componentInterfaceName))))));
304
+ ];
305
+ const globalInterface = context.classProperties.map((property) => {
306
+ return toPropertySignature(property);
307
+ });
308
+ const ast = template.default.ast(`
309
+ export interface ${context.className}JSX {
310
+ ${jsx.map(print).join('')}
311
+ }
312
+ export interface ${context.className}AttributeJSX {
313
+ ${attributeJSX.map(print).join('')}
314
+ }
315
+ declare global {
316
+ interface ${componentInterfaceName} extends HTMLElement {
317
+ ${globalInterface.map(print).join('')}
318
+ }
319
+ var ${componentInterfaceName}: {
320
+ prototype: ${componentInterfaceName};
321
+ new (): ${componentInterfaceName};
322
+ };
323
+ interface HTMLElementTagNameMap {
324
+ "${tag}": ${componentInterfaceName};
325
+ }
326
+ namespace JSX {
327
+ interface IntrinsicElements {
328
+ "${tag}": ${context.className}AttributeJSX & {
329
+ [key: string]: any;
330
+ };
331
+ }
332
+ }
333
+ }
334
+ export type ${context.className}Element = globalThis.${componentInterfaceName}
335
+ `, {
336
+ plugins: ['typescript']
337
+ });
338
+ path.node.body.push(...ast);
337
339
  }
338
340
  });
339
341
  }
@@ -1,4 +1,4 @@
1
- import * as t from '@babel/types';
1
+ import t from '@babel/types';
2
2
  import { paramCase } from 'change-case';
3
3
  import path from 'path';
4
4
  import * as CONSTANTS from '../../constants/index.js';
@@ -1,7 +1,7 @@
1
- import { File, ImportDeclaration } from '@babel/types';
1
+ import t from '@babel/types';
2
2
  interface AddDependencyReturns {
3
3
  local?: string;
4
- node: ImportDeclaration;
4
+ node: t.ImportDeclaration;
5
5
  }
6
- export declare function addDependency(path: File | any, source: string, local?: string, imported?: string, comment?: boolean): AddDependencyReturns;
6
+ export declare function addDependency(path: t.File | any, source: string, local?: string, imported?: string, comment?: boolean): AddDependencyReturns;
7
7
  export {};
@@ -1,2 +1,2 @@
1
- import { Expression } from '@babel/types';
2
- export declare const getInitializer: (node: Expression) => string | undefined;
1
+ import t from '@babel/types';
2
+ export declare const getInitializer: (node: t.Expression) => string | undefined;
@@ -1,2 +1,2 @@
1
- import { File, Node } from '@babel/types';
2
- export declare const getType: (directory: string, file: File, node: Node) => Node;
1
+ import t from '@babel/types';
2
+ export declare const getType: (directory: string, file: t.File, node: t.Node) => t.Node;
@@ -10,4 +10,6 @@ export * from './print.js';
10
10
  export * from './removeUnusedImport.js';
11
11
  export * from './renderTemplate.js';
12
12
  export * from './tags.js';
13
+ export * from './toEventTypeAnnotation.js';
14
+ export * from './toPropertySignature.js';
13
15
  export * from './visitor.js';
@@ -10,4 +10,6 @@ export * from './print.js';
10
10
  export * from './removeUnusedImport.js';
11
11
  export * from './renderTemplate.js';
12
12
  export * from './tags.js';
13
+ export * from './toEventTypeAnnotation.js';
14
+ export * from './toPropertySignature.js';
13
15
  export * from './visitor.js';
@@ -1,4 +1,4 @@
1
- import { Node } from '@babel/types';
1
+ import t from '@babel/types';
2
2
  export interface Tag {
3
3
  key?: string;
4
4
  value?: string;
@@ -7,7 +7,7 @@ export interface TagParsed {
7
7
  name?: string;
8
8
  description?: string;
9
9
  }
10
- export declare const getTag: (node: Node, key?: string) => Tag | undefined;
11
- export declare const getTags: (node: Node, filter?: string) => Array<Tag>;
12
- export declare const hasTag: (node: Node, name: string) => Boolean;
10
+ export declare const getTag: (node: t.Node, key?: string) => Tag | undefined;
11
+ export declare const getTags: (node: t.Node, filter?: string) => Array<Tag>;
12
+ export declare const hasTag: (node: t.Node, name: string) => Boolean;
13
13
  export declare const parseTag: (tag: Tag, spliter?: string) => TagParsed;
@@ -0,0 +1,2 @@
1
+ import t from '@babel/types';
2
+ export declare const toEventTypeAnnotation: (typeAnnotation: t.Noop | t.TSTypeAnnotation | t.TypeAnnotation | null | undefined) => t.TSTypeAnnotation;
@@ -0,0 +1,9 @@
1
+ import t from '@babel/types';
2
+ export const toEventTypeAnnotation = (typeAnnotation) => {
3
+ var _a;
4
+ return t.tsTypeAnnotation(t.tsFunctionType(undefined, [
5
+ Object.assign({}, t.identifier('event'), {
6
+ typeAnnotation: t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CustomEvent'), (_a = typeAnnotation === null || typeAnnotation === void 0 ? void 0 : typeAnnotation['typeAnnotation']) === null || _a === void 0 ? void 0 : _a.typeParameters))
7
+ })
8
+ ], t.tsTypeAnnotation(t.tsVoidKeyword())));
9
+ };
@@ -0,0 +1,7 @@
1
+ import t from '@babel/types';
2
+ export interface IToPropertySignatureOptions {
3
+ optional?: boolean;
4
+ keyTransformer?: (key: string) => string;
5
+ typeAnnotationTransformer?: (typeAnnotation: t.Noop | t.TSTypeAnnotation | t.TypeAnnotation | null | undefined) => t.TSTypeAnnotation | null | undefined;
6
+ }
7
+ export declare const toPropertySignature: (property: t.ClassProperty, options?: IToPropertySignatureOptions) => t.TSPropertySignature;
@@ -0,0 +1,10 @@
1
+ import t from '@babel/types';
2
+ export const toPropertySignature = (property, options) => {
3
+ var _a, _b, _c;
4
+ const key = property.key;
5
+ const typeAnnotation = property.typeAnnotation;
6
+ return Object.assign(t.tSPropertySignature(t.stringLiteral(((_a = options === null || options === void 0 ? void 0 : options.keyTransformer) === null || _a === void 0 ? void 0 : _a.call(options, key.name)) || key.name), ((_b = options === null || options === void 0 ? void 0 : options.typeAnnotationTransformer) === null || _b === void 0 ? void 0 : _b.call(options, typeAnnotation)) || typeAnnotation), {
7
+ optional: (_c = options === null || options === void 0 ? void 0 : options.optional) !== null && _c !== void 0 ? _c : property.optional,
8
+ leadingComments: t.cloneNode(property, true).leadingComments
9
+ });
10
+ };
@@ -38,7 +38,6 @@ export declare const TYPE_NUMBER: number;
38
38
  export declare const TYPE_OBJECT: number;
39
39
  export declare const TYPE_STRING: number;
40
40
  export declare const TYPE_UNDEFINED: number;
41
- export declare const TYPE_UNIT: number;
42
41
  export declare const UTILS_ATTRIBUTES_IMPORTED = "attributes";
43
42
  export declare const UTILS_ATTRIBUTES_LOCAL = "UTILS_ATTRIBUTES";
44
43
  export declare const UTILS_HOST_IMPORTED = "host";
@@ -46,7 +46,6 @@ export const TYPE_NUMBER = 2 ** 6;
46
46
  export const TYPE_OBJECT = 2 ** 7;
47
47
  export const TYPE_STRING = 2 ** 8;
48
48
  export const TYPE_UNDEFINED = 2 ** 9;
49
- export const TYPE_UNIT = 2 ** 10; // TODO
50
49
  // utils
51
50
  export const UTILS_ATTRIBUTES_IMPORTED = 'attributes';
52
51
  export const UTILS_ATTRIBUTES_LOCAL = 'UTILS_ATTRIBUTES';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htmlplus/element",
3
- "version": "0.7.8",
3
+ "version": "0.8.0",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "author": "Masood Abdolian <m.abdolian@gmail.com>",
@@ -34,6 +34,7 @@
34
34
  "dependencies": {
35
35
  "@babel/generator": "^7.20.4",
36
36
  "@babel/parser": "^7.20.3",
37
+ "@babel/template": "^7.22.15",
37
38
  "@babel/traverse": "^7.20.1",
38
39
  "@babel/types": "^7.20.2",
39
40
  "@types/node": "^18.11.9",
@@ -47,7 +48,6 @@
47
48
  "devDependencies": {
48
49
  "@trivago/prettier-plugin-sort-imports": "^4.0.0",
49
50
  "cpy": "^9.0.1",
50
- "nodemon": "^2.0.20",
51
51
  "prettier": "^2.8.0",
52
52
  "rimraf": "^3.0.2",
53
53
  "ts-node": "^10.9.1",
@@ -1 +0,0 @@
1
- export declare const fromAttribute: (input: any, type: any) => any;