@esportsplus/template 0.38.2 → 0.40.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.
Files changed (39) hide show
  1. package/README.md +5 -26
  2. package/build/compiler/codegen.d.ts +3 -2
  3. package/build/compiler/codegen.js +102 -142
  4. package/build/compiler/constants.d.ts +16 -0
  5. package/build/compiler/constants.js +19 -0
  6. package/build/compiler/index.d.ts +6 -3
  7. package/build/compiler/index.js +29 -38
  8. package/build/compiler/parser.d.ts +3 -3
  9. package/build/compiler/parser.js +5 -4
  10. package/build/compiler/plugins/tsc.d.ts +3 -2
  11. package/build/compiler/plugins/tsc.js +4 -2
  12. package/build/compiler/plugins/vite.js +4 -3
  13. package/build/compiler/{analyzer.d.ts → ts-analyzer.d.ts} +2 -2
  14. package/build/compiler/{analyzer.js → ts-analyzer.js} +16 -18
  15. package/build/compiler/ts-parser.d.ts +5 -1
  16. package/build/compiler/ts-parser.js +27 -45
  17. package/build/constants.d.ts +1 -16
  18. package/build/constants.js +1 -19
  19. package/package.json +7 -3
  20. package/src/compiler/codegen.ts +135 -217
  21. package/src/compiler/constants.ts +26 -0
  22. package/src/compiler/index.ts +33 -58
  23. package/src/compiler/parser.ts +7 -6
  24. package/src/compiler/plugins/tsc.ts +4 -2
  25. package/src/compiler/plugins/vite.ts +4 -3
  26. package/src/compiler/{analyzer.ts → ts-analyzer.ts} +17 -20
  27. package/src/compiler/ts-parser.ts +35 -67
  28. package/src/constants.ts +0 -25
  29. package/test/counter.ts +113 -0
  30. package/test/effects.ts +1 -1
  31. package/test/events.ts +1 -1
  32. package/test/imported-values.ts +1 -1
  33. package/test/integration/tsconfig.json +0 -1
  34. package/test/nested.ts +20 -1
  35. package/test/slots.ts +1 -1
  36. package/test/spread.ts +1 -1
  37. package/test/static.ts +1 -1
  38. package/test/templates.ts +1 -1
  39. package/test/vite.config.ts +2 -1
@@ -1,4 +1,4 @@
1
- import { COMPILER_TYPES } from '../constants.js';
2
1
  import { ts } from '@esportsplus/typescript';
3
- declare const analyze: (expr: ts.Expression, checker?: ts.TypeChecker) => COMPILER_TYPES;
2
+ import { TYPES } from './constants.js';
3
+ declare const analyze: (expr: ts.Expression, checker?: ts.TypeChecker) => TYPES;
4
4
  export { analyze };
@@ -1,5 +1,5 @@
1
- import { COMPILER_ENTRYPOINT, COMPILER_ENTRYPOINT_REACTIVITY, COMPILER_TYPES } from '../constants.js';
2
1
  import { ts } from '@esportsplus/typescript';
2
+ import { ENTRYPOINT, ENTRYPOINT_REACTIVITY, TYPES } from './constants.js';
3
3
  function isTypeFunction(type, checker) {
4
4
  if (type.isUnion()) {
5
5
  for (let i = 0, n = type.types.length; i < n; i++) {
@@ -16,17 +16,17 @@ const analyze = (expr, checker) => {
16
16
  expr = expr.expression;
17
17
  }
18
18
  if (ts.isArrowFunction(expr) || ts.isFunctionExpression(expr)) {
19
- return COMPILER_TYPES.Effect;
19
+ return TYPES.Effect;
20
20
  }
21
21
  if (ts.isCallExpression(expr) &&
22
22
  ts.isPropertyAccessExpression(expr.expression) &&
23
23
  ts.isIdentifier(expr.expression.expression) &&
24
- expr.expression.expression.text === COMPILER_ENTRYPOINT &&
25
- expr.expression.name.text === COMPILER_ENTRYPOINT_REACTIVITY) {
26
- return COMPILER_TYPES.ArraySlot;
24
+ expr.expression.expression.text === ENTRYPOINT &&
25
+ expr.expression.name.text === ENTRYPOINT_REACTIVITY) {
26
+ return TYPES.ArraySlot;
27
27
  }
28
- if (ts.isTaggedTemplateExpression(expr) && ts.isIdentifier(expr.tag) && expr.tag.text === COMPILER_ENTRYPOINT) {
29
- return COMPILER_TYPES.DocumentFragment;
28
+ if (ts.isTaggedTemplateExpression(expr) && ts.isIdentifier(expr.tag) && expr.tag.text === ENTRYPOINT) {
29
+ return TYPES.DocumentFragment;
30
30
  }
31
31
  if (ts.isNumericLiteral(expr) ||
32
32
  ts.isStringLiteral(expr) ||
@@ -35,31 +35,29 @@ const analyze = (expr, checker) => {
35
35
  expr.kind === ts.SyntaxKind.FalseKeyword ||
36
36
  expr.kind === ts.SyntaxKind.NullKeyword ||
37
37
  expr.kind === ts.SyntaxKind.UndefinedKeyword) {
38
- return COMPILER_TYPES.Static;
38
+ return TYPES.Static;
39
39
  }
40
40
  if (ts.isTemplateExpression(expr)) {
41
- return COMPILER_TYPES.Primitive;
41
+ return TYPES.Primitive;
42
42
  }
43
43
  if (ts.isConditionalExpression(expr)) {
44
44
  let whenFalse = analyze(expr.whenFalse, checker), whenTrue = analyze(expr.whenTrue, checker);
45
45
  if (whenTrue === whenFalse) {
46
46
  return whenTrue;
47
47
  }
48
- if (whenTrue === COMPILER_TYPES.Effect || whenFalse === COMPILER_TYPES.Effect) {
49
- return COMPILER_TYPES.Effect;
48
+ if (whenTrue === TYPES.Effect || whenFalse === TYPES.Effect) {
49
+ return TYPES.Effect;
50
50
  }
51
- return COMPILER_TYPES.Unknown;
51
+ return TYPES.Unknown;
52
52
  }
53
53
  if (checker && (ts.isIdentifier(expr) || ts.isPropertyAccessExpression(expr) || ts.isCallExpression(expr))) {
54
54
  try {
55
- let type = checker.getTypeAtLocation(expr);
56
- if (isTypeFunction(type, checker)) {
57
- return COMPILER_TYPES.Effect;
55
+ if (isTypeFunction(checker.getTypeAtLocation(expr), checker)) {
56
+ return TYPES.Effect;
58
57
  }
59
58
  }
60
- catch {
61
- }
59
+ catch { }
62
60
  }
63
- return COMPILER_TYPES.Unknown;
61
+ return TYPES.Unknown;
64
62
  };
65
63
  export { analyze };
@@ -14,7 +14,11 @@ type TemplateInfo = {
14
14
  node: ts.TaggedTemplateExpression;
15
15
  start: number;
16
16
  };
17
+ declare const extractTemplateParts: (template: ts.TemplateLiteral) => {
18
+ expressions: ts.Expression[];
19
+ literals: string[];
20
+ };
17
21
  declare const findHtmlTemplates: (sourceFile: ts.SourceFile, checker?: ts.TypeChecker) => TemplateInfo[];
18
22
  declare const findReactiveCalls: (sourceFile: ts.SourceFile, checker?: ts.TypeChecker) => ReactiveCallInfo[];
19
- export { findHtmlTemplates, findReactiveCalls };
23
+ export { extractTemplateParts, findHtmlTemplates, findReactiveCalls };
20
24
  export type { ReactiveCallInfo, TemplateInfo };
@@ -1,38 +1,14 @@
1
1
  import { ts } from '@esportsplus/typescript';
2
- import { COMPILER_ENTRYPOINT, COMPILER_ENTRYPOINT_REACTIVITY, PACKAGE } from '../constants.js';
3
- function isHtmlFromPackage(node, checker) {
4
- if (node.text !== COMPILER_ENTRYPOINT) {
5
- return false;
6
- }
7
- if (!checker) {
8
- return true;
9
- }
10
- let symbol = checker.getSymbolAtLocation(node);
11
- if (!symbol) {
12
- return true;
13
- }
14
- if (symbol.flags & ts.SymbolFlags.Alias) {
15
- symbol = checker.getAliasedSymbol(symbol);
16
- }
17
- let declarations = symbol.getDeclarations();
18
- if (!declarations || declarations.length === 0) {
19
- return true;
20
- }
21
- for (let i = 0, n = declarations.length; i < n; i++) {
22
- let filename = declarations[i].getSourceFile().fileName;
23
- if (filename.includes(PACKAGE) || filename.includes('@esportsplus/template')) {
24
- return true;
25
- }
26
- }
27
- return false;
28
- }
2
+ import { imports } from '@esportsplus/typescript/compiler';
3
+ import { ENTRYPOINT, ENTRYPOINT_REACTIVITY, PACKAGE } from './constants.js';
29
4
  function visitReactiveCalls(node, calls, checker) {
30
5
  if (ts.isCallExpression(node) &&
31
6
  ts.isPropertyAccessExpression(node.expression) &&
32
7
  ts.isIdentifier(node.expression.expression) &&
33
- node.expression.name.text === COMPILER_ENTRYPOINT_REACTIVITY &&
8
+ node.expression.name.text === ENTRYPOINT_REACTIVITY &&
34
9
  node.arguments.length === 2 &&
35
- isHtmlFromPackage(node.expression.expression, checker)) {
10
+ node.expression.expression.text === ENTRYPOINT &&
11
+ (!checker || imports.includes(checker, node.expression.expression, PACKAGE, ENTRYPOINT))) {
36
12
  calls.push({
37
13
  arrayArg: node.arguments[0],
38
14
  callbackArg: node.arguments[1],
@@ -47,19 +23,11 @@ function visitTemplates(node, depth, templates, checker) {
47
23
  let nextDepth = (ts.isArrowFunction(node) || ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) || ts.isMethodDeclaration(node))
48
24
  ? depth + 1
49
25
  : depth;
50
- if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isHtmlFromPackage(node.tag, checker)) {
51
- let expressions = [], literals = [], template = node.template;
52
- if (ts.isNoSubstitutionTemplateLiteral(template)) {
53
- literals.push(template.text);
54
- }
55
- else if (ts.isTemplateExpression(template)) {
56
- literals.push(template.head.text);
57
- for (let i = 0, n = template.templateSpans.length; i < n; i++) {
58
- let span = template.templateSpans[i];
59
- expressions.push(span.expression);
60
- literals.push(span.literal.text);
61
- }
62
- }
26
+ if (ts.isTaggedTemplateExpression(node) &&
27
+ ts.isIdentifier(node.tag) &&
28
+ node.tag.text === ENTRYPOINT &&
29
+ (!checker || imports.includes(checker, node.tag, PACKAGE, ENTRYPOINT))) {
30
+ let { expressions, literals } = extractTemplateParts(node.template);
63
31
  templates.push({
64
32
  depth,
65
33
  end: node.end,
@@ -71,15 +39,29 @@ function visitTemplates(node, depth, templates, checker) {
71
39
  }
72
40
  ts.forEachChild(node, child => visitTemplates(child, nextDepth, templates, checker));
73
41
  }
42
+ const extractTemplateParts = (template) => {
43
+ let expressions = [], literals = [];
44
+ if (ts.isNoSubstitutionTemplateLiteral(template)) {
45
+ literals.push(template.text);
46
+ }
47
+ else if (ts.isTemplateExpression(template)) {
48
+ literals.push(template.head.text);
49
+ for (let i = 0, n = template.templateSpans.length; i < n; i++) {
50
+ let span = template.templateSpans[i];
51
+ expressions.push(span.expression);
52
+ literals.push(span.literal.text);
53
+ }
54
+ }
55
+ return { expressions, literals };
56
+ };
74
57
  const findHtmlTemplates = (sourceFile, checker) => {
75
58
  let templates = [];
76
59
  visitTemplates(sourceFile, 0, templates, checker);
77
- templates.sort((a, b) => a.depth !== b.depth ? b.depth - a.depth : a.start - b.start);
78
- return templates;
60
+ return templates.sort((a, b) => a.depth !== b.depth ? b.depth - a.depth : a.start - b.start);
79
61
  };
80
62
  const findReactiveCalls = (sourceFile, checker) => {
81
63
  let calls = [];
82
64
  visitReactiveCalls(sourceFile, calls, checker);
83
65
  return calls;
84
66
  };
85
- export { findHtmlTemplates, findReactiveCalls };
67
+ export { extractTemplateParts, findHtmlTemplates, findReactiveCalls };
@@ -1,25 +1,10 @@
1
1
  declare const ARRAY_SLOT: unique symbol;
2
2
  declare const CLEANUP: unique symbol;
3
- declare const COMPILER_ENTRYPOINT = "html";
4
- declare const COMPILER_ENTRYPOINT_REACTIVITY = "reactive";
5
- declare const COMPILER_NAMESPACE: string;
6
- declare const enum COMPILER_TYPES {
7
- ArraySlot = "array-slot",
8
- Attributes = "attributes",
9
- Attribute = "attribute",
10
- DocumentFragment = "document-fragment",
11
- Effect = "effect",
12
- Node = "node",
13
- Primitive = "primitive",
14
- Static = "static",
15
- Unknown = "unknown"
16
- }
17
3
  declare const DIRECT_ATTACH_EVENTS: Set<string>;
18
4
  declare const LIFECYCLE_EVENTS: Set<string>;
19
- declare const PACKAGE = "@esportsplus/template";
20
5
  declare const SLOT_HTML = "<!--$-->";
21
6
  declare const STATE_HYDRATING = 0;
22
7
  declare const STATE_NONE = 1;
23
8
  declare const STATE_WAITING = 2;
24
9
  declare const STORE: unique symbol;
25
- export { ARRAY_SLOT, CLEANUP, COMPILER_ENTRYPOINT, COMPILER_ENTRYPOINT_REACTIVITY, COMPILER_NAMESPACE, COMPILER_TYPES, DIRECT_ATTACH_EVENTS, LIFECYCLE_EVENTS, PACKAGE, SLOT_HTML, STATE_HYDRATING, STATE_NONE, STATE_WAITING, STORE, };
10
+ export { ARRAY_SLOT, CLEANUP, DIRECT_ATTACH_EVENTS, LIFECYCLE_EVENTS, SLOT_HTML, STATE_HYDRATING, STATE_NONE, STATE_WAITING, STORE, };
@@ -1,22 +1,5 @@
1
- import { uid } from '@esportsplus/typescript/compiler';
2
1
  const ARRAY_SLOT = Symbol('template.array.slot');
3
2
  const CLEANUP = Symbol('template.cleanup');
4
- const COMPILER_ENTRYPOINT = 'html';
5
- const COMPILER_ENTRYPOINT_REACTIVITY = 'reactive';
6
- const COMPILER_NAMESPACE = uid('template');
7
- var COMPILER_TYPES;
8
- (function (COMPILER_TYPES) {
9
- COMPILER_TYPES["ArraySlot"] = "array-slot";
10
- COMPILER_TYPES["Attributes"] = "attributes";
11
- COMPILER_TYPES["Attribute"] = "attribute";
12
- COMPILER_TYPES["DocumentFragment"] = "document-fragment";
13
- COMPILER_TYPES["Effect"] = "effect";
14
- COMPILER_TYPES["Node"] = "node";
15
- COMPILER_TYPES["Primitive"] = "primitive";
16
- COMPILER_TYPES["Static"] = "static";
17
- COMPILER_TYPES["Unknown"] = "unknown";
18
- })(COMPILER_TYPES || (COMPILER_TYPES = {}));
19
- ;
20
3
  const DIRECT_ATTACH_EVENTS = new Set([
21
4
  'onblur',
22
5
  'onerror',
@@ -29,10 +12,9 @@ const DIRECT_ATTACH_EVENTS = new Set([
29
12
  const LIFECYCLE_EVENTS = new Set([
30
13
  'onconnect', 'ondisconnect', 'onrender', 'onresize', 'ontick'
31
14
  ]);
32
- const PACKAGE = '@esportsplus/template';
33
15
  const SLOT_HTML = '<!--$-->';
34
16
  const STATE_HYDRATING = 0;
35
17
  const STATE_NONE = 1;
36
18
  const STATE_WAITING = 2;
37
19
  const STORE = Symbol('template.store');
38
- export { ARRAY_SLOT, CLEANUP, COMPILER_ENTRYPOINT, COMPILER_ENTRYPOINT_REACTIVITY, COMPILER_NAMESPACE, COMPILER_TYPES, DIRECT_ATTACH_EVENTS, LIFECYCLE_EVENTS, PACKAGE, SLOT_HTML, STATE_HYDRATING, STATE_NONE, STATE_WAITING, STORE, };
20
+ export { ARRAY_SLOT, CLEANUP, DIRECT_ATTACH_EVENTS, LIFECYCLE_EVENTS, SLOT_HTML, STATE_HYDRATING, STATE_NONE, STATE_WAITING, STORE, };
package/package.json CHANGED
@@ -2,8 +2,8 @@
2
2
  "author": "ICJR",
3
3
  "dependencies": {
4
4
  "@esportsplus/queue": "^0.2.0",
5
- "@esportsplus/reactivity": "^0.28.1",
6
- "@esportsplus/typescript": "^0.25.0",
5
+ "@esportsplus/reactivity": "^0.29.9",
6
+ "@esportsplus/typescript": "^0.27.0",
7
7
  "@esportsplus/utilities": "^0.27.2",
8
8
  "serve": "^14.2.5"
9
9
  },
@@ -17,6 +17,10 @@
17
17
  "types": "./build/index.d.ts",
18
18
  "default": "./build/index.js"
19
19
  },
20
+ "./compiler": {
21
+ "types": "./build/compiler/index.d.ts",
22
+ "default": "./build/compiler/index.js"
23
+ },
20
24
  "./compiler/tsc": {
21
25
  "types": "./build/compiler/plugins/tsc.d.ts",
22
26
  "default": "./build/compiler/plugins/tsc.js"
@@ -35,7 +39,7 @@
35
39
  },
36
40
  "type": "module",
37
41
  "types": "./build/index.d.ts",
38
- "version": "0.38.2",
42
+ "version": "0.40.0",
39
43
  "scripts": {
40
44
  "build": "tsc",
41
45
  "build:test": "vite build --config test/vite.config.ts",