@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.
- package/client/decorators/property.js +4 -5
- package/compiler/plugins/customElement.js +61 -59
- package/compiler/plugins/extract.js +1 -1
- package/compiler/utils/addDependency.d.ts +3 -3
- package/compiler/utils/getInitializer.d.ts +2 -2
- package/compiler/utils/getType.d.ts +2 -2
- package/compiler/utils/index.d.ts +2 -0
- package/compiler/utils/index.js +2 -0
- package/compiler/utils/tags.d.ts +4 -4
- package/compiler/utils/toEventTypeAnnotation.d.ts +2 -0
- package/compiler/utils/toEventTypeAnnotation.js +9 -0
- package/compiler/utils/toPropertySignature.d.ts +7 -0
- package/compiler/utils/toPropertySignature.js +10 -0
- package/package.json +2 -2
|
@@ -10,15 +10,14 @@ export function Property(options) {
|
|
|
10
10
|
}
|
|
11
11
|
function set(next) {
|
|
12
12
|
const previous = this[symbol];
|
|
13
|
-
|
|
14
|
-
if (parsed === previous)
|
|
13
|
+
if (next === previous)
|
|
15
14
|
return;
|
|
16
|
-
this[symbol] =
|
|
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,
|
|
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
|
|
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),
|
|
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
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
...context.
|
|
283
|
-
|
|
284
|
-
|
|
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
|
-
|
|
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
|
-
|
|
300
|
+
keyTransformer: (key) => 'on' + paramCase(key),
|
|
301
|
+
typeAnnotationTransformer: toEventTypeAnnotation
|
|
295
302
|
});
|
|
296
303
|
})
|
|
297
|
-
]
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
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,7 +1,7 @@
|
|
|
1
|
-
import
|
|
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
|
|
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
|
|
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';
|
package/compiler/utils/index.js
CHANGED
|
@@ -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';
|
package/compiler/utils/tags.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
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,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.
|
|
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",
|