From d0a5ef62c35814b1273a2613abfb2ead4433d328 Mon Sep 17 00:00:00 2001 From: jcros8 Date: Tue, 3 Oct 2017 20:22:19 -0400 Subject: [PATCH 1/2] Added step over functionality for yield (#4232) --- src/workers/parser/steps.js | 10 ++-- src/workers/parser/tests/fixtures/async.js | 5 ++ src/workers/parser/tests/steps.spec.js | 65 ++++++++++++++-------- src/workers/parser/utils/helpers.js | 8 +++ 4 files changed, 60 insertions(+), 28 deletions(-) diff --git a/src/workers/parser/steps.js b/src/workers/parser/steps.js index 29af7f4aed..ccfbc2d5c5 100644 --- a/src/workers/parser/steps.js +++ b/src/workers/parser/steps.js @@ -1,11 +1,11 @@ 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); + const awaitExpression = getAsyncExpression(source, pausedPosition); if (!awaitExpression) { return null; } @@ -13,18 +13,18 @@ export function getNextStep(source: Source, pausedPosition: AstPosition) { return getLocationAfterAwaitExpression(awaitStatement, pausedPosition); } -function getAwaitExpression(source: Source, pausedPosition: AstPosition) { +function getAsyncExpression(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( diff --git a/src/workers/parser/tests/fixtures/async.js b/src/workers/parser/tests/fixtures/async.js index 43216be635..86726760e8 100644 --- a/src/workers/parser/tests/fixtures/async.js +++ b/src/workers/parser/tests/fixtures/async.js @@ -8,3 +8,8 @@ async function stuff() { await foo(1); await foo(2); } + +function* bar() { + yield 1; + yield 2; +} diff --git a/src/workers/parser/tests/steps.spec.js b/src/workers/parser/tests/steps.spec.js index f00c523a7a..93eb326885 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("async"); + const pausePosition = { line: 13, column: 2, sourceId: "async" }; + expect(getNextStep(source, pausePosition)).toEqual({ + ...pausePosition, + line: 14 + }); + }); + + it("second yield call", () => { + const source = getSource("async"); + const pausePosition = { line: 14, column: 2, sourceId: "async" }; + 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) || From 70532f422c5c2163dace6a20aac7c669db82a4dd Mon Sep 17 00:00:00 2001 From: jcros8 Date: Wed, 4 Oct 2017 19:20:06 -0400 Subject: [PATCH 2/2] Consistent parser steps naming and generators fixture --- src/workers/parser/steps.js | 15 ++++++--------- src/workers/parser/tests/fixtures/async.js | 5 ----- src/workers/parser/tests/fixtures/generators.js | 4 ++++ src/workers/parser/tests/steps.spec.js | 10 +++++----- 4 files changed, 15 insertions(+), 19 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 ccfbc2d5c5..ec4ab43c47 100644 --- a/src/workers/parser/steps.js +++ b/src/workers/parser/steps.js @@ -5,15 +5,15 @@ import { isAwaitExpression, isYieldExpression } from "./utils/helpers"; import type { NodePath } from "babel-traverse"; export function getNextStep(source: Source, pausedPosition: AstPosition) { - const awaitExpression = getAsyncExpression(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 getAsyncExpression(source: Source, pausedPosition: AstPosition) { +function getSteppableExpression(source: Source, pausedPosition: AstPosition) { const closestPath = getClosestPath(source, pausedPosition); if (!closestPath) { @@ -27,10 +27,7 @@ function getAsyncExpression(source: Source, pausedPosition: AstPosition) { 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/async.js b/src/workers/parser/tests/fixtures/async.js index 86726760e8..43216be635 100644 --- a/src/workers/parser/tests/fixtures/async.js +++ b/src/workers/parser/tests/fixtures/async.js @@ -8,8 +8,3 @@ async function stuff() { await foo(1); await foo(2); } - -function* bar() { - yield 1; - yield 2; -} 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 93eb326885..737a657086 100644 --- a/src/workers/parser/tests/steps.spec.js +++ b/src/workers/parser/tests/steps.spec.js @@ -37,17 +37,17 @@ describe("getNextStep", () => { describe("yield", () => { it("first yield call", () => { - const source = getSource("async"); - const pausePosition = { line: 13, column: 2, sourceId: "async" }; + const source = getSource("generators"); + const pausePosition = { line: 2, column: 2, sourceId: "generators" }; expect(getNextStep(source, pausePosition)).toEqual({ ...pausePosition, - line: 14 + line: 3 }); }); it("second yield call", () => { - const source = getSource("async"); - const pausePosition = { line: 14, column: 2, sourceId: "async" }; + const source = getSource("generators"); + const pausePosition = { line: 3, column: 2, sourceId: "generators" }; expect(getNextStep(source, pausePosition)).toEqual(null); }); });