babel-plugin-essor 0.0.0 → 0.0.2

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.js CHANGED
@@ -14904,55 +14904,7 @@ init_esm_shims();
14904
14904
 
14905
14905
  // src/transform.ts
14906
14906
  init_esm_shims();
14907
- import { types as t2 } from "@babel/core";
14908
-
14909
- // src/shared.ts
14910
- init_esm_shims();
14911
14907
  import { types as t } from "@babel/core";
14912
- var imports = /* @__PURE__ */ new Set();
14913
-
14914
- // src/transform.ts
14915
- function transformProgram() {
14916
- return {
14917
- enter(path) {
14918
- path.state = {
14919
- h: path.scope.generateUidIdentifier("h$"),
14920
- signal: path.scope.generateUidIdentifier("signal$"),
14921
- computed: path.scope.generateUidIdentifier("computed$"),
14922
- template: path.scope.generateUidIdentifier("template$"),
14923
- toSignalObject: path.scope.generateUidIdentifier("toSignalObject&"),
14924
- tmplDeclaration: t2.variableDeclaration("const", [])
14925
- };
14926
- imports.clear();
14927
- },
14928
- exit(path) {
14929
- const state = path.state;
14930
- if (state.tmplDeclaration.declarations.length > 0) {
14931
- const index = path.node.body.findIndex(
14932
- (node) => !t2.isImportDeclaration(node) && !t2.isExportDeclaration(node)
14933
- );
14934
- path.node.body.splice(index, 0, state.tmplDeclaration);
14935
- }
14936
- if (imports.size > 0) {
14937
- path.node.body.unshift(createImport(state, "essor"));
14938
- }
14939
- }
14940
- };
14941
- }
14942
- function createImport(state, from) {
14943
- const ImportSpecifier = [];
14944
- imports.forEach((name) => {
14945
- const local = t2.identifier(state[name].name);
14946
- const imported = t2.identifier(name);
14947
- ImportSpecifier.push(t2.importSpecifier(local, imported));
14948
- });
14949
- const importSource = t2.stringLiteral(from);
14950
- return t2.importDeclaration(ImportSpecifier, importSource);
14951
- }
14952
-
14953
- // src/jsx-transform.ts
14954
- init_esm_shims();
14955
- import { types as t3 } from "@babel/core";
14956
14908
 
14957
14909
  // src/constants.ts
14958
14910
  init_esm_shims();
@@ -14993,8 +14945,48 @@ var svgTags = [
14993
14945
  "text",
14994
14946
  "use"
14995
14947
  ];
14948
+ var imports = /* @__PURE__ */ new Set();
14949
+
14950
+ // src/transform.ts
14951
+ var transformProgram = {
14952
+ enter(path) {
14953
+ path.state = {
14954
+ h: path.scope.generateUidIdentifier("h$"),
14955
+ signal: path.scope.generateUidIdentifier("signal$"),
14956
+ computed: path.scope.generateUidIdentifier("computed$"),
14957
+ template: path.scope.generateUidIdentifier("template$"),
14958
+ toSignalObject: path.scope.generateUidIdentifier("toSignalObject&"),
14959
+ tmplDeclaration: t.variableDeclaration("const", [])
14960
+ };
14961
+ imports.clear();
14962
+ },
14963
+ exit(path) {
14964
+ const state = path.state;
14965
+ if (state.tmplDeclaration.declarations.length > 0) {
14966
+ const index = path.node.body.findIndex(
14967
+ (node) => !t.isImportDeclaration(node) && !t.isExportDeclaration(node)
14968
+ );
14969
+ path.node.body.splice(index, 0, state.tmplDeclaration);
14970
+ }
14971
+ if (imports.size > 0) {
14972
+ path.node.body.unshift(createImport(state, "essor"));
14973
+ }
14974
+ }
14975
+ };
14976
+ function createImport(state, from) {
14977
+ const ImportSpecifier = [];
14978
+ imports.forEach((name) => {
14979
+ const local = t.identifier(state[name].name);
14980
+ const imported = t.identifier(name);
14981
+ ImportSpecifier.push(t.importSpecifier(local, imported));
14982
+ });
14983
+ const importSource = t.stringLiteral(from);
14984
+ return t.importDeclaration(ImportSpecifier, importSource);
14985
+ }
14996
14986
 
14997
14987
  // src/jsx-transform.ts
14988
+ init_esm_shims();
14989
+ import { types as t2 } from "@babel/core";
14998
14990
  function transformJSX(path) {
14999
14991
  const result = {
15000
14992
  index: 1,
@@ -15011,11 +15003,11 @@ function createEssorNode(path, result) {
15011
15003
  const state = path.state;
15012
15004
  let tmpl;
15013
15005
  if (path.isJSXElement() && isComponent(getTagName(path.node))) {
15014
- tmpl = t3.identifier(getTagName(path.node));
15006
+ tmpl = t2.identifier(getTagName(path.node));
15015
15007
  } else {
15016
15008
  tmpl = path.scope.generateUidIdentifier("_tmpl$");
15017
- const template = t3.callExpression(state.template, [t3.stringLiteral(result.template)]);
15018
- const declarator = t3.variableDeclarator(tmpl, template);
15009
+ const template = t2.callExpression(state.template, [t2.stringLiteral(result.template)]);
15010
+ const declarator = t2.variableDeclarator(tmpl, template);
15019
15011
  state.tmplDeclaration.declarations.push(declarator);
15020
15012
  imports.add("template");
15021
15013
  }
@@ -15025,7 +15017,7 @@ function createEssorNode(path, result) {
15025
15017
  args.push(key);
15026
15018
  }
15027
15019
  imports.add("h");
15028
- return t3.callExpression(state.h, args);
15020
+ return t2.callExpression(state.h, args);
15029
15021
  }
15030
15022
  function createProps(props) {
15031
15023
  const result = [];
@@ -15035,33 +15027,33 @@ function createProps(props) {
15035
15027
  continue;
15036
15028
  }
15037
15029
  if (Array.isArray(value)) {
15038
- value = t3.arrayExpression(value);
15030
+ value = t2.arrayExpression(value);
15039
15031
  }
15040
- if (typeof value === "object" && value !== null && !t3.isNode(value)) {
15032
+ if (typeof value === "object" && value !== null && !t2.isNode(value)) {
15041
15033
  value = createProps(value);
15042
15034
  }
15043
15035
  if (typeof value === "string") {
15044
- value = t3.stringLiteral(value);
15036
+ value = t2.stringLiteral(value);
15045
15037
  }
15046
15038
  if (typeof value === "number") {
15047
- value = t3.numericLiteral(value);
15039
+ value = t2.numericLiteral(value);
15048
15040
  }
15049
15041
  if (typeof value === "boolean") {
15050
- value = t3.booleanLiteral(value);
15042
+ value = t2.booleanLiteral(value);
15051
15043
  }
15052
15044
  if (value === void 0) {
15053
- value = t3.tsUndefinedKeyword();
15045
+ value = t2.tsUndefinedKeyword();
15054
15046
  }
15055
15047
  if (value === null) {
15056
- value = t3.nullLiteral();
15048
+ value = t2.nullLiteral();
15057
15049
  }
15058
15050
  if (prop === "_$spread$") {
15059
- result.push(t3.spreadElement(value));
15051
+ result.push(t2.spreadElement(value));
15060
15052
  } else {
15061
- result.push(t3.objectProperty(t3.stringLiteral(prop), value));
15053
+ result.push(t2.objectProperty(t2.stringLiteral(prop), value));
15062
15054
  }
15063
15055
  }
15064
- return t3.objectExpression(result);
15056
+ return t2.objectExpression(result);
15065
15057
  }
15066
15058
  function getAttrProps(path) {
15067
15059
  const props = {};
@@ -15087,15 +15079,15 @@ function getAttrProps(path) {
15087
15079
  if (/^key|ref|on.+$/.test(name)) {
15088
15080
  props[name] = expression.node;
15089
15081
  } else if (/^bind:.+/.test(name)) {
15090
- props[name] = t3.arrowFunctionExpression([], expression.node);
15082
+ props[name] = t2.arrowFunctionExpression([], expression.node);
15091
15083
  const value2 = path.scope.generateUidIdentifier("value");
15092
- props[`update:${name.slice(5).toLocaleLowerCase()}`] = t3.arrowFunctionExpression(
15084
+ props[`update:${name.slice(5).toLocaleLowerCase()}`] = t2.arrowFunctionExpression(
15093
15085
  [value2],
15094
- t3.assignmentExpression("=", expression.node, value2)
15086
+ t2.assignmentExpression("=", expression.node, value2)
15095
15087
  );
15096
15088
  } else {
15097
15089
  if (expression.isConditionalExpression()) {
15098
- props[name] = t3.arrowFunctionExpression([], expression.node);
15090
+ props[name] = t2.arrowFunctionExpression([], expression.node);
15099
15091
  } else {
15100
15092
  props[name] = expression.node;
15101
15093
  }
@@ -15126,7 +15118,7 @@ function transformElement(path, result, isRoot) {
15126
15118
  result.props = props;
15127
15119
  const children = getChildren(path);
15128
15120
  if (children.length > 0) {
15129
- const childrenGetter = children.length === 1 ? children[0] : t3.arrayExpression(children);
15121
+ const childrenGetter = children.length === 1 ? children[0] : t2.arrayExpression(children);
15130
15122
  result.props.children = childrenGetter;
15131
15123
  }
15132
15124
  } else {
@@ -15157,7 +15149,7 @@ function getChildren(path) {
15157
15149
  } else if (child.isJSXExpressionContainer()) {
15158
15150
  child.replaceWith(child.get("expression"));
15159
15151
  } else if (child.isJSXText()) {
15160
- child.replaceWith(t3.stringLiteral(child.node.value));
15152
+ child.replaceWith(t2.stringLiteral(child.node.value));
15161
15153
  } else {
15162
15154
  throw new Error("Unsupported JSX child");
15163
15155
  }
@@ -15268,17 +15260,17 @@ function replaceChild(node, result) {
15268
15260
  (_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
15269
15261
  (_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
15270
15262
  result.props[result.parentIndex].children.push(
15271
- t3.arrayExpression([
15272
- t3.arrowFunctionExpression([], node),
15273
- result.isLastChild ? t3.nullLiteral() : t3.identifier(String(result.index))
15263
+ t2.arrayExpression([
15264
+ t2.arrowFunctionExpression([], node),
15265
+ result.isLastChild ? t2.nullLiteral() : t2.identifier(String(result.index))
15274
15266
  ])
15275
15267
  );
15276
15268
  }
15277
15269
  function getAttrName(attribute) {
15278
- if (t3.isJSXIdentifier(attribute.name)) {
15270
+ if (t2.isJSXIdentifier(attribute.name)) {
15279
15271
  return attribute.name.name;
15280
15272
  }
15281
- if (t3.isJSXNamespacedName(attribute.name)) {
15273
+ if (t2.isJSXNamespacedName(attribute.name)) {
15282
15274
  return `${attribute.name.namespace.name}:${attribute.name.name.name}`;
15283
15275
  }
15284
15276
  throw new Error("Unsupported attribute type");
@@ -15302,7 +15294,7 @@ function setNodeText(path, text) {
15302
15294
  if (path.isJSXExpressionContainer()) {
15303
15295
  const expression = path.get("expression");
15304
15296
  if (expression.isStringLiteral() || expression.isNumericLiteral()) {
15305
- expression.replaceWith(t3.stringLiteral(text));
15297
+ expression.replaceWith(t2.stringLiteral(text));
15306
15298
  }
15307
15299
  }
15308
15300
  }
@@ -15333,180 +15325,42 @@ function isValidChild(path) {
15333
15325
  return Object.keys(path.node).length > 0;
15334
15326
  }
15335
15327
  function jsxElementNameToString(node) {
15336
- if (t3.isJSXMemberExpression(node)) {
15328
+ if (t2.isJSXMemberExpression(node)) {
15337
15329
  return `${jsxElementNameToString(node.object)}.${node.property.name}`;
15338
15330
  }
15339
- if (t3.isJSXIdentifier(node) || t3.isIdentifier(node)) {
15331
+ if (t2.isJSXIdentifier(node) || t2.isIdentifier(node)) {
15340
15332
  return node.name;
15341
15333
  }
15342
15334
  return `${node.namespace.name}:${node.name.name}`;
15343
15335
  }
15344
15336
 
15345
- // src/signal/props.ts
15346
- init_esm_shims();
15347
- import { types as t4 } from "@babel/core";
15348
- function replaceProps(path) {
15349
- const firstParam = path.node.params[0];
15350
- if (!t4.isReturnStatement(path.node.body)) {
15351
- return;
15352
- }
15353
- if (!firstParam) {
15354
- return;
15355
- }
15356
- if (!t4.isObjectPattern(firstParam)) {
15357
- return;
15358
- }
15359
- const returnStatement = path.get("body").get("body").find((statement) => statement.isReturnStatement());
15360
- if (!returnStatement) {
15361
- return;
15362
- }
15363
- const returnValue = returnStatement.node.argument;
15364
- if (!t4.isJSXElement(returnValue)) {
15365
- return;
15366
- }
15367
- const properties = firstParam.properties;
15368
- const propsDeclaration = t4.variableDeclaration("const", [
15369
- t4.variableDeclarator(
15370
- t4.objectPattern(properties),
15371
- t4.callExpression(t4.identifier("$"), [t4.identifier("props")])
15372
- )
15373
- ]);
15374
- path.node.params[0] = t4.identifier("props");
15375
- path.node.body.body.unshift(propsDeclaration);
15376
- }
15377
-
15378
- // src/signal/symbol.ts
15337
+ // src/symbol-replace.ts
15379
15338
  init_esm_shims();
15380
15339
  var import_types = __toESM(require_lib3(), 1);
15381
- import { types as t6 } from "@babel/core";
15382
-
15383
- // src/signal/pattern.ts
15384
- init_esm_shims();
15385
- import { types as t5 } from "@babel/core";
15386
- function replaceObjectPattern(path, properties) {
15387
- properties.forEach((prop) => {
15388
- var _a, _b, _c;
15389
- if (prop.type === "RestElement") {
15390
- const variableName2 = prop.argument.name;
15391
- path.scope.traverse(path.scope.block, {
15392
- MemberExpression(memberPath) {
15393
- if (memberPath.node.object.name === variableName2) {
15394
- memberPath.node.object = t5.identifier(`${variableName2}.value`);
15395
- }
15396
- }
15397
- });
15398
- return;
15399
- }
15400
- if (((_a = prop.value) == null ? void 0 : _a.type) === "ObjectPattern") {
15401
- replaceObjectPattern(path, prop.value.properties);
15402
- }
15403
- const variableName = ((_b = prop == null ? void 0 : prop.value) == null ? void 0 : _b.name) || ((_c = prop == null ? void 0 : prop.argument) == null ? void 0 : _c.name);
15404
- if (!variableName) {
15405
- return;
15406
- }
15407
- path.scope.rename(variableName, `${variableName}.value`);
15408
- if (prop.value) {
15409
- prop.value = t5.identifier(`${variableName}`);
15410
- }
15411
- if (prop.argument) {
15412
- prop.argument = t5.identifier(`${variableName}`);
15413
- }
15414
- });
15415
- }
15416
- function replaceArrayPattern(path, elements) {
15417
- elements.forEach((prop, index) => {
15418
- var _a;
15419
- if (prop.type === "RestElement") {
15420
- const variableName2 = prop.argument.name;
15421
- path.scope.traverse(path.scope.block, {
15422
- MemberExpression(memberPath) {
15423
- if (memberPath.node.object.name === variableName2) {
15424
- memberPath.node.object = t5.identifier(`${variableName2}.value`);
15425
- }
15426
- }
15427
- });
15428
- return;
15429
- }
15430
- if (t5.isObjectPattern(prop)) {
15431
- replaceObjectPattern(path, prop.properties);
15432
- }
15433
- if (t5.isArrayPattern(prop)) {
15434
- replaceArrayPattern(path, prop.elements);
15435
- }
15436
- const variableName = prop.name || ((_a = prop == null ? void 0 : prop.argument) == null ? void 0 : _a.name);
15437
- if (!variableName) {
15438
- return;
15439
- }
15440
- path.scope.rename(variableName, `${variableName}.value`);
15441
- if (t5.isIdentifier(prop)) {
15442
- elements[index] = t5.memberExpression(t5.identifier(variableName), t5.identifier("value"));
15443
- }
15444
- });
15445
- }
15446
- function replacePatternToReactive(path) {
15447
- if (!t5.isObjectPattern(path.node.id) && !t5.isArrayPattern(path.node.id)) {
15448
- return;
15449
- }
15450
- const init = path.get("init");
15451
- const argument = init.get("arguments")[0];
15452
- let callee = init.get("callee");
15453
- if (Array.isArray(callee)) {
15454
- callee = callee[0];
15455
- }
15456
- if (!init.isCallExpression()) {
15457
- return;
15458
- }
15459
- if ((argument == null ? void 0 : argument.node) && callee.isIdentifier({ name: "$" })) {
15460
- init.replaceWith(
15461
- t5.callExpression(
15462
- t5.identifier(path.state.toSignalObject.name),
15463
- [argument == null ? void 0 : argument.node].filter(Boolean)
15464
- )
15465
- );
15466
- if (t5.isArrayPattern(path.node.id)) {
15467
- replaceArrayPattern(path, path.node.id.elements);
15468
- } else if (t5.isObjectPattern(path.node.id)) {
15469
- replaceObjectPattern(path, path.node.id.properties);
15470
- }
15471
- } else if (t5.isCallExpression(callee.node) && callee.node.name && callee.node.name.indexOf("$") === 0) {
15472
- init.replaceWith(
15473
- t5.callExpression(t5.identifier(path.state.toSignalObject.name), [
15474
- t5.callExpression(callee.node, [argument == null ? void 0 : argument.node].filter(Boolean))
15475
- ])
15476
- );
15477
- if (t5.isArrayPattern(path.node.id)) {
15478
- replaceArrayPattern(path, path.node.id.elements);
15479
- } else if (t5.isObjectPattern(path.node.id)) {
15480
- replaceObjectPattern(path, path.node.id.properties);
15481
- }
15482
- }
15483
- }
15484
-
15485
- // src/signal/symbol.ts
15340
+ import { types as t3 } from "@babel/core";
15486
15341
  function replaceSymbol(path) {
15487
15342
  const init = path.node.init;
15488
15343
  const variableName = path.node.id.name;
15489
- if (t6.isObjectPattern(path.node.id) || t6.isArrayPattern(path.node.id)) {
15490
- replacePatternToReactive(path);
15344
+ if (t3.isObjectPattern(path.node.id) || t3.isArrayPattern(path.node.id)) {
15491
15345
  return;
15492
15346
  }
15493
15347
  if (!variableName.startsWith("$")) {
15494
15348
  return;
15495
15349
  }
15496
- if (init && (t6.isFunctionExpression(init) || t6.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
15497
- const newInit = t6.callExpression(t6.identifier(path.state.computed.name), init ? [init] : []);
15350
+ if (init && (t3.isFunctionExpression(init) || t3.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
15351
+ const newInit = t3.callExpression(t3.identifier(path.state.computed.name), init ? [init] : []);
15498
15352
  imports.add("computed");
15499
15353
  path.node.init = newInit;
15500
15354
  } else {
15501
15355
  const originalImportDeclarationNodes = (0, import_types.cloneNode)(path.get("id").node, true);
15502
- const newInit = t6.callExpression(t6.identifier(path.state.signal.name), init ? [init] : []);
15356
+ const newInit = t3.callExpression(t3.identifier(path.state.signal.name), init ? [init] : []);
15503
15357
  imports.add("signal");
15504
15358
  path.node.init = newInit;
15505
15359
  path.scope.rename(variableName, `${variableName}.value`);
15506
15360
  path.get("id").replaceWith(originalImportDeclarationNodes);
15507
15361
  path.scope.traverse(path.scope.block, {
15508
15362
  Identifier(innerPath) {
15509
- if (t6.isExportSpecifier(innerPath.parent)) {
15363
+ if (t3.isExportSpecifier(innerPath.parent)) {
15510
15364
  const { name } = innerPath.node;
15511
15365
  if (name.endsWith(".value")) {
15512
15366
  innerPath.node.name = name.slice(0, -6);
@@ -15517,9 +15371,9 @@ function replaceSymbol(path) {
15517
15371
  }
15518
15372
  }
15519
15373
 
15520
- // src/signal/import.ts
15374
+ // src/import-replace.ts
15521
15375
  init_esm_shims();
15522
- function replaceImportDeclaration(path) {
15376
+ function replaceImport(path) {
15523
15377
  const imports2 = path.node.specifiers;
15524
15378
  imports2.forEach((specifier) => {
15525
15379
  const variableName = specifier.local.name;
@@ -15541,12 +15395,11 @@ function src_default() {
15541
15395
  parserOpts.plugins.push("jsx");
15542
15396
  },
15543
15397
  visitor: {
15544
- Program: transformProgram(),
15398
+ Program: transformProgram,
15545
15399
  JSXElement: transformJSX,
15546
15400
  JSXFragment: transformJSX,
15547
- FunctionDeclaration: replaceProps,
15548
15401
  VariableDeclarator: replaceSymbol,
15549
- ImportDeclaration: replaceImportDeclaration
15402
+ ImportDeclaration: replaceImport
15550
15403
  }
15551
15404
  };
15552
15405
  }