* Recursively looks up type for ast expression until it's found * @param ast * @returns {String|null}
(ast)
| 355 | * @returns {String|null} |
| 356 | */ |
| 357 | getType(ast) { |
| 358 | if (Array.isArray(ast)) { |
| 359 | return this.getType(ast[ast.length - 1]); |
| 360 | } |
| 361 | switch (ast.type) { |
| 362 | case 'BlockStatement': |
| 363 | return this.getType(ast.body); |
| 364 | case 'ArrayExpression': |
| 365 | const childType = this.getType(ast.elements[0]); |
| 366 | switch (childType) { |
| 367 | case 'Array(2)': |
| 368 | case 'Array(3)': |
| 369 | case 'Array(4)': |
| 370 | return `Matrix(${ast.elements.length})`; |
| 371 | } |
| 372 | return `Array(${ ast.elements.length })`; |
| 373 | case 'Literal': |
| 374 | const literalKey = this.astKey(ast); |
| 375 | if (this.literalTypes[literalKey]) { |
| 376 | return this.literalTypes[literalKey]; |
| 377 | } |
| 378 | if (Number.isInteger(ast.value)) { |
| 379 | return 'LiteralInteger'; |
| 380 | } else if (ast.value === true || ast.value === false) { |
| 381 | return 'Boolean'; |
| 382 | } else { |
| 383 | return 'Number'; |
| 384 | } |
| 385 | case 'AssignmentExpression': |
| 386 | return this.getType(ast.left); |
| 387 | case 'CallExpression': |
| 388 | if (this.isAstMathFunction(ast)) { |
| 389 | return 'Number'; |
| 390 | } |
| 391 | if (!ast.callee || !ast.callee.name) { |
| 392 | if (ast.callee.type === 'SequenceExpression' && ast.callee.expressions[ast.callee.expressions.length - 1].property.name) { |
| 393 | const functionName = ast.callee.expressions[ast.callee.expressions.length - 1].property.name; |
| 394 | this.inferArgumentTypesIfNeeded(functionName, ast.arguments); |
| 395 | return this.lookupReturnType(functionName, ast, this); |
| 396 | } |
| 397 | if (this.getVariableSignature(ast.callee, true) === 'this.color') { |
| 398 | return null; |
| 399 | } |
| 400 | if (ast.callee.type === 'MemberExpression' && ast.callee.object && ast.callee.property && ast.callee.property.name && ast.arguments) { |
| 401 | const functionName = ast.callee.property.name; |
| 402 | this.inferArgumentTypesIfNeeded(functionName, ast.arguments); |
| 403 | return this.lookupReturnType(functionName, ast, this); |
| 404 | } |
| 405 | throw this.astErrorOutput('Unknown call expression', ast); |
| 406 | } |
| 407 | if (ast.callee && ast.callee.name) { |
| 408 | const functionName = ast.callee.name; |
| 409 | this.inferArgumentTypesIfNeeded(functionName, ast.arguments); |
| 410 | return this.lookupReturnType(functionName, ast, this); |
| 411 | } |
| 412 | throw this.astErrorOutput(`Unhandled getType Type "${ ast.type }"`, ast); |
| 413 | case 'LogicalExpression': |
| 414 | return 'Boolean'; |
no test coverage detected