| 1 | import * as whitespace from "./whitespace"; |
| 2 | import * as parens from "./parentheses"; |
| 3 | import { |
| 4 | FLIPPED_ALIAS_KEYS, |
| 5 | isCallExpression, |
| 6 | isExpressionStatement, |
| 7 | isMemberExpression, |
| 8 | isNewExpression, |
| 9 | } from "@babel/types"; |
| 10 | import type * as t from "@babel/types"; |
| 11 | |
| 12 | import type { WhitespaceFlag } from "./whitespace"; |
| 13 | |
| 14 | export type NodeHandlers<R> = { |
| 15 | [K in string]?: ( |
| 16 | node: K extends t.Node["type"] ? Extract<t.Node, { type: K }> : t.Node, |
| 17 | // todo: |
| 18 | // node: K extends keyof typeof t |
| 19 | // ? Extract<typeof t[K], { type: "string" }> |
| 20 | // : t.Node, |
| 21 | parent: t.Node, |
| 22 | stack: t.Node[], |
| 23 | ) => R; |
| 24 | }; |
| 25 | |
| 26 | function expandAliases<R>(obj: NodeHandlers<R>) { |
| 27 | const newObj: NodeHandlers<R> = {}; |
| 28 | |
| 29 | function add( |
| 30 | type: string, |
| 31 | func: (node: t.Node, parent: t.Node, stack: t.Node[]) => R, |
| 32 | ) { |
| 33 | const fn = newObj[type]; |
| 34 | newObj[type] = fn |
| 35 | ? function (node, parent, stack) { |
| 36 | const result = fn(node, parent, stack); |
| 37 | |
| 38 | return result == null ? func(node, parent, stack) : result; |
| 39 | } |
| 40 | : func; |
| 41 | } |
| 42 | |
| 43 | for (const type of Object.keys(obj)) { |
| 44 | const aliases = FLIPPED_ALIAS_KEYS[type]; |
| 45 | if (aliases) { |
| 46 | for (const alias of aliases) { |
| 47 | add(alias, obj[type]); |
| 48 | } |
| 49 | } else { |
| 50 | add(type, obj[type]); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return newObj; |
| 55 | } |
| 56 | |
| 57 | // Rather than using `t.is` on each object property, we pre-expand any type aliases |
| 58 | // into concrete types so that the 'find' call below can be as fast as possible. |
| 59 | const expandedParens = expandAliases(parens); |
| 60 | const expandedWhitespaceNodes = expandAliases(whitespace.nodes); |
| 61 | |
| 62 | function find<R>( |
| 63 | obj: NodeHandlers<R>, |
| 64 | node: t.Node, |
| 65 | parent: t.Node, |
| 66 | printStack?: t.Node[], |
| 67 | ): R | null { |
| 68 | const fn = obj[node.type]; |
| 69 | return fn ? fn(node, parent, printStack) : null; |
| 70 | } |
| 71 | |
| 72 | function isOrHasCallExpression(node: t.Node): boolean { |
| 73 | if (isCallExpression(node)) { |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | return isMemberExpression(node) && isOrHasCallExpression(node.object); |
| 78 | } |
| 79 | |
| 80 | export function needsWhitespace( |
| 81 | node: t.Node, |
| 82 | parent: t.Node, |
| 83 | type: WhitespaceFlag, |
| 84 | ): boolean { |
| 85 | if (!node) return false; |
| 86 | |
| 87 | if (isExpressionStatement(node)) { |
| 88 | node = node.expression; |
| 89 | } |
| 90 | |
| 91 | const flag = find(expandedWhitespaceNodes, node, parent); |
| 92 | |
| 93 | if (typeof flag === "number") { |
| 94 | return (flag & type) !== 0; |
| 95 | } |
| 96 | |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | export function needsWhitespaceBefore(node: t.Node, parent: t.Node) { |
| 101 | return needsWhitespace(node, parent, 1); |
| 102 | } |
| 103 | |
| 104 | export function needsWhitespaceAfter(node: t.Node, parent: t.Node) { |
| 105 | return needsWhitespace(node, parent, 2); |
| 106 | } |
| 107 | |
| 108 | export function needsParens( |
| 109 | node: t.Node, |
| 110 | parent: t.Node, |
| 111 | printStack?: t.Node[], |
| 112 | ) { |
| 113 | if (!parent) return false; |
| 114 | |
| 115 | if (isNewExpression(parent) && parent.callee === node) { |
| 116 | if (isOrHasCallExpression(node)) return true; |
| 117 | } |
| 118 | |
| 119 | return find(expandedParens, node, parent, printStack); |
| 120 | } |