Skip to content

Commit

Permalink
Merge pull request #1267 from vuejs/support-expression-statement-fn-b…
Browse files Browse the repository at this point in the history
…lock

Support statement expression in arrow function block
  • Loading branch information
octref authored May 7, 2019
2 parents 4af0623 + 02039f0 commit bbed70d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ suite('transformTemplate', () => {
);
});

test('ArrowFunction: statement block', () => {
check('(bar) => { foo + bar; }', '(bar) => { this.foo + bar; }');
});

test('TemplateExpression', () => {
check('`font-size: ${size}px`', '`font-size: ${this.size}px`');
});
Expand Down
14 changes: 12 additions & 2 deletions server/src/services/typescriptService/transformTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,14 +627,24 @@ export function getTemplateTransformFunctions(ts: T_TypeScript) {
res = ts.createArrayLiteral(exp.elements.map(e => injectThis(e, scope, start)));
} else if (ts.isSpreadElement(exp)) {
res = ts.createSpread(injectThis(exp.expression, scope, start));
} else if (ts.isArrowFunction(exp) && !ts.isBlock(exp.body)) {
} else if (ts.isArrowFunction(exp)) {
const fnScope = scope.concat(flatMap(exp.parameters, collectScope));
const body = ts.isBlock(exp.body)
? ts.createBlock(
// Only handle expression statement
exp.body.statements.filter(ts.isExpressionStatement).map(st => {
return ts.createExpressionStatement(injectThis(st.expression, fnScope, start));
})
)
: injectThis(exp.body, fnScope, start);

res = ts.createArrowFunction(
exp.modifiers,
exp.typeParameters,
exp.parameters,
exp.type,
exp.equalsGreaterThanToken,
injectThis(exp.body, scope.concat(flatMap(exp.parameters, collectScope)), start)
body
);
} else if (ts.isTemplateExpression(exp)) {
const injectedSpans = exp.templateSpans.map(span => {
Expand Down

0 comments on commit bbed70d

Please sign in to comment.