Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.
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
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)) {

@codehag codehag Oct 4, 2017

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be separated, or the function (getAsyncExpression) renamed (maybe to getSteppableExpression ?), since yield is not async and this would lead to misunderstandings later on.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up going with getSteppableExpression for now as separating out the two cases led to more conditional logic living in getNextStep.

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", () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 nice test!

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