babel-plugin-essor 0.0.16 → 0.0.17-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 +146 -2
- package/dist/index.js +146 -2
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -835,8 +835,17 @@ function isSignal(name) {
|
|
|
835
835
|
const prefix = (_a = getCompileContext().options.signalPrefix) != null ? _a : "$";
|
|
836
836
|
return !!name && x(name, prefix);
|
|
837
837
|
}
|
|
838
|
+
function stripSignalPrefix(name) {
|
|
839
|
+
var _a;
|
|
840
|
+
const prefix = (_a = getCompileContext().options.signalPrefix) != null ? _a : "$";
|
|
841
|
+
return x(name, prefix) ? name.slice(prefix.length) : name;
|
|
842
|
+
}
|
|
838
843
|
function replaceSymbol(path) {
|
|
839
844
|
const { init, id } = path.node;
|
|
845
|
+
if (core.types.isObjectPattern(id) || core.types.isArrayPattern(id)) {
|
|
846
|
+
transformDestructuring(path);
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
840
849
|
if (!core.types.isIdentifier(id)) return;
|
|
841
850
|
if (!isSignal(id.name)) return;
|
|
842
851
|
if (isAlreadySignalCall(init)) return;
|
|
@@ -852,6 +861,141 @@ function isAlreadySignalCall(init) {
|
|
|
852
861
|
const { importIdentifiers } = getCompileContext();
|
|
853
862
|
return calleeName === importIdentifiers.signal.name || calleeName === importIdentifiers.computed.name;
|
|
854
863
|
}
|
|
864
|
+
function transformDestructuring(path) {
|
|
865
|
+
var _a;
|
|
866
|
+
const { init, id } = path.node;
|
|
867
|
+
const pattern = id;
|
|
868
|
+
if (!init) {
|
|
869
|
+
if (patternHasSignalBinding(pattern)) path.skip();
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
if (!isSignalSource(init) && !patternHasSignalBinding(pattern)) return;
|
|
873
|
+
const declPath = path.parentPath;
|
|
874
|
+
if (!declPath || !declPath.isVariableDeclaration()) return;
|
|
875
|
+
if ((_a = declPath.parentPath) == null ? void 0 : _a.isForStatement()) {
|
|
876
|
+
path.skip();
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
const base = signalSourceBase(init);
|
|
880
|
+
const declarators = bindPattern(pattern, base);
|
|
881
|
+
const declaration = declPath.node;
|
|
882
|
+
const index = declaration.declarations.indexOf(path.node);
|
|
883
|
+
declaration.declarations.splice(index, 1, ...declarators);
|
|
884
|
+
path.skip();
|
|
885
|
+
}
|
|
886
|
+
function clone(node) {
|
|
887
|
+
return core.types.cloneNode(node, true);
|
|
888
|
+
}
|
|
889
|
+
function isSignalSource(init) {
|
|
890
|
+
if (core.types.isIdentifier(init)) return isSignal(init.name);
|
|
891
|
+
if (core.types.isMemberExpression(init) && core.types.isIdentifier(init.object) && isSignal(init.object.name)) {
|
|
892
|
+
return isMemberAccessingProperty(init, "value");
|
|
893
|
+
}
|
|
894
|
+
return false;
|
|
895
|
+
}
|
|
896
|
+
function signalSourceBase(init) {
|
|
897
|
+
if (core.types.isIdentifier(init) && isSignal(init.name)) {
|
|
898
|
+
return core.types.memberExpression(core.types.identifier(init.name), core.types.identifier("value"));
|
|
899
|
+
}
|
|
900
|
+
return clone(init);
|
|
901
|
+
}
|
|
902
|
+
function patternHasSignalBinding(node) {
|
|
903
|
+
if (core.types.isIdentifier(node)) return isSignal(node.name);
|
|
904
|
+
if (core.types.isObjectPattern(node)) {
|
|
905
|
+
return node.properties.some(
|
|
906
|
+
(p2) => core.types.isRestElement(p2) ? patternHasSignalBinding(p2.argument) : patternHasSignalBinding(p2.value)
|
|
907
|
+
);
|
|
908
|
+
}
|
|
909
|
+
if (core.types.isArrayPattern(node)) {
|
|
910
|
+
return node.elements.some((el) => el != null && patternHasSignalBinding(el));
|
|
911
|
+
}
|
|
912
|
+
if (core.types.isAssignmentPattern(node)) return patternHasSignalBinding(node.left);
|
|
913
|
+
if (core.types.isRestElement(node)) return patternHasSignalBinding(node.argument);
|
|
914
|
+
return false;
|
|
915
|
+
}
|
|
916
|
+
function bindPattern(pattern, base) {
|
|
917
|
+
return core.types.isObjectPattern(pattern) ? bindObjectPattern(pattern, base) : bindArrayPattern(pattern, base);
|
|
918
|
+
}
|
|
919
|
+
function bindObjectPattern(pattern, base) {
|
|
920
|
+
const out = [];
|
|
921
|
+
const handledKeys = [];
|
|
922
|
+
for (const prop of pattern.properties) {
|
|
923
|
+
if (core.types.isRestElement(prop)) {
|
|
924
|
+
out.push(buildObjectRest(prop, base, handledKeys));
|
|
925
|
+
continue;
|
|
926
|
+
}
|
|
927
|
+
const { keyExpr, computed, target } = resolveObjectProp(prop);
|
|
928
|
+
handledKeys.push({ key: clone(keyExpr), computed });
|
|
929
|
+
const access = core.types.memberExpression(clone(base), keyExpr, computed);
|
|
930
|
+
out.push(...bindTarget(target, access));
|
|
931
|
+
}
|
|
932
|
+
return out;
|
|
933
|
+
}
|
|
934
|
+
function resolveObjectProp(prop) {
|
|
935
|
+
const target = prop.value;
|
|
936
|
+
const shorthandId = core.types.isAssignmentPattern(target) ? target.left : target;
|
|
937
|
+
if (prop.shorthand && core.types.isIdentifier(shorthandId) && isSignal(shorthandId.name)) {
|
|
938
|
+
return { keyExpr: core.types.identifier(stripSignalPrefix(shorthandId.name)), computed: false, target };
|
|
939
|
+
}
|
|
940
|
+
if (prop.computed) {
|
|
941
|
+
return { keyExpr: prop.key, computed: true, target };
|
|
942
|
+
}
|
|
943
|
+
return { keyExpr: prop.key, computed: !core.types.isIdentifier(prop.key), target };
|
|
944
|
+
}
|
|
945
|
+
function bindArrayPattern(pattern, base) {
|
|
946
|
+
const out = [];
|
|
947
|
+
pattern.elements.forEach((el, i2) => {
|
|
948
|
+
if (el == null) return;
|
|
949
|
+
if (core.types.isRestElement(el)) {
|
|
950
|
+
const slice = core.types.callExpression(core.types.memberExpression(clone(base), core.types.identifier("slice")), [
|
|
951
|
+
core.types.numericLiteral(i2)
|
|
952
|
+
]);
|
|
953
|
+
out.push(...bindTarget(el.argument, slice));
|
|
954
|
+
return;
|
|
955
|
+
}
|
|
956
|
+
const access = core.types.memberExpression(clone(base), core.types.numericLiteral(i2), true);
|
|
957
|
+
out.push(...bindTarget(el, access));
|
|
958
|
+
});
|
|
959
|
+
return out;
|
|
960
|
+
}
|
|
961
|
+
function bindTarget(target, access) {
|
|
962
|
+
if (core.types.isAssignmentPattern(target)) {
|
|
963
|
+
return bindTarget(target.left, applyDefault(access, target.right));
|
|
964
|
+
}
|
|
965
|
+
if (core.types.isIdentifier(target)) {
|
|
966
|
+
const value = isSignal(target.name) ? makeComputed(access) : access;
|
|
967
|
+
return [core.types.variableDeclarator(core.types.identifier(target.name), value)];
|
|
968
|
+
}
|
|
969
|
+
if (core.types.isObjectPattern(target)) return bindObjectPattern(target, access);
|
|
970
|
+
if (core.types.isArrayPattern(target)) return bindArrayPattern(target, access);
|
|
971
|
+
return [core.types.variableDeclarator(target, access)];
|
|
972
|
+
}
|
|
973
|
+
function makeComputed(access) {
|
|
974
|
+
return core.types.callExpression(useImport("computed"), [core.types.arrowFunctionExpression([], access)]);
|
|
975
|
+
}
|
|
976
|
+
function applyDefault(access, def) {
|
|
977
|
+
return core.types.conditionalExpression(
|
|
978
|
+
core.types.binaryExpression("===", clone(access), core.types.identifier("undefined")),
|
|
979
|
+
def,
|
|
980
|
+
clone(access)
|
|
981
|
+
);
|
|
982
|
+
}
|
|
983
|
+
function buildObjectRest(rest, base, handledKeys) {
|
|
984
|
+
const omits = (target) => core.types.objectPattern([
|
|
985
|
+
...handledKeys.map(({ key, computed }) => core.types.objectProperty(key, genUid("_omit$"), computed)),
|
|
986
|
+
core.types.restElement(target)
|
|
987
|
+
]);
|
|
988
|
+
const arg = rest.argument;
|
|
989
|
+
if (core.types.isIdentifier(arg) && isSignal(arg.name)) {
|
|
990
|
+
const inner = genUid("_rest$");
|
|
991
|
+
const body = core.types.blockStatement([
|
|
992
|
+
core.types.variableDeclaration("const", [core.types.variableDeclarator(omits(inner), clone(base))]),
|
|
993
|
+
core.types.returnStatement(clone(inner))
|
|
994
|
+
]);
|
|
995
|
+
return core.types.variableDeclarator(core.types.identifier(arg.name), makeComputed(body));
|
|
996
|
+
}
|
|
997
|
+
return core.types.variableDeclarator(omits(arg), clone(base));
|
|
998
|
+
}
|
|
855
999
|
function symbolIdentifier(path) {
|
|
856
1000
|
const name = path.node.name;
|
|
857
1001
|
if (!isSignal(name)) return;
|
|
@@ -1451,8 +1595,8 @@ function setNodeText(path, text) {
|
|
|
1451
1595
|
function createComponentPropKey(name, forceStringLiteralKeys = false) {
|
|
1452
1596
|
return forceStringLiteralKeys || !core.types.isValidIdentifier(name) ? core.types.stringLiteral(name) : core.types.identifier(name);
|
|
1453
1597
|
}
|
|
1454
|
-
function cloneValue(value,
|
|
1455
|
-
return
|
|
1598
|
+
function cloneValue(value, clone2 = false) {
|
|
1599
|
+
return clone2 ? core.types.cloneNode(value, true) : value;
|
|
1456
1600
|
}
|
|
1457
1601
|
function createPropNode(name, value, kind, options) {
|
|
1458
1602
|
const key = createComponentPropKey(name, options.forceStringLiteralKeys);
|
package/dist/index.js
CHANGED
|
@@ -833,8 +833,17 @@ function isSignal(name) {
|
|
|
833
833
|
const prefix = (_a = getCompileContext().options.signalPrefix) != null ? _a : "$";
|
|
834
834
|
return !!name && x(name, prefix);
|
|
835
835
|
}
|
|
836
|
+
function stripSignalPrefix(name) {
|
|
837
|
+
var _a;
|
|
838
|
+
const prefix = (_a = getCompileContext().options.signalPrefix) != null ? _a : "$";
|
|
839
|
+
return x(name, prefix) ? name.slice(prefix.length) : name;
|
|
840
|
+
}
|
|
836
841
|
function replaceSymbol(path) {
|
|
837
842
|
const { init, id } = path.node;
|
|
843
|
+
if (types.isObjectPattern(id) || types.isArrayPattern(id)) {
|
|
844
|
+
transformDestructuring(path);
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
838
847
|
if (!types.isIdentifier(id)) return;
|
|
839
848
|
if (!isSignal(id.name)) return;
|
|
840
849
|
if (isAlreadySignalCall(init)) return;
|
|
@@ -850,6 +859,141 @@ function isAlreadySignalCall(init) {
|
|
|
850
859
|
const { importIdentifiers } = getCompileContext();
|
|
851
860
|
return calleeName === importIdentifiers.signal.name || calleeName === importIdentifiers.computed.name;
|
|
852
861
|
}
|
|
862
|
+
function transformDestructuring(path) {
|
|
863
|
+
var _a;
|
|
864
|
+
const { init, id } = path.node;
|
|
865
|
+
const pattern = id;
|
|
866
|
+
if (!init) {
|
|
867
|
+
if (patternHasSignalBinding(pattern)) path.skip();
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
if (!isSignalSource(init) && !patternHasSignalBinding(pattern)) return;
|
|
871
|
+
const declPath = path.parentPath;
|
|
872
|
+
if (!declPath || !declPath.isVariableDeclaration()) return;
|
|
873
|
+
if ((_a = declPath.parentPath) == null ? void 0 : _a.isForStatement()) {
|
|
874
|
+
path.skip();
|
|
875
|
+
return;
|
|
876
|
+
}
|
|
877
|
+
const base = signalSourceBase(init);
|
|
878
|
+
const declarators = bindPattern(pattern, base);
|
|
879
|
+
const declaration = declPath.node;
|
|
880
|
+
const index = declaration.declarations.indexOf(path.node);
|
|
881
|
+
declaration.declarations.splice(index, 1, ...declarators);
|
|
882
|
+
path.skip();
|
|
883
|
+
}
|
|
884
|
+
function clone(node) {
|
|
885
|
+
return types.cloneNode(node, true);
|
|
886
|
+
}
|
|
887
|
+
function isSignalSource(init) {
|
|
888
|
+
if (types.isIdentifier(init)) return isSignal(init.name);
|
|
889
|
+
if (types.isMemberExpression(init) && types.isIdentifier(init.object) && isSignal(init.object.name)) {
|
|
890
|
+
return isMemberAccessingProperty(init, "value");
|
|
891
|
+
}
|
|
892
|
+
return false;
|
|
893
|
+
}
|
|
894
|
+
function signalSourceBase(init) {
|
|
895
|
+
if (types.isIdentifier(init) && isSignal(init.name)) {
|
|
896
|
+
return types.memberExpression(types.identifier(init.name), types.identifier("value"));
|
|
897
|
+
}
|
|
898
|
+
return clone(init);
|
|
899
|
+
}
|
|
900
|
+
function patternHasSignalBinding(node) {
|
|
901
|
+
if (types.isIdentifier(node)) return isSignal(node.name);
|
|
902
|
+
if (types.isObjectPattern(node)) {
|
|
903
|
+
return node.properties.some(
|
|
904
|
+
(p2) => types.isRestElement(p2) ? patternHasSignalBinding(p2.argument) : patternHasSignalBinding(p2.value)
|
|
905
|
+
);
|
|
906
|
+
}
|
|
907
|
+
if (types.isArrayPattern(node)) {
|
|
908
|
+
return node.elements.some((el) => el != null && patternHasSignalBinding(el));
|
|
909
|
+
}
|
|
910
|
+
if (types.isAssignmentPattern(node)) return patternHasSignalBinding(node.left);
|
|
911
|
+
if (types.isRestElement(node)) return patternHasSignalBinding(node.argument);
|
|
912
|
+
return false;
|
|
913
|
+
}
|
|
914
|
+
function bindPattern(pattern, base) {
|
|
915
|
+
return types.isObjectPattern(pattern) ? bindObjectPattern(pattern, base) : bindArrayPattern(pattern, base);
|
|
916
|
+
}
|
|
917
|
+
function bindObjectPattern(pattern, base) {
|
|
918
|
+
const out = [];
|
|
919
|
+
const handledKeys = [];
|
|
920
|
+
for (const prop of pattern.properties) {
|
|
921
|
+
if (types.isRestElement(prop)) {
|
|
922
|
+
out.push(buildObjectRest(prop, base, handledKeys));
|
|
923
|
+
continue;
|
|
924
|
+
}
|
|
925
|
+
const { keyExpr, computed, target } = resolveObjectProp(prop);
|
|
926
|
+
handledKeys.push({ key: clone(keyExpr), computed });
|
|
927
|
+
const access = types.memberExpression(clone(base), keyExpr, computed);
|
|
928
|
+
out.push(...bindTarget(target, access));
|
|
929
|
+
}
|
|
930
|
+
return out;
|
|
931
|
+
}
|
|
932
|
+
function resolveObjectProp(prop) {
|
|
933
|
+
const target = prop.value;
|
|
934
|
+
const shorthandId = types.isAssignmentPattern(target) ? target.left : target;
|
|
935
|
+
if (prop.shorthand && types.isIdentifier(shorthandId) && isSignal(shorthandId.name)) {
|
|
936
|
+
return { keyExpr: types.identifier(stripSignalPrefix(shorthandId.name)), computed: false, target };
|
|
937
|
+
}
|
|
938
|
+
if (prop.computed) {
|
|
939
|
+
return { keyExpr: prop.key, computed: true, target };
|
|
940
|
+
}
|
|
941
|
+
return { keyExpr: prop.key, computed: !types.isIdentifier(prop.key), target };
|
|
942
|
+
}
|
|
943
|
+
function bindArrayPattern(pattern, base) {
|
|
944
|
+
const out = [];
|
|
945
|
+
pattern.elements.forEach((el, i2) => {
|
|
946
|
+
if (el == null) return;
|
|
947
|
+
if (types.isRestElement(el)) {
|
|
948
|
+
const slice = types.callExpression(types.memberExpression(clone(base), types.identifier("slice")), [
|
|
949
|
+
types.numericLiteral(i2)
|
|
950
|
+
]);
|
|
951
|
+
out.push(...bindTarget(el.argument, slice));
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
const access = types.memberExpression(clone(base), types.numericLiteral(i2), true);
|
|
955
|
+
out.push(...bindTarget(el, access));
|
|
956
|
+
});
|
|
957
|
+
return out;
|
|
958
|
+
}
|
|
959
|
+
function bindTarget(target, access) {
|
|
960
|
+
if (types.isAssignmentPattern(target)) {
|
|
961
|
+
return bindTarget(target.left, applyDefault(access, target.right));
|
|
962
|
+
}
|
|
963
|
+
if (types.isIdentifier(target)) {
|
|
964
|
+
const value = isSignal(target.name) ? makeComputed(access) : access;
|
|
965
|
+
return [types.variableDeclarator(types.identifier(target.name), value)];
|
|
966
|
+
}
|
|
967
|
+
if (types.isObjectPattern(target)) return bindObjectPattern(target, access);
|
|
968
|
+
if (types.isArrayPattern(target)) return bindArrayPattern(target, access);
|
|
969
|
+
return [types.variableDeclarator(target, access)];
|
|
970
|
+
}
|
|
971
|
+
function makeComputed(access) {
|
|
972
|
+
return types.callExpression(useImport("computed"), [types.arrowFunctionExpression([], access)]);
|
|
973
|
+
}
|
|
974
|
+
function applyDefault(access, def) {
|
|
975
|
+
return types.conditionalExpression(
|
|
976
|
+
types.binaryExpression("===", clone(access), types.identifier("undefined")),
|
|
977
|
+
def,
|
|
978
|
+
clone(access)
|
|
979
|
+
);
|
|
980
|
+
}
|
|
981
|
+
function buildObjectRest(rest, base, handledKeys) {
|
|
982
|
+
const omits = (target) => types.objectPattern([
|
|
983
|
+
...handledKeys.map(({ key, computed }) => types.objectProperty(key, genUid("_omit$"), computed)),
|
|
984
|
+
types.restElement(target)
|
|
985
|
+
]);
|
|
986
|
+
const arg = rest.argument;
|
|
987
|
+
if (types.isIdentifier(arg) && isSignal(arg.name)) {
|
|
988
|
+
const inner = genUid("_rest$");
|
|
989
|
+
const body = types.blockStatement([
|
|
990
|
+
types.variableDeclaration("const", [types.variableDeclarator(omits(inner), clone(base))]),
|
|
991
|
+
types.returnStatement(clone(inner))
|
|
992
|
+
]);
|
|
993
|
+
return types.variableDeclarator(types.identifier(arg.name), makeComputed(body));
|
|
994
|
+
}
|
|
995
|
+
return types.variableDeclarator(omits(arg), clone(base));
|
|
996
|
+
}
|
|
853
997
|
function symbolIdentifier(path) {
|
|
854
998
|
const name = path.node.name;
|
|
855
999
|
if (!isSignal(name)) return;
|
|
@@ -1449,8 +1593,8 @@ function setNodeText(path, text) {
|
|
|
1449
1593
|
function createComponentPropKey(name, forceStringLiteralKeys = false) {
|
|
1450
1594
|
return forceStringLiteralKeys || !types.isValidIdentifier(name) ? types.stringLiteral(name) : types.identifier(name);
|
|
1451
1595
|
}
|
|
1452
|
-
function cloneValue(value,
|
|
1453
|
-
return
|
|
1596
|
+
function cloneValue(value, clone2 = false) {
|
|
1597
|
+
return clone2 ? types.cloneNode(value, true) : value;
|
|
1454
1598
|
}
|
|
1455
1599
|
function createPropNode(name, value, kind, options) {
|
|
1456
1600
|
const key = createComponentPropKey(name, options.forceStringLiteralKeys);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "babel-plugin-essor",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.17-beta.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [],
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@babel/core": "^7.29.7",
|
|
36
36
|
"@babel/generator": "^7.29.7",
|
|
37
|
-
"@estjs/shared": "0.0.
|
|
37
|
+
"@estjs/shared": "0.0.17-beta.1"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@babel/traverse": "^7.29.7",
|