@htmlplus/element 0.4.3 → 0.4.4

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,4 +1,5 @@
1
1
  import t from '@babel/types';
2
+ import { pascalCase } from 'change-case';
2
3
  import * as CONSTANTS from '../../constants/index.js';
3
4
  import { print, visitor } from '../utils/index.js';
4
5
  const defaults = {
@@ -116,13 +117,32 @@ export const customElement = (options) => {
116
117
  if (options === null || options === void 0 ? void 0 : options.typings) {
117
118
  visitor(ast, {
118
119
  Program(path) {
120
+ path.node.body.push(t.exportNamedDeclaration(t.tsInterfaceDeclaration(
121
+ // TODO
122
+ t.identifier(context.componentClassName + 'JSX'), null, [], t.tsInterfaceBody([
123
+ ...context.classProperties.map((property) => Object.assign(t.tSPropertySignature(property.key, property.typeAnnotation), {
124
+ optional: property.optional,
125
+ leadingComments: t.cloneNode(property, true).leadingComments
126
+ })),
127
+ ...context.classEvents.map((event) => {
128
+ var _a, _b;
129
+ return Object.assign(t.tSPropertySignature(t.identifier('on' + pascalCase(event.key['name'])), t.tsTypeAnnotation(t.tsFunctionType(undefined, [
130
+ Object.assign({}, t.identifier('event'), {
131
+ 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))
132
+ })
133
+ ], t.tsTypeAnnotation(t.tsVoidKeyword())))), {
134
+ optional: true,
135
+ leadingComments: t.cloneNode(event, true).leadingComments
136
+ });
137
+ })
138
+ ]))));
119
139
  path.node.body.push(Object.assign(t.tsModuleDeclaration(t.identifier('global'), t.tsModuleBlock([
120
140
  t.tsInterfaceDeclaration(t.identifier(context.componentInterfaceName), null, [
121
141
  t.tSExpressionWithTypeArguments(t.identifier('HTMLElement')) // TODO
122
142
  ], t.tsInterfaceBody([
123
143
  ...context.classProperties.map((property) => Object.assign(t.tSPropertySignature(property.key, property.typeAnnotation), {
124
144
  optional: property.optional,
125
- leadingComments: property.leadingComments
145
+ leadingComments: t.cloneNode(property, true).leadingComments
126
146
  }))
127
147
  ])),
128
148
  t.variableDeclaration('var', [
@@ -135,30 +155,25 @@ export const customElement = (options) => {
135
155
  ]),
136
156
  t.tsInterfaceDeclaration(t.identifier('HTMLElementTagNameMap'), null, [], t.tsInterfaceBody([
137
157
  t.tSPropertySignature(t.stringLiteral(context.componentTag), t.tSTypeAnnotation(t.tSIntersectionType([t.tSTypeReference(t.identifier(context.componentInterfaceName))])))
158
+ ])),
159
+ t.tSModuleDeclaration(t.identifier('JSX'), t.tSModuleBlock([
160
+ t.tSInterfaceDeclaration(t.identifier('IntrinsicElements'), undefined, undefined, t.tSInterfaceBody([
161
+ t.tSPropertySignature(t.stringLiteral(context.componentTag), t.tSTypeAnnotation(t.tSIntersectionType([
162
+ t.tSTypeReference(t.identifier(context.componentClassName + 'JSX')),
163
+ t.tSTypeLiteral([
164
+ t.tSIndexSignature([
165
+ Object.assign({}, t.identifier('key'), {
166
+ typeAnnotation: t.tSTypeAnnotation(t.tSStringKeyword())
167
+ })
168
+ ], t.tSTypeAnnotation(t.tSAnyKeyword()))
169
+ ])
170
+ ])))
171
+ ]))
138
172
  ]))
139
173
  ])), {
140
174
  declare: true,
141
175
  global: true
142
176
  }));
143
- path.node.body.push(t.exportNamedDeclaration(t.tsInterfaceDeclaration(
144
- // TODO
145
- t.identifier(context.componentClassName + 'JSX'), null, [], t.tsInterfaceBody([
146
- ...context.classProperties.map((property) => Object.assign(t.tSPropertySignature(property.key, property.typeAnnotation), {
147
- optional: property.optional,
148
- leadingComments: property.leadingComments
149
- })),
150
- ...context.classEvents.map((event) => {
151
- var _a, _b;
152
- return Object.assign(t.tSPropertySignature(event.key, t.tsTypeAnnotation(t.tsFunctionType(undefined, [
153
- Object.assign({}, t.identifier('event'), {
154
- 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))
155
- })
156
- ], t.tsTypeAnnotation(t.tsVoidKeyword())))), {
157
- optional: true,
158
- leadingComments: event.leadingComments
159
- });
160
- })
161
- ]))));
162
177
  }
163
178
  });
164
179
  }
@@ -1,8 +1,11 @@
1
+ import { pascalCase } from 'change-case';
1
2
  import { __dirname, isDirectoryEmpty, renderTemplate } from '../../utils/index.js';
2
3
  const defaults = {
3
4
  compact: false,
4
5
  destination: '',
5
- eventName: undefined,
6
+ eventName(eventName) {
7
+ return eventName;
8
+ },
6
9
  importerComponent(context) {
7
10
  return `YOUR_CORE_PACKAGE_NAME#${context.componentClassName}`;
8
11
  },
@@ -36,9 +39,10 @@ export const customElementReact = (options) => {
36
39
  };
37
40
  };
38
41
  const classEvents = context.classEvents.map((classEvent) => {
39
- var _a;
40
- const name = ((_a = options.eventName) === null || _a === void 0 ? void 0 : _a.call(options, classEvent.key.name)) || classEvent.key.name;
41
- return Object.assign(Object.assign({}, classEvent), { converted: 'on' + name.charAt(0).toUpperCase() + name.slice(1) });
42
+ const from = 'on' + pascalCase(classEvent.key.name);
43
+ const to = options.eventName(from);
44
+ return Object.assign(Object.assign({}, classEvent), { from,
45
+ to });
42
46
  });
43
47
  const fileName = context.fileName;
44
48
  const importerComponent = parse(options.importerComponent(context));
@@ -11,7 +11,7 @@ type Rename<T, R extends { [K in keyof R]: K extends keyof T ? PropertyKey : "Er
11
11
 
12
12
  type Renamed = Rename<{{importerComponentType.variable}}, {
13
13
  {{#each classEvents}}
14
- {{key.name}}: '{{converted}}',
14
+ {{from}}: '{{to}}',
15
15
  {{/each}}
16
16
  }>
17
17
 
@@ -7,3 +7,4 @@ export * from './parse.js';
7
7
  export * from './read.js';
8
8
  export * from './style.js';
9
9
  export * from './validate.js';
10
+ export * from './webTypes.js';
@@ -7,3 +7,4 @@ export * from './parse.js';
7
7
  export * from './read.js';
8
8
  export * from './style.js';
9
9
  export * from './validate.js';
10
+ export * from './webTypes.js';
@@ -0,0 +1,11 @@
1
+ import { Global } from '../../types/index.js';
2
+ export interface WebTypesOptions {
3
+ destination: string;
4
+ docUrl: () => string;
5
+ packageName: string;
6
+ packageVersion: string;
7
+ }
8
+ export declare const webTypes: (options: WebTypesOptions) => {
9
+ name: string;
10
+ finish: (global: Global) => void;
11
+ };
@@ -0,0 +1,82 @@
1
+ import { camelCase, paramCase } from 'change-case';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import { getTags, print } from '../utils/index.js';
5
+ export const webTypes = (options) => {
6
+ const name = 'webTypes';
7
+ const finish = (global) => {
8
+ const json = {
9
+ '$schema': 'http://json.schemastore.org/web-types',
10
+ 'name': options.packageName,
11
+ 'version': options.packageVersion,
12
+ 'description-markup': 'markdown',
13
+ 'framework-config': {
14
+ 'enable-when': {
15
+ 'node-packages': [options.packageName]
16
+ }
17
+ },
18
+ 'contributions': {
19
+ html: {
20
+ elements: global.contexts
21
+ .sort((a, b) => ((a.componentTag || '') > (b.componentTag || '') ? 1 : -1))
22
+ .map((context) => {
23
+ var _a, _b;
24
+ return ({
25
+ 'name': context.componentTag,
26
+ 'description': '',
27
+ 'doc-url': options.docUrl(),
28
+ 'js': {
29
+ properties: [
30
+ ...(context.classProperties || []).map((property) => {
31
+ var _a, _b;
32
+ return ({
33
+ name: camelCase(property.key['name']),
34
+ description: (_a = getTags(property).find((tag) => !tag.key)) === null || _a === void 0 ? void 0 : _a.value,
35
+ value: {
36
+ type: print((_b = property.typeAnnotation) === null || _b === void 0 ? void 0 : _b['typeAnnotation'])
37
+ }
38
+ });
39
+ }),
40
+ ...(context.classMethods || []).map((method) => {
41
+ var _a;
42
+ return ({
43
+ name: camelCase(method.key['name']),
44
+ description: (_a = getTags(method).find((tag) => !tag.key)) === null || _a === void 0 ? void 0 : _a.value,
45
+ value: {}
46
+ });
47
+ })
48
+ ],
49
+ events: (_a = context.classEvents) === null || _a === void 0 ? void 0 : _a.map((event) => {
50
+ var _a;
51
+ return ({
52
+ name: paramCase(event.key['name']),
53
+ description: (_a = getTags(event).find((tag) => !tag.key)) === null || _a === void 0 ? void 0 : _a.value
54
+ });
55
+ })
56
+ },
57
+ 'attributes': (_b = context.classProperties) === null || _b === void 0 ? void 0 : _b.map((property) => {
58
+ var _a, _b;
59
+ return ({
60
+ name: paramCase(property.key['name']),
61
+ description: (_a = getTags(property).find((tag) => !tag.key)) === null || _a === void 0 ? void 0 : _a.value,
62
+ value: {
63
+ type: print((_b = property.typeAnnotation) === null || _b === void 0 ? void 0 : _b['typeAnnotation'])
64
+ }
65
+ });
66
+ })
67
+ });
68
+ })
69
+ }
70
+ }
71
+ };
72
+ const dirname = path.dirname(options.destination);
73
+ if (!fs.existsSync(dirname))
74
+ fs.mkdirSync(dirname, { recursive: true });
75
+ const raw = JSON.stringify(json, null, 2);
76
+ fs.writeFileSync(options.destination, raw, 'utf8');
77
+ };
78
+ return {
79
+ name,
80
+ finish
81
+ };
82
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htmlplus/element",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "license": "MIT",
5
5
  "author": "Masood Abdolian <m.abdolian@gmail.com>",
6
6
  "description": "Compiler of HTMLPlus",