1+ /* This Source Code Form is subject to the terms of the Mozilla Public
2+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3+ * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
4+
5+ // @flow
6+
17import { traverseAst } from "./utils/ast" ;
28import * as t from "@babel/types" ;
39
10+ import type { AstLocation } from "./types" ;
11+ import type { BabelNode } from "@babel/types" ;
12+ import type { SimplePath } from "./utils/simple-path" ;
13+
14+ export type PausePoint = { |
15+ location : AstLocation ,
16+ types : { | breakpoint : boolean , stepOver : boolean | }
17+ | } ;
18+
19+ export type PausePoints = PausePoint [ ] ;
20+
421const isControlFlow = node =>
522 t . isForStatement ( node ) || t . isWhileStatement ( node ) || t . isIfStatement ( node ) ;
623
@@ -15,69 +32,69 @@ const inExpression = parent =>
1532 t . isCallExpression ( parent . node ) ||
1633 t . isTemplateLiteral ( parent . node ) ;
1734
18- export function getPausePoints ( sourceId ) {
35+ export function getPausePoints ( sourceId : string ) {
1936 const state = [ ] ;
2037 traverseAst ( sourceId , { enter : onEnter } , state ) ;
2138 return state ;
2239}
2340
24- function formatNode ( location , types ) {
25- return { location, types } ;
26- }
27-
28- function onEnter ( node , ancestors , state ) {
41+ function onEnter ( node : BabelNode , ancestors : SimplePath [ ] , state ) {
2942 const parent = ancestors [ ancestors . length - 1 ] ;
3043
31- if ( isAssignment ( node ) || isImport ( node ) || isControlFlow ( node ) ) {
32- state . push (
33- formatNode ( node . loc . start , { breakpoint : true , stepOver : true } )
34- ) ;
44+ if (
45+ isAssignment ( node ) ||
46+ isImport ( node ) ||
47+ isControlFlow ( node ) ||
48+ t . isDebuggerStatement ( node )
49+ ) {
50+ addPoint ( state , node . loc . start ) ;
3551 }
3652
3753 if ( isReturn ( node ) ) {
3854 if ( t . isCallExpression ( node . argument ) ) {
39- state . push (
40- formatNode ( node . loc . start , { breakpoint : false , stepOver : false } )
41- ) ;
55+ addEmptyPoint ( state , node . loc . start ) ;
4256 } else {
43- state . push (
44- formatNode ( node . loc . start , { breakpoint : true , stepOver : true } )
45- ) ;
57+ addPoint ( state , node . loc . start ) ;
4658 }
4759 }
4860
4961 if ( t . isCallExpression ( node ) ) {
50- state . push (
51- formatNode ( node . loc . start , {
52- breakpoint : true ,
53-
54- // NOTE: we do not want to land inside an expression e.g. [], {}, call
55- stepOver : ! inExpression ( parent )
56- } )
57- ) ;
58- }
62+ addPoint ( state , node . loc . start , {
63+ breakpoint : true ,
5964
60- if ( t . isDebuggerStatement ( node ) ) {
61- state . push (
62- formatNode ( node . loc . start , { breakpoint : true , stepOver : true } )
63- ) ;
65+ // NOTE: we do not want to land inside an expression e.g. [], {}, call
66+ stepOver : ! inExpression ( parent )
67+ } ) ;
6468 }
6569
6670 if ( t . isFunction ( node ) ) {
6771 const { line, column } = node . loc . end ;
68- state . push ( formatNode ( node . loc . start , { breakpoint : true } ) ) ;
69- state . push (
70- formatNode (
71- { line, column : column - 1 } ,
72- { breakpoint : true , stepOver : true }
73- )
74- ) ;
72+ addBreakPoint ( state , node . loc . start ) ;
73+ addPoint ( state , { line, column : column - 1 } ) ;
7574 }
7675
7776 if ( t . isProgram ( node ) ) {
7877 const lastStatement = node . body [ node . body . length - 1 ] ;
79- state . push (
80- formatNode ( lastStatement . loc . end , { breakpoint : true , stepOver : true } )
81- ) ;
78+ addPoint ( state , lastStatement . loc . end ) ;
8279 }
8380}
81+
82+ function formatNode ( location , types ) {
83+ return { location, types } ;
84+ }
85+
86+ function addPoint (
87+ state ,
88+ location ,
89+ types = { breakpoint : true , stepOver : true }
90+ ) {
91+ state . push ( formatNode ( location , types ) ) ;
92+ }
93+
94+ function addEmptyPoint ( state , location ) {
95+ addPoint ( state , location , { breakpoint : false , stepOver : false } ) ;
96+ }
97+
98+ function addBreakPoint ( state , location ) {
99+ addPoint ( state , location , { breakpoint : true , stepOver : false } ) ;
100+ }
0 commit comments