Skip to content

Commit

Permalink
Added step over functionality for yield (firefox-devtools#4232) (fire…
Browse files Browse the repository at this point in the history
…fox-devtools#4276)

* Added step over functionality for yield (firefox-devtools#4232)

* Consistent parser steps naming and generators fixture
  • Loading branch information
jackjocross authored and amitzur committed Oct 5, 2017
1 parent 755f798 commit 5850073
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 35 deletions.
21 changes: 9 additions & 12 deletions src/workers/parser/steps.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/workers/parser/tests/fixtures/generators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function* foo() {
yield 1;
yield 2;
}
65 changes: 42 additions & 23 deletions src/workers/parser/tests/steps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
8 changes: 8 additions & 0 deletions src/workers/parser/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down

0 comments on commit 5850073

Please sign in to comment.