| 1 | import type Printer from "../printer"; |
| 2 | import { |
| 3 | isExportDefaultDeclaration, |
| 4 | isExportNamedDeclaration, |
| 5 | } from "@babel/types"; |
| 6 | import type * as t from "@babel/types"; |
| 7 | import * as charCodes from "charcodes"; |
| 8 | |
| 9 | export function ClassDeclaration( |
| 10 | this: Printer, |
| 11 | node: t.ClassDeclaration, |
| 12 | parent: t.Node, |
| 13 | ) { |
| 14 | if (process.env.BABEL_8_BREAKING) { |
| 15 | this.printJoin(node.decorators, node); |
| 16 | } else { |
| 17 | if ( |
| 18 | !this.format.decoratorsBeforeExport || |
| 19 | (!isExportDefaultDeclaration(parent) && !isExportNamedDeclaration(parent)) |
| 20 | ) { |
| 21 | this.printJoin(node.decorators, node); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | if (node.declare) { |
| 26 | // TS |
| 27 | this.word("declare"); |
| 28 | this.space(); |
| 29 | } |
| 30 | |
| 31 | if (node.abstract) { |
| 32 | // TS |
| 33 | this.word("abstract"); |
| 34 | this.space(); |
| 35 | } |
| 36 | |
| 37 | this.word("class"); |
| 38 | |
| 39 | if (node.id) { |
| 40 | this.space(); |
| 41 | this.print(node.id, node); |
| 42 | } |
| 43 | |
| 44 | this.print(node.typeParameters, node); |
| 45 | |
| 46 | if (node.superClass) { |
| 47 | this.space(); |
| 48 | this.word("extends"); |
| 49 | this.space(); |
| 50 | this.print(node.superClass, node); |
| 51 | this.print(node.superTypeParameters, node); |
| 52 | } |
| 53 | |
| 54 | if (node.implements) { |
| 55 | this.space(); |
| 56 | this.word("implements"); |
| 57 | this.space(); |
| 58 | this.printList(node.implements, node); |
| 59 | } |
| 60 | |
| 61 | this.space(); |
| 62 | this.print(node.body, node); |
| 63 | } |
| 64 | |
| 65 | export { ClassDeclaration as ClassExpression }; |
| 66 | |
| 67 | export function ClassBody(this: Printer, node: t.ClassBody) { |
| 68 | this.token("{"); |
| 69 | if (node.body.length === 0) { |
| 70 | this.token("}"); |
| 71 | } else { |
| 72 | this.newline(); |
| 73 | |
| 74 | this.indent(); |
| 75 | this.printSequence(node.body, node); |
| 76 | this.dedent(); |
| 77 | |
| 78 | if (!this.endsWith(charCodes.lineFeed)) this.newline(); |
| 79 | |
| 80 | this.sourceWithOffset("end", node.loc, 0, -1); |
| 81 | |
| 82 | this.rightBrace(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | export function ClassProperty(this: Printer, node: t.ClassProperty) { |
| 87 | this.printJoin(node.decorators, node); |
| 88 | |
| 89 | // catch up to property key, avoid line break |
| 90 | // between member modifiers and the property key. |
| 91 | const endLine = node.key.loc?.end?.line; |
| 92 | if (endLine) this.catchUp(endLine); |
| 93 | |
| 94 | this.tsPrintClassMemberModifiers(node); |
| 95 | |
| 96 | if (node.computed) { |
| 97 | this.token("["); |
| 98 | this.print(node.key, node); |
| 99 | this.token("]"); |
| 100 | } else { |
| 101 | this._variance(node); |
| 102 | this.print(node.key, node); |
| 103 | } |
| 104 | |
| 105 | // TS |
| 106 | if (node.optional) { |
| 107 | this.token("?"); |
| 108 | } |
| 109 | if (node.definite) { |
| 110 | this.token("!"); |
| 111 | } |
| 112 | |
| 113 | this.print(node.typeAnnotation, node); |
| 114 | if (node.value) { |
| 115 | this.space(); |
| 116 | this.token("="); |
| 117 | this.space(); |
| 118 | this.print(node.value, node); |
| 119 | } |
| 120 | this.semicolon(); |
| 121 | } |
| 122 | |
| 123 | export function ClassAccessorProperty( |
| 124 | this: Printer, |
| 125 | node: t.ClassAccessorProperty, |
| 126 | ) { |
| 127 | this.printJoin(node.decorators, node); |
| 128 | |
| 129 | // catch up to property key, avoid line break |
| 130 | // between member modifiers and the property key. |
| 131 | const endLine = node.key.loc?.end?.line; |
| 132 | if (endLine) this.catchUp(endLine); |
| 133 | |
| 134 | // TS does not support class accessor property yet |
| 135 | this.tsPrintClassMemberModifiers(node); |
| 136 | |
| 137 | this.word("accessor", true); |
| 138 | this.space(); |
| 139 | |
| 140 | if (node.computed) { |
| 141 | this.token("["); |
| 142 | this.print(node.key, node); |
| 143 | this.token("]"); |
| 144 | } else { |
| 145 | // Todo: Flow does not support class accessor property yet. |
| 146 | this._variance(node); |
| 147 | this.print(node.key, node); |
| 148 | } |
| 149 | |
| 150 | // TS |
| 151 | if (node.optional) { |
| 152 | this.token("?"); |
| 153 | } |
| 154 | if (node.definite) { |
| 155 | this.token("!"); |
| 156 | } |
| 157 | |
| 158 | this.print(node.typeAnnotation, node); |
| 159 | if (node.value) { |
| 160 | this.space(); |
| 161 | this.token("="); |
| 162 | this.space(); |
| 163 | this.print(node.value, node); |
| 164 | } |
| 165 | this.semicolon(); |
| 166 | } |
| 167 | |
| 168 | export function ClassPrivateProperty( |
| 169 | this: Printer, |
| 170 | node: t.ClassPrivateProperty, |
| 171 | ) { |
| 172 | this.printJoin(node.decorators, node); |
| 173 | if (node.static) { |
| 174 | this.word("static"); |
| 175 | this.space(); |
| 176 | } |
| 177 | this.print(node.key, node); |
| 178 | this.print(node.typeAnnotation, node); |
| 179 | if (node.value) { |
| 180 | this.space(); |
| 181 | this.token("="); |
| 182 | this.space(); |
| 183 | this.print(node.value, node); |
| 184 | } |
| 185 | this.semicolon(); |
| 186 | } |
| 187 | |
| 188 | export function ClassMethod(this: Printer, node: t.ClassMethod) { |
| 189 | this._classMethodHead(node); |
| 190 | this.space(); |
| 191 | this.print(node.body, node); |
| 192 | } |
| 193 | |
| 194 | export function ClassPrivateMethod(this: Printer, node: t.ClassPrivateMethod) { |
| 195 | this._classMethodHead(node); |
| 196 | this.space(); |
| 197 | this.print(node.body, node); |
| 198 | } |
| 199 | |
| 200 | export function _classMethodHead( |
| 201 | this: Printer, |
| 202 | node: t.ClassMethod | t.ClassPrivateMethod | t.TSDeclareMethod, |
| 203 | ) { |
| 204 | this.printJoin(node.decorators, node); |
| 205 | |
| 206 | // catch up to method key, avoid line break |
| 207 | // between member modifiers/method heads and the method key. |
| 208 | const endLine = node.key.loc?.end?.line; |
| 209 | if (endLine) this.catchUp(endLine); |
| 210 | |
| 211 | this.tsPrintClassMemberModifiers(node); |
| 212 | this._methodHead(node); |
| 213 | } |
| 214 | |
| 215 | export function StaticBlock(this: Printer, node: t.StaticBlock) { |
| 216 | this.word("static"); |
| 217 | this.space(); |
| 218 | this.token("{"); |
| 219 | if (node.body.length === 0) { |
| 220 | this.token("}"); |
| 221 | } else { |
| 222 | this.newline(); |
| 223 | this.printSequence(node.body, node, { |
| 224 | indent: true, |
| 225 | }); |
| 226 | |
| 227 | this.sourceWithOffset("end", node.loc, 0, -1); |
| 228 | |
| 229 | this.rightBrace(); |
| 230 | } |
| 231 | } |