|
| 1 | +import * as angular from '@angular/compiler'; |
| 2 | + |
| 3 | +import { Source } from './source.ts'; |
| 4 | +import { transformVisitor } from './transform-visitor.ts'; |
| 5 | +import type { NGEmptyExpression, NGNode, RawNGSpan } from './types.ts'; |
| 6 | + |
| 7 | +class Transformer extends Source { |
| 8 | + node: angular.AST; |
| 9 | + ancestors: angular.AST[]; |
| 10 | + |
| 11 | + constructor({ |
| 12 | + node, |
| 13 | + text, |
| 14 | + ancestors, |
| 15 | + }: { |
| 16 | + node: angular.AST; |
| 17 | + text: string; |
| 18 | + ancestors: angular.AST[]; |
| 19 | + }) { |
| 20 | + super(text); |
| 21 | + this.node = node; |
| 22 | + this.ancestors = ancestors; |
| 23 | + } |
| 24 | + |
| 25 | + create<T extends NGNode>( |
| 26 | + properties: Partial<T> & { type: T['type'] }, |
| 27 | + location: angular.AST | RawNGSpan | [number, number], |
| 28 | + ancestors: angular.AST[], |
| 29 | + ) { |
| 30 | + const node = super.createNode(properties, location); |
| 31 | + |
| 32 | + if (ancestors[0] instanceof angular.ParenthesizedExpression) { |
| 33 | + node.extra = { |
| 34 | + ...node.extra, |
| 35 | + parenthesized: true, |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + return node; |
| 40 | + } |
| 41 | + |
| 42 | + createNode<T extends NGNode>( |
| 43 | + properties: Partial<T> & { type: T['type'] }, |
| 44 | + location: angular.AST | RawNGSpan | [number, number] = this.node, |
| 45 | + ancestorsToCreate: angular.AST[] = this.ancestors, |
| 46 | + ) { |
| 47 | + return this.create(properties, location, ancestorsToCreate); |
| 48 | + } |
| 49 | + |
| 50 | + transformChild<T extends NGNode>(child: angular.AST) { |
| 51 | + return new Transformer({ |
| 52 | + node: child, |
| 53 | + ancestors: [this.node, ...this.ancestors], |
| 54 | + text: this.text, |
| 55 | + }).transform<T>(); |
| 56 | + } |
| 57 | + |
| 58 | + transformChildren<T extends NGNode>(children: angular.AST[]) { |
| 59 | + return children.map((child) => this.transformChild<T>(child)); |
| 60 | + } |
| 61 | + |
| 62 | + transform<T extends NGNode = NGNode>() { |
| 63 | + const { node } = this; |
| 64 | + if (node instanceof angular.EmptyExpr) { |
| 65 | + return this.createNode<NGEmptyExpression>( |
| 66 | + { type: 'NGEmptyExpression' }, |
| 67 | + node.sourceSpan, |
| 68 | + ) as T; |
| 69 | + } |
| 70 | + |
| 71 | + return node.visit(transformVisitor, this) as T; |
| 72 | + } |
| 73 | + |
| 74 | + static transform(node: angular.AST, text: string) { |
| 75 | + return new Transformer({ node, text, ancestors: [] }).transform(); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +const transform = (node: angular.AST, text: string) => { |
| 80 | + return Transformer.transform(node, text); |
| 81 | +}; |
| 82 | + |
| 83 | +export { transform, Transformer }; |
0 commit comments