(parentRef: ParentReference, astNode: ASTNode)
| 158 | } |
| 159 | |
| 160 | function createNodeFromAst(parentRef: ParentReference, astNode: ASTNode): SplootNode { |
| 161 | let node = null |
| 162 | switch (astNode.type) { |
| 163 | case 'File': |
| 164 | const fileNode = astNode as FileKind |
| 165 | node = new JavascriptFile(parentRef) |
| 166 | populateChildSetFromAst(node.getBody(), fileNode.program.body) |
| 167 | break |
| 168 | case 'FunctionDeclaration': |
| 169 | const funcNode = astNode as FunctionDeclarationKind |
| 170 | if (funcNode.async) { |
| 171 | node = new AsyncFunctionDeclaration(parentRef) |
| 172 | populateChildSetFromAst(node.getIdentifier(), [funcNode.id]) |
| 173 | populateChildSetFromAst(node.getParams(), funcNode.params) |
| 174 | populateChildSetFromAst(node.getBody(), funcNode.body.body) |
| 175 | } else { |
| 176 | node = new FunctionDeclaration(parentRef) |
| 177 | populateChildSetFromAst(node.getIdentifier(), [funcNode.id]) |
| 178 | populateChildSetFromAst(node.getParams(), funcNode.params) |
| 179 | populateChildSetFromAst(node.getBody(), funcNode.body.body) |
| 180 | } |
| 181 | break |
| 182 | case 'VariableDeclaration': |
| 183 | const decNode = astNode as VariableDeclarationKind |
| 184 | if (decNode.declarations.length > 1) { |
| 185 | throw 'More than one variable declaration in a statement' |
| 186 | } |
| 187 | // This hack assumes only one variable declaration at a time. |
| 188 | const declarator = decNode.declarations[0] as VariableDeclaratorKind |
| 189 | node = new VariableDeclaration(parentRef) |
| 190 | const name = (declarator.id as IdentifierKind).name |
| 191 | const identifierNode = new DeclaredIdentifier(null, name) |
| 192 | node.getDeclarationIdentifier().addChild(identifierNode) |
| 193 | const initExpression = (node as VariableDeclaration).getInit().getChild(0) as SplootExpression |
| 194 | populateExpressionNodeFromAst(initExpression, declarator.init) |
| 195 | break |
| 196 | case 'IfStatement': |
| 197 | const ifNode = astNode as IfStatementKind |
| 198 | node = new IfStatement(parentRef) |
| 199 | const conditionExpression = node.getCondition().getChild(0) |
| 200 | populateExpressionNodeFromAst(conditionExpression, ifNode.test) |
| 201 | const cons = ifNode.consequent as BlockStatementKind |
| 202 | populateChildSetFromAst(node.getTrueBlock(), cons.body) |
| 203 | if (ifNode.alternate) { |
| 204 | if (ifNode.alternate.type === 'BlockStatement') { |
| 205 | const block = ifNode.alternate as BlockStatementKind |
| 206 | populateChildSetFromAst(node.getElseBlock(), block.body) |
| 207 | } |
| 208 | } |
| 209 | break |
| 210 | case 'Identifier': |
| 211 | // Assume variable reference for now (variable declaration is handled separately) |
| 212 | const idNode = astNode as IdentifierKind |
| 213 | node = new VariableReference(parentRef, idNode.name) |
| 214 | break |
| 215 | case 'ExpressionStatement': |
| 216 | // In our world, expressions can be statements too. |
| 217 | const expNode = astNode as ExpressionStatementKind |
no test coverage detected