babel-plugin-vasille 3.1.5 → 4.0.0
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/README.md +9 -17
- package/lib/call.js +45 -34
- package/lib/css-transformer.js +45 -47
- package/lib/expression.js +147 -139
- package/lib/index.js +4 -1
- package/lib/internal.js +4 -19
- package/lib/jsx-detect.js +3 -54
- package/lib/jsx.js +253 -123
- package/lib/lib.js +116 -80
- package/lib/mesh.js +580 -400
- package/lib/order-check.js +48 -0
- package/lib/router.js +41 -0
- package/lib/transformer.js +121 -75
- package/{lib-node/internal.js → lib/utils.js} +10 -37
- package/package.json +12 -14
- package/lib-node/call.js +0 -104
- package/lib-node/css-transformer.js +0 -248
- package/lib-node/expression.js +0 -573
- package/lib-node/index.js +0 -14
- package/lib-node/jsx-detect.js +0 -100
- package/lib-node/jsx.js +0 -439
- package/lib-node/lib.js +0 -153
- package/lib-node/mesh.js +0 -919
- package/lib-node/transformer.js +0 -144
package/lib/lib.js
CHANGED
|
@@ -33,121 +33,157 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.Errors = void 0;
|
|
37
|
+
exports.err = err;
|
|
36
38
|
exports.named = named;
|
|
39
|
+
exports.processCalculateCall = processCalculateCall;
|
|
37
40
|
exports.parseCalculateCall = parseCalculateCall;
|
|
41
|
+
exports.bindCall = bindCall;
|
|
38
42
|
exports.exprCall = exprCall;
|
|
39
|
-
exports.forwardOnlyExpr = forwardOnlyExpr;
|
|
40
|
-
exports.own = own;
|
|
41
43
|
exports.ref = ref;
|
|
42
|
-
exports.reactiveObject = reactiveObject;
|
|
43
44
|
exports.arrayModel = arrayModel;
|
|
44
45
|
exports.setModel = setModel;
|
|
45
46
|
exports.mapModel = mapModel;
|
|
47
|
+
exports.processModelCall = processModelCall;
|
|
48
|
+
exports.checkReactiveName = checkReactiveName;
|
|
49
|
+
exports.checkNonReactiveName = checkNonReactiveName;
|
|
46
50
|
const t = __importStar(require("@babel/types"));
|
|
47
51
|
const expression_js_1 = require("./expression.js");
|
|
48
52
|
const internal_js_1 = require("./internal.js");
|
|
49
53
|
const call_js_1 = require("./call.js");
|
|
54
|
+
const mesh_1 = require("./mesh");
|
|
55
|
+
var Errors;
|
|
56
|
+
(function (Errors) {
|
|
57
|
+
Errors[Errors["IncorrectArguments"] = 1] = "IncorrectArguments";
|
|
58
|
+
Errors[Errors["IncompatibleContext"] = 2] = "IncompatibleContext";
|
|
59
|
+
Errors[Errors["TokenNotSupported"] = 3] = "TokenNotSupported";
|
|
60
|
+
Errors[Errors["Dilemma"] = 4] = "Dilemma";
|
|
61
|
+
Errors[Errors["ParserError"] = 5] = "ParserError";
|
|
62
|
+
Errors[Errors["RulesOfVasille"] = 6] = "RulesOfVasille";
|
|
63
|
+
})(Errors || (exports.Errors = Errors = {}));
|
|
64
|
+
function err(e, node, content, internal, ret) {
|
|
65
|
+
const limit = Error.stackTraceLimit;
|
|
66
|
+
Error.stackTraceLimit = 0;
|
|
67
|
+
const error = node.buildCodeFrameError(`Vasille[${e}]{${Errors[e]}}: ${content}`, Error);
|
|
68
|
+
Error.stackTraceLimit = limit;
|
|
69
|
+
if (!internal.firstError) {
|
|
70
|
+
internal.firstError = error;
|
|
71
|
+
}
|
|
72
|
+
console.log(error);
|
|
73
|
+
return ret;
|
|
74
|
+
}
|
|
75
|
+
function unprefixedName(name) {
|
|
76
|
+
return name[0] === "$" ? name.substring(1) : name;
|
|
77
|
+
}
|
|
50
78
|
function named(call, name, internal, argPos) {
|
|
51
|
-
if (internal.devMode &&
|
|
79
|
+
if (internal.devMode && name) {
|
|
52
80
|
while (argPos && call.arguments.length < argPos) {
|
|
53
81
|
call.arguments.push(t.buildUndefinedNode());
|
|
54
82
|
}
|
|
55
|
-
call.arguments.push(...(typeof name === "string" ? [
|
|
83
|
+
call.arguments.push(...(typeof name === "string" ? [name] : name.map(item => item)).map(name => t.stringLiteral(unprefixedName(name))));
|
|
56
84
|
}
|
|
57
85
|
return call;
|
|
58
86
|
}
|
|
59
|
-
function
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (
|
|
66
|
-
|
|
67
|
-
throw path.buildCodeFrameError("Vasille: Argument of calculate cannot have parameters");
|
|
68
|
-
}
|
|
69
|
-
const exprData = (0, expression_js_1.checkNode)(path.get("arguments")[0], internal);
|
|
70
|
-
call.params = [...exprData.found.keys()].map(name => (0, expression_js_1.encodeName)(name));
|
|
71
|
-
return [call, t.arrayExpression([...exprData.found.values()])];
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
throw path.buildCodeFrameError("Vasille: Argument of calculate must be a function");
|
|
87
|
+
function processCalculateCall(path, internal) {
|
|
88
|
+
const call = path.node.arguments[0];
|
|
89
|
+
if (path.node.arguments.length !== 1) {
|
|
90
|
+
return err(Errors.IncorrectArguments, path, "Incorrect number of arguments", internal, false);
|
|
91
|
+
}
|
|
92
|
+
if (t.isFunctionExpression(call) || t.isArrowFunctionExpression(call)) {
|
|
93
|
+
if (call.params.length > 0) {
|
|
94
|
+
return err(Errors.IncorrectArguments, path.get("arguments")[0], "Argument of calculate cannot have parameters", internal, false);
|
|
75
95
|
}
|
|
96
|
+
const exprData = (0, expression_js_1.checkNode)(path.get("arguments")[0], internal);
|
|
97
|
+
call.params = [...exprData.found.keys()].map(name => (0, expression_js_1.encodeName)(name));
|
|
98
|
+
path.node.arguments.unshift(internal.isComposing ? internal_js_1.ctx : t.nullLiteral());
|
|
99
|
+
path.node.arguments.push(t.arrayExpression([...exprData.found.values()]));
|
|
100
|
+
return true;
|
|
76
101
|
}
|
|
77
|
-
return
|
|
102
|
+
return err(Errors.IncorrectArguments, path, "Argument of calculate must be a function", internal, false);
|
|
78
103
|
}
|
|
79
|
-
function
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
104
|
+
function parseCalculateCall(path, internal) {
|
|
105
|
+
if (path.isCallExpression() && (0, call_js_1.calls)(path, ["calculate", "watch"], internal)) {
|
|
106
|
+
return processCalculateCall(path, internal);
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
function bindCall(path, expr, data, internal, name) {
|
|
111
|
+
const names = [...data.keys()].map(expression_js_1.encodeName);
|
|
112
|
+
const dependencies = t.arrayExpression([...data.values()]);
|
|
113
|
+
if (names.length > 0 && expr) {
|
|
114
|
+
path.replaceWith(named(internal.expr(t.arrowFunctionExpression(names, expr), dependencies), name, internal, 3));
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
function exprCall(path, expr, internal, opts) {
|
|
120
|
+
if (parseCalculateCall(path, internal)) {
|
|
121
|
+
named(path.node, opts.name, internal, 3);
|
|
122
|
+
return true;
|
|
85
123
|
}
|
|
86
124
|
if (t.isCallExpression(expr) &&
|
|
87
|
-
(0, call_js_1.calls)(
|
|
125
|
+
(0, call_js_1.calls)(path, ["bind"], internal) &&
|
|
88
126
|
expr.arguments.length === 1 &&
|
|
89
127
|
t.isExpression(expr.arguments[0])) {
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
128
|
+
const argPath = path.get("arguments")[0];
|
|
129
|
+
const exprData = (0, expression_js_1.checkNode)(argPath, internal);
|
|
130
|
+
if (exprData.self) {
|
|
131
|
+
path.replaceWith(internal.forward(exprData.self));
|
|
132
|
+
}
|
|
133
|
+
else if (exprData.found.size > 0) {
|
|
134
|
+
argPath.replaceWith(t.arrowFunctionExpression([...exprData.found.keys()].map(expression_js_1.encodeName), argPath.node));
|
|
135
|
+
expr.arguments.unshift(internal.isComposing ? internal_js_1.ctx : t.nullLiteral());
|
|
136
|
+
expr.arguments.push(t.arrayExpression([...exprData.found.values()]));
|
|
137
|
+
named(expr, opts.name, internal, 3);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
path.replaceWith(named(internal.ref(argPath.node), opts.name, internal, 1));
|
|
93
141
|
}
|
|
142
|
+
return true;
|
|
94
143
|
}
|
|
95
144
|
const exprData = (0, expression_js_1.checkNode)(path, internal);
|
|
96
145
|
if (exprData.self) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
: t.memberExpression(internal_js_1.ctx, t.identifier("expr")), [t.arrowFunctionExpression(names, expr), dependencies]), name, internal);
|
|
105
|
-
}
|
|
106
|
-
return null;
|
|
107
|
-
}
|
|
108
|
-
function forwardOnlyExpr(path, expr, internal) {
|
|
109
|
-
const calculateCall = parseCalculateCall(path, internal);
|
|
110
|
-
if (calculateCall) {
|
|
111
|
-
return t.callExpression(t.memberExpression(internal.id, t.identifier("ex")), calculateCall);
|
|
112
|
-
}
|
|
113
|
-
const exprData = (0, expression_js_1.checkNode)(path, internal);
|
|
114
|
-
return exprData.self
|
|
115
|
-
? t.callExpression(t.memberExpression(internal.id, t.identifier("fo")), [exprData.self])
|
|
116
|
-
: exprData.found.size > 0 && expr
|
|
117
|
-
? t.callExpression(t.memberExpression(internal.id, t.identifier("ex")), [
|
|
118
|
-
t.arrowFunctionExpression([...exprData.found.keys()].map(name => (0, expression_js_1.encodeName)(name)), expr),
|
|
119
|
-
t.arrayExpression([...exprData.found.values()]),
|
|
120
|
-
])
|
|
121
|
-
: null;
|
|
122
|
-
}
|
|
123
|
-
function own(expr, internal, name) {
|
|
124
|
-
if (internal.stateOnly &&
|
|
125
|
-
t.isCallExpression(expr) &&
|
|
126
|
-
t.isMemberExpression(expr.callee) &&
|
|
127
|
-
t.isIdentifier(expr.callee.property) &&
|
|
128
|
-
expr.callee.property.name === "fo" &&
|
|
129
|
-
t.isIdentifier(expr.callee.object) &&
|
|
130
|
-
expr.callee.object === internal.id) {
|
|
131
|
-
return expr;
|
|
146
|
+
if (!opts.strong || (0, expression_js_1.exprIsSure)(path, internal)) {
|
|
147
|
+
path.replaceWith(exprData.self);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
path.replaceWith(internal.ensure(exprData.self));
|
|
151
|
+
}
|
|
152
|
+
return true;
|
|
132
153
|
}
|
|
133
|
-
return
|
|
134
|
-
? t.memberExpression(internal.id, t.identifier("fo"))
|
|
135
|
-
: t.memberExpression(internal_js_1.ctx, t.identifier("own")), [expr]), name, internal);
|
|
154
|
+
return bindCall(path, expr, exprData.found, internal, opts.name);
|
|
136
155
|
}
|
|
137
156
|
function ref(expr, internal, name) {
|
|
138
|
-
return named(
|
|
139
|
-
? t.memberExpression(internal.id, t.identifier("r"))
|
|
140
|
-
: t.memberExpression(internal_js_1.ctx, t.identifier("ref")), expr ? [expr] : []), name, internal, 1);
|
|
141
|
-
}
|
|
142
|
-
function reactiveObject(init, internal, name) {
|
|
143
|
-
return named(t.callExpression(t.memberExpression(internal.id, t.identifier(internal.stateOnly ? "sro" : "ro")), internal.stateOnly ? [init] : [internal_js_1.ctx, init]), name, internal);
|
|
157
|
+
return named(internal.ref(expr), name, internal, 1);
|
|
144
158
|
}
|
|
145
|
-
function arrayModel(
|
|
146
|
-
return named(
|
|
159
|
+
function arrayModel(args, internal, name) {
|
|
160
|
+
return named(internal.arrayModel(args[0]), name, internal, 2);
|
|
147
161
|
}
|
|
148
162
|
function setModel(args, internal, name) {
|
|
149
|
-
return named(
|
|
163
|
+
return named(internal.setModel(args[0]), name, internal, 2);
|
|
150
164
|
}
|
|
151
165
|
function mapModel(args, internal, name) {
|
|
152
|
-
return named(
|
|
166
|
+
return named(internal.mapModel(args[0]), name, internal, 2);
|
|
167
|
+
}
|
|
168
|
+
function processModelCall(path, type, isConst, internal, name) {
|
|
169
|
+
const args = path.node.arguments;
|
|
170
|
+
if (!isConst) {
|
|
171
|
+
err(Errors.RulesOfVasille, path, `${type} models must be declared as constants`, internal);
|
|
172
|
+
}
|
|
173
|
+
(0, mesh_1.meshAllUnknown)(path.get("arguments"), internal);
|
|
174
|
+
path.replaceWith(type === "Map"
|
|
175
|
+
? mapModel(args, internal, name)
|
|
176
|
+
: type === "Set"
|
|
177
|
+
? setModel(args, internal, name)
|
|
178
|
+
: arrayModel(args, internal, name));
|
|
179
|
+
}
|
|
180
|
+
function checkReactiveName(idPath, internal) {
|
|
181
|
+
if (!(idPath.isIdentifier() && idPath.node.name.startsWith("$"))) {
|
|
182
|
+
err(Errors.RulesOfVasille, idPath, "Reactive variable name must start with $", internal);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
function checkNonReactiveName(idPath, internal) {
|
|
186
|
+
if (idPath.node.name.startsWith("$")) {
|
|
187
|
+
err(Errors.RulesOfVasille, idPath, "Non-reactive variable name must not start with $", internal);
|
|
188
|
+
}
|
|
153
189
|
}
|