Skip to content

Commit

Permalink
fixed .debug(): only inject at the beginning of methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoverson committed Jun 25, 2020
1 parent afca815 commit 658b174
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,19 +273,33 @@ export class RefactorSession {

debug(selector: SelectorOrNode) {
const nodes = findNodes(this.ast, selector);
const injectIntoBody = (body: FunctionBody) => {
if (body.statements.length > 0) {
this.insertBefore(body.statements[0], new DebuggerStatement());
} else {
this.replace(body, new FunctionBody({
directives: [],
statements: [
new DebuggerStatement(),
]
}));
}
}
nodes.forEach(node => {
switch (node.type) {
case 'FunctionExpression':
case 'FunctionDeclaration':
case 'Method':
this.insertBefore(node.body.statements, new DebuggerStatement());
injectIntoBody(node.body);
break;
case 'ArrowExpression':
if (node.body.type !== 'FunctionBody') {
this.replace(node.body, new FunctionBody({directives:[], statements:[
new DebuggerStatement(),
new ReturnStatement({expression: node.body })
]}))
} else {
injectIntoBody(node.body);
}
default:
debug('can not call inject debugger statement on %o node', node.type);
Expand Down
4 changes: 2 additions & 2 deletions test/exposed-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ describe('API', function() {
chai.expect(nodes.length).to.equal(1);
});
it('.debug()', () => {
const refactor = new RefactorSession(`b = _ => foo(); a.x = function(){b();}`);
const refactor = new RefactorSession(`b = _ => foo(); c = _ => {bar()}; a.x = function(){b();c();}`);
refactor.debug(`FunctionExpression, ArrowExpression`);
chai.expect(refactor.ast).to.deep.equal(parseScript('b = _ => {debugger; return foo()}; a.x = function(){debugger;b();}'));
chai.expect(refactor.ast).to.deep.equal(parseScript('b = _ => {debugger; return foo()}; c = _ => {debugger; bar()}; a.x = function(){debugger;b();c();}'));
});
it('.findOne()', () => {
const refactor = new RefactorSession(`function foo(){}\nfunction bar(){}`);
Expand Down

0 comments on commit 658b174

Please sign in to comment.