@dcloudio/uni-mp-compiler 0.0.1-nvue3.3030820220125001
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/LICENSE +202 -0
- package/dist/ast.d.ts +16 -0
- package/dist/ast.js +91 -0
- package/dist/codegen.d.ts +14 -0
- package/dist/codegen.js +322 -0
- package/dist/compile.d.ts +13 -0
- package/dist/compile.js +108 -0
- package/dist/decodeHtml.d.ts +2 -0
- package/dist/decodeHtml.js +131 -0
- package/dist/errors.d.ts +16 -0
- package/dist/errors.js +19 -0
- package/dist/identifier.d.ts +7 -0
- package/dist/identifier.js +35 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +40 -0
- package/dist/namedChars.json +2233 -0
- package/dist/options.d.ts +88 -0
- package/dist/options.js +2 -0
- package/dist/parserOptions.d.ts +7 -0
- package/dist/parserOptions.js +63 -0
- package/dist/runtimeHelpers.d.ts +13 -0
- package/dist/runtimeHelpers.js +32 -0
- package/dist/template/codegen.d.ts +19 -0
- package/dist/template/codegen.js +381 -0
- package/dist/transform.d.ts +73 -0
- package/dist/transform.js +518 -0
- package/dist/transforms/transformClass.d.ts +5 -0
- package/dist/transforms/transformClass.js +162 -0
- package/dist/transforms/transformComponent.d.ts +11 -0
- package/dist/transforms/transformComponent.js +179 -0
- package/dist/transforms/transformElement.d.ts +9 -0
- package/dist/transforms/transformElement.js +191 -0
- package/dist/transforms/transformExpression.d.ts +5 -0
- package/dist/transforms/transformExpression.js +283 -0
- package/dist/transforms/transformIdentifier.d.ts +2 -0
- package/dist/transforms/transformIdentifier.js +95 -0
- package/dist/transforms/transformKey.d.ts +4 -0
- package/dist/transforms/transformKey.js +18 -0
- package/dist/transforms/transformRef.d.ts +3 -0
- package/dist/transforms/transformRef.js +126 -0
- package/dist/transforms/transformRoot.d.ts +2 -0
- package/dist/transforms/transformRoot.js +42 -0
- package/dist/transforms/transformSlot.d.ts +3 -0
- package/dist/transforms/transformSlot.js +144 -0
- package/dist/transforms/transformStyle.d.ts +5 -0
- package/dist/transforms/transformStyle.js +156 -0
- package/dist/transforms/transformTag.d.ts +2 -0
- package/dist/transforms/transformTag.js +15 -0
- package/dist/transforms/utils.d.ts +25 -0
- package/dist/transforms/utils.js +133 -0
- package/dist/transforms/vBind.d.ts +2 -0
- package/dist/transforms/vBind.js +49 -0
- package/dist/transforms/vFor.d.ts +35 -0
- package/dist/transforms/vFor.js +299 -0
- package/dist/transforms/vHtml.d.ts +2 -0
- package/dist/transforms/vHtml.js +35 -0
- package/dist/transforms/vIf.d.ts +13 -0
- package/dist/transforms/vIf.js +139 -0
- package/dist/transforms/vModel.d.ts +2 -0
- package/dist/transforms/vModel.js +178 -0
- package/dist/transforms/vOn.d.ts +8 -0
- package/dist/transforms/vOn.js +166 -0
- package/dist/transforms/vSlot.d.ts +16 -0
- package/dist/transforms/vSlot.js +274 -0
- package/package.json +36 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processIf = exports.transformIf = exports.isIfElementNode = void 0;
|
|
4
|
+
const types_1 = require("@babel/types");
|
|
5
|
+
const compiler_core_1 = require("@vue/compiler-core");
|
|
6
|
+
const ast_1 = require("../ast");
|
|
7
|
+
const transform_1 = require("../transform");
|
|
8
|
+
const transformExpression_1 = require("./transformExpression");
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
function isIfElementNode(node) {
|
|
11
|
+
return !!node.vIf;
|
|
12
|
+
}
|
|
13
|
+
exports.isIfElementNode = isIfElementNode;
|
|
14
|
+
exports.transformIf = (0, transform_1.createStructuralDirectiveTransform)(/^(if|else|else-if)$/, (node, dir, context) => {
|
|
15
|
+
return processIf(node, dir, context, (ifNode, branch, isRoot) => {
|
|
16
|
+
const { currentScope: parentScope, popScope } = context;
|
|
17
|
+
const ifOptions = {
|
|
18
|
+
name: dir.name,
|
|
19
|
+
};
|
|
20
|
+
branch.vIf = ifOptions;
|
|
21
|
+
const condition = dir.exp ? (0, ast_1.parseExpr)(dir.exp, context) : undefined;
|
|
22
|
+
const vIfScope = context.addVIfScope({
|
|
23
|
+
name: dir.name,
|
|
24
|
+
condition,
|
|
25
|
+
});
|
|
26
|
+
if (condition) {
|
|
27
|
+
if (!(0, utils_1.isStaticLiteral)(condition)) {
|
|
28
|
+
ifOptions.condition = (0, utils_1.rewriteExpression)(dir.exp, context, condition, parentScope).content;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
ifOptions.condition = dir.exp.content;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return () => {
|
|
35
|
+
if (isRoot) {
|
|
36
|
+
parentScope.properties.push((0, ast_1.createVIfSpreadElement)(vIfScope));
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const vIfSpreadElement = findVIfSpreadElement(parentScope);
|
|
40
|
+
if (!vIfSpreadElement) {
|
|
41
|
+
popScope();
|
|
42
|
+
return context.onError((0, compiler_core_1.createCompilerError)(30 /* X_V_ELSE_NO_ADJACENT_IF */, node.loc));
|
|
43
|
+
}
|
|
44
|
+
let alternate = (0, ast_1.createObjectExpression)([]);
|
|
45
|
+
if (dir.name === 'else-if') {
|
|
46
|
+
alternate = (0, ast_1.createVIfConditionalExpression)(vIfScope);
|
|
47
|
+
}
|
|
48
|
+
else if (dir.name === 'else') {
|
|
49
|
+
alternate = (0, ast_1.createObjectExpression)(vIfScope.properties);
|
|
50
|
+
}
|
|
51
|
+
findVIfConditionalExpression(vIfSpreadElement.argument).alternate = alternate;
|
|
52
|
+
}
|
|
53
|
+
popScope();
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
function processIf(node, dir, context, processCodegen) {
|
|
58
|
+
if (dir.name !== 'else' &&
|
|
59
|
+
(!dir.exp || !dir.exp.content.trim())) {
|
|
60
|
+
const loc = dir.exp ? dir.exp.loc : node.loc;
|
|
61
|
+
context.onError((0, compiler_core_1.createCompilerError)(28 /* X_V_IF_NO_EXPRESSION */, dir.loc));
|
|
62
|
+
dir.exp = (0, compiler_core_1.createSimpleExpression)(`true`, false, loc);
|
|
63
|
+
}
|
|
64
|
+
if (context.prefixIdentifiers && dir.exp) {
|
|
65
|
+
// dir.exp can only be simple expression because vIf transform is applied
|
|
66
|
+
// before expression transform.
|
|
67
|
+
dir.exp = (0, transformExpression_1.processExpression)(dir.exp, context);
|
|
68
|
+
}
|
|
69
|
+
if (dir.name === 'if') {
|
|
70
|
+
const ifNode = {
|
|
71
|
+
type: 9 /* IF */,
|
|
72
|
+
loc: node.loc,
|
|
73
|
+
branches: [node],
|
|
74
|
+
};
|
|
75
|
+
context.replaceNode(ifNode);
|
|
76
|
+
if (processCodegen) {
|
|
77
|
+
return processCodegen(ifNode, node, true);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
// locate the adjacent v-if
|
|
82
|
+
const siblings = context.parent.children;
|
|
83
|
+
let i = siblings.indexOf(node);
|
|
84
|
+
while (i-- >= -1) {
|
|
85
|
+
const sibling = siblings[i];
|
|
86
|
+
if (sibling && sibling.type === 3 /* COMMENT */) {
|
|
87
|
+
context.removeNode(sibling);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (sibling &&
|
|
91
|
+
sibling.type === 2 /* TEXT */ &&
|
|
92
|
+
!sibling.content.trim().length) {
|
|
93
|
+
context.removeNode(sibling);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (sibling && sibling.type === 9 /* IF */) {
|
|
97
|
+
// Check if v-else was followed by v-else-if
|
|
98
|
+
if (dir.name === 'else-if' &&
|
|
99
|
+
sibling.branches[sibling.branches.length - 1].vIf.condition === undefined) {
|
|
100
|
+
context.onError((0, compiler_core_1.createCompilerError)(30 /* X_V_ELSE_NO_ADJACENT_IF */, node.loc));
|
|
101
|
+
}
|
|
102
|
+
// move the node to the if node's branches
|
|
103
|
+
context.removeNode();
|
|
104
|
+
sibling.branches.push(node);
|
|
105
|
+
const onExit = processCodegen &&
|
|
106
|
+
processCodegen(sibling, node, false);
|
|
107
|
+
// since the branch was removed, it will not be traversed.
|
|
108
|
+
// make sure to traverse here.
|
|
109
|
+
(0, transform_1.traverseNode)(node, context);
|
|
110
|
+
// call on exit
|
|
111
|
+
if (onExit)
|
|
112
|
+
onExit();
|
|
113
|
+
// make sure to reset currentNode after traversal to indicate this
|
|
114
|
+
// node has been removed.
|
|
115
|
+
context.currentNode = null;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
context.onError((0, compiler_core_1.createCompilerError)(30 /* X_V_ELSE_NO_ADJACENT_IF */, node.loc));
|
|
119
|
+
}
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
exports.processIf = processIf;
|
|
125
|
+
function findVIfSpreadElement({ properties }) {
|
|
126
|
+
const len = properties.length;
|
|
127
|
+
for (let i = len - 1; i >= 0; i--) {
|
|
128
|
+
const prop = properties[i];
|
|
129
|
+
if ((0, types_1.isSpreadElement)(prop)) {
|
|
130
|
+
return prop;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function findVIfConditionalExpression(vIfConditionalExpression) {
|
|
135
|
+
if ((0, types_1.isConditionalExpression)(vIfConditionalExpression.alternate)) {
|
|
136
|
+
return findVIfConditionalExpression(vIfConditionalExpression.alternate);
|
|
137
|
+
}
|
|
138
|
+
return vIfConditionalExpression;
|
|
139
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformModel = void 0;
|
|
4
|
+
const shared_1 = require("@vue/shared");
|
|
5
|
+
const compiler_core_1 = require("@vue/compiler-core");
|
|
6
|
+
const compiler_dom_1 = require("@vue/compiler-dom");
|
|
7
|
+
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
|
8
|
+
const runtimeHelpers_1 = require("../runtimeHelpers");
|
|
9
|
+
const codegen_1 = require("../codegen");
|
|
10
|
+
const vOn_1 = require("./vOn");
|
|
11
|
+
const transformModel = (dir, node, _context) => {
|
|
12
|
+
const context = _context;
|
|
13
|
+
const baseResult = (0, compiler_core_1.transformModel)(dir, node, _context);
|
|
14
|
+
// base transform has errors OR component v-model (only need props)
|
|
15
|
+
if (!baseResult.props.length || node.tagType === 1 /* COMPONENT */) {
|
|
16
|
+
return transformComponentVModel(baseResult.props, node, context);
|
|
17
|
+
}
|
|
18
|
+
if (dir.arg) {
|
|
19
|
+
context.onError((0, compiler_dom_1.createDOMCompilerError)(55 /* X_V_MODEL_ARG_ON_ELEMENT */, dir.arg.loc));
|
|
20
|
+
}
|
|
21
|
+
function checkDuplicatedValue() {
|
|
22
|
+
const value = (0, compiler_core_1.findProp)(node, 'value');
|
|
23
|
+
if (value) {
|
|
24
|
+
context.onError((0, compiler_dom_1.createDOMCompilerError)(57 /* X_V_MODEL_UNNECESSARY_VALUE */, value.loc));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const { tag } = node;
|
|
28
|
+
if (tag === 'input' || tag === 'textarea') {
|
|
29
|
+
checkDuplicatedValue();
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
context.onError((0, compiler_dom_1.createDOMCompilerError)(54 /* X_V_MODEL_ON_INVALID_ELEMENT */, dir.loc));
|
|
33
|
+
}
|
|
34
|
+
return transformElementVModel(baseResult.props, node, context);
|
|
35
|
+
};
|
|
36
|
+
exports.transformModel = transformModel;
|
|
37
|
+
function findInputDirectiveNode(props) {
|
|
38
|
+
return props.find((prop) => {
|
|
39
|
+
var _a;
|
|
40
|
+
return prop.type === 7 /* DIRECTIVE */ &&
|
|
41
|
+
prop.name === 'on' &&
|
|
42
|
+
((_a = prop.arg) === null || _a === void 0 ? void 0 : _a.type) === 4 /* SIMPLE_EXPRESSION */ &&
|
|
43
|
+
prop.arg.content === 'input';
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function transformElementVModel(props, node, context) {
|
|
47
|
+
const dirs = transformVModel(props, node, context, {
|
|
48
|
+
binding: 'value',
|
|
49
|
+
event: 'input',
|
|
50
|
+
formatEventCode(code) {
|
|
51
|
+
return code.replace(`= $event`, `= $event.detail.value`);
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
if (dirs.length === 2) {
|
|
55
|
+
// 快手小程序的 input v-model 被转换到 data-e-o 中,补充到 data-e-o 中
|
|
56
|
+
const inputExp = findDatasetEventOpts(node);
|
|
57
|
+
if (inputExp) {
|
|
58
|
+
inputExp.children[2] = combineVOn(dirs[1].exp, inputExp.children[2], node, context);
|
|
59
|
+
dirs.length = 1;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
const inputDir = findInputDirectiveNode(node.props);
|
|
63
|
+
if (inputDir && inputDir.exp) {
|
|
64
|
+
// 合并到已有的 input 事件中
|
|
65
|
+
inputDir.exp = combineVOn(dirs[1].exp, inputDir.exp, node, context);
|
|
66
|
+
dirs.length = 1;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return { props: dirs };
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* {
|
|
74
|
+
* "type": 7,
|
|
75
|
+
* "name": "bind",
|
|
76
|
+
* "loc": {},
|
|
77
|
+
* "modifiers": [],
|
|
78
|
+
* "arg": {
|
|
79
|
+
* "type": 4,
|
|
80
|
+
* "loc": {},
|
|
81
|
+
* "content": "data-e-o",
|
|
82
|
+
* "isStatic": true,
|
|
83
|
+
* "constType": 3
|
|
84
|
+
* },
|
|
85
|
+
* "exp": {
|
|
86
|
+
* "type": 8,
|
|
87
|
+
* "loc": {},
|
|
88
|
+
* "children": ["{", {
|
|
89
|
+
* "type": 8,
|
|
90
|
+
* "loc": {},
|
|
91
|
+
* "children": ["'input'", ": ", {
|
|
92
|
+
* "type": 8,
|
|
93
|
+
* "loc": {},
|
|
94
|
+
* "children": ["_o(", {
|
|
95
|
+
* "type": 4,
|
|
96
|
+
* "content": "_ctx.input",
|
|
97
|
+
* "isStatic": false,
|
|
98
|
+
* "constType": 0,
|
|
99
|
+
* "loc": {}
|
|
100
|
+
* }, ")"]
|
|
101
|
+
* }, ","]
|
|
102
|
+
* }, "}"]
|
|
103
|
+
* }
|
|
104
|
+
* }
|
|
105
|
+
* @param node
|
|
106
|
+
* @returns
|
|
107
|
+
*/
|
|
108
|
+
function findDatasetEventOpts(node) {
|
|
109
|
+
const eventOptsProp = (0, compiler_core_1.findProp)(node, uni_cli_shared_1.ATTR_DATASET_EVENT_OPTS, true, false);
|
|
110
|
+
if (!eventOptsProp) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const { exp } = eventOptsProp;
|
|
114
|
+
if ((exp === null || exp === void 0 ? void 0 : exp.type) !== 8 /* COMPOUND_EXPRESSION */) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
for (let i = 0; i < exp.children.length; i++) {
|
|
118
|
+
const childExp = exp.children[i];
|
|
119
|
+
if ((0, shared_1.isSymbol)(childExp) || (0, shared_1.isString)(childExp)) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (childExp.type !== 8 /* COMPOUND_EXPRESSION */) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (childExp.children[0] !== `'input'`) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const inputExp = childExp.children[2];
|
|
129
|
+
if ((0, shared_1.isSymbol)(inputExp) ||
|
|
130
|
+
(0, shared_1.isString)(inputExp) ||
|
|
131
|
+
inputExp.type !== 8 /* COMPOUND_EXPRESSION */) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
return childExp;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function parseVOn(exp, context) {
|
|
138
|
+
return (0, codegen_1.genExpr)(exp).slice(context.helperString(runtimeHelpers_1.V_ON).length + 1, -1);
|
|
139
|
+
}
|
|
140
|
+
function combineVOn(exp1, exp2, node, context) {
|
|
141
|
+
return (0, vOn_1.wrapperVOn)((0, compiler_core_1.createCompoundExpression)([
|
|
142
|
+
`[`,
|
|
143
|
+
parseVOn(exp1, context),
|
|
144
|
+
',',
|
|
145
|
+
parseVOn(exp2, context),
|
|
146
|
+
`]`,
|
|
147
|
+
]), node, context);
|
|
148
|
+
}
|
|
149
|
+
function transformComponentVModel(props, node, context) {
|
|
150
|
+
return {
|
|
151
|
+
props: transformVModel(props, node, context, {
|
|
152
|
+
formatEventCode(code) {
|
|
153
|
+
return code;
|
|
154
|
+
},
|
|
155
|
+
}),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function transformVModel(props, node, context, { binding, event, formatEventCode, }) {
|
|
159
|
+
if (props.length !== 2) {
|
|
160
|
+
return [];
|
|
161
|
+
}
|
|
162
|
+
const { key: modelValueArg, value: modelValeExpr } = props[0];
|
|
163
|
+
const { key: onUpdateArg, value: onUpdateExpr } = props[1];
|
|
164
|
+
if (modelValueArg.type !== 4 /* SIMPLE_EXPRESSION */) {
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
if (onUpdateArg.type !== 4 /* SIMPLE_EXPRESSION */ ||
|
|
168
|
+
!onUpdateArg.content.startsWith('onUpdate:')) {
|
|
169
|
+
return [];
|
|
170
|
+
}
|
|
171
|
+
const vBindModelValue = (0, uni_cli_shared_1.createBindDirectiveNode)(binding || modelValueArg.content, (0, codegen_1.genExpr)(modelValeExpr));
|
|
172
|
+
const vOnUpdate = (0, uni_cli_shared_1.createOnDirectiveNode)(event || (0, shared_1.camelize)(onUpdateArg.content.replace('onUpdate:', 'update-')), formatEventCode((0, codegen_1.genExpr)((0, vOn_1.wrapperVOn)(
|
|
173
|
+
// onUpdateExpr 通常是 ExpressionNode 或者被 cache 的 ExpressionNode
|
|
174
|
+
(onUpdateExpr.type === 20 /* JS_CACHE_EXPRESSION */
|
|
175
|
+
? onUpdateExpr.value
|
|
176
|
+
: onUpdateExpr), node, context))));
|
|
177
|
+
return [vBindModelValue, vOnUpdate];
|
|
178
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { DirectiveNode, ExpressionNode, SimpleExpressionNode, DirectiveTransform, ElementNode } from '@vue/compiler-core';
|
|
2
|
+
import { TransformContext } from '../transform';
|
|
3
|
+
export interface VOnDirectiveNode extends DirectiveNode {
|
|
4
|
+
arg: ExpressionNode;
|
|
5
|
+
exp: SimpleExpressionNode | undefined;
|
|
6
|
+
}
|
|
7
|
+
export declare const transformOn: DirectiveTransform;
|
|
8
|
+
export declare function wrapperVOn(value: ExpressionNode, node: ElementNode, context: TransformContext): ExpressionNode;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wrapperVOn = exports.transformOn = void 0;
|
|
4
|
+
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
|
5
|
+
const compiler_core_1 = require("@vue/compiler-core");
|
|
6
|
+
const shared_1 = require("@vue/shared");
|
|
7
|
+
const __1 = require("..");
|
|
8
|
+
const runtimeHelpers_1 = require("../runtimeHelpers");
|
|
9
|
+
const transformExpression_1 = require("./transformExpression");
|
|
10
|
+
const fnExpRE = /^\s*([\w$_]+|(async\s*)?\([^)]*?\))\s*=>|^\s*(async\s+)?function(?:\s+[\w$]+)?\s*\(/;
|
|
11
|
+
const transformOn = (dir, node, _context, augmentor) => {
|
|
12
|
+
const context = _context;
|
|
13
|
+
const { loc, modifiers, arg } = dir;
|
|
14
|
+
if (!dir.exp && !modifiers.length) {
|
|
15
|
+
context.onError((0, compiler_core_1.createCompilerError)(35 /* X_V_ON_NO_EXPRESSION */, loc));
|
|
16
|
+
}
|
|
17
|
+
let eventName;
|
|
18
|
+
if (arg.type === 4 /* SIMPLE_EXPRESSION */) {
|
|
19
|
+
if (arg.isStatic) {
|
|
20
|
+
const rawName = arg.content;
|
|
21
|
+
// for all event listeners, auto convert it to camelCase. See issue #2249
|
|
22
|
+
eventName = (0, compiler_core_1.createSimpleExpression)((0, shared_1.toHandlerKey)((0, shared_1.camelize)(rawName)), true, arg.loc);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
// #2388
|
|
26
|
+
eventName = (0, compiler_core_1.createCompoundExpression)([
|
|
27
|
+
// `${context.helperString(TO_HANDLER_KEY)}(`,
|
|
28
|
+
arg,
|
|
29
|
+
// `)`,
|
|
30
|
+
]);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
// already a compound expression.
|
|
35
|
+
eventName = arg;
|
|
36
|
+
eventName.children.unshift(`${context.helperString(compiler_core_1.TO_HANDLER_KEY)}(`);
|
|
37
|
+
eventName.children.push(`)`);
|
|
38
|
+
}
|
|
39
|
+
// handler processing
|
|
40
|
+
let exp = dir.exp;
|
|
41
|
+
if (exp && !exp.content.trim()) {
|
|
42
|
+
exp = undefined;
|
|
43
|
+
}
|
|
44
|
+
let shouldCache = context.cacheHandlers && !exp && !context.inVOnce;
|
|
45
|
+
if (exp) {
|
|
46
|
+
const isMemberExp = (0, compiler_core_1.isMemberExpression)(exp.content, context);
|
|
47
|
+
const isInlineStatement = !(isMemberExp || fnExpRE.test(exp.content));
|
|
48
|
+
const hasMultipleStatements = exp.content.includes(`;`);
|
|
49
|
+
// process the expression since it's been skipped
|
|
50
|
+
if (context.prefixIdentifiers) {
|
|
51
|
+
isInlineStatement && context.addIdentifiers(`$event`);
|
|
52
|
+
exp = dir.exp = (0, transformExpression_1.processExpression)(exp, context, false, hasMultipleStatements);
|
|
53
|
+
isInlineStatement && context.removeIdentifiers(`$event`);
|
|
54
|
+
// with scope analysis, the function is hoistable if it has no reference
|
|
55
|
+
// to scope variables.
|
|
56
|
+
shouldCache =
|
|
57
|
+
context.cacheHandlers &&
|
|
58
|
+
// unnecessary to cache inside v-once
|
|
59
|
+
!context.inVOnce &&
|
|
60
|
+
// runtime constants don't need to be cached
|
|
61
|
+
// (this is analyzed by compileScript in SFC <script setup>)
|
|
62
|
+
!(exp.type === 4 /* SIMPLE_EXPRESSION */ && exp.constType > 0) &&
|
|
63
|
+
// #1541 bail if this is a member exp handler passed to a component -
|
|
64
|
+
// we need to use the original function to preserve arity,
|
|
65
|
+
// e.g. <transition> relies on checking cb.length to determine
|
|
66
|
+
// transition end handling. Inline function is ok since its arity
|
|
67
|
+
// is preserved even when cached.
|
|
68
|
+
!(isMemberExp && node.tagType === 1 /* COMPONENT */) &&
|
|
69
|
+
// bail if the function references closure variables (v-for, v-slot)
|
|
70
|
+
// it must be passed fresh to avoid stale values.
|
|
71
|
+
!(0, compiler_core_1.hasScopeRef)(exp, context.identifiers) &&
|
|
72
|
+
// wxs event
|
|
73
|
+
!isFilterExpr(exp, context);
|
|
74
|
+
// If the expression is optimizable and is a member expression pointing
|
|
75
|
+
// to a function, turn it into invocation (and wrap in an arrow function
|
|
76
|
+
// below) so that it always accesses the latest value when called - thus
|
|
77
|
+
// avoiding the need to be patched.
|
|
78
|
+
if (shouldCache && isMemberExp) {
|
|
79
|
+
if (exp.type === 4 /* SIMPLE_EXPRESSION */) {
|
|
80
|
+
exp.content = `${exp.content} && ${exp.content}(...args)`;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
exp.children = [...exp.children, ` && `, ...exp.children, `(...args)`];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (isInlineStatement || (shouldCache && isMemberExp)) {
|
|
88
|
+
// wrap inline statement in a function expression
|
|
89
|
+
exp = (0, compiler_core_1.createCompoundExpression)([
|
|
90
|
+
`${isInlineStatement
|
|
91
|
+
? context.isTS
|
|
92
|
+
? `($event: any)`
|
|
93
|
+
: `$event`
|
|
94
|
+
: `${context.isTS ? `\n//@ts-ignore\n` : ``}(...args)`} => ${hasMultipleStatements ? `{` : `(`}`,
|
|
95
|
+
exp,
|
|
96
|
+
hasMultipleStatements ? `}` : `)`,
|
|
97
|
+
]);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
let ret = {
|
|
101
|
+
props: [
|
|
102
|
+
(0, compiler_core_1.createObjectProperty)(eventName, exp || (0, compiler_core_1.createSimpleExpression)(`() => {}`, false, loc)),
|
|
103
|
+
],
|
|
104
|
+
};
|
|
105
|
+
// apply extended compiler augmentor
|
|
106
|
+
if (augmentor) {
|
|
107
|
+
ret = augmentor(ret);
|
|
108
|
+
}
|
|
109
|
+
// TODO
|
|
110
|
+
if (shouldCache) {
|
|
111
|
+
// cache handlers so that it's always the same handler being passed down.
|
|
112
|
+
// this avoids unnecessary re-renders when users use inline handlers on
|
|
113
|
+
// components.
|
|
114
|
+
// ret.props[0].value = wrapper(
|
|
115
|
+
// context.cache(ret.props[0].value) as ExpressionNode,
|
|
116
|
+
// context
|
|
117
|
+
// )
|
|
118
|
+
ret.props[0].value = wrapperVOn(ret.props[0].value, node, context);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
ret.props[0].value = wrapperVOn(ret.props[0].value, node, context);
|
|
122
|
+
}
|
|
123
|
+
// mark the key as handler for props normalization check
|
|
124
|
+
ret.props.forEach((p) => (p.key.isHandlerKey = true));
|
|
125
|
+
return ret;
|
|
126
|
+
};
|
|
127
|
+
exports.transformOn = transformOn;
|
|
128
|
+
function isFilterExpr(value, context) {
|
|
129
|
+
if (context.filters.length && value.type === 8 /* COMPOUND_EXPRESSION */) {
|
|
130
|
+
const firstChild = value.children[0];
|
|
131
|
+
if (firstChild.type === 4 /* SIMPLE_EXPRESSION */ &&
|
|
132
|
+
context.filters.includes(firstChild.content)) {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
function wrapperVOn(value, node, context) {
|
|
139
|
+
var _a;
|
|
140
|
+
if ((0, transformExpression_1.isBuiltInIdentifier)(value)) {
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
// wxs event
|
|
144
|
+
if (isFilterExpr(value, context)) {
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
const keys = [];
|
|
148
|
+
if (((_a = context.miniProgram.event) === null || _a === void 0 ? void 0 : _a.key) && context.inVFor) {
|
|
149
|
+
const keyProp = (0, compiler_core_1.findProp)(node, 'key');
|
|
150
|
+
// 仅对 v-for 中 item.id 类型做处理,使用索引无需处理,避免引发更多问题
|
|
151
|
+
if (keyProp && (0, uni_cli_shared_1.isDirectiveNode)(keyProp) && keyProp.exp) {
|
|
152
|
+
const keyCode = (0, __1.genExpr)(keyProp.exp);
|
|
153
|
+
if (keyCode && keyCode.includes('.')) {
|
|
154
|
+
keys.push(',');
|
|
155
|
+
keys.push((0, __1.genExpr)(keyProp.exp));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return (0, compiler_core_1.createCompoundExpression)([
|
|
160
|
+
`${context.helperString(runtimeHelpers_1.V_ON)}(`,
|
|
161
|
+
value,
|
|
162
|
+
...keys,
|
|
163
|
+
`)`,
|
|
164
|
+
]);
|
|
165
|
+
}
|
|
166
|
+
exports.wrapperVOn = wrapperVOn;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BinaryExpression } from '@babel/types';
|
|
2
|
+
import { ComponentNode, CompoundExpressionNode, DirectiveNode } from '@vue/compiler-core';
|
|
3
|
+
import { CodegenScope, CodegenVForScope } from '../options';
|
|
4
|
+
import { NodeTransform, TransformContext } from '../transform';
|
|
5
|
+
export declare const transformSlot: NodeTransform;
|
|
6
|
+
export declare function rewriteVSlot(dir: DirectiveNode, context: TransformContext): void;
|
|
7
|
+
export declare function findSlotName(slotDir: DirectiveNode): string | CompoundExpressionNode;
|
|
8
|
+
export declare function findCurrentPath(id: string, scope: CodegenScope): BinaryExpression | import("@babel/types").StringLiteral;
|
|
9
|
+
/**
|
|
10
|
+
* 目前无用
|
|
11
|
+
* @param vForScope
|
|
12
|
+
* @param parentScope
|
|
13
|
+
* @param context
|
|
14
|
+
*/
|
|
15
|
+
export declare function rewriteScopedSlotVForScope(vForScope: CodegenVForScope, parentScope: CodegenScope, context: TransformContext): void;
|
|
16
|
+
export declare function createVSlotCallExpression(slotComponent: ComponentNode, vForScope: CodegenVForScope, context: TransformContext): import("@babel/types").CallExpression;
|