@@ -11,6 +11,9 @@ import {
1111 Block ,
1212 FunctionExpression ,
1313 Statement ,
14+ FunctionDeclaration ,
15+ Node ,
16+ Expression ,
1417} from 'ts-morph' ;
1518
1619function hasThenCallExpression ( expression : CallExpression ) : boolean {
@@ -22,7 +25,7 @@ function hasThenCallExpression(expression: CallExpression): boolean {
2225function isThenPropertyAccessExpression (
2326 expression : PropertyAccessExpression ,
2427) : boolean {
25- return expression . getName ( ) === 'then' ;
28+ return expression . getName && expression . getName ( ) === 'then' ;
2629}
2730
2831function getCaller ( expression ) { }
@@ -72,86 +75,59 @@ export function promiseToAsync(sources: SourceFile[], options) {
7275 // First then call.
7376 const call : CallExpression = calls [ 0 ] ;
7477
75- // Reference to then property access.
76- const expression = call . getExpression ( ) as PropertyAccessExpression ;
77-
7878 // The arguments passed to then().
79- const args : any = call . getArguments ( ) ;
79+ const args = call . getArguments ( ) ;
8080
8181 // The arrow function. e.g. promise.then(() => {})
82- const fn : FunctionExpression = args . length && args [ 0 ] ;
82+ const fn : FunctionExpression =
83+ args . length && ( args [ 0 ] as FunctionExpression ) ;
8384
8485 let s : Statement ;
8586
86- if ( fn ) {
87- // Create awaited assignment statement with all args as destructured assignment.
88- s = source . addVariableStatement ( {
87+ // Reference to then property access.
88+ const expression = call . getExpression ( ) as PropertyAccessExpression ;
89+
90+ // The containing parent block.
91+ const block : Block = statement . getParent ( ) as Block ;
92+
93+ const params = fn . getParameters ( ) ;
94+
95+ // We only care to declare a variable is there is a corresponding
96+ // parameter in a function passed to the then() call.1
97+ if ( fn && params . length ) {
98+ // Create awaited assignment statement with all args as an assignment.
99+ // TODO: Account for multiple params with destructuring
100+ s = block . insertVariableStatement ( statement . getChildIndex ( ) , {
89101 declarationKind : VariableDeclarationKind . Const , // defaults to "let"
90102 declarations : [
91103 {
92104 // Move the variable from argument list in the then arrow function
93105 // to a constant reference.
94- name : args . getParameters ( ) [ 0 ] . toString ( ) ,
106+ name : params [ 0 ] . getName ( ) ,
95107
96108 // 'await' and the name of the promise being awaited.
97- initializer : 'await ' + expression . getExpression ( ) . getFullText ( ) ,
109+ initializer : 'await ' + expression . getExpression ( ) . getText ( ) ,
98110 } ,
99111 ] ,
100112 } ) ;
101113 } else {
102- s = source . addStatements ( [
103- 'await ' + expression . getExpression ( ) . getFullText ( ) ,
114+ s = block . addStatements ( [
115+ 'await ' + expression . getExpression ( ) . getText ( ) ,
104116 ] ) [ 0 ] ;
105117 }
106118
107- // The containing parent block.
108- const parent : Block = statement . getParent ( ) as Block ;
109-
110119 // Copy the body of the function expression to below the new await expression.
111- parent . insertStatements (
112- s . getChildIndex ( ) ,
113- fn . getStatementsWithComments ( ) . map ( i => i . toString ( ) ) ,
114- ) ;
120+ fn &&
121+ block . insertStatements (
122+ s . getChildIndex ( ) + 1 ,
123+ fn . getStatementsWithComments ( ) . map ( ( s : Statement ) => s . getText ( ) ) ,
124+ ) ;
125+
126+ const functionDeclaration : FunctionDeclaration = block . getParent ( ) as FunctionDeclaration ;
127+ functionDeclaration . toggleModifier ( 'async' , true ) ;
115128
116129 // Clean up.
117130 statement . remove ( ) ;
118-
119- // If only 1 argument, don't wrap with destructuring.
120131 } ) ;
121132 } ) ;
122133}
123- // export default (sources, options) => {
124- // sources.forEach(source => {
125- // const classes = source.getClasses();
126- //
127- // classes.forEach(c => {
128- // const constructors = c.getConstructors();
129- // constructors.forEach(constructor => {
130- // const body = constructor.getBody();
131- // const statements = body.getStatements();
132- //
133- // // Find ngInject
134- // const ngInject = statements.find((statement: Statement) => {
135- // return statement.getText().includes("'ngInject'");
136- // });
137- //
138- // if (!ngInject) {
139- // return;
140- // }
141- //
142- // ngInject.remove();
143- //
144- // const arr = constructor.getParameters().map(param => param.getName());
145- //
146- // const prop = c.addProperty({
147- // isStatic: true,
148- // name: '$inject',
149- // type: 'string[]',
150- // });
151- // const expression =
152- // '[' + arr.map(i => "'" + i.toString() + "'").join(', ') + ']';
153- // prop.setInitializer(expression);
154- // });
155- // });
156- // });
157- // };
0 commit comments