@motifx/compiler 1.2.0-rc.3 → 1.2.0-rc.5

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/dist/index.cjs CHANGED
@@ -457,7 +457,7 @@ const motifComponent = () => { return "_mc"; };
457
457
  const motifFragment = () => { return "_mf"; };
458
458
  const motifFunctionComponent = () => { return "_mfc"; };
459
459
  const motifCompiled = () => { return "_mv"; };
460
- const COMPILER_CONTRACT = 2;
460
+ const COMPILER_CONTRACT = 1;
461
461
 
462
462
  let active$1 = null;
463
463
  let seen = null;
@@ -536,11 +536,64 @@ const makeHtmlAttr = (prop, name, value, path) => {
536
536
  }
537
537
  return null;
538
538
  };
539
+ function classKeyOf(p) {
540
+ if (p.computed)
541
+ return p.key;
542
+ if (t__namespace.isIdentifier(p.key))
543
+ return t__namespace.stringLiteral(p.key.name);
544
+ if (t__namespace.isStringLiteral(p.key))
545
+ return t__namespace.stringLiteral(p.key.value);
546
+ if (t__namespace.isNumericLiteral(p.key))
547
+ return t__namespace.stringLiteral(String(p.key.value));
548
+ return null;
549
+ }
550
+ function literalTruthOf(val) {
551
+ if (t__namespace.isBooleanLiteral(val))
552
+ return val.value;
553
+ if (t__namespace.isStringLiteral(val))
554
+ return val.value !== '';
555
+ if (t__namespace.isNumericLiteral(val))
556
+ return val.value !== 0 && !Number.isNaN(val.value);
557
+ if (t__namespace.isNullLiteral(val))
558
+ return false;
559
+ if (t__namespace.isIdentifier(val) && val.name === 'undefined')
560
+ return false;
561
+ if (t__namespace.isTemplateLiteral(val) && val.expressions.length === 0)
562
+ return val.quasis.some(q => (q.value.cooked ?? q.value.raw) !== '');
563
+ return undefined;
564
+ }
565
+ function splitClassObject(obj, staticNames, live) {
566
+ const props = [];
567
+ obj.properties.forEach(p => {
568
+ if (!t__namespace.isObjectProperty(p)) {
569
+ props.push(p);
570
+ return;
571
+ }
572
+ const key = classKeyOf(p);
573
+ if (key === null || !t__namespace.isExpression(p.value))
574
+ return;
575
+ const truth = literalTruthOf(p.value);
576
+ if (truth === false)
577
+ return;
578
+ if (truth === true) {
579
+ if (t__namespace.isStringLiteral(key))
580
+ staticNames.push(key);
581
+ else
582
+ props.push(t__namespace.objectProperty(key, t__namespace.booleanLiteral(true), true));
583
+ return;
584
+ }
585
+ const getter = t__namespace.isFunctionExpression(p.value) || t__namespace.isArrowFunctionExpression(p.value)
586
+ ? p.value
587
+ : t__namespace.arrowFunctionExpression([], p.value);
588
+ props.push(t__namespace.objectProperty(key, getter, p.computed));
589
+ });
590
+ if (props.length > 0)
591
+ live.push(t__namespace.objectExpression(props));
592
+ }
539
593
  const mergeHtmlAttributes = (htmlAttrlist) => {
540
594
  const htmlProps = [];
541
595
  if (htmlAttrlist.length > 0) {
542
596
  var totalAttr = [];
543
- var totalClass = [];
544
597
  var totalBindClass = [];
545
598
  var totalStringClass = [];
546
599
  htmlAttrlist.forEach(attr => {
@@ -550,38 +603,7 @@ const mergeHtmlAttributes = (htmlAttrlist) => {
550
603
  : attr.key.quasis.map(q => q.value.raw).join("").toLowerCase();
551
604
  if (keyValue === "class" || keyValue === "classname") {
552
605
  if (t__namespace.isObjectExpression(attr.value)) {
553
- const objectProperties = [];
554
- attr.value.properties.forEach(p => {
555
- if (t__namespace.isObjectProperty(p)) {
556
- const val = p.value;
557
- if (t__namespace.isStringLiteral(val)) {
558
- totalStringClass.push(p.key);
559
- }
560
- else if (t__namespace.isBooleanLiteral(val)) {
561
- totalStringClass.push(p.key);
562
- }
563
- else if (t__namespace.isFunctionExpression(val) || t__namespace.isArrowFunctionExpression(val)) {
564
- objectProperties.push(p);
565
- }
566
- else if (t__namespace.isTemplateLiteral(val)) {
567
- if (val.expressions.length === 0) {
568
- totalStringClass.push(t__namespace.stringLiteral(val.quasis.map(q => q.value.raw).join("")));
569
- }
570
- else {
571
- totalBindClass.push(t__namespace.arrowFunctionExpression([], val));
572
- }
573
- }
574
- else {
575
- totalClass.push(p);
576
- }
577
- }
578
- else if (t__namespace.isFunctionExpression(p) || t__namespace.isArrowFunctionExpression(p)) {
579
- totalBindClass.push(p);
580
- }
581
- });
582
- if (objectProperties.length > 0) {
583
- totalBindClass.push(t__namespace.objectExpression(objectProperties));
584
- }
606
+ splitClassObject(attr.value, totalStringClass, totalBindClass);
585
607
  }
586
608
  else if (t__namespace.isMemberExpression(attr.value) || t__namespace.isCallExpression(attr.value)) {
587
609
  totalBindClass.push(attr.value);
@@ -608,34 +630,7 @@ const mergeHtmlAttributes = (htmlAttrlist) => {
608
630
  if (!element)
609
631
  return;
610
632
  if (t__namespace.isObjectExpression(element)) {
611
- element.properties.forEach(p => {
612
- if (t__namespace.isObjectProperty(p)) {
613
- const val = p.value;
614
- if (t__namespace.isStringLiteral(val)) {
615
- totalStringClass.push(p.key);
616
- }
617
- else if (t__namespace.isBooleanLiteral(val)) {
618
- totalStringClass.push(p.key);
619
- }
620
- else if (t__namespace.isFunctionExpression(val) || t__namespace.isArrowFunctionExpression(val)) {
621
- totalBindClass.push(val);
622
- }
623
- else if (t__namespace.isTemplateLiteral(val)) {
624
- if (val.expressions.length === 0) {
625
- totalStringClass.push(t__namespace.stringLiteral(val.quasis.map(q => q.value.raw).join("")));
626
- }
627
- else {
628
- totalBindClass.push(t__namespace.arrowFunctionExpression([], val));
629
- }
630
- }
631
- else {
632
- totalClass.push(p);
633
- }
634
- }
635
- else if (t__namespace.isFunctionExpression(p) || t__namespace.isArrowFunctionExpression(p)) {
636
- totalBindClass.push(p);
637
- }
638
- });
633
+ splitClassObject(element, totalStringClass, totalBindClass);
639
634
  }
640
635
  else if (t__namespace.isStringLiteral(element)) {
641
636
  totalStringClass.push(element);
@@ -724,9 +719,6 @@ const mergeHtmlAttributes = (htmlAttrlist) => {
724
719
  if (totalBindClass.length > 0) {
725
720
  htmlProps.push(t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.identifier('sender.class.add'), [t__namespace.arrayExpression(totalBindClass)])));
726
721
  }
727
- if (totalClass.length > 0) {
728
- htmlProps.push(t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.identifier('sender.class.add'), [t__namespace.objectExpression(totalClass)])));
729
- }
730
722
  if (totalStringClass.length > 0) {
731
723
  htmlProps.push(t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.identifier('sender.class.add'), [...totalStringClass])));
732
724
  }
@@ -1424,6 +1416,19 @@ function lifecycleHookOf(name) {
1424
1416
  hook = lower.slice(2);
1425
1417
  return hook !== null && LIFECYCLE_HOOKS.has(hook) ? hook : null;
1426
1418
  }
1419
+ const HOOK_KEYS = {
1420
+ built: 'onBuilt', building: 'onBuilding', mounted: 'onMounted', config: 'onConfig', configured: 'onConfigured',
1421
+ initializing: 'onInitializing', initialized: 'onInitialized', disposing: 'onDisposing', disposed: 'onDisposed',
1422
+ visibilitychanged: 'onVisibilityChanged', activated: 'onActivated', deactivated: 'onDeactivated',
1423
+ };
1424
+ function hookKey(name) {
1425
+ const hook = lifecycleHookOf(name);
1426
+ if (hook !== null)
1427
+ return HOOK_KEYS[hook];
1428
+ if (name.toLowerCase() === 'oninitializecomponent')
1429
+ return 'oninitializeComponent';
1430
+ return name;
1431
+ }
1427
1432
  /** `.some/.every/.find/...` gibi KISA DEVRE yapan dizi yüklemleri (MJX004). */
1428
1433
  const SHORT_CIRCUIT_ARRAY_METHODS = new Set(['some', 'every', 'find', 'findindex', 'findlast', 'findlastindex']);
1429
1434
  /** Bir üye zincirinin kökü `this` mi? (`this.state.rows.some(...)` → evet) */
@@ -1733,14 +1738,14 @@ const getProps = (props, path, state) => {
1733
1738
  if (value != null) {
1734
1739
  var ce = makeComponentTraps(prop, name, value);
1735
1740
  if (ce !== null) {
1741
+ ce.key = id(hookKey(name));
1736
1742
  componentEvents.push(ce);
1737
1743
  explainProp(prop, 'component-event', 'lifecycle', ce, 'n/a', DEPS.none, 'lifecycle hook; called once during component setup/build/dispose');
1738
1744
  }
1739
1745
  }
1740
1746
  }
1741
1747
  else if (!plainTag && mergedHook !== null && value != null && t__namespace.isExpression(value)) {
1742
- const key = name.toLowerCase().startsWith('on') ? name : 'on' + mergedHook;
1743
- const lifecycleProp = t__namespace.objectProperty(t__namespace.isValidIdentifier(key) ? id(key) : t__namespace.stringLiteral(key), value);
1748
+ const lifecycleProp = t__namespace.objectProperty(id(HOOK_KEYS[mergedHook]), value);
1744
1749
  componentEvents.push(lifecycleProp);
1745
1750
  explainProp(prop, 'component-event', 'lifecycle', lifecycleProp, 'n/a', DEPS.none, 'lifecycle hook; on a component tag it is applied via runover to the tag\'s component (the returned root for a function component)');
1746
1751
  }
@@ -1873,7 +1878,7 @@ const getProps = (props, path, state) => {
1873
1878
  warn('MJX007', `Unknown directive "${name}": it is passed on as the prop "on${clearedName}". Supported: ${supportedDirectivesText()}.`, prop, fileName);
1874
1879
  }
1875
1880
  if (t__namespace.isExpression(value)) {
1876
- objectExpression.push(t__namespace.objectProperty(id("on" + clearedName), value));
1881
+ objectExpression.push(t__namespace.objectProperty(id(hookKey("on" + clearedName)), value));
1877
1882
  }
1878
1883
  else {
1879
1884
  objectProps.push(value);
@@ -1926,10 +1931,17 @@ const getProps = (props, path, state) => {
1926
1931
  if (atr !== null) {
1927
1932
  htmlAttrlist.push(atr);
1928
1933
  const lname = name.toLowerCase();
1929
- const c = classifyAttrValue(prop, value, atr);
1930
- const call = lname === 'class' || lname === 'classname' ? 'sender.class.add' : lname === 'style' ? 'sender.style' : 'sender.attr.add';
1931
- const shown = call === 'sender.attr.add' ? `${call}({ "${name}": ${codeOf(atr.value)} })` : `${call}(${codeOf(atr.value)})`;
1932
- explainProp(prop, 'attr', c.shape, shown, c.reactive, c.deps, c.note);
1934
+ if ((lname === 'class' || lname === 'classname') && (t__namespace.isObjectExpression(atr.value) || t__namespace.isArrayExpression(atr.value))) {
1935
+ const lowered = mergeHtmlAttributes([atr]);
1936
+ const live = lowered.some(s => t__namespace.isCallExpression(s.expression) && t__namespace.isArrayExpression(s.expression.arguments[0]));
1937
+ explainProp(prop, 'attr', 'class.object', lowered.map(s => codeOf(s.expression)).join('; '), live ? 'live' : 'static', live ? DEPS.getter : DEPS.none, live ? 'each key toggles its class; values other than literals are wrapped in getters' : undefined);
1938
+ }
1939
+ else {
1940
+ const c = classifyAttrValue(prop, value, atr);
1941
+ const call = lname === 'class' || lname === 'classname' ? 'sender.class.add' : lname === 'style' ? 'sender.style' : 'sender.attr.add';
1942
+ const shown = call === 'sender.attr.add' ? `${call}({ "${name}": ${codeOf(atr.value)} })` : `${call}(${codeOf(atr.value)})`;
1943
+ explainProp(prop, 'attr', c.shape, shown, c.reactive, c.deps, c.note);
1944
+ }
1933
1945
  }
1934
1946
  }
1935
1947
  else {
@@ -1959,7 +1971,7 @@ const getProps = (props, path, state) => {
1959
1971
  objectExpression.push(t__namespace.objectProperty(t__namespace.stringLiteral(name), value));
1960
1972
  }
1961
1973
  else {
1962
- objectExpression.push(t__namespace.objectProperty(id(name), value));
1974
+ objectExpression.push(t__namespace.objectProperty(id(hookKey(name)), value));
1963
1975
  }
1964
1976
  {
1965
1977
  const c = classifyPropValue(prop, value);
@@ -1988,7 +2000,7 @@ const getProps = (props, path, state) => {
1988
2000
  mergedHooks.forEach((entries, hook) => {
1989
2001
  if (entries.length === 0)
1990
2002
  return;
1991
- const merged = t__namespace.objectProperty(id('on' + hook), t__namespace.arrayExpression(entries.map(e => e.value)));
2003
+ const merged = t__namespace.objectProperty(id(HOOK_KEYS[hook]), t__namespace.arrayExpression(entries.map(e => e.value)));
1992
2004
  componentEvents.push(merged);
1993
2005
  entries.forEach(e => explainProp(e.prop, 'component-event', 'lifecycle', merged, 'n/a', DEPS.none, 'all spellings of the same hook on this tag are merged into one list; each is called once, in source order'));
1994
2006
  });
@@ -3194,7 +3206,7 @@ class Compiler {
3194
3206
  return;
3195
3207
  if (!t__namespace.isArrowFunctionExpression(only.value) && !t__namespace.isFunctionExpression(only.value))
3196
3208
  return;
3197
- node.arguments[1] = only.value;
3209
+ node.arguments.push(t__namespace.numericLiteral(1));
3198
3210
  }
3199
3211
  });
3200
3212
  absorbGeneratedCoreImports(topLevel, coreImports);