@katabatic/compiler 1.1.1 → 1.1.3

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@katabatic/compiler",
3
3
  "license": "MIT",
4
- "version": "1.1.1",
4
+ "version": "1.1.3",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",
@@ -30,7 +30,13 @@ export function CallExpression(node, ctx) {
30
30
  }
31
31
 
32
32
  if (is.defineCustomElement(node)) {
33
+ const template = getTemplate(ctx)
33
34
  const name = node.arguments[0].value
35
+
36
+ template.metadata ??= {}
37
+ template.metadata.customElementName = name
38
+
34
39
  ctx.state.customElement.name = name
40
+ return
35
41
  }
36
42
  }
@@ -8,8 +8,8 @@ export function IfBlock(node, ctx) {
8
8
  const program = getProgram(ctx)
9
9
  const blocks = getBlocks(ctx)
10
10
  matchExpression(node.test, program, blocks)
11
-
12
- const hasElseif = node.alternate?.nodes.some(is.ifBlock) ?? false
11
+
12
+ const hasElseif = node.alternate?.nodes.some(is.elseIfBlock) ?? false
13
13
 
14
14
  node.metadata ??= {}
15
15
  node.metadata.hasElseif = hasElseif
package/src/builders.js CHANGED
@@ -648,79 +648,28 @@ export function $instrument(value) {
648
648
  }
649
649
  }
650
650
 
651
- export function $getBindingProp(object, value, body) {
652
- return {
653
- type: 'CallExpression',
654
- callee: {
655
- type: 'MemberExpression',
656
- object: { type: 'ThisExpression' },
657
- property: {
658
- type: 'MemberExpression',
659
- object: {
660
- type: 'Identifier',
661
- name: '$'
662
- },
663
- property: {
664
- type: 'Identifier',
665
- name: 'getBindingProp'
666
- },
667
- computed: false,
668
- optional: false
669
- },
670
- computed: false,
671
- optional: false
672
- },
673
- arguments: [
674
- object,
675
- { type: 'Literal', value },
676
- {
677
- type: 'ArrowFunctionExpression',
678
- id: null,
679
- expression: false,
680
- generator: false,
681
- async: false,
682
- params: [{ type: 'Identifier', name: 'v' }],
683
- body
684
- }
685
- ],
686
- optional: false
687
- }
688
- }
689
-
690
- export function $setBindingProp(object, value, arg) {
651
+ export function createElement(value) {
691
652
  return {
692
653
  type: 'CallExpression',
693
654
  callee: {
694
655
  type: 'MemberExpression',
695
- object: { type: 'ThisExpression' },
696
- property: {
697
- type: 'MemberExpression',
698
- object: {
699
- type: 'Identifier',
700
- name: '$'
701
- },
702
- property: {
703
- type: 'Identifier',
704
- name: 'setBindingProp'
705
- },
706
- computed: false,
707
- optional: false
708
- },
656
+ object: { type: 'Identifier', name: 'document' },
657
+ property: { type: 'Identifier', name: 'createElement' },
709
658
  computed: false,
710
659
  optional: false
711
660
  },
712
- arguments: [object, { type: 'Literal', value }, arg],
661
+ arguments: [{ type: 'Literal', value }],
713
662
  optional: false
714
663
  }
715
664
  }
716
665
 
717
- export function createElement(value) {
666
+ export function createComment(value = '') {
718
667
  return {
719
668
  type: 'CallExpression',
720
669
  callee: {
721
670
  type: 'MemberExpression',
722
671
  object: { type: 'Identifier', name: 'document' },
723
- property: { type: 'Identifier', name: 'createElement' },
672
+ property: { type: 'Identifier', name: 'createComment' },
724
673
  computed: false,
725
674
  optional: false
726
675
  },
@@ -862,31 +811,28 @@ export function insertBefore(node, insertNode) {
862
811
  }
863
812
 
864
813
  return {
865
- type: 'ExpressionStatement',
866
- expression: {
867
- type: 'CallExpression',
868
- callee: {
814
+ type: 'CallExpression',
815
+ callee: {
816
+ type: 'MemberExpression',
817
+ object: {
869
818
  type: 'MemberExpression',
870
- object: {
871
- type: 'MemberExpression',
872
- object: node,
873
- property: {
874
- type: 'Identifier',
875
- name: 'parentNode'
876
- },
877
- computed: false,
878
- optional: false
879
- },
819
+ object: node,
880
820
  property: {
881
821
  type: 'Identifier',
882
- name: 'insertBefore'
822
+ name: 'parentNode'
883
823
  },
884
824
  computed: false,
885
825
  optional: false
886
826
  },
887
- arguments: [insertNode, node],
827
+ property: {
828
+ type: 'Identifier',
829
+ name: 'insertBefore'
830
+ },
831
+ computed: false,
888
832
  optional: false
889
- }
833
+ },
834
+ arguments: [insertNode, node],
835
+ optional: false
890
836
  }
891
837
  }
892
838
 
package/src/checkers.js CHANGED
@@ -139,6 +139,6 @@ export function shadowRootModeAttribute(node) {
139
139
  return node.type === 'Attribute' && node.name === 'shadowRootMode'
140
140
  }
141
141
 
142
- export function ifBlock(node) {
143
- return node.type === 'IfBlock'
142
+ export function elseIfBlock(node) {
143
+ return node.type === 'IfBlock' && node.elseif
144
144
  }
@@ -70,3 +70,7 @@ function position(fragment, node) {
70
70
  export function getProgram(ctx) {
71
71
  return ctx.path[0]
72
72
  }
73
+
74
+ export function getTemplate(ctx) {
75
+ return ctx.path[0]
76
+ }
@@ -52,21 +52,14 @@ export function Attribute(node, ctx) {
52
52
  ])
53
53
  ctx.state.effects.push(stmt)
54
54
  } else if (bindingId) {
55
- const stmt = b.$effect([
56
- b.logical(
57
- '||',
58
- b.$setBindingProp(bindingId, node.name, template.expressions[0]),
59
- b.setAttribute(elementId, b.literal(node.name), template.expressions[0])
60
- )
55
+ const stmt1 = b.$effect([
56
+ b.assignment(b.member(bindingId, node.name), template.expressions[0])
57
+ ])
58
+ const stmt2 = b.$effect([
59
+ b.assignment(template.expressions[0], b.member(bindingId, node.name))
61
60
  ])
62
- ctx.state.effects.push(stmt)
63
61
 
64
- const stmt2 = b.$getBindingProp(
65
- bindingId,
66
- node.name,
67
- b.assignment(template.expressions[0], b.id('v'))
68
- )
69
- ctx.state.changed.push(stmt2)
62
+ ctx.state.effects.push(stmt1, stmt2)
70
63
  } else {
71
64
  const stmt = b.$effect([
72
65
  b.setAttribute(elementId, b.literal(node.name), template.expressions[0])
@@ -32,7 +32,7 @@ export function IfBlock(node, ctx) {
32
32
 
33
33
  return [...stmts1, ...stmts2, stmt3]
34
34
  }
35
- return blocks
35
+ return [...init.elem, ...blocks]
36
36
  }
37
37
  }
38
38
 
@@ -49,7 +49,11 @@ export function IfBlock(node, ctx) {
49
49
  ctx.state.blocks.push(blockStmt)
50
50
  appendText(ctx.state.template, '<!-- -->')
51
51
  } else {
52
- const blockStmt = b.ifBlock(b.id('anchor'), testStmt, consequentStmt, alternateStmt)
52
+ const anchorId = nextElementId(ctx)
53
+ const anchorStmt = b.declaration(anchorId, b.insertBefore('anchor', b.createComment()))
54
+ const blockStmt = b.ifBlock(anchorId, testStmt, consequentStmt, alternateStmt)
55
+
56
+ ctx.state.init.elem.push(anchorStmt)
53
57
  ctx.state.blocks.push(blockStmt)
54
58
  }
55
59
  }
@@ -1,4 +1,5 @@
1
1
  import * as b from '../../builders.js'
2
+ import { getTemplate } from '../context.js'
2
3
 
3
4
  export function Selector(node, ctx) {
4
5
  node = CssTreeNodeFix(node, ctx)
@@ -11,6 +12,22 @@ export function Selector(node, ctx) {
11
12
  scope()
12
13
  children.push(child)
13
14
  break
15
+ case 'PseudoClassSelector':
16
+ if (child.name === 'host') {
17
+ const template = getTemplate(ctx)
18
+
19
+ if (!template.metadata?.shadowRootMode) {
20
+ children.push({
21
+ type: 'TypeSelector',
22
+ name:
23
+ template.metadata?.customElementName ??
24
+ ctx.state.context.customElementName
25
+ })
26
+ } else {
27
+ children.push(child)
28
+ }
29
+ break
30
+ }
14
31
  default:
15
32
  unscoped.push(child)
16
33
  break
@@ -35,7 +52,7 @@ export const CssTree = {
35
52
  PseudoClassSelector: CssTreeNodeFix
36
53
  }
37
54
 
38
- function CssTreeNodeFix(node, ctx) {
55
+ export function CssTreeNodeFix(node, ctx) {
39
56
  const children = node.children?.map((c) => ctx.visit(c)) ?? null
40
57
  return { ...node, children }
41
58
  }