@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.
Files changed (65) hide show
  1. package/LICENSE +202 -0
  2. package/dist/ast.d.ts +16 -0
  3. package/dist/ast.js +91 -0
  4. package/dist/codegen.d.ts +14 -0
  5. package/dist/codegen.js +322 -0
  6. package/dist/compile.d.ts +13 -0
  7. package/dist/compile.js +108 -0
  8. package/dist/decodeHtml.d.ts +2 -0
  9. package/dist/decodeHtml.js +131 -0
  10. package/dist/errors.d.ts +16 -0
  11. package/dist/errors.js +19 -0
  12. package/dist/identifier.d.ts +7 -0
  13. package/dist/identifier.js +35 -0
  14. package/dist/index.d.ts +12 -0
  15. package/dist/index.js +40 -0
  16. package/dist/namedChars.json +2233 -0
  17. package/dist/options.d.ts +88 -0
  18. package/dist/options.js +2 -0
  19. package/dist/parserOptions.d.ts +7 -0
  20. package/dist/parserOptions.js +63 -0
  21. package/dist/runtimeHelpers.d.ts +13 -0
  22. package/dist/runtimeHelpers.js +32 -0
  23. package/dist/template/codegen.d.ts +19 -0
  24. package/dist/template/codegen.js +381 -0
  25. package/dist/transform.d.ts +73 -0
  26. package/dist/transform.js +518 -0
  27. package/dist/transforms/transformClass.d.ts +5 -0
  28. package/dist/transforms/transformClass.js +162 -0
  29. package/dist/transforms/transformComponent.d.ts +11 -0
  30. package/dist/transforms/transformComponent.js +179 -0
  31. package/dist/transforms/transformElement.d.ts +9 -0
  32. package/dist/transforms/transformElement.js +191 -0
  33. package/dist/transforms/transformExpression.d.ts +5 -0
  34. package/dist/transforms/transformExpression.js +283 -0
  35. package/dist/transforms/transformIdentifier.d.ts +2 -0
  36. package/dist/transforms/transformIdentifier.js +95 -0
  37. package/dist/transforms/transformKey.d.ts +4 -0
  38. package/dist/transforms/transformKey.js +18 -0
  39. package/dist/transforms/transformRef.d.ts +3 -0
  40. package/dist/transforms/transformRef.js +126 -0
  41. package/dist/transforms/transformRoot.d.ts +2 -0
  42. package/dist/transforms/transformRoot.js +42 -0
  43. package/dist/transforms/transformSlot.d.ts +3 -0
  44. package/dist/transforms/transformSlot.js +144 -0
  45. package/dist/transforms/transformStyle.d.ts +5 -0
  46. package/dist/transforms/transformStyle.js +156 -0
  47. package/dist/transforms/transformTag.d.ts +2 -0
  48. package/dist/transforms/transformTag.js +15 -0
  49. package/dist/transforms/utils.d.ts +25 -0
  50. package/dist/transforms/utils.js +133 -0
  51. package/dist/transforms/vBind.d.ts +2 -0
  52. package/dist/transforms/vBind.js +49 -0
  53. package/dist/transforms/vFor.d.ts +35 -0
  54. package/dist/transforms/vFor.js +299 -0
  55. package/dist/transforms/vHtml.d.ts +2 -0
  56. package/dist/transforms/vHtml.js +35 -0
  57. package/dist/transforms/vIf.d.ts +13 -0
  58. package/dist/transforms/vIf.js +139 -0
  59. package/dist/transforms/vModel.d.ts +2 -0
  60. package/dist/transforms/vModel.js +178 -0
  61. package/dist/transforms/vOn.d.ts +8 -0
  62. package/dist/transforms/vOn.js +166 -0
  63. package/dist/transforms/vSlot.d.ts +16 -0
  64. package/dist/transforms/vSlot.js +274 -0
  65. package/package.json +36 -0
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rewritePropsBinding = exports.isPropsBinding = exports.rewriteBinding = exports.transformComponent = void 0;
4
+ const shared_1 = require("@vue/shared");
5
+ const compiler_core_1 = require("@vue/compiler-core");
6
+ const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
7
+ const transform_1 = require("../transform");
8
+ const utils_1 = require("./utils");
9
+ const codegen_1 = require("../codegen");
10
+ const types_1 = require("@babel/types");
11
+ const runtimeHelpers_1 = require("../runtimeHelpers");
12
+ const ast_1 = require("../ast");
13
+ const vIf_1 = require("./vIf");
14
+ const transformComponent = (node, context) => {
15
+ if (!(0, uni_cli_shared_1.isUserComponent)(node, context)) {
16
+ return;
17
+ }
18
+ addComponentType(node, context);
19
+ addVueId(node, context);
20
+ processBooleanAttr(node);
21
+ return function postTransformComponent() {
22
+ context.vueIds.pop();
23
+ };
24
+ };
25
+ exports.transformComponent = transformComponent;
26
+ function addComponentType(node, context) {
27
+ if (!context.isMiniProgramComponent(node.tag)) {
28
+ return;
29
+ }
30
+ node.props.push((0, uni_cli_shared_1.createAttributeNode)(utils_1.ATTR_COM_TYPE, 'm'));
31
+ }
32
+ function addVueId(node, context) {
33
+ let { hashId, scopes, currentScope, currentVueId } = context;
34
+ if (!hashId) {
35
+ return;
36
+ }
37
+ let vueId = hashId + '-' + scopes.vueId++;
38
+ const indexs = [];
39
+ while (currentScope) {
40
+ if ((0, transform_1.isVForScope)(currentScope)) {
41
+ indexs.push(`+'-'+${currentScope.indexAlias}`);
42
+ }
43
+ currentScope = currentScope.parent;
44
+ }
45
+ const inFor = !!indexs.length;
46
+ if (inFor) {
47
+ vueId = `'${vueId}'` + indexs.reverse().join('');
48
+ }
49
+ context.vueIds.push(vueId);
50
+ let value = vueId;
51
+ if (currentVueId) {
52
+ const isParentDynamic = currentVueId.includes('+');
53
+ const isCurrentDynamic = vueId.includes('+');
54
+ if (isParentDynamic || isCurrentDynamic) {
55
+ value = `(${vueId})+','+(${isParentDynamic ? currentVueId : `'${currentVueId}'`})`;
56
+ }
57
+ else {
58
+ value = vueId + ',' + currentVueId;
59
+ }
60
+ }
61
+ if (value.includes('+')) {
62
+ return node.props.push((0, uni_cli_shared_1.createBindDirectiveNode)(utils_1.ATTR_VUE_ID, value));
63
+ }
64
+ return node.props.push((0, uni_cli_shared_1.createAttributeNode)(utils_1.ATTR_VUE_ID, value));
65
+ }
66
+ /**
67
+ * <uni-collapse accordion/> => <uni-collapse :accordion="true"/>
68
+ * 否则部分平台(快手)可能获取到的 accordion 是空字符串
69
+ * @param param0
70
+ */
71
+ function processBooleanAttr({ props }) {
72
+ props.forEach((prop, index) => {
73
+ if (prop.type === 6 /* ATTRIBUTE */ &&
74
+ typeof prop.value === 'undefined') {
75
+ props.splice(index, 1, (0, uni_cli_shared_1.createBindDirectiveNode)(prop.name, 'true'));
76
+ }
77
+ });
78
+ }
79
+ const builtInProps = [
80
+ 'id',
81
+ 'class',
82
+ 'style',
83
+ utils_1.ATTR_VUE_ID,
84
+ utils_1.ATTR_VUE_PROPS,
85
+ utils_1.ATTR_VUE_SLOTS,
86
+ utils_1.ATTR_VUE_REF,
87
+ utils_1.ATTR_VUE_REF_IN_FOR,
88
+ utils_1.ATTR_COM_TYPE,
89
+ 'eO',
90
+ 'e-o',
91
+ 'onVI',
92
+ 'ref',
93
+ 'slot',
94
+ 'key',
95
+ 'is',
96
+ ];
97
+ function isComponentProp(name) {
98
+ if (builtInProps.includes(name)) {
99
+ return false;
100
+ }
101
+ if (name.startsWith('data-')) {
102
+ return false;
103
+ }
104
+ return true;
105
+ }
106
+ /**
107
+ * 重写组件 props 绑定
108
+ * @param node
109
+ * @param context
110
+ */
111
+ function rewriteBinding({ tag, props }, context) {
112
+ var _a;
113
+ const isMiniProgramComponent = context.isMiniProgramComponent(tag);
114
+ const createObjectProperty = isMiniProgramComponent
115
+ ? (name, value) => (0, types_1.objectProperty)((0, types_1.identifier)((0, shared_1.camelize)(name)), value)
116
+ : (name, value) => {
117
+ const computed = !(0, compiler_core_1.isSimpleIdentifier)(name);
118
+ return (0, types_1.objectProperty)(computed ? (0, types_1.stringLiteral)(name) : (0, types_1.identifier)(name), value, computed);
119
+ };
120
+ const properties = [];
121
+ for (let i = 0; i < props.length; i++) {
122
+ const prop = props[i];
123
+ if (prop.type === 6 /* ATTRIBUTE */) {
124
+ const { name } = prop;
125
+ if (!isComponentProp(name)) {
126
+ continue;
127
+ }
128
+ properties.push(createObjectProperty(name, (0, types_1.stringLiteral)(((_a = prop.value) === null || _a === void 0 ? void 0 : _a.content) || '')));
129
+ }
130
+ else if (prop.type === 7 /* DIRECTIVE */) {
131
+ if (prop.name !== 'bind') {
132
+ continue;
133
+ }
134
+ const { arg, exp } = prop;
135
+ if (!arg || !exp) {
136
+ continue;
137
+ }
138
+ if ((0, compiler_core_1.isStaticExp)(arg)) {
139
+ if (!isComponentProp(arg.content)) {
140
+ continue;
141
+ }
142
+ // :name="name"
143
+ const valueExpr = (0, ast_1.parseExpr)((0, codegen_1.genExpr)(exp), context, exp);
144
+ if (!valueExpr) {
145
+ continue;
146
+ }
147
+ properties.push(createObjectProperty(arg.content, valueExpr));
148
+ }
149
+ else {
150
+ // :[dynamic]="dynamic"
151
+ const leftExpr = (0, ast_1.parseExpr)((0, codegen_1.genExpr)(arg), context, exp);
152
+ if (!leftExpr) {
153
+ continue;
154
+ }
155
+ const valueExpr = (0, ast_1.parseExpr)((0, codegen_1.genExpr)(exp), context, exp);
156
+ if (!valueExpr) {
157
+ continue;
158
+ }
159
+ properties.push((0, types_1.objectProperty)((0, types_1.logicalExpression)('||', leftExpr, (0, types_1.stringLiteral)('')), valueExpr, true));
160
+ }
161
+ }
162
+ props.splice(i, 1);
163
+ i--;
164
+ }
165
+ if (properties.length) {
166
+ props.push((0, uni_cli_shared_1.createBindDirectiveNode)(utils_1.ATTR_VUE_PROPS, (0, codegen_1.genBabelExpr)((0, types_1.objectExpression)(properties))));
167
+ }
168
+ }
169
+ exports.rewriteBinding = rewriteBinding;
170
+ function isPropsBinding({ arg }) {
171
+ return (arg &&
172
+ arg.type === 4 /* SIMPLE_EXPRESSION */ &&
173
+ arg.content === utils_1.ATTR_VUE_PROPS);
174
+ }
175
+ exports.isPropsBinding = isPropsBinding;
176
+ function rewritePropsBinding(dir, node, context) {
177
+ dir.exp = (0, compiler_core_1.createSimpleExpression)((0, codegen_1.genBabelExpr)((0, utils_1.rewirteWithHelper)(runtimeHelpers_1.RENDER_PROPS, (0, ast_1.parseExpr)(dir.exp, context), dir.loc, context)) + ((0, vIf_1.isIfElementNode)(node) && node.vIf.name === 'else' ? `||''` : ''));
178
+ }
179
+ exports.rewritePropsBinding = rewritePropsBinding;
@@ -0,0 +1,9 @@
1
+ import { ElementNode, TemplateLiteral, Property } from '@vue/compiler-core';
2
+ import { NodeTransform, TransformContext } from '../transform';
3
+ export interface DirectiveTransformResult {
4
+ props: Property[];
5
+ needRuntime?: boolean | symbol;
6
+ ssrTagParts?: TemplateLiteral['elements'];
7
+ }
8
+ export declare const transformElement: NodeTransform;
9
+ export declare function processProps(node: ElementNode, context: TransformContext, props?: ElementNode['props']): void;
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processProps = exports.transformElement = void 0;
4
+ const shared_1 = require("@vue/shared");
5
+ const compiler_core_1 = require("@vue/compiler-core");
6
+ const uni_shared_1 = require("@dcloudio/uni-shared");
7
+ const errors_1 = require("../errors");
8
+ const vModel_1 = require("./vModel");
9
+ const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
10
+ const transformElement = (node, context) => {
11
+ return function postTransformElement() {
12
+ node = context.currentNode;
13
+ if (!(node.type === 1 /* ELEMENT */ &&
14
+ (node.tagType === 0 /* ELEMENT */ ||
15
+ node.tagType === 1 /* COMPONENT */))) {
16
+ return;
17
+ }
18
+ if (node.tagType === 1 /* COMPONENT */) {
19
+ processComponent(node, context);
20
+ }
21
+ if (context.scopeId) {
22
+ addScopeId(node, context.scopeId);
23
+ }
24
+ const { props } = node;
25
+ if (props.length > 0) {
26
+ processProps(node, context);
27
+ }
28
+ };
29
+ };
30
+ exports.transformElement = transformElement;
31
+ function addScopeId(node, scopeId) {
32
+ return (0, uni_cli_shared_1.addStaticClass)(node, scopeId);
33
+ }
34
+ function processComponent(node, context) {
35
+ const { tag } = node;
36
+ if (context.bindingComponents[tag]) {
37
+ return;
38
+ }
39
+ // 1. dynamic component
40
+ if ((0, uni_shared_1.isComponentTag)(tag)) {
41
+ return context.onError((0, errors_1.createMPCompilerError)(8 /* X_DYNAMIC_COMPONENT_NOT_SUPPORTED */, node.loc));
42
+ }
43
+ if ((0, compiler_core_1.findDir)(node, 'is')) {
44
+ return context.onError((0, errors_1.createMPCompilerError)(6 /* X_V_IS_NOT_SUPPORTED */, node.loc));
45
+ }
46
+ // TODO not supported
47
+ // const isProp = findProp(node, 'is')
48
+ // if (isProp) {
49
+ // }
50
+ // 2. built-in components (Teleport, Transition, KeepAlive, Suspense...)
51
+ const builtIn = (0, compiler_core_1.isCoreComponent)(tag) || context.isBuiltInComponent(tag);
52
+ if (builtIn) {
53
+ return context.onError((0, errors_1.createMPCompilerError)(7 /* X_NOT_SUPPORTED */, node.loc, tag));
54
+ }
55
+ // 3. user component (from setup bindings)
56
+ const fromSetup = resolveSetupReference(tag, context);
57
+ if (fromSetup) {
58
+ return (context.bindingComponents[tag] = {
59
+ name: fromSetup,
60
+ type: "setup" /* SETUP */,
61
+ });
62
+ }
63
+ const dotIndex = tag.indexOf('.');
64
+ if (dotIndex > 0) {
65
+ return context.onError((0, errors_1.createMPCompilerError)(7 /* X_NOT_SUPPORTED */, node.loc, tag));
66
+ }
67
+ // 4. Self referencing component (inferred from filename)
68
+ if (context.selfName && (0, shared_1.capitalize)((0, shared_1.camelize)(tag)) === context.selfName) {
69
+ return (context.bindingComponents[tag] = {
70
+ name: (0, compiler_core_1.toValidAssetId)(tag, `component`),
71
+ type: "self" /* SELF */,
72
+ });
73
+ }
74
+ // 5. user component (resolve)
75
+ context.bindingComponents[tag] = {
76
+ name: (0, compiler_core_1.toValidAssetId)(tag, `component`),
77
+ type: "unknown" /* UNKNOWN */,
78
+ };
79
+ context.helper(compiler_core_1.RESOLVE_COMPONENT);
80
+ }
81
+ function resolveSetupReference(name, context) {
82
+ const bindings = context.bindingMetadata;
83
+ if (!bindings || bindings.__isScriptSetup === false) {
84
+ return;
85
+ }
86
+ const camelName = (0, shared_1.camelize)(name);
87
+ const PascalName = (0, shared_1.capitalize)(camelName);
88
+ const checkType = (type) => {
89
+ if (bindings[name] === type) {
90
+ return name;
91
+ }
92
+ if (bindings[camelName] === type) {
93
+ return camelName;
94
+ }
95
+ if (bindings[PascalName] === type) {
96
+ return PascalName;
97
+ }
98
+ };
99
+ const fromConst = checkType("setup-const" /* SETUP_CONST */);
100
+ if (fromConst) {
101
+ return context.inline
102
+ ? // in inline mode, const setup bindings (e.g. imports) can be used as-is
103
+ fromConst
104
+ : `$setup[${JSON.stringify(fromConst)}]`;
105
+ }
106
+ const fromMaybeRef = checkType("setup-let" /* SETUP_LET */) ||
107
+ checkType("setup-ref" /* SETUP_REF */) ||
108
+ checkType("setup-maybe-ref" /* SETUP_MAYBE_REF */);
109
+ if (fromMaybeRef) {
110
+ return context.inline
111
+ ? // setup scope bindings that may be refs need to be unrefed
112
+ `${context.helperString(compiler_core_1.UNREF)}(${fromMaybeRef})`
113
+ : `$setup[${JSON.stringify(fromMaybeRef)}]`;
114
+ }
115
+ }
116
+ function processProps(node, context, props = node.props) {
117
+ const { tag } = node;
118
+ const isComponent = node.tagType === 1 /* COMPONENT */;
119
+ for (let i = 0; i < props.length; i++) {
120
+ const prop = props[i];
121
+ if (prop.type === 7 /* DIRECTIVE */) {
122
+ // directives
123
+ const { name, arg, loc } = prop;
124
+ const isVBind = name === 'bind';
125
+ const isVOn = name === 'on';
126
+ // skip v-slot - it is handled by its dedicated transform.
127
+ if (name === 'slot') {
128
+ if (!isComponent) {
129
+ context.onError((0, compiler_core_1.createCompilerError)(40 /* X_V_SLOT_MISPLACED */, loc));
130
+ }
131
+ continue;
132
+ }
133
+ // skip v-once/v-memo - they are handled by dedicated transforms.
134
+ if (name === 'once' || name === 'memo') {
135
+ continue;
136
+ }
137
+ // skip v-is and :is on <component>
138
+ if (name === 'is' ||
139
+ (isVBind && (0, compiler_core_1.isStaticArgOf)(arg, 'is') && (0, uni_shared_1.isComponentTag)(tag))) {
140
+ continue;
141
+ }
142
+ if (isVBind || isVOn) {
143
+ // v-on=""
144
+ // v-bind=""
145
+ if (!arg) {
146
+ context.onError((0, errors_1.createMPCompilerError)(isVBind
147
+ ? 2 /* X_V_BIND_NO_ARGUMENT */
148
+ : 0 /* X_V_ON_NO_ARGUMENT */, loc));
149
+ continue;
150
+ }
151
+ // v-on:[a]=""
152
+ // v-on:[a.b]=""
153
+ // v-bind:[a]=""
154
+ // v-bind:[a.b]=""
155
+ if (!(arg.type === 4 /* SIMPLE_EXPRESSION */ && arg.isStatic)) {
156
+ context.onError((0, errors_1.createMPCompilerError)(isVBind
157
+ ? 3 /* X_V_BIND_DYNAMIC_ARGUMENT */
158
+ : 1 /* X_V_ON_DYNAMIC_EVENT */, loc));
159
+ continue;
160
+ }
161
+ }
162
+ const directiveTransform = context.directiveTransforms[name];
163
+ if (name !== 'model' && directiveTransform) {
164
+ const { props } = directiveTransform(prop, node, context);
165
+ if (props.length) {
166
+ prop.exp = props[0].value;
167
+ }
168
+ }
169
+ }
170
+ }
171
+ const transformVModel = (context.directiveTransforms.model ||
172
+ vModel_1.transformModel);
173
+ processVModel(node, transformVModel, context);
174
+ }
175
+ exports.processProps = processProps;
176
+ function processVModel(node, transformVModel, context) {
177
+ const { props } = node;
178
+ const dirs = [];
179
+ for (let i = 0; i < props.length; i++) {
180
+ const prop = props[i];
181
+ if (prop.type === 7 /* DIRECTIVE */ && prop.name === 'model') {
182
+ dirs.push(...transformVModel(prop, node, context)
183
+ .props);
184
+ props.splice(i, 1);
185
+ i--;
186
+ }
187
+ }
188
+ if (dirs.length) {
189
+ props.push(...dirs);
190
+ }
191
+ }
@@ -0,0 +1,5 @@
1
+ import { ExpressionNode, SimpleExpressionNode } from '@vue/compiler-core';
2
+ import { NodeTransform, TransformContext } from '../transform';
3
+ export declare const transformExpression: NodeTransform;
4
+ export declare function processExpression(node: SimpleExpressionNode, context: TransformContext, asParams?: boolean, asRawStatements?: boolean, localVars?: Record<string, number>): ExpressionNode;
5
+ export declare function isBuiltInIdentifier(id: string | ExpressionNode): boolean;
@@ -0,0 +1,283 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isBuiltInIdentifier = exports.processExpression = exports.transformExpression = void 0;
4
+ const shared_1 = require("@vue/shared");
5
+ const compiler_core_1 = require("@vue/compiler-core");
6
+ const parser_1 = require("@babel/parser");
7
+ const isLiteralWhitelisted = /*#__PURE__*/ (0, shared_1.makeMap)('true,false,null,this');
8
+ const transformExpression = (node, context) => {
9
+ if (node.type === 5 /* INTERPOLATION */) {
10
+ node.content = processExpression(node.content, context);
11
+ }
12
+ else if (node.type === 1 /* ELEMENT */) {
13
+ // handle directives on element
14
+ for (let i = 0; i < node.props.length; i++) {
15
+ const dir = node.props[i];
16
+ // do not process for v-on & v-for since they are special handled
17
+ if (dir.type === 7 /* DIRECTIVE */ && dir.name !== 'for') {
18
+ const exp = dir.exp;
19
+ const arg = dir.arg;
20
+ // do not process exp if this is v-on:arg - we need special handling
21
+ // for wrapping inline statements.
22
+ if (exp &&
23
+ exp.type === 4 /* SIMPLE_EXPRESSION */ &&
24
+ !(dir.name === 'on' && arg)) {
25
+ dir.exp = processExpression(exp, context,
26
+ // slot args must be processed as function params
27
+ dir.name === 'slot');
28
+ }
29
+ if (arg && arg.type === 4 /* SIMPLE_EXPRESSION */ && !arg.isStatic) {
30
+ dir.arg = processExpression(arg, context);
31
+ }
32
+ }
33
+ }
34
+ }
35
+ };
36
+ exports.transformExpression = transformExpression;
37
+ function processExpression(node, context,
38
+ // some expressions like v-slot props & v-for aliases should be parsed as
39
+ // function params
40
+ asParams = false,
41
+ // v-on handler values may contain multiple statements
42
+ asRawStatements = false, localVars = Object.create(context.identifiers)) {
43
+ if (!node.content.trim()) {
44
+ return node;
45
+ }
46
+ const { inline, bindingMetadata } = context;
47
+ const rewriteIdentifier = (raw, parent, id) => {
48
+ const type = (0, shared_1.hasOwn)(bindingMetadata, raw) && bindingMetadata[raw];
49
+ if (inline) {
50
+ // x = y
51
+ const isAssignmentLVal = parent && parent.type === 'AssignmentExpression' && parent.left === id;
52
+ // x++
53
+ const isUpdateArg = parent && parent.type === 'UpdateExpression' && parent.argument === id;
54
+ // ({ x } = y)
55
+ const isDestructureAssignment = parent && (0, compiler_core_1.isInDestructureAssignment)(parent, parentStack);
56
+ if (type === "setup-const" /* SETUP_CONST */ || localVars[raw]) {
57
+ return raw;
58
+ }
59
+ else if (type === "setup-ref" /* SETUP_REF */) {
60
+ return `${raw}.value`;
61
+ }
62
+ else if (type === "setup-maybe-ref" /* SETUP_MAYBE_REF */) {
63
+ // const binding that may or may not be ref
64
+ // if it's not a ref, then assignments don't make sense -
65
+ // so we ignore the non-ref assignment case and generate code
66
+ // that assumes the value to be a ref for more efficiency
67
+ return isAssignmentLVal || isUpdateArg || isDestructureAssignment
68
+ ? `${raw}.value`
69
+ : `${context.helperString(compiler_core_1.UNREF)}(${raw})`;
70
+ }
71
+ else if (type === "setup-let" /* SETUP_LET */) {
72
+ if (isAssignmentLVal) {
73
+ // let binding.
74
+ // this is a bit more tricky as we need to cover the case where
75
+ // let is a local non-ref value, and we need to replicate the
76
+ // right hand side value.
77
+ // x = y --> isRef(x) ? x.value = y : x = y
78
+ const { right: rVal, operator } = parent;
79
+ const rExp = rawExp.slice(rVal.start - 1, rVal.end - 1);
80
+ const rExpString = stringifyExpression(processExpression((0, compiler_core_1.createSimpleExpression)(rExp, false), context, false, false, knownIds));
81
+ return `${context.helperString(compiler_core_1.IS_REF)}(${raw})${context.isTS ? ` //@ts-ignore\n` : ``} ? ${raw}.value ${operator} ${rExpString} : ${raw}`;
82
+ }
83
+ else if (isUpdateArg) {
84
+ // make id replace parent in the code range so the raw update operator
85
+ // is removed
86
+ id.start = parent.start;
87
+ id.end = parent.end;
88
+ const { prefix: isPrefix, operator } = parent;
89
+ const prefix = isPrefix ? operator : ``;
90
+ const postfix = isPrefix ? `` : operator;
91
+ // let binding.
92
+ // x++ --> isRef(a) ? a.value++ : a++
93
+ return `${context.helperString(compiler_core_1.IS_REF)}(${raw})${context.isTS ? ` //@ts-ignore\n` : ``} ? ${prefix}${raw}.value${postfix} : ${prefix}${raw}${postfix}`;
94
+ }
95
+ else if (isDestructureAssignment) {
96
+ // TODO
97
+ // let binding in a destructure assignment - it's very tricky to
98
+ // handle both possible cases here without altering the original
99
+ // structure of the code, so we just assume it's not a ref here
100
+ // for now
101
+ return raw;
102
+ }
103
+ else {
104
+ return `${context.helperString(compiler_core_1.UNREF)}(${raw})`;
105
+ }
106
+ }
107
+ else if (type === "props" /* PROPS */) {
108
+ // use __props which is generated by compileScript so in ts mode
109
+ // it gets correct type
110
+ return `__props.${raw}`;
111
+ }
112
+ }
113
+ else {
114
+ if (type && type.startsWith('setup')) {
115
+ // setup bindings in non-inline mode
116
+ return `$setup.${raw}`;
117
+ }
118
+ else if (type) {
119
+ return `$${type}.${raw}`;
120
+ }
121
+ }
122
+ // fallback to ctx
123
+ return `_ctx.${raw}`;
124
+ };
125
+ // fast path if expression is a simple identifier.
126
+ const rawExp = node.content;
127
+ // bail constant on parens (function invocation) and dot (member access)
128
+ const bailConstant = rawExp.indexOf(`(`) > -1 || rawExp.indexOf('.') > 0;
129
+ if ((0, compiler_core_1.isSimpleIdentifier)(rawExp)) {
130
+ const isScopeVarReference = context.identifiers[rawExp];
131
+ const isAllowedGlobal = (0, shared_1.isGloballyWhitelisted)(rawExp);
132
+ const isLiteral = isLiteralWhitelisted(rawExp);
133
+ const isFilter = context.filters.includes(rawExp);
134
+ const isBuiltIn = isBuiltInIdentifier(rawExp);
135
+ if (!asParams &&
136
+ !isScopeVarReference &&
137
+ !isAllowedGlobal &&
138
+ !isLiteral &&
139
+ !isFilter &&
140
+ !isBuiltIn) {
141
+ // const bindings exposed from setup can be skipped for patching but
142
+ // cannot be hoisted to module scope
143
+ if (bindingMetadata[node.content] === "setup-const" /* SETUP_CONST */) {
144
+ node.constType = 1 /* CAN_SKIP_PATCH */;
145
+ }
146
+ node.content = rewriteIdentifier(rawExp);
147
+ }
148
+ else if (!isScopeVarReference) {
149
+ if (isLiteral) {
150
+ node.constType = 3 /* CAN_STRINGIFY */;
151
+ }
152
+ else {
153
+ node.constType = 2 /* CAN_HOIST */;
154
+ }
155
+ }
156
+ return node;
157
+ }
158
+ let ast;
159
+ // exp needs to be parsed differently:
160
+ // 1. Multiple inline statements (v-on, with presence of `;`): parse as raw
161
+ // exp, but make sure to pad with spaces for consistent ranges
162
+ // 2. Expressions: wrap with parens (for e.g. object expressions)
163
+ // 3. Function arguments (v-for, v-slot): place in a function argument position
164
+ const source = asRawStatements
165
+ ? ` ${rawExp} `
166
+ : `(${rawExp})${asParams ? `=>{}` : ``}`;
167
+ try {
168
+ ast = (0, parser_1.parse)(source, {
169
+ plugins: context.expressionPlugins,
170
+ }).program;
171
+ }
172
+ catch (e) {
173
+ context.onError((0, compiler_core_1.createCompilerError)(44 /* X_INVALID_EXPRESSION */, node.loc, undefined, '\n' + source + '\n' + e.message));
174
+ return node;
175
+ }
176
+ const ids = [];
177
+ const parentStack = [];
178
+ const knownIds = Object.create(context.identifiers);
179
+ context.filters.forEach((name) => {
180
+ knownIds[name] = 1;
181
+ });
182
+ (0, compiler_core_1.walkIdentifiers)(ast, (node, parent, _, isReferenced, isLocal) => {
183
+ if ((0, compiler_core_1.isStaticPropertyKey)(node, parent)) {
184
+ return;
185
+ }
186
+ const needPrefix = isReferenced && canPrefix(node);
187
+ if (needPrefix && !isLocal) {
188
+ if ((0, compiler_core_1.isStaticProperty)(parent) && parent.shorthand) {
189
+ // property shorthand like { foo }, we need to add the key since
190
+ // we rewrite the value
191
+ ;
192
+ node.prefix = `${node.name}: `;
193
+ }
194
+ node.name = rewriteIdentifier(node.name, parent, node);
195
+ ids.push(node);
196
+ }
197
+ else {
198
+ // The identifier is considered constant unless it's pointing to a
199
+ // local scope variable (a v-for alias, or a v-slot prop)
200
+ if (!(needPrefix && isLocal) && !bailConstant) {
201
+ ;
202
+ node.isConstant = true;
203
+ }
204
+ // also generate sub-expressions for other identifiers for better
205
+ // source map support. (except for property keys which are static)
206
+ ids.push(node);
207
+ }
208
+ }, true, // invoke on ALL identifiers
209
+ parentStack, knownIds);
210
+ // We break up the compound expression into an array of strings and sub
211
+ // expressions (for identifiers that have been prefixed). In codegen, if
212
+ // an ExpressionNode has the `.children` property, it will be used instead of
213
+ // `.content`.
214
+ const children = [];
215
+ ids.sort((a, b) => a.start - b.start);
216
+ ids.forEach((id, i) => {
217
+ // range is offset by -1 due to the wrapping parens when parsed
218
+ const start = id.start - 1;
219
+ const end = id.end - 1;
220
+ const last = ids[i - 1];
221
+ const leadingText = rawExp.slice(last ? last.end - 1 : 0, start);
222
+ if (leadingText.length || id.prefix) {
223
+ children.push(leadingText + (id.prefix || ``));
224
+ }
225
+ const source = rawExp.slice(start, end);
226
+ children.push((0, compiler_core_1.createSimpleExpression)(id.name, false, {
227
+ source,
228
+ start: (0, compiler_core_1.advancePositionWithClone)(node.loc.start, source, start),
229
+ end: (0, compiler_core_1.advancePositionWithClone)(node.loc.start, source, end),
230
+ }, id.isConstant ? 3 /* CAN_STRINGIFY */ : 0 /* NOT_CONSTANT */));
231
+ if (i === ids.length - 1 && end < rawExp.length) {
232
+ children.push(rawExp.slice(end));
233
+ }
234
+ });
235
+ let ret;
236
+ if (children.length) {
237
+ ret = (0, compiler_core_1.createCompoundExpression)(children, node.loc);
238
+ }
239
+ else {
240
+ ret = node;
241
+ ret.constType = bailConstant
242
+ ? 0 /* NOT_CONSTANT */
243
+ : 3 /* CAN_STRINGIFY */;
244
+ }
245
+ ret.identifiers = Object.keys(knownIds);
246
+ return ret;
247
+ }
248
+ exports.processExpression = processExpression;
249
+ function canPrefix(id) {
250
+ // skip whitelisted globals
251
+ if ((0, shared_1.isGloballyWhitelisted)(id.name)) {
252
+ return false;
253
+ }
254
+ // special case for webpack compilation
255
+ if (id.name === 'require') {
256
+ return false;
257
+ }
258
+ return true;
259
+ }
260
+ function stringifyExpression(exp) {
261
+ if ((0, shared_1.isString)(exp)) {
262
+ return exp;
263
+ }
264
+ else if (exp.type === 4 /* SIMPLE_EXPRESSION */) {
265
+ return exp.content;
266
+ }
267
+ else {
268
+ return exp.children
269
+ .map(stringifyExpression)
270
+ .join('');
271
+ }
272
+ }
273
+ const builtInIdentifiers = ['__l'];
274
+ function isBuiltInIdentifier(id) {
275
+ if (!(0, shared_1.isString)(id)) {
276
+ if (id.type !== 4 /* SIMPLE_EXPRESSION */) {
277
+ return false;
278
+ }
279
+ id = id.content;
280
+ }
281
+ return builtInIdentifiers.includes(id);
282
+ }
283
+ exports.isBuiltInIdentifier = isBuiltInIdentifier;
@@ -0,0 +1,2 @@
1
+ import { NodeTransform } from '../transform';
2
+ export declare const transformIdentifier: NodeTransform;