Skip to content

Commit 12b863e

Browse files
committed
refactor(language-core): simplify event codegen
1 parent 5a79d39 commit 12b863e

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

packages/language-core/lib/codegen/template/elementEvents.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,11 @@ export function* generateEventExpression(
122122
prop: CompilerDOM.DirectiveNode,
123123
): Generator<Code> {
124124
if (prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
125-
let prefix = `(`;
126-
let suffix = `)`;
127125
let isFirstMapping = true;
128126

129127
const ast = createTsAst(options.ts, ctx.inlineTsAsts, prop.exp.content);
130128
const isCompound = isCompoundExpression(options.ts, ast);
131-
132-
if (isCompound) {
133-
ctx.addLocalVariable('$event');
134-
135-
yield `(...[$event]) => {${newLine}`;
136-
yield* ctx.generateConditionGuards();
137-
prefix = ``;
138-
suffix = ``;
139-
}
140-
141-
yield* generateInterpolation(
129+
const interpolation = generateInterpolation(
142130
options,
143131
ctx,
144132
'template',
@@ -162,17 +150,23 @@ export function* generateEventExpression(
162150
},
163151
prop.exp.content,
164152
prop.exp.loc.start.offset,
165-
prefix,
166-
suffix,
153+
isCompound ? `` : `(`,
154+
isCompound ? `` : `)`,
167155
);
168156

169157
if (isCompound) {
170-
ctx.removeLocalVariable('$event');
171-
158+
yield `(...[$event]) => {${newLine}`;
159+
ctx.addLocalVariable('$event');
160+
yield* ctx.generateConditionGuards();
161+
yield* interpolation;
172162
yield endOfLine;
163+
ctx.removeLocalVariable('$event');
173164
yield* ctx.generateAutoImportCompletion();
174165
yield `}`;
175166
}
167+
else {
168+
yield* interpolation;
169+
}
176170
}
177171
else {
178172
yield `() => {}`;
@@ -186,6 +180,7 @@ export function* generateModelEventExpression(
186180
): Generator<Code> {
187181
if (prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
188182
yield `(...[$event]) => {${newLine}`;
183+
ctx.addLocalVariable('$event');
189184
yield* ctx.generateConditionGuards();
190185
yield* generateInterpolation(
191186
options,
@@ -196,6 +191,8 @@ export function* generateModelEventExpression(
196191
prop.exp.loc.start.offset,
197192
);
198193
yield ` = $event${endOfLine}`;
194+
ctx.removeLocalVariable('$event');
195+
yield* ctx.generateAutoImportCompletion();
199196
yield `}`;
200197
}
201198
else {

0 commit comments

Comments
 (0)