| 1 | import { |
| 2 | FLIPPED_ALIAS_KEYS, |
| 3 | isArrayExpression, |
| 4 | isAssignmentExpression, |
| 5 | isBinary, |
| 6 | isBlockStatement, |
| 7 | isCallExpression, |
| 8 | isFunction, |
| 9 | isIdentifier, |
| 10 | isLiteral, |
| 11 | isMemberExpression, |
| 12 | isObjectExpression, |
| 13 | isOptionalCallExpression, |
| 14 | isOptionalMemberExpression, |
| 15 | isStringLiteral, |
| 16 | } from "@babel/types"; |
| 17 | import * as charCodes from "charcodes"; |
| 18 | |
| 19 | import type { NodeHandlers } from "./index"; |
| 20 | |
| 21 | import type * as t from "@babel/types"; |
| 22 | |
| 23 | const enum WhitespaceFlag { |
| 24 | before = 1 << 0, |
| 25 | after = 1 << 1, |
| 26 | } |
| 27 | |
| 28 | export type { WhitespaceFlag }; |
| 29 | |
| 30 | function crawlInternal( |
| 31 | node: t.Node, |
| 32 | state: { hasCall: boolean; hasFunction: boolean; hasHelper: boolean }, |
| 33 | ) { |
| 34 | if (!node) return state; |
| 35 | |
| 36 | if (isMemberExpression(node) || isOptionalMemberExpression(node)) { |
| 37 | crawlInternal(node.object, state); |
| 38 | if (node.computed) crawlInternal(node.property, state); |
| 39 | } else if (isBinary(node) || isAssignmentExpression(node)) { |
| 40 | crawlInternal(node.left, state); |
| 41 | crawlInternal(node.right, state); |
| 42 | } else if (isCallExpression(node) || isOptionalCallExpression(node)) { |
| 43 | state.hasCall = true; |
| 44 | crawlInternal(node.callee, state); |
| 45 | } else if (isFunction(node)) { |
| 46 | state.hasFunction = true; |
| 47 | } else if (isIdentifier(node)) { |
| 48 | state.hasHelper = |
| 49 | // @ts-expect-error todo(flow->ts): node.callee is not really expected here… |
| 50 | state.hasHelper || (node.callee && isHelper(node.callee)); |
| 51 | } |
| 52 | |
| 53 | return state; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Crawl a node to test if it contains a CallExpression, a Function, or a Helper. |
| 58 | * |
| 59 | * @example |
| 60 | * crawl(node) |
| 61 | * // { hasCall: false, hasFunction: true, hasHelper: false } |
| 62 | */ |
| 63 | |
| 64 | function crawl(node: t.Node) { |
| 65 | return crawlInternal(node, { |
| 66 | hasCall: false, |
| 67 | hasFunction: false, |
| 68 | hasHelper: false, |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Test if a node is or has a helper. |
| 74 | */ |
| 75 | |
| 76 | function isHelper(node: t.Node): boolean { |
| 77 | if (!node) return false; |
| 78 | |
| 79 | if (isMemberExpression(node)) { |
| 80 | return isHelper(node.object) || isHelper(node.property); |
| 81 | } else if (isIdentifier(node)) { |
| 82 | return ( |
| 83 | node.name === "require" || |
| 84 | node.name.charCodeAt(0) === charCodes.underscore |
| 85 | ); |
| 86 | } else if (isCallExpression(node)) { |
| 87 | return isHelper(node.callee); |
| 88 | } else if (isBinary(node) || isAssignmentExpression(node)) { |
| 89 | return ( |
| 90 | (isIdentifier(node.left) && isHelper(node.left)) || isHelper(node.right) |
| 91 | ); |
| 92 | } else { |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | function isType(node: t.Node) { |
| 98 | return ( |
| 99 | isLiteral(node) || |
| 100 | isObjectExpression(node) || |
| 101 | isArrayExpression(node) || |
| 102 | isIdentifier(node) || |
| 103 | isMemberExpression(node) |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Tests for node types that need whitespace. |
| 109 | */ |
| 110 | |
| 111 | export const nodes: NodeHandlers<WhitespaceFlag> = { |
| 112 | /** |
| 113 | * Test if AssignmentExpression needs whitespace. |
| 114 | */ |
| 115 | |
| 116 | AssignmentExpression(node: t.AssignmentExpression): WhitespaceFlag { |
| 117 | const state = crawl(node.right); |
| 118 | if ((state.hasCall && state.hasHelper) || state.hasFunction) { |
| 119 | return state.hasFunction |
| 120 | ? WhitespaceFlag.before | WhitespaceFlag.after |
| 121 | : WhitespaceFlag.after; |
| 122 | } |
| 123 | }, |
| 124 | |
| 125 | /** |
| 126 | * Test if SwitchCase needs whitespace. |
| 127 | */ |
| 128 | |
| 129 | SwitchCase(node: t.SwitchCase, parent: t.SwitchStatement): WhitespaceFlag { |
| 130 | return ( |
| 131 | (!!node.consequent.length || parent.cases[0] === node |
| 132 | ? WhitespaceFlag.before |
| 133 | : 0) | |
| 134 | (!node.consequent.length && parent.cases[parent.cases.length - 1] === node |
| 135 | ? WhitespaceFlag.after |
| 136 | : 0) |
| 137 | ); |
| 138 | }, |
| 139 | |
| 140 | /** |
| 141 | * Test if LogicalExpression needs whitespace. |
| 142 | */ |
| 143 | |
| 144 | LogicalExpression(node: t.LogicalExpression): WhitespaceFlag { |
| 145 | if (isFunction(node.left) || isFunction(node.right)) { |
| 146 | return WhitespaceFlag.after; |
| 147 | } |
| 148 | }, |
| 149 | |
| 150 | /** |
| 151 | * Test if Literal needs whitespace. |
| 152 | */ |
| 153 | |
| 154 | Literal(node: t.Literal): WhitespaceFlag { |
| 155 | if (isStringLiteral(node) && node.value === "use strict") { |
| 156 | return WhitespaceFlag.after; |
| 157 | } |
| 158 | }, |
| 159 | |
| 160 | /** |
| 161 | * Test if CallExpressionish needs whitespace. |
| 162 | */ |
| 163 | |
| 164 | CallExpression(node: t.CallExpression): WhitespaceFlag { |
| 165 | if (isFunction(node.callee) || isHelper(node)) { |
| 166 | return WhitespaceFlag.before | WhitespaceFlag.after; |
| 167 | } |
| 168 | }, |
| 169 | |
| 170 | OptionalCallExpression(node: t.OptionalCallExpression): WhitespaceFlag { |
| 171 | if (isFunction(node.callee)) { |
| 172 | return WhitespaceFlag.before | WhitespaceFlag.after; |
| 173 | } |
| 174 | }, |
| 175 | |
| 176 | /** |
| 177 | * Test if VariableDeclaration needs whitespace. |
| 178 | */ |
| 179 | |
| 180 | VariableDeclaration(node: t.VariableDeclaration): WhitespaceFlag { |
| 181 | for (let i = 0; i < node.declarations.length; i++) { |
| 182 | const declar = node.declarations[i]; |
| 183 | |
| 184 | let enabled = isHelper(declar.id) && !isType(declar.init); |
| 185 | if (!enabled && declar.init) { |
| 186 | const state = crawl(declar.init); |
| 187 | enabled = (isHelper(declar.init) && state.hasCall) || state.hasFunction; |
| 188 | } |
| 189 | |
| 190 | if (enabled) { |
| 191 | return WhitespaceFlag.before | WhitespaceFlag.after; |
| 192 | } |
| 193 | } |
| 194 | }, |
| 195 | |
| 196 | /** |
| 197 | * Test if IfStatement needs whitespace. |
| 198 | */ |
| 199 | |
| 200 | IfStatement(node: t.IfStatement): WhitespaceFlag { |
| 201 | if (isBlockStatement(node.consequent)) { |
| 202 | return WhitespaceFlag.before | WhitespaceFlag.after; |
| 203 | } |
| 204 | }, |
| 205 | }; |
| 206 | |
| 207 | /** |
| 208 | * Test if Property needs whitespace. |
| 209 | */ |
| 210 | |
| 211 | nodes.ObjectProperty = |
| 212 | nodes.ObjectTypeProperty = |
| 213 | nodes.ObjectMethod = |
| 214 | function ( |
| 215 | node: t.ObjectProperty | t.ObjectTypeProperty | t.ObjectMethod, |
| 216 | parent: t.ObjectExpression, |
| 217 | ): WhitespaceFlag { |
| 218 | if (parent.properties[0] === node) { |
| 219 | return WhitespaceFlag.before; |
| 220 | } |
| 221 | }; |
| 222 | |
| 223 | nodes.ObjectTypeCallProperty = function ( |
| 224 | node: t.ObjectTypeCallProperty, |
| 225 | parent: t.ObjectTypeAnnotation, |
| 226 | ): WhitespaceFlag { |
| 227 | if (parent.callProperties[0] === node && !parent.properties?.length) { |
| 228 | return WhitespaceFlag.before; |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | nodes.ObjectTypeIndexer = function ( |
| 233 | node: t.ObjectTypeIndexer, |
| 234 | parent: t.ObjectTypeAnnotation, |
| 235 | ): WhitespaceFlag { |
| 236 | if ( |
| 237 | parent.indexers[0] === node && |
| 238 | !parent.properties?.length && |
| 239 | !parent.callProperties?.length |
| 240 | ) { |
| 241 | return WhitespaceFlag.before; |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | nodes.ObjectTypeInternalSlot = function ( |
| 246 | node: t.ObjectTypeInternalSlot, |
| 247 | parent: t.ObjectTypeAnnotation, |
| 248 | ): WhitespaceFlag { |
| 249 | if ( |
| 250 | parent.internalSlots[0] === node && |
| 251 | !parent.properties?.length && |
| 252 | !parent.callProperties?.length && |
| 253 | !parent.indexers?.length |
| 254 | ) { |
| 255 | return WhitespaceFlag.before; |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | /** |
| 260 | * Add whitespace tests for nodes and their aliases. |
| 261 | */ |
| 262 | |
| 263 | ( |
| 264 | [ |
| 265 | ["Function", true], |
| 266 | ["Class", true], |
| 267 | ["Loop", true], |
| 268 | ["LabeledStatement", true], |
| 269 | ["SwitchStatement", true], |
| 270 | ["TryStatement", true], |
| 271 | ] as const |
| 272 | ).forEach(function ([type, amounts]) { |
| 273 | [type as string] |
| 274 | .concat(FLIPPED_ALIAS_KEYS[type] || []) |
| 275 | .forEach(function (type) { |
| 276 | const ret = amounts ? WhitespaceFlag.before | WhitespaceFlag.after : 0; |
| 277 | nodes[type] = () => ret; |
| 278 | }); |
| 279 | }); |