babel-plugin-essor 0.0.0 → 0.0.1-beta.1

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
@@ -14915,55 +14915,7 @@ init_cjs_shims();
14915
14915
 
14916
14916
  // src/transform.ts
14917
14917
  init_cjs_shims();
14918
- var import_core2 = require("@babel/core");
14919
-
14920
- // src/shared.ts
14921
- init_cjs_shims();
14922
14918
  var import_core = require("@babel/core");
14923
- var imports = /* @__PURE__ */ new Set();
14924
-
14925
- // src/transform.ts
14926
- function transformProgram() {
14927
- return {
14928
- enter(path) {
14929
- path.state = {
14930
- h: path.scope.generateUidIdentifier("h$"),
14931
- signal: path.scope.generateUidIdentifier("signal$"),
14932
- computed: path.scope.generateUidIdentifier("computed$"),
14933
- template: path.scope.generateUidIdentifier("template$"),
14934
- toSignalObject: path.scope.generateUidIdentifier("toSignalObject&"),
14935
- tmplDeclaration: import_core2.types.variableDeclaration("const", [])
14936
- };
14937
- imports.clear();
14938
- },
14939
- exit(path) {
14940
- const state = path.state;
14941
- if (state.tmplDeclaration.declarations.length > 0) {
14942
- const index = path.node.body.findIndex(
14943
- (node) => !import_core2.types.isImportDeclaration(node) && !import_core2.types.isExportDeclaration(node)
14944
- );
14945
- path.node.body.splice(index, 0, state.tmplDeclaration);
14946
- }
14947
- if (imports.size > 0) {
14948
- path.node.body.unshift(createImport(state, "essor"));
14949
- }
14950
- }
14951
- };
14952
- }
14953
- function createImport(state, from) {
14954
- const ImportSpecifier = [];
14955
- imports.forEach((name) => {
14956
- const local = import_core2.types.identifier(state[name].name);
14957
- const imported = import_core2.types.identifier(name);
14958
- ImportSpecifier.push(import_core2.types.importSpecifier(local, imported));
14959
- });
14960
- const importSource = import_core2.types.stringLiteral(from);
14961
- return import_core2.types.importDeclaration(ImportSpecifier, importSource);
14962
- }
14963
-
14964
- // src/jsx-transform.ts
14965
- init_cjs_shims();
14966
- var import_core3 = require("@babel/core");
14967
14919
 
14968
14920
  // src/constants.ts
14969
14921
  init_cjs_shims();
@@ -15004,8 +14956,48 @@ var svgTags = [
15004
14956
  "text",
15005
14957
  "use"
15006
14958
  ];
14959
+ var imports = /* @__PURE__ */ new Set();
14960
+
14961
+ // src/transform.ts
14962
+ var transformProgram = {
14963
+ enter(path) {
14964
+ path.state = {
14965
+ h: path.scope.generateUidIdentifier("h$"),
14966
+ signal: path.scope.generateUidIdentifier("signal$"),
14967
+ computed: path.scope.generateUidIdentifier("computed$"),
14968
+ template: path.scope.generateUidIdentifier("template$"),
14969
+ toSignalObject: path.scope.generateUidIdentifier("toSignalObject&"),
14970
+ tmplDeclaration: import_core.types.variableDeclaration("const", [])
14971
+ };
14972
+ imports.clear();
14973
+ },
14974
+ exit(path) {
14975
+ const state = path.state;
14976
+ if (state.tmplDeclaration.declarations.length > 0) {
14977
+ const index = path.node.body.findIndex(
14978
+ (node) => !import_core.types.isImportDeclaration(node) && !import_core.types.isExportDeclaration(node)
14979
+ );
14980
+ path.node.body.splice(index, 0, state.tmplDeclaration);
14981
+ }
14982
+ if (imports.size > 0) {
14983
+ path.node.body.unshift(createImport(state, "essor"));
14984
+ }
14985
+ }
14986
+ };
14987
+ function createImport(state, from) {
14988
+ const ImportSpecifier = [];
14989
+ imports.forEach((name) => {
14990
+ const local = import_core.types.identifier(state[name].name);
14991
+ const imported = import_core.types.identifier(name);
14992
+ ImportSpecifier.push(import_core.types.importSpecifier(local, imported));
14993
+ });
14994
+ const importSource = import_core.types.stringLiteral(from);
14995
+ return import_core.types.importDeclaration(ImportSpecifier, importSource);
14996
+ }
15007
14997
 
15008
14998
  // src/jsx-transform.ts
14999
+ init_cjs_shims();
15000
+ var import_core2 = require("@babel/core");
15009
15001
  function transformJSX(path) {
15010
15002
  const result = {
15011
15003
  index: 1,
@@ -15022,11 +15014,11 @@ function createEssorNode(path, result) {
15022
15014
  const state = path.state;
15023
15015
  let tmpl;
15024
15016
  if (path.isJSXElement() && isComponent(getTagName(path.node))) {
15025
- tmpl = import_core3.types.identifier(getTagName(path.node));
15017
+ tmpl = import_core2.types.identifier(getTagName(path.node));
15026
15018
  } else {
15027
15019
  tmpl = path.scope.generateUidIdentifier("_tmpl$");
15028
- const template = import_core3.types.callExpression(state.template, [import_core3.types.stringLiteral(result.template)]);
15029
- const declarator = import_core3.types.variableDeclarator(tmpl, template);
15020
+ const template = import_core2.types.callExpression(state.template, [import_core2.types.stringLiteral(result.template)]);
15021
+ const declarator = import_core2.types.variableDeclarator(tmpl, template);
15030
15022
  state.tmplDeclaration.declarations.push(declarator);
15031
15023
  imports.add("template");
15032
15024
  }
@@ -15036,7 +15028,7 @@ function createEssorNode(path, result) {
15036
15028
  args.push(key);
15037
15029
  }
15038
15030
  imports.add("h");
15039
- return import_core3.types.callExpression(state.h, args);
15031
+ return import_core2.types.callExpression(state.h, args);
15040
15032
  }
15041
15033
  function createProps(props) {
15042
15034
  const result = [];
@@ -15046,33 +15038,33 @@ function createProps(props) {
15046
15038
  continue;
15047
15039
  }
15048
15040
  if (Array.isArray(value)) {
15049
- value = import_core3.types.arrayExpression(value);
15041
+ value = import_core2.types.arrayExpression(value);
15050
15042
  }
15051
- if (typeof value === "object" && value !== null && !import_core3.types.isNode(value)) {
15043
+ if (typeof value === "object" && value !== null && !import_core2.types.isNode(value)) {
15052
15044
  value = createProps(value);
15053
15045
  }
15054
15046
  if (typeof value === "string") {
15055
- value = import_core3.types.stringLiteral(value);
15047
+ value = import_core2.types.stringLiteral(value);
15056
15048
  }
15057
15049
  if (typeof value === "number") {
15058
- value = import_core3.types.numericLiteral(value);
15050
+ value = import_core2.types.numericLiteral(value);
15059
15051
  }
15060
15052
  if (typeof value === "boolean") {
15061
- value = import_core3.types.booleanLiteral(value);
15053
+ value = import_core2.types.booleanLiteral(value);
15062
15054
  }
15063
15055
  if (value === void 0) {
15064
- value = import_core3.types.tsUndefinedKeyword();
15056
+ value = import_core2.types.tsUndefinedKeyword();
15065
15057
  }
15066
15058
  if (value === null) {
15067
- value = import_core3.types.nullLiteral();
15059
+ value = import_core2.types.nullLiteral();
15068
15060
  }
15069
15061
  if (prop === "_$spread$") {
15070
- result.push(import_core3.types.spreadElement(value));
15062
+ result.push(import_core2.types.spreadElement(value));
15071
15063
  } else {
15072
- result.push(import_core3.types.objectProperty(import_core3.types.stringLiteral(prop), value));
15064
+ result.push(import_core2.types.objectProperty(import_core2.types.stringLiteral(prop), value));
15073
15065
  }
15074
15066
  }
15075
- return import_core3.types.objectExpression(result);
15067
+ return import_core2.types.objectExpression(result);
15076
15068
  }
15077
15069
  function getAttrProps(path) {
15078
15070
  const props = {};
@@ -15098,15 +15090,15 @@ function getAttrProps(path) {
15098
15090
  if (/^key|ref|on.+$/.test(name)) {
15099
15091
  props[name] = expression.node;
15100
15092
  } else if (/^bind:.+/.test(name)) {
15101
- props[name] = import_core3.types.arrowFunctionExpression([], expression.node);
15093
+ props[name] = import_core2.types.arrowFunctionExpression([], expression.node);
15102
15094
  const value2 = path.scope.generateUidIdentifier("value");
15103
- props[`update:${name.slice(5).toLocaleLowerCase()}`] = import_core3.types.arrowFunctionExpression(
15095
+ props[`update:${name.slice(5).toLocaleLowerCase()}`] = import_core2.types.arrowFunctionExpression(
15104
15096
  [value2],
15105
- import_core3.types.assignmentExpression("=", expression.node, value2)
15097
+ import_core2.types.assignmentExpression("=", expression.node, value2)
15106
15098
  );
15107
15099
  } else {
15108
15100
  if (expression.isConditionalExpression()) {
15109
- props[name] = import_core3.types.arrowFunctionExpression([], expression.node);
15101
+ props[name] = import_core2.types.arrowFunctionExpression([], expression.node);
15110
15102
  } else {
15111
15103
  props[name] = expression.node;
15112
15104
  }
@@ -15137,7 +15129,7 @@ function transformElement(path, result, isRoot) {
15137
15129
  result.props = props;
15138
15130
  const children = getChildren(path);
15139
15131
  if (children.length > 0) {
15140
- const childrenGetter = children.length === 1 ? children[0] : import_core3.types.arrayExpression(children);
15132
+ const childrenGetter = children.length === 1 ? children[0] : import_core2.types.arrayExpression(children);
15141
15133
  result.props.children = childrenGetter;
15142
15134
  }
15143
15135
  } else {
@@ -15168,7 +15160,7 @@ function getChildren(path) {
15168
15160
  } else if (child.isJSXExpressionContainer()) {
15169
15161
  child.replaceWith(child.get("expression"));
15170
15162
  } else if (child.isJSXText()) {
15171
- child.replaceWith(import_core3.types.stringLiteral(child.node.value));
15163
+ child.replaceWith(import_core2.types.stringLiteral(child.node.value));
15172
15164
  } else {
15173
15165
  throw new Error("Unsupported JSX child");
15174
15166
  }
@@ -15279,17 +15271,17 @@ function replaceChild(node, result) {
15279
15271
  (_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
15280
15272
  (_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
15281
15273
  result.props[result.parentIndex].children.push(
15282
- import_core3.types.arrayExpression([
15283
- import_core3.types.arrowFunctionExpression([], node),
15284
- result.isLastChild ? import_core3.types.nullLiteral() : import_core3.types.identifier(String(result.index))
15274
+ import_core2.types.arrayExpression([
15275
+ import_core2.types.arrowFunctionExpression([], node),
15276
+ result.isLastChild ? import_core2.types.nullLiteral() : import_core2.types.identifier(String(result.index))
15285
15277
  ])
15286
15278
  );
15287
15279
  }
15288
15280
  function getAttrName(attribute) {
15289
- if (import_core3.types.isJSXIdentifier(attribute.name)) {
15281
+ if (import_core2.types.isJSXIdentifier(attribute.name)) {
15290
15282
  return attribute.name.name;
15291
15283
  }
15292
- if (import_core3.types.isJSXNamespacedName(attribute.name)) {
15284
+ if (import_core2.types.isJSXNamespacedName(attribute.name)) {
15293
15285
  return `${attribute.name.namespace.name}:${attribute.name.name.name}`;
15294
15286
  }
15295
15287
  throw new Error("Unsupported attribute type");
@@ -15313,7 +15305,7 @@ function setNodeText(path, text) {
15313
15305
  if (path.isJSXExpressionContainer()) {
15314
15306
  const expression = path.get("expression");
15315
15307
  if (expression.isStringLiteral() || expression.isNumericLiteral()) {
15316
- expression.replaceWith(import_core3.types.stringLiteral(text));
15308
+ expression.replaceWith(import_core2.types.stringLiteral(text));
15317
15309
  }
15318
15310
  }
15319
15311
  }
@@ -15344,180 +15336,42 @@ function isValidChild(path) {
15344
15336
  return Object.keys(path.node).length > 0;
15345
15337
  }
15346
15338
  function jsxElementNameToString(node) {
15347
- if (import_core3.types.isJSXMemberExpression(node)) {
15339
+ if (import_core2.types.isJSXMemberExpression(node)) {
15348
15340
  return `${jsxElementNameToString(node.object)}.${node.property.name}`;
15349
15341
  }
15350
- if (import_core3.types.isJSXIdentifier(node) || import_core3.types.isIdentifier(node)) {
15342
+ if (import_core2.types.isJSXIdentifier(node) || import_core2.types.isIdentifier(node)) {
15351
15343
  return node.name;
15352
15344
  }
15353
15345
  return `${node.namespace.name}:${node.name.name}`;
15354
15346
  }
15355
15347
 
15356
- // src/signal/props.ts
15357
- init_cjs_shims();
15358
- var import_core4 = require("@babel/core");
15359
- function replaceProps(path) {
15360
- const firstParam = path.node.params[0];
15361
- if (!import_core4.types.isReturnStatement(path.node.body)) {
15362
- return;
15363
- }
15364
- if (!firstParam) {
15365
- return;
15366
- }
15367
- if (!import_core4.types.isObjectPattern(firstParam)) {
15368
- return;
15369
- }
15370
- const returnStatement = path.get("body").get("body").find((statement) => statement.isReturnStatement());
15371
- if (!returnStatement) {
15372
- return;
15373
- }
15374
- const returnValue = returnStatement.node.argument;
15375
- if (!import_core4.types.isJSXElement(returnValue)) {
15376
- return;
15377
- }
15378
- const properties = firstParam.properties;
15379
- const propsDeclaration = import_core4.types.variableDeclaration("const", [
15380
- import_core4.types.variableDeclarator(
15381
- import_core4.types.objectPattern(properties),
15382
- import_core4.types.callExpression(import_core4.types.identifier("$"), [import_core4.types.identifier("props")])
15383
- )
15384
- ]);
15385
- path.node.params[0] = import_core4.types.identifier("props");
15386
- path.node.body.body.unshift(propsDeclaration);
15387
- }
15388
-
15389
- // src/signal/symbol.ts
15348
+ // src/symbol-replace.ts
15390
15349
  init_cjs_shims();
15391
- var import_core6 = require("@babel/core");
15350
+ var import_core3 = require("@babel/core");
15392
15351
  var import_types = __toESM(require_lib3(), 1);
15393
-
15394
- // src/signal/pattern.ts
15395
- init_cjs_shims();
15396
- var import_core5 = require("@babel/core");
15397
- function replaceObjectPattern(path, properties) {
15398
- properties.forEach((prop) => {
15399
- var _a, _b, _c;
15400
- if (prop.type === "RestElement") {
15401
- const variableName2 = prop.argument.name;
15402
- path.scope.traverse(path.scope.block, {
15403
- MemberExpression(memberPath) {
15404
- if (memberPath.node.object.name === variableName2) {
15405
- memberPath.node.object = import_core5.types.identifier(`${variableName2}.value`);
15406
- }
15407
- }
15408
- });
15409
- return;
15410
- }
15411
- if (((_a = prop.value) == null ? void 0 : _a.type) === "ObjectPattern") {
15412
- replaceObjectPattern(path, prop.value.properties);
15413
- }
15414
- 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);
15415
- if (!variableName) {
15416
- return;
15417
- }
15418
- path.scope.rename(variableName, `${variableName}.value`);
15419
- if (prop.value) {
15420
- prop.value = import_core5.types.identifier(`${variableName}`);
15421
- }
15422
- if (prop.argument) {
15423
- prop.argument = import_core5.types.identifier(`${variableName}`);
15424
- }
15425
- });
15426
- }
15427
- function replaceArrayPattern(path, elements) {
15428
- elements.forEach((prop, index) => {
15429
- var _a;
15430
- if (prop.type === "RestElement") {
15431
- const variableName2 = prop.argument.name;
15432
- path.scope.traverse(path.scope.block, {
15433
- MemberExpression(memberPath) {
15434
- if (memberPath.node.object.name === variableName2) {
15435
- memberPath.node.object = import_core5.types.identifier(`${variableName2}.value`);
15436
- }
15437
- }
15438
- });
15439
- return;
15440
- }
15441
- if (import_core5.types.isObjectPattern(prop)) {
15442
- replaceObjectPattern(path, prop.properties);
15443
- }
15444
- if (import_core5.types.isArrayPattern(prop)) {
15445
- replaceArrayPattern(path, prop.elements);
15446
- }
15447
- const variableName = prop.name || ((_a = prop == null ? void 0 : prop.argument) == null ? void 0 : _a.name);
15448
- if (!variableName) {
15449
- return;
15450
- }
15451
- path.scope.rename(variableName, `${variableName}.value`);
15452
- if (import_core5.types.isIdentifier(prop)) {
15453
- elements[index] = import_core5.types.memberExpression(import_core5.types.identifier(variableName), import_core5.types.identifier("value"));
15454
- }
15455
- });
15456
- }
15457
- function replacePatternToReactive(path) {
15458
- if (!import_core5.types.isObjectPattern(path.node.id) && !import_core5.types.isArrayPattern(path.node.id)) {
15459
- return;
15460
- }
15461
- const init = path.get("init");
15462
- const argument = init.get("arguments")[0];
15463
- let callee = init.get("callee");
15464
- if (Array.isArray(callee)) {
15465
- callee = callee[0];
15466
- }
15467
- if (!init.isCallExpression()) {
15468
- return;
15469
- }
15470
- if ((argument == null ? void 0 : argument.node) && callee.isIdentifier({ name: "$" })) {
15471
- init.replaceWith(
15472
- import_core5.types.callExpression(
15473
- import_core5.types.identifier(path.state.toSignalObject.name),
15474
- [argument == null ? void 0 : argument.node].filter(Boolean)
15475
- )
15476
- );
15477
- if (import_core5.types.isArrayPattern(path.node.id)) {
15478
- replaceArrayPattern(path, path.node.id.elements);
15479
- } else if (import_core5.types.isObjectPattern(path.node.id)) {
15480
- replaceObjectPattern(path, path.node.id.properties);
15481
- }
15482
- } else if (import_core5.types.isCallExpression(callee.node) && callee.node.name && callee.node.name.indexOf("$") === 0) {
15483
- init.replaceWith(
15484
- import_core5.types.callExpression(import_core5.types.identifier(path.state.toSignalObject.name), [
15485
- import_core5.types.callExpression(callee.node, [argument == null ? void 0 : argument.node].filter(Boolean))
15486
- ])
15487
- );
15488
- if (import_core5.types.isArrayPattern(path.node.id)) {
15489
- replaceArrayPattern(path, path.node.id.elements);
15490
- } else if (import_core5.types.isObjectPattern(path.node.id)) {
15491
- replaceObjectPattern(path, path.node.id.properties);
15492
- }
15493
- }
15494
- }
15495
-
15496
- // src/signal/symbol.ts
15497
15352
  function replaceSymbol(path) {
15498
15353
  const init = path.node.init;
15499
15354
  const variableName = path.node.id.name;
15500
- if (import_core6.types.isObjectPattern(path.node.id) || import_core6.types.isArrayPattern(path.node.id)) {
15501
- replacePatternToReactive(path);
15355
+ if (import_core3.types.isObjectPattern(path.node.id) || import_core3.types.isArrayPattern(path.node.id)) {
15502
15356
  return;
15503
15357
  }
15504
15358
  if (!variableName.startsWith("$")) {
15505
15359
  return;
15506
15360
  }
15507
- if (init && (import_core6.types.isFunctionExpression(init) || import_core6.types.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
15508
- const newInit = import_core6.types.callExpression(import_core6.types.identifier(path.state.computed.name), init ? [init] : []);
15361
+ if (init && (import_core3.types.isFunctionExpression(init) || import_core3.types.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
15362
+ const newInit = import_core3.types.callExpression(import_core3.types.identifier(path.state.computed.name), init ? [init] : []);
15509
15363
  imports.add("computed");
15510
15364
  path.node.init = newInit;
15511
15365
  } else {
15512
15366
  const originalImportDeclarationNodes = (0, import_types.cloneNode)(path.get("id").node, true);
15513
- const newInit = import_core6.types.callExpression(import_core6.types.identifier(path.state.signal.name), init ? [init] : []);
15367
+ const newInit = import_core3.types.callExpression(import_core3.types.identifier(path.state.signal.name), init ? [init] : []);
15514
15368
  imports.add("signal");
15515
15369
  path.node.init = newInit;
15516
15370
  path.scope.rename(variableName, `${variableName}.value`);
15517
15371
  path.get("id").replaceWith(originalImportDeclarationNodes);
15518
15372
  path.scope.traverse(path.scope.block, {
15519
15373
  Identifier(innerPath) {
15520
- if (import_core6.types.isExportSpecifier(innerPath.parent)) {
15374
+ if (import_core3.types.isExportSpecifier(innerPath.parent)) {
15521
15375
  const { name } = innerPath.node;
15522
15376
  if (name.endsWith(".value")) {
15523
15377
  innerPath.node.name = name.slice(0, -6);
@@ -15528,9 +15382,9 @@ function replaceSymbol(path) {
15528
15382
  }
15529
15383
  }
15530
15384
 
15531
- // src/signal/import.ts
15385
+ // src/import-replace.ts
15532
15386
  init_cjs_shims();
15533
- function replaceImportDeclaration(path) {
15387
+ function replaceImport(path) {
15534
15388
  const imports2 = path.node.specifiers;
15535
15389
  imports2.forEach((specifier) => {
15536
15390
  const variableName = specifier.local.name;
@@ -15552,12 +15406,11 @@ function src_default() {
15552
15406
  parserOpts.plugins.push("jsx");
15553
15407
  },
15554
15408
  visitor: {
15555
- Program: transformProgram(),
15409
+ Program: transformProgram,
15556
15410
  JSXElement: transformJSX,
15557
15411
  JSXFragment: transformJSX,
15558
- FunctionDeclaration: replaceProps,
15559
15412
  VariableDeclarator: replaceSymbol,
15560
- ImportDeclaration: replaceImportDeclaration
15413
+ ImportDeclaration: replaceImport
15561
15414
  }
15562
15415
  };
15563
15416
  }