Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for yield return #143

Merged
merged 3 commits into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions src/mappers/mapOp.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { Op as CoffeeOp } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
import { Op as CoffeeOp, Return as CoffeeReturn } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
import { inspect } from 'util';
import { MultiplyOp, Op, SubtractOp, UnaryNegateOp } from '../nodes';
import { MultiplyOp, Node, Op, SubtractOp, UnaryNegateOp, Yield, YieldFrom, YieldReturn } from '../nodes';
import ParseContext from '../util/ParseContext';
import mapAny from './mapAny';
import { UnsupportedNodeError } from './mapAnyWithFallback';
import mapBase from './mapBase';

export default function mapOp(context: ParseContext, node: CoffeeOp): Op {
export default function mapOp(context: ParseContext, node: CoffeeOp): Node {
switch (node.operator) {
case '-':
return mapSubtractOp(context, node);

case '*':
return mapMultiplyOp(context, node);

case 'yield':
return mapYieldOp(context, node);

case 'yield*':
return mapYieldFromOp(context, node);
}

throw new UnsupportedNodeError(node);
Expand All @@ -35,7 +41,7 @@ function mapSubtractOp(context: ParseContext, node: CoffeeOp): Op {
}
}

function mapMultiplyOp(context: ParseContext, node: CoffeeOp) {
function mapMultiplyOp(context: ParseContext, node: CoffeeOp): Op {
let { line, column, start, end, raw, virtual } = mapBase(context, node);

if (!node.second) {
Expand All @@ -48,3 +54,29 @@ function mapMultiplyOp(context: ParseContext, node: CoffeeOp) {
mapAny(context, node.second)
);
}

function mapYieldOp(context: ParseContext, node: CoffeeOp): Node {
let { line, column, start, end, raw, virtual } = mapBase(context, node);

if (node.first instanceof CoffeeReturn) {
let expression = node.first.expression;
return new YieldReturn(
line, column, start, end, raw, virtual,
expression ? mapAny(context, expression) : null,
);
} else {
return new Yield(
line, column, start, end, raw, virtual,
mapAny(context, node.first)
);
}
}

function mapYieldFromOp(context: ParseContext, node: CoffeeOp): YieldFrom {
let { line, column, start, end, raw, virtual } = mapBase(context, node);

return new YieldFrom(
line, column, start, end, raw, virtual,
mapAny(context, node.first)
);
}
51 changes: 51 additions & 0 deletions src/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,23 @@ export class Return extends Node {
}
}

export class YieldReturn extends Node {
readonly expression: Node | null;

constructor(
line: number,
column: number,
start: number,
end: number,
raw: string,
virtual: boolean,
expression: Node | null
) {
super('YieldReturn', line, column, start, end, raw, virtual);
this.expression = expression;
}
}

export class This extends Node {
constructor(
line: number,
Expand Down Expand Up @@ -1007,6 +1024,40 @@ export class SeqOp extends BinaryOp {
}
}

export class Yield extends Node {
readonly expression: Node;

constructor(
line: number,
column: number,
start: number,
end: number,
raw: string,
virtual: boolean,
expression: Node
) {
super('Yield', line, column, start, end, raw, virtual);
this.expression = expression;
}
}

export class YieldFrom extends Node {
readonly expression: Node;

constructor(
line: number,
column: number,
start: number,
end: number,
raw: string,
virtual: boolean,
expression: Node
) {
super('YieldFrom', line, column, start, end, raw, virtual);
this.expression = expression;
}
}

export abstract class BaseFunction extends Node {
readonly parameters: Array<Node>;
readonly body: Block;
Expand Down
15 changes: 11 additions & 4 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1171,10 +1171,17 @@ function convert(context: ParseContext, map: (context: ParseContext, node: Base,
arguments: []
});

case 'yield':
return makeNode(context, 'Yield', op.locationData, {
expression: convertChild(op.first)
});
case 'yield': {
if (type(op.first) === 'Return') {
return makeNode(context, 'YieldReturn', op.locationData, {
expression: convertChild(op.first.expression),
});
} else {
return makeNode(context, 'Yield', op.locationData, {
expression: convertChild(op.first)
});
}
}

case 'yield*':
return makeNode(context, 'YieldFrom', op.locationData, {
Expand Down
1 change: 1 addition & 0 deletions test/examples/yield-return-empty/input.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-> yield return
57 changes: 57 additions & 0 deletions test/examples/yield-return-empty/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"type": "Program",
"line": 1,
"column": 1,
"range": [
0,
16
],
"raw": "-> yield return\n",
"body": {
"type": "Block",
"line": 1,
"column": 1,
"range": [
0,
15
],
"statements": [
{
"type": "GeneratorFunction",
"line": 1,
"column": 1,
"range": [
0,
15
],
"body": {
"type": "Block",
"line": 1,
"column": 4,
"range": [
3,
15
],
"statements": [
{
"type": "YieldReturn",
"line": 1,
"column": 4,
"range": [
3,
15
],
"expression": null,
"raw": "yield return"
}
],
"raw": "yield return",
"inline": true
},
"parameters": [],
"raw": "-> yield return"
}
],
"raw": "-> yield return"
}
}
1 change: 1 addition & 0 deletions test/examples/yield-return/input.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-> yield return 3
67 changes: 67 additions & 0 deletions test/examples/yield-return/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"type": "Program",
"line": 1,
"column": 1,
"range": [
0,
18
],
"raw": "-> yield return 3\n",
"body": {
"type": "Block",
"line": 1,
"column": 1,
"range": [
0,
17
],
"statements": [
{
"type": "GeneratorFunction",
"line": 1,
"column": 1,
"range": [
0,
17
],
"body": {
"type": "Block",
"line": 1,
"column": 4,
"range": [
3,
17
],
"statements": [
{
"type": "YieldReturn",
"line": 1,
"column": 4,
"range": [
3,
17
],
"expression": {
"type": "Int",
"line": 1,
"column": 17,
"range": [
16,
17
],
"raw": "3",
"data": 3
},
"raw": "yield return 3"
}
],
"raw": "yield return 3",
"inline": true
},
"parameters": [],
"raw": "-> yield return 3"
}
],
"raw": "-> yield return 3"
}
}