@htmlplus/element 0.5.7 → 0.5.8

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,22 +1,29 @@
1
1
  import { camelCase, paramCase } from 'change-case';
2
2
  import * as CONSTANTS from '../../constants/index.js';
3
- import { call, getMembers, isServer, parseValue, request } from '../utils/index.js';
3
+ import { call, fromAttribute, getMembers, isServer, request } from '../utils/index.js';
4
4
  export function Element(tag) {
5
5
  return function (constructor) {
6
6
  if (isServer())
7
7
  return;
8
8
  if (customElements.get(tag))
9
9
  return;
10
+ const members = getMembers(constructor);
10
11
  class Plus extends HTMLElement {
11
12
  constructor() {
12
13
  super();
13
14
  this.attachShadow({ mode: 'open' });
14
15
  this[CONSTANTS.API_INSTANCE] = new constructor();
16
+ Object.keys(members)
17
+ .filter((key) => members[key].type != CONSTANTS.TYPE_FUNCTION)
18
+ .forEach((key) => {
19
+ members[key].default = this[CONSTANTS.API_INSTANCE][key];
20
+ });
15
21
  this[CONSTANTS.API_INSTANCE][CONSTANTS.API_HOST] = () => this;
16
22
  }
17
- // TODO: ignore functions
18
23
  static get observedAttributes() {
19
- return Object.keys(getMembers(constructor)).map((key) => paramCase(key));
24
+ return Object.keys(members)
25
+ .filter((key) => members[key].type != CONSTANTS.TYPE_FUNCTION)
26
+ .map((key) => paramCase(key));
20
27
  }
21
28
  adoptedCallback() {
22
29
  call(this[CONSTANTS.API_INSTANCE], CONSTANTS.LIFECYCLE_ADOPTED);
@@ -27,8 +34,8 @@ export function Element(tag) {
27
34
  if (instance[CONSTANTS.API_LOCKED])
28
35
  return;
29
36
  const name = camelCase(attribute);
30
- const type = (_a = getMembers(instance)[name]) === null || _a === void 0 ? void 0 : _a.type;
31
- const value = parseValue(next, type);
37
+ const type = (_a = members[name]) === null || _a === void 0 ? void 0 : _a.type;
38
+ const value = fromAttribute(next, type);
32
39
  if (instance[name] === value)
33
40
  return;
34
41
  instance[name] = value;
@@ -4,5 +4,9 @@ export interface PropertyOptions {
4
4
  * Whether property value is reflected back to the associated attribute. default is `false`.
5
5
  */
6
6
  reflect?: boolean;
7
+ /**
8
+ * TODO
9
+ */
10
+ type?: number;
7
11
  }
8
12
  export declare function Property(options?: PropertyOptions): (target: PlusElement, propertyKey: PropertyKey) => void;
@@ -1,9 +1,10 @@
1
1
  import * as CONSTANTS from '../../constants/index.js';
2
- import { defineProperty, host, request, appendToMethod, updateAttribute, getConfig, getMembers, getTag } from '../utils/index.js';
2
+ import { addMember, appendToMethod, defineProperty, getConfig, getMembers, getTag, host, request, updateAttribute } from '../utils/index.js';
3
3
  export function Property(options) {
4
4
  return function (target, propertyKey) {
5
5
  const name = String(propertyKey);
6
6
  const symbol = Symbol();
7
+ addMember(target.constructor, name, options);
7
8
  function get() {
8
9
  return this[symbol];
9
10
  }
@@ -25,7 +26,7 @@ export function Property(options) {
25
26
  var _a;
26
27
  const element = host(this);
27
28
  // TODO: experimental for global config
28
- if (((_a = getMembers(this)[name]) === null || _a === void 0 ? void 0 : _a.default) === this[name]) {
29
+ if (((_a = getMembers(this.constructor)[name]) === null || _a === void 0 ? void 0 : _a.default) === this[name]) {
29
30
  const config = getConfig('component', getTag(target), 'property', name);
30
31
  if (typeof config != 'undefined')
31
32
  this[name] = config;
@@ -0,0 +1,2 @@
1
+ import { PlusElement } from '../../types';
2
+ export declare const addMember: (target: PlusElement, key: string, data: any) => void;
@@ -0,0 +1,6 @@
1
+ import * as CONSTANTS from '../../constants/index.js';
2
+ export const addMember = (target, key, data) => {
3
+ var _a;
4
+ target[_a = CONSTANTS.STATIC_MEMBERS] || (target[_a] = {});
5
+ target[CONSTANTS.STATIC_MEMBERS][key] = data;
6
+ };
@@ -0,0 +1 @@
1
+ export declare const fromAttribute: (input: any, type: any) => any;
@@ -0,0 +1,53 @@
1
+ import * as CONSTANTS from '../../constants/index.js';
2
+ import { typeOf } from './typeOf.js';
3
+ export const fromAttribute = (input, type) => {
4
+ const string = `${input}`;
5
+ if (CONSTANTS.TYPE_BOOLEAN & type) {
6
+ if (string === '')
7
+ return true;
8
+ if (string === 'false')
9
+ return false;
10
+ if (string === 'true')
11
+ return true;
12
+ }
13
+ if (CONSTANTS.TYPE_NUMBER & type) {
14
+ if (string != '' && !isNaN(input)) {
15
+ return parseFloat(input);
16
+ }
17
+ }
18
+ if (CONSTANTS.TYPE_NULL & type) {
19
+ if (string === 'null') {
20
+ return null;
21
+ }
22
+ }
23
+ if (CONSTANTS.TYPE_DATE & type) {
24
+ const value = new Date(input);
25
+ if (value.toString() != 'Invalid Date') {
26
+ return value;
27
+ }
28
+ }
29
+ if (CONSTANTS.TYPE_ARRAY & type) {
30
+ try {
31
+ const value = JSON.parse(input);
32
+ if (typeOf(value) == 'array') {
33
+ return value;
34
+ }
35
+ }
36
+ catch (_a) { }
37
+ }
38
+ if (CONSTANTS.TYPE_OBJECT & type) {
39
+ try {
40
+ const value = JSON.parse(input);
41
+ if (typeOf(value) == 'object') {
42
+ return value;
43
+ }
44
+ }
45
+ catch (_b) { }
46
+ }
47
+ if (CONSTANTS.TYPE_UNDEFINED & type) {
48
+ if (string === 'undefined') {
49
+ return undefined;
50
+ }
51
+ }
52
+ return input;
53
+ };
@@ -1,5 +1,5 @@
1
1
  import * as CONSTANTS from '../../constants/index.js';
2
2
  // TODO
3
3
  export const getMembers = (target) => {
4
- return target.constructor[CONSTANTS.STATIC_MEMBERS] || target[CONSTANTS.STATIC_MEMBERS] || {};
4
+ return target[CONSTANTS.STATIC_MEMBERS] || {};
5
5
  };
@@ -1,7 +1,9 @@
1
+ export * from './addMember.js';
1
2
  export * from './appendToMethod.js';
2
3
  export * from './call.js';
3
4
  export * from './config.js';
4
5
  export * from './defineProperty.js';
6
+ export * from './fromAttribute.js';
5
7
  export * from './getFramework.js';
6
8
  export * from './getMembers.js';
7
9
  export * from './getStyles.js';
@@ -11,7 +13,6 @@ export * from './isEvent.js';
11
13
  export * from './isServer.js';
12
14
  export * from './off.js';
13
15
  export * from './on.js';
14
- export * from './parseValue.js';
15
16
  export * from './request.js';
16
17
  export * from './shadowRoot.js';
17
18
  export * from './syncAttributes.js';
@@ -1,7 +1,9 @@
1
+ export * from './addMember.js';
1
2
  export * from './appendToMethod.js';
2
3
  export * from './call.js';
3
4
  export * from './config.js';
4
5
  export * from './defineProperty.js';
6
+ export * from './fromAttribute.js';
5
7
  export * from './getFramework.js';
6
8
  export * from './getMembers.js';
7
9
  export * from './getStyles.js';
@@ -11,7 +13,6 @@ export * from './isEvent.js';
11
13
  export * from './isServer.js';
12
14
  export * from './off.js';
13
15
  export * from './on.js';
14
- export * from './parseValue.js';
15
16
  export * from './request.js';
16
17
  export * from './shadowRoot.js';
17
18
  export * from './syncAttributes.js';
@@ -29,11 +29,18 @@ export const compiler = (...plugins) => {
29
29
  let context = {
30
30
  filePath
31
31
  };
32
+ const parsed = path.parse(filePath);
32
33
  for (const plugin of plugins) {
33
34
  if (!plugin.run)
34
35
  continue;
35
- log(`Plugin '${plugin.name}' is executing on '${path.basename(filePath)}' file.`);
36
- context = (await plugin.run(context, global)) || context;
36
+ log(`Plugin '${plugin.name}' is executing on '${path.basename(parsed.dir)}/${parsed.base}' file.`);
37
+ try {
38
+ context = (await plugin.run(context, global)) || context;
39
+ }
40
+ catch (error) {
41
+ log(`Error in '${plugin.name}' plugin on '${path.basename(parsed.dir)}/${parsed.base}' file.`, true);
42
+ throw error;
43
+ }
37
44
  global.contexts = global.contexts
38
45
  .filter((current) => {
39
46
  return current.filePath != context.filePath;
@@ -1,7 +1,7 @@
1
1
  import t from '@babel/types';
2
2
  import { pascalCase } from 'change-case';
3
3
  import * as CONSTANTS from '../../constants/index.js';
4
- import { addDependency, print, visitor } from '../utils/index.js';
4
+ import { addDependency, getType, print, visitor } from '../utils/index.js';
5
5
  export const CUSTOM_ELEMENT_OPTIONS = {
6
6
  // prefix: undefined,
7
7
  typings: true
@@ -98,49 +98,112 @@ export const customElement = (options) => {
98
98
  t.addComment(node, 'leading', CONSTANTS.COMMENT_AUTO_ADDED_DEPENDENCY, true);
99
99
  }
100
100
  });
101
+ // adds type to properties
102
+ visitor(ast, {
103
+ Decorator(path) {
104
+ var _a, _b;
105
+ const { expression } = path.node;
106
+ if (((_a = expression.callee) === null || _a === void 0 ? void 0 : _a.name) != CONSTANTS.DECORATOR_PROPERTY)
107
+ return;
108
+ let type = 0;
109
+ const extract = (input) => {
110
+ switch (input === null || input === void 0 ? void 0 : input.type) {
111
+ case 'BooleanLiteral':
112
+ type |= CONSTANTS.TYPE_BOOLEAN;
113
+ break;
114
+ case 'NumericLiteral':
115
+ type |= CONSTANTS.TYPE_NUMBER;
116
+ break;
117
+ case 'StringLiteral':
118
+ type |= CONSTANTS.TYPE_ENUM;
119
+ break;
120
+ case 'TSArrayType':
121
+ type |= CONSTANTS.TYPE_ARRAY;
122
+ break;
123
+ case 'TSBooleanKeyword':
124
+ type |= CONSTANTS.TYPE_BOOLEAN;
125
+ break;
126
+ case 'TSLiteralType':
127
+ extract(input.literal);
128
+ break;
129
+ case 'TSNullKeyword':
130
+ type |= CONSTANTS.TYPE_NULL;
131
+ break;
132
+ case 'TSNumberKeyword':
133
+ type |= CONSTANTS.TYPE_NUMBER;
134
+ break;
135
+ case 'TSObjectKeyword':
136
+ type |= CONSTANTS.TYPE_OBJECT;
137
+ break;
138
+ case 'TSStringKeyword':
139
+ type |= CONSTANTS.TYPE_STRING;
140
+ break;
141
+ case 'TSTypeLiteral':
142
+ type |= CONSTANTS.TYPE_OBJECT;
143
+ break;
144
+ case 'TSTypeReference':
145
+ if (!input.typeName)
146
+ break;
147
+ switch (input.typeName.name) {
148
+ case 'Array':
149
+ type |= CONSTANTS.TYPE_ARRAY;
150
+ break;
151
+ case 'Boolean':
152
+ type |= CONSTANTS.TYPE_BOOLEAN;
153
+ break;
154
+ case 'bool':
155
+ type |= CONSTANTS.TYPE_BOOLEAN;
156
+ break;
157
+ case 'Date':
158
+ type |= CONSTANTS.TYPE_DATE;
159
+ break;
160
+ case 'Number':
161
+ type |= CONSTANTS.TYPE_NUMBER;
162
+ break;
163
+ case 'Object':
164
+ type |= CONSTANTS.TYPE_OBJECT;
165
+ break;
166
+ }
167
+ break;
168
+ case 'TSUnionType':
169
+ input.types.forEach(extract);
170
+ break;
171
+ }
172
+ };
173
+ extract(getType(ast, (_b = path.parentPath.node.typeAnnotation) === null || _b === void 0 ? void 0 : _b.typeAnnotation, { directory: context.directoryPath }));
174
+ if (!expression.arguments.length) {
175
+ expression.arguments.push(t.objectExpression([]));
176
+ }
177
+ const [argument] = expression.arguments;
178
+ argument.properties = argument.properties.filter((property) => {
179
+ return property.key.name != CONSTANTS.STATIC_MEMBERS_TYPE;
180
+ });
181
+ argument.properties.push(t.objectProperty(t.identifier(CONSTANTS.STATIC_MEMBERS_TYPE), t.numericLiteral(type)));
182
+ }
183
+ });
101
184
  // attaches members
102
185
  visitor(ast, {
103
186
  ClassDeclaration(path) {
104
187
  const { body, id } = path.node;
105
188
  if (id.name != context.className)
106
189
  return;
107
- const node = t.classProperty(t.identifier(CONSTANTS.STATIC_MEMBERS), t.objectExpression([
108
- ...context.classProperties.map((property) => {
109
- var _a, _b, _c, _d, _e, _f;
110
- const properties = [];
111
- if (property.value) {
112
- properties.push(t.objectProperty(t.identifier(CONSTANTS.STATIC_MEMBERS_INITIALIZER), property.value));
113
- }
114
- const type = (() => {
115
- var _a, _b;
116
- switch ((_b = (_a = property.typeAnnotation) === null || _a === void 0 ? void 0 : _a.typeAnnotation) === null || _b === void 0 ? void 0 : _b.type) {
117
- case 'TSBooleanKeyword':
118
- return t.stringLiteral(CONSTANTS.TYPE_BOOLEAN);
119
- case 'TSStringKeyword':
120
- return t.stringLiteral(CONSTANTS.TYPE_STRING);
121
- case 'TSNumberKeyword':
122
- return t.stringLiteral(CONSTANTS.TYPE_NUMBER);
123
- }
124
- })();
125
- if (type) {
126
- properties.push(t.objectProperty(t.identifier(CONSTANTS.STATIC_MEMBERS_TYPE), type));
127
- }
128
- // TODO
129
- // prettier-ignore
130
- (_f = (_e = (_d = (_c = (_b = (_a = property === null || property === void 0 ? void 0 : property.decorators) === null || _a === void 0 ? void 0 : _a.find((decorator) => {
131
- var _a, _b;
132
- return ((_b = (_a = decorator.expression) === null || _a === void 0 ? void 0 : _a['callee']) === null || _b === void 0 ? void 0 : _b.name) == CONSTANTS.DECORATOR_PROPERTY;
133
- })) === null || _b === void 0 ? void 0 : _b.expression) === null || _c === void 0 ? void 0 : _c['arguments']) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.properties) === null || _f === void 0 ? void 0 : _f.forEach((property) => properties.push(property));
134
- return t.objectProperty(t.identifier(property.key['name']), t.objectExpression(properties));
135
- }),
136
- ...context.classMethods.map((method) => t.objectProperty(t.identifier(method.key['name']), t.objectExpression([
137
- t.objectProperty(t.identifier(CONSTANTS.STATIC_MEMBERS_TYPE), t.stringLiteral(CONSTANTS.TYPE_FUNCTION))
138
- ])))
139
- ]), undefined, undefined, undefined, true);
190
+ const node = t.classProperty(t.identifier(CONSTANTS.STATIC_MEMBERS), t.objectExpression(context.classMethods.map((method) => t.objectProperty(t.identifier(method.key['name']), t.objectExpression([
191
+ t.objectProperty(t.identifier(CONSTANTS.STATIC_MEMBERS_TYPE), t.numericLiteral(CONSTANTS.TYPE_FUNCTION))
192
+ ])))), undefined, undefined, undefined, true);
140
193
  t.addComment(node, 'leading', CONSTANTS.COMMENT_AUTO_ADDED_PROPERTY, true);
141
194
  body.body.unshift(node);
142
195
  }
143
196
  });
197
+ // removes methods decorators
198
+ visitor(ast, {
199
+ Decorator(path) {
200
+ var _a;
201
+ const { expression } = path.node;
202
+ if (((_a = expression.callee) === null || _a === void 0 ? void 0 : _a.name) != CONSTANTS.DECORATOR_METHOD)
203
+ return;
204
+ path.remove();
205
+ }
206
+ });
144
207
  // attaches typings
145
208
  if (options === null || options === void 0 ? void 0 : options.typings) {
146
209
  visitor(ast, {
@@ -1,2 +1,2 @@
1
- import { File } from '@babel/types';
2
- export declare const getType: (file: File, node: any, options: any) => any;
1
+ import { File, Node } from '@babel/types';
2
+ export declare const getType: (file: File, node: Node, options: any) => any;
@@ -1,6 +1,8 @@
1
1
  import { parse } from '@babel/parser';
2
+ import glob from 'fast-glob';
2
3
  import fs from 'fs-extra';
3
4
  import { dirname, resolve } from 'path';
5
+ import { join } from 'path';
4
6
  import { visitor } from './visitor.js';
5
7
  // TODO: return type
6
8
  export const getType = (file, node, options) => {
@@ -12,7 +14,7 @@ export const getType = (file, node, options) => {
12
14
  let result;
13
15
  visitor(file, {
14
16
  ClassDeclaration(path) {
15
- if (path.node.id.name != node.typeName.name)
17
+ if (path.node.id.name != node.typeName['name'])
16
18
  return;
17
19
  result = path.node;
18
20
  path.stop();
@@ -20,7 +22,7 @@ export const getType = (file, node, options) => {
20
22
  ImportDeclaration(path) {
21
23
  for (const specifier of path.node.specifiers) {
22
24
  const alias = specifier.local.name;
23
- if (alias != node.typeName.name)
25
+ if (alias != node.typeName['name'])
24
26
  continue;
25
27
  let key;
26
28
  switch (specifier.type) {
@@ -35,14 +37,18 @@ export const getType = (file, node, options) => {
35
37
  break;
36
38
  }
37
39
  try {
40
+ const reference = glob
41
+ .sync(['.ts*', '/index.ts*'].map((key) => {
42
+ return join(directory, path.node.source.value).replace(/\\/g, '/') + key;
43
+ }))
44
+ .find((reference) => fs.existsSync(reference));
45
+ const content = fs.readFileSync(reference, 'utf8');
38
46
  const filePath = resolve(directory, path.node.source.value + '.ts');
39
- path.$ast =
40
- path.$ast ||
41
- parse(fs.readFileSync(filePath, 'utf8'), {
42
- allowImportExportEverywhere: true,
43
- plugins: ['typescript'],
44
- ranges: false
45
- });
47
+ path.$ast || (path.$ast = parse(content, {
48
+ allowImportExportEverywhere: true,
49
+ plugins: ['typescript'],
50
+ ranges: false
51
+ }));
46
52
  result = getType(path.$ast, node, {
47
53
  directory: dirname(filePath)
48
54
  });
@@ -53,15 +59,15 @@ export const getType = (file, node, options) => {
53
59
  }
54
60
  },
55
61
  TSInterfaceDeclaration(path) {
56
- if (path.node.id.name != node.typeName.name)
62
+ if (path.node.id.name != node.typeName['name'])
57
63
  return;
58
64
  result = path.node;
59
65
  path.stop();
60
66
  },
61
67
  TSTypeAliasDeclaration(path) {
62
- if (path.node.id.name != node.typeName.name)
68
+ if (path.node.id.name != node.typeName['name'])
63
69
  return;
64
- result = path.node;
70
+ result = path.node.typeAnnotation;
65
71
  path.stop();
66
72
  }
67
73
  });
@@ -27,10 +27,15 @@ export declare const STATIC_MEMBERS_TYPE = "type";
27
27
  export declare const STATIC_STYLES = "STYLES";
28
28
  export declare const STATIC_TAG = "TAG";
29
29
  export declare const STYLE_IMPORTED = "STYLE_IMPORTED";
30
- export declare const TYPE_BOOLEAN = "Boolean";
31
- export declare const TYPE_DATE = "Date";
32
- export declare const TYPE_FUNCTION = "Function";
33
- export declare const TYPE_STRING = "String";
34
- export declare const TYPE_NUMBER = "Number";
30
+ export declare const TYPE_ARRAY = 1;
31
+ export declare const TYPE_BOOLEAN = 2;
32
+ export declare const TYPE_DATE = 4;
33
+ export declare const TYPE_ENUM = 8;
34
+ export declare const TYPE_FUNCTION = 16;
35
+ export declare const TYPE_NULL = 32;
36
+ export declare const TYPE_NUMBER = 64;
37
+ export declare const TYPE_OBJECT = 128;
38
+ export declare const TYPE_STRING = 256;
39
+ export declare const TYPE_UNDEFINED = 512;
35
40
  export declare const UTIL_STYLE_MAPPER = "styles";
36
41
  export declare const VENDOR_UHTML_PATH = "@htmlplus/element/client/vendors/uhtml.js";
@@ -35,11 +35,16 @@ export const STATIC_TAG = 'TAG';
35
35
  // style
36
36
  export const STYLE_IMPORTED = 'STYLE_IMPORTED';
37
37
  // types
38
- export const TYPE_BOOLEAN = 'Boolean';
39
- export const TYPE_DATE = 'Date';
40
- export const TYPE_FUNCTION = 'Function';
41
- export const TYPE_STRING = 'String';
42
- export const TYPE_NUMBER = 'Number';
38
+ export const TYPE_ARRAY = 1;
39
+ export const TYPE_BOOLEAN = 2;
40
+ export const TYPE_DATE = 4;
41
+ export const TYPE_ENUM = 8;
42
+ export const TYPE_FUNCTION = 16;
43
+ export const TYPE_NULL = 32;
44
+ export const TYPE_NUMBER = 64;
45
+ export const TYPE_OBJECT = 128;
46
+ export const TYPE_STRING = 256;
47
+ export const TYPE_UNDEFINED = 512;
43
48
  // utils
44
49
  export const UTIL_STYLE_MAPPER = 'styles';
45
50
  // vendors
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htmlplus/element",
3
- "version": "0.5.7",
3
+ "version": "0.5.8",
4
4
  "license": "MIT",
5
5
  "author": "Masood Abdolian <m.abdolian@gmail.com>",
6
6
  "description": "A powerful library for building scalable, reusable, fast, tastable and lightweight design system for any web technologies. Powerd by Web Component.",
@@ -1 +0,0 @@
1
- export declare const parseValue: (value: any, type: any) => any;
@@ -1,15 +0,0 @@
1
- import * as CONSTANTS from '../../constants/index.js';
2
- import { toBoolean } from './toBoolean.js';
3
- // TODO: input type & validate date
4
- export const parseValue = (value, type) => {
5
- switch (type) {
6
- case CONSTANTS.TYPE_BOOLEAN:
7
- return toBoolean(value);
8
- case CONSTANTS.TYPE_DATE:
9
- return new Date(value);
10
- case CONSTANTS.TYPE_NUMBER:
11
- return isNaN(value) ? value : parseFloat(value);
12
- default:
13
- return value;
14
- }
15
- };