From 585007396f5f94f8a40ccc84b20edbdfd7a80dd1 Mon Sep 17 00:00:00 2001 From: Jack Cross Date: Thu, 5 Oct 2017 09:42:59 -0400 Subject: [PATCH] Added step over functionality for yield (#4232) (#4276) * Added step over functionality for yield (#4232) * Consistent parser steps naming and generators fixture --- src/workers/parser/steps.js | 21 +++--- .../parser/tests/fixtures/generators.js | 4 ++ src/workers/parser/tests/steps.spec.js | 65 ++++++++++++------- src/workers/parser/utils/helpers.js | 8 +++ 4 files changed, 63 insertions(+), 35 deletions(-) create mode 100644 src/workers/parser/tests/fixtures/generators.js diff --git a/src/workers/parser/steps.js b/src/workers/parser/steps.js index 29af7f4aed..ec4ab43c47 100644 --- a/src/workers/parser/steps.js +++ b/src/workers/parser/steps.js @@ -1,36 +1,33 @@ import { Source } from "../../../flow-typed/debugger-html"; import { AstPosition } from "./types"; import { getClosestPath } from "./utils/closest"; -import { isAwaitExpression } from "./utils/helpers"; +import { isAwaitExpression, isYieldExpression } from "./utils/helpers"; import type { NodePath } from "babel-traverse"; export function getNextStep(source: Source, pausedPosition: AstPosition) { - const awaitExpression = getAwaitExpression(source, pausedPosition); - if (!awaitExpression) { + const currentExpression = getSteppableExpression(source, pausedPosition); + if (!currentExpression) { return null; } - const awaitStatement = awaitExpression.getStatementParent(); - return getLocationAfterAwaitExpression(awaitStatement, pausedPosition); + const currentStatement = currentExpression.getStatementParent(); + return _getNextStep(currentStatement, pausedPosition); } -function getAwaitExpression(source: Source, pausedPosition: AstPosition) { +function getSteppableExpression(source: Source, pausedPosition: AstPosition) { const closestPath = getClosestPath(source, pausedPosition); if (!closestPath) { return null; } - if (isAwaitExpression(closestPath)) { + if (isAwaitExpression(closestPath) || isYieldExpression(closestPath)) { return closestPath; } - return closestPath.find(p => p.isAwaitExpression()); + return closestPath.find(p => p.isAwaitExpression() || p.isYieldExpression()); } -function getLocationAfterAwaitExpression( - statement: NodePath, - position: AstPosition -) { +function _getNextStep(statement: NodePath, position: AstPosition) { const nextStatement = statement.getSibling(statement.key + 1); if (nextStatement.node) { return { diff --git a/src/workers/parser/tests/fixtures/generators.js b/src/workers/parser/tests/fixtures/generators.js new file mode 100644 index 0000000000..7c71838827 --- /dev/null +++ b/src/workers/parser/tests/fixtures/generators.js @@ -0,0 +1,4 @@ +function* foo() { + yield 1; + yield 2; +} diff --git a/src/workers/parser/tests/steps.spec.js b/src/workers/parser/tests/steps.spec.js index f00c523a7a..737a657086 100644 --- a/src/workers/parser/tests/steps.spec.js +++ b/src/workers/parser/tests/steps.spec.js @@ -2,34 +2,53 @@ import { getNextStep } from "../steps"; import { getSource } from "./helpers"; describe("getNextStep", () => { - it("first await call", () => { - const source = getSource("async"); - const pausePosition = { line: 8, column: 2, sourceId: "async" }; - expect(getNextStep(source, pausePosition)).toEqual({ - ...pausePosition, - line: 9 + describe("await", () => { + it("first await call", () => { + const source = getSource("async"); + const pausePosition = { line: 8, column: 2, sourceId: "async" }; + expect(getNextStep(source, pausePosition)).toEqual({ + ...pausePosition, + line: 9 + }); }); - }); - it("first await call expression", () => { - const source = getSource("async"); - const pausePosition = { line: 8, column: 9, sourceId: "async" }; - expect(getNextStep(source, pausePosition)).toEqual({ - ...pausePosition, - line: 9, - column: 2 + it("first await call expression", () => { + const source = getSource("async"); + const pausePosition = { line: 8, column: 9, sourceId: "async" }; + expect(getNextStep(source, pausePosition)).toEqual({ + ...pausePosition, + line: 9, + column: 2 + }); + }); + + it("second await call", () => { + const source = getSource("async"); + const pausePosition = { line: 9, column: 2, sourceId: "async" }; + expect(getNextStep(source, pausePosition)).toEqual(null); }); - }); - it("second await call", () => { - const source = getSource("async"); - const pausePosition = { line: 9, column: 2, sourceId: "async" }; - expect(getNextStep(source, pausePosition)).toEqual(null); + it("second call expression", () => { + const source = getSource("async"); + const pausePosition = { line: 9, column: 9, sourceId: "async" }; + expect(getNextStep(source, pausePosition)).toEqual(null); + }); }); - it("second call expression", () => { - const source = getSource("async"); - const pausePosition = { line: 9, column: 9, sourceId: "async" }; - expect(getNextStep(source, pausePosition)).toEqual(null); + describe("yield", () => { + it("first yield call", () => { + const source = getSource("generators"); + const pausePosition = { line: 2, column: 2, sourceId: "generators" }; + expect(getNextStep(source, pausePosition)).toEqual({ + ...pausePosition, + line: 3 + }); + }); + + it("second yield call", () => { + const source = getSource("generators"); + const pausePosition = { line: 3, column: 2, sourceId: "generators" }; + expect(getNextStep(source, pausePosition)).toEqual(null); + }); }); }); diff --git a/src/workers/parser/utils/helpers.js b/src/workers/parser/utils/helpers.js index 03e89826bc..6e4f860837 100644 --- a/src/workers/parser/utils/helpers.js +++ b/src/workers/parser/utils/helpers.js @@ -24,6 +24,14 @@ export function isAwaitExpression(path: NodePath) { ); } +export function isYieldExpression(path: NodePath) { + return ( + t.isYieldExpression(path) || + t.isYieldExpression(path.container.init) || + t.isYieldExpression(path.parentPath) + ); +} + export function isVariable(path: NodePath) { return ( t.isVariableDeclaration(path) ||