| 240 | return text; |
| 241 | } |
| 242 | function transpile(ast: BaseNode) { |
| 243 | let insideClass = false; |
| 244 | let decoratedClasses = []; |
| 245 | |
| 246 | // Find all classes that have been decorated with @registerGObjectClass |
| 247 | walk(ast, { |
| 248 | enter: function (node, parent, prop, index) { |
| 249 | let decoratedNames = getDecoratorTargets(node); |
| 250 | if (decoratedNames !== null) { |
| 251 | for (let name of decoratedNames) { |
| 252 | decoratedClasses.push(name); |
| 253 | } |
| 254 | } |
| 255 | }, |
| 256 | leave: function (node, parent, prop, index) {}, |
| 257 | }); |
| 258 | |
| 259 | walk(ast, { |
| 260 | enter: function (node, parent, prop, index) { |
| 261 | if (isClassExpression(node)) { |
| 262 | if (decoratedClasses.indexOf(node.id.name) !== -1) { |
| 263 | insideClass = true; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (insideClass) { |
| 268 | if (isMethodDefinition(node) && node.kind == 'constructor') { |
| 269 | if (isIdentifier(node.key)) { |
| 270 | // We have found a constructor, change it to a normal method named `_init` |
| 271 | node.kind = 'method'; |
| 272 | node.key.name = '_init'; |
| 273 | } |
| 274 | } |
| 275 | if (isSimpleCallExpression(node)) { |
| 276 | if (node.callee.type == 'Super') { |
| 277 | // We found a `super(...)` call |
| 278 | // Change it to `super._init(...)` |
| 279 | node.callee = { |
| 280 | type: 'MemberExpression', |
| 281 | object: node.callee, |
| 282 | property: { |
| 283 | type: 'Identifier', |
| 284 | name: '_init', |
| 285 | }, |
| 286 | computed: false, |
| 287 | optional: false, |
| 288 | }; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | }, |
| 293 | leave: function (node, parent, prop, index) { |
| 294 | if (isClassExpression(node)) { |
| 295 | if (decoratedClasses.indexOf(node.id.name) !== -1) { |
| 296 | console.assert(insideClass); |
| 297 | insideClass = false; |
| 298 | } |
| 299 | } |