Skip to content

Commit

Permalink
made statements() work through .body properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoverson committed Jul 22, 2020
1 parent 0de34e6 commit a2c901c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/misc/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export function isNodeWithStatements(input: any): input is NodesWithStatements {
return 'statements' in input;
}

export function innerBodyStatements(input: any): Node {
return 'body' in input ? input.body : input;
}

export function isLiteral(
input: any,
): input is
Expand Down
12 changes: 7 additions & 5 deletions src/refactor-session-chainable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,9 @@ export class RefactorSessionChainable {
}

/**
* Returns the selects the statements for the selected nodes. Does nothing for nodes that have no statements property.
* Returns the selects the statements for the selected nodes. Note: it will "uplevel" the inner statements of nodes with a `.body` property.
*
* Does nothing for nodes that have no statements property.
*
* @example
*
Expand All @@ -928,9 +930,9 @@ export class RefactorSessionChainable {
* @public
*/
statements() {
const statements: Statement[] = this.filter(isNodeWithStatements).flatMap(
(node: NodesWithStatements) => node.statements,
);
const statements: Statement[] = this.map(innerBodyStatements)
.filter(isNodeWithStatements)
.flatMap((node: NodesWithStatements) => node.statements);
return this.$(statements);
}

Expand Down Expand Up @@ -1193,7 +1195,7 @@ export function refactor(input: string | Node, options?: GlobalStateOptions): Re
*/
import {FunctionDeclaration} from 'shift-ast';
import {PureFunctionAssessment, PureFunctionAssessmentOptions} from './refactor-plugin-unsafe';
import {isNodeWithStatements} from './misc/util';
import {isNodeWithStatements, innerBodyStatements} from './misc/util';
import {parseScript} from 'shift-parser';
import {matches} from './misc/query';
const sorry = function(): PureFunctionAssessment | PureFunctionAssessmentOptions | FunctionDeclaration | undefined {
Expand Down
8 changes: 8 additions & 0 deletions test/refactor-chainable-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ describe('chainable interface', () => {
expect($s(firstFn).nameString()).to.equal('foo');
});

it('statements should also get .body.statements', () => {
const src = `try { var foo = 1; } catch(e){}`;
const $s = refactor(src);
const innerStatements = $s($s.statements().first('TryCatchStatement')).statements();
expect(innerStatements.length).to.equal(1);
expect(innerStatements.first().type).to.equal('VariableDeclarationStatement');
});

describe('methods w/o arguments', () => {
it('.delete() should delete self', () => {
const src = `idExp;function foo(){}\nfoo();`;
Expand Down

0 comments on commit a2c901c

Please sign in to comment.