|
| 1 | +import ts from "typescript"; |
| 2 | +import type { Context, NodeParser } from "../NodeParser.js"; |
| 3 | +import type { SubNodeParser } from "../SubNodeParser.js"; |
| 4 | +import { AnyType } from "../Type/AnyType.js"; |
| 5 | +import type { BaseType } from "../Type/BaseType.js"; |
| 6 | +import { BooleanType } from "../Type/BooleanType.js"; |
| 7 | +import { LiteralType } from "../Type/LiteralType.js"; |
| 8 | +import { NumberType } from "../Type/NumberType.js"; |
| 9 | +import { StringType } from "../Type/StringType.js"; |
| 10 | +import { UnionType } from "../Type/UnionType.js"; |
| 11 | +import { AliasType } from "../Type/AliasType.js"; |
| 12 | + |
| 13 | +export class BinaryExpressionNodeParser implements SubNodeParser { |
| 14 | + public constructor(protected childNodeParser: NodeParser) {} |
| 15 | + |
| 16 | + public supportsNode(node: ts.Node): boolean { |
| 17 | + return node.kind === ts.SyntaxKind.BinaryExpression; |
| 18 | + } |
| 19 | + |
| 20 | + public createType(node: ts.BinaryExpression, context: Context): BaseType { |
| 21 | + const leftType = this.childNodeParser.createType(node.left, context); |
| 22 | + const rightType = this.childNodeParser.createType(node.right, context); |
| 23 | + |
| 24 | + if (leftType instanceof AnyType || rightType instanceof AnyType) { |
| 25 | + return new AnyType(); |
| 26 | + } |
| 27 | + |
| 28 | + if (this.isStringLike(leftType) || this.isStringLike(rightType)) { |
| 29 | + return new StringType(); |
| 30 | + } |
| 31 | + |
| 32 | + if (this.isDefinitelyNumberLike(leftType) && this.isDefinitelyNumberLike(rightType)) { |
| 33 | + return new NumberType(); |
| 34 | + } |
| 35 | + |
| 36 | + if (this.isBooleanLike(leftType) && this.isBooleanLike(rightType)) { |
| 37 | + return new BooleanType(); |
| 38 | + } |
| 39 | + |
| 40 | + // Anything else (objects, any, unknown, weird unions, etc.) return |
| 41 | + // 'string' because at runtime + will usually go through ToPrimitive and |
| 42 | + // end up in the "string concatenation" branch when non-numeric stuff is |
| 43 | + // involved. |
| 44 | + return new StringType(); |
| 45 | + } |
| 46 | + |
| 47 | + private isStringLike(type: BaseType): boolean { |
| 48 | + if (type instanceof AliasType) { |
| 49 | + return this.isStringLike(type.getType()); |
| 50 | + } |
| 51 | + |
| 52 | + if (type instanceof StringType) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + if (type instanceof LiteralType && type.isString()) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + // Any union member being string-like is enough. |
| 61 | + if (type instanceof UnionType) { |
| 62 | + return type.getTypes().some((t) => this.isStringLike(t)); |
| 63 | + } |
| 64 | + |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + private isBooleanLike(type: BaseType): boolean { |
| 69 | + if (type instanceof BooleanType) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + if (type instanceof LiteralType && typeof type.getValue() === "boolean") { |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + private isDefinitelyNumberLike(type: BaseType): boolean { |
| 81 | + if (type instanceof AliasType) { |
| 82 | + return this.isDefinitelyNumberLike(type.getType()); |
| 83 | + } |
| 84 | + |
| 85 | + if (type instanceof NumberType) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + if (type instanceof LiteralType && typeof type.getValue() === "number") { |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + if (type instanceof UnionType) { |
| 94 | + return type.getTypes().every((t) => this.isDefinitelyNumberLike(t)); |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | +} |
0 commit comments