Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Resolve #2191: Add spaces after return and yield if necessary
  • Loading branch information
lukastaegert committed May 16, 2018
1 parent 6065388 commit 4e8435c
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/ast/nodes/ReturnStatement.ts
Expand Up @@ -2,17 +2,30 @@ import { UNKNOWN_EXPRESSION } from '../values';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import * as NodeType from './NodeType';
import { ExpressionNode, StatementBase } from './shared/Node';
import { RenderOptions } from '../../utils/renderHelpers';
import MagicString from 'magic-string';

export default class ReturnStatement extends StatementBase {
type: NodeType.tReturnStatement;
argument: ExpressionNode | null;

hasEffects(options: ExecutionPathOptions) {
return super.hasEffects(options) || !options.ignoreReturnAwaitYield();
return (
!options.ignoreReturnAwaitYield() || (this.argument && this.argument.hasEffects(options))
);
}

initialise() {
this.included = false;
this.scope.addReturnExpression(this.argument || UNKNOWN_EXPRESSION);
}

render(code: MagicString, options: RenderOptions) {
if (this.argument) {
this.argument.render(code, options);
if (this.argument.start === this.start + 6 /* 'return'.length */) {
code.prependLeft(this.start + 6, ' ');
}
}
}
}
15 changes: 14 additions & 1 deletion src/ast/nodes/YieldExpression.ts
@@ -1,13 +1,26 @@
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import * as NodeType from './NodeType';
import { ExpressionNode, NodeBase } from './shared/Node';
import { RenderOptions } from '../../utils/renderHelpers';
import MagicString from 'magic-string';

export default class YieldExpression extends NodeBase {
type: NodeType.tYieldExpression;
argument: ExpressionNode | null;
delegate: boolean;

hasEffects(options: ExecutionPathOptions) {
return super.hasEffects(options) || !options.ignoreReturnAwaitYield();
return (
!options.ignoreReturnAwaitYield() || (this.argument && this.argument.hasEffects(options))
);
}

render(code: MagicString, options: RenderOptions) {
if (this.argument) {
this.argument.render(code, options);
if (this.argument.start === this.start + 5 /* 'yield'.length */) {
code.prependLeft(this.start + 5, ' ');
}
}
}
}
3 changes: 3 additions & 0 deletions test/form/samples/return-statement/missing-space/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'Inserts space when simplifying return statement without space'
};
5 changes: 5 additions & 0 deletions test/form/samples/return-statement/missing-space/_expected.js
@@ -0,0 +1,5 @@
function test() {
return null;
}

console.log(test());
5 changes: 5 additions & 0 deletions test/form/samples/return-statement/missing-space/main.js
@@ -0,0 +1,5 @@
function test() {
return!1||null;
}

console.log(test());
3 changes: 3 additions & 0 deletions test/form/samples/yield-expression/missing-space/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'Inserts space when simplifying yield expression without space'
};
7 changes: 7 additions & 0 deletions test/form/samples/yield-expression/missing-space/_expected.js
@@ -0,0 +1,7 @@
function* test() {
yield null;
}

for (const x of test()) {
console.log(x);
}
7 changes: 7 additions & 0 deletions test/form/samples/yield-expression/missing-space/main.js
@@ -0,0 +1,7 @@
function* test() {
yield!1||null;
}

for (const x of test()) {
console.log(x);
}

0 comments on commit 4e8435c

Please sign in to comment.