@htmlplus/element 0.7.9 → 0.8.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.
@@ -10,15 +10,14 @@ export function Property(options) {
10
10
  }
11
11
  function set(next) {
12
12
  const previous = this[symbol];
13
- const parsed = toProperty(next, options === null || options === void 0 ? void 0 : options.type);
14
- if (parsed === previous)
13
+ if (next === previous)
15
14
  return;
16
- this[symbol] = parsed;
15
+ this[symbol] = next;
17
16
  request(this, name, previous, (skipped) => {
18
17
  if (!(options === null || options === void 0 ? void 0 : options.reflect) || skipped)
19
18
  return;
20
19
  target[CONSTANTS.API_LOCKED] = true;
21
- updateAttribute(host(this), name, parsed);
20
+ updateAttribute(host(this), name, next);
22
21
  target[CONSTANTS.API_LOCKED] = false;
23
22
  });
24
23
  }
@@ -33,7 +32,7 @@ export function Property(options) {
33
32
  return this[propertyKey];
34
33
  };
35
34
  const set = (input) => {
36
- this[propertyKey] = input;
35
+ this[propertyKey] = toProperty(input, options === null || options === void 0 ? void 0 : options.type);
37
36
  };
38
37
  // TODO: configurable
39
38
  defineProperty(element, propertyKey, { get, set, configurable: true });
@@ -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.classEvents.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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htmlplus/element",
3
- "version": "0.7.9",
3
+ "version": "0.8.1",
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",