|
| 1 | +package printer |
| 2 | + |
| 3 | +import "github.com/microsoft/typescript-go/internal/ast" |
| 4 | + |
| 5 | +type PrivateIdentifierKind int |
| 6 | + |
| 7 | +const ( |
| 8 | + PrivateIdentifierKindMethod PrivateIdentifierKind = iota |
| 9 | + PrivateIdentifierKindField |
| 10 | + PrivateIdentifierKindAccessor |
| 11 | + PrivateIdentifierKindUntransformed |
| 12 | +) |
| 13 | + |
| 14 | +func (k PrivateIdentifierKind) String() string { |
| 15 | + switch k { |
| 16 | + case PrivateIdentifierKindMethod: |
| 17 | + return "m" |
| 18 | + case PrivateIdentifierKindField: |
| 19 | + return "f" |
| 20 | + case PrivateIdentifierKindAccessor: |
| 21 | + return "a" |
| 22 | + case PrivateIdentifierKindUntransformed: |
| 23 | + return "untransformed" |
| 24 | + default: |
| 25 | + panic("Unhandled PrivateIdentifierKind") |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +type PrivateIdentifierInfo interface { |
| 30 | + Kind() PrivateIdentifierKind |
| 31 | + IsValid() bool |
| 32 | + IsStatic() bool |
| 33 | + BrandCheckIdentifier() *ast.Identifier |
| 34 | +} |
| 35 | + |
| 36 | +type PrivateIdentifierInfoBase struct { |
| 37 | + /** |
| 38 | + * brandCheckIdentifier can contain: |
| 39 | + * - For instance field: The WeakMap that will be the storage for the field. |
| 40 | + * - For instance methods or accessors: The WeakSet that will be used for brand checking. |
| 41 | + * - For static members: The constructor that will be used for brand checking. |
| 42 | + */ |
| 43 | + brandCheckIdentifier *ast.Identifier |
| 44 | + // Stores if the identifier is static or not |
| 45 | + isStatic bool |
| 46 | + // Stores if the identifier declaration is valid or not. Reserved names (e.g. #constructor) |
| 47 | + // or duplicate identifiers are considered invalid. |
| 48 | + isValid bool |
| 49 | +} |
| 50 | + |
| 51 | +type PrivateIdentifierAccessorInfo struct { |
| 52 | + PrivateIdentifierInfoBase |
| 53 | + kind PrivateIdentifierKind |
| 54 | + // Identifier for a variable that will contain the private get accessor implementation, if any. |
| 55 | + GetterName *ast.Identifier |
| 56 | + // Identifier for a variable that will contain the private set accessor implementation, if any. |
| 57 | + SetterName *ast.Identifier |
| 58 | +} |
| 59 | + |
| 60 | +func (p *PrivateIdentifierAccessorInfo) Kind() PrivateIdentifierKind { |
| 61 | + return p.kind |
| 62 | +} |
| 63 | +func (p *PrivateIdentifierAccessorInfo) IsValid() bool { |
| 64 | + return p.PrivateIdentifierInfoBase.isValid |
| 65 | +} |
| 66 | +func (p *PrivateIdentifierAccessorInfo) IsStatic() bool { |
| 67 | + return p.PrivateIdentifierInfoBase.isStatic |
| 68 | +} |
| 69 | +func (p *PrivateIdentifierAccessorInfo) BrandCheckIdentifier() *ast.Identifier { |
| 70 | + return p.PrivateIdentifierInfoBase.brandCheckIdentifier |
| 71 | +} |
| 72 | + |
| 73 | +func NewPrivateIdentifierAccessorInfo(brandCheckIdentifier *ast.Identifier, getterName *ast.Identifier, setterName *ast.Identifier, isValid bool, isStatc bool) *PrivateIdentifierAccessorInfo { |
| 74 | + return &PrivateIdentifierAccessorInfo{ |
| 75 | + kind: PrivateIdentifierKindAccessor, |
| 76 | + PrivateIdentifierInfoBase: PrivateIdentifierInfoBase{ |
| 77 | + brandCheckIdentifier: brandCheckIdentifier, |
| 78 | + isStatic: isStatc, |
| 79 | + isValid: isValid, |
| 80 | + }, |
| 81 | + GetterName: getterName, |
| 82 | + SetterName: setterName, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +type PrivateIdentifierMethodInfo struct { |
| 87 | + PrivateIdentifierInfoBase |
| 88 | + kind PrivateIdentifierKind |
| 89 | + // Identifier for a variable that will contain the private method implementation. |
| 90 | + MethodName *ast.Identifier |
| 91 | +} |
| 92 | + |
| 93 | +func (p *PrivateIdentifierMethodInfo) Kind() PrivateIdentifierKind { |
| 94 | + return p.kind |
| 95 | +} |
| 96 | +func (p *PrivateIdentifierMethodInfo) IsValid() bool { |
| 97 | + return p.PrivateIdentifierInfoBase.isValid |
| 98 | +} |
| 99 | +func (p *PrivateIdentifierMethodInfo) IsStatic() bool { |
| 100 | + return p.PrivateIdentifierInfoBase.isStatic |
| 101 | +} |
| 102 | +func (p *PrivateIdentifierMethodInfo) BrandCheckIdentifier() *ast.Identifier { |
| 103 | + return p.PrivateIdentifierInfoBase.brandCheckIdentifier |
| 104 | +} |
| 105 | + |
| 106 | +func NewPrivateIdentifierMethodInfo(brandCheckIdentifier *ast.Identifier, methodName *ast.Identifier, isValid bool, isStatc bool) *PrivateIdentifierMethodInfo { |
| 107 | + return &PrivateIdentifierMethodInfo{ |
| 108 | + kind: PrivateIdentifierKindMethod, |
| 109 | + PrivateIdentifierInfoBase: PrivateIdentifierInfoBase{ |
| 110 | + brandCheckIdentifier: brandCheckIdentifier, |
| 111 | + isStatic: isStatc, |
| 112 | + isValid: isValid, |
| 113 | + }, |
| 114 | + MethodName: methodName, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +type PrivateIdentifierInstanceFieldInfo struct { |
| 119 | + PrivateIdentifierInfoBase |
| 120 | + kind PrivateIdentifierKind |
| 121 | +} |
| 122 | + |
| 123 | +func NewPrivateIdentifierInstanceFieldInfo(brandCheckIdentifier *ast.Identifier, isValid bool) *PrivateIdentifierInstanceFieldInfo { |
| 124 | + return &PrivateIdentifierInstanceFieldInfo{ |
| 125 | + kind: PrivateIdentifierKindField, |
| 126 | + PrivateIdentifierInfoBase: PrivateIdentifierInfoBase{ |
| 127 | + brandCheckIdentifier: brandCheckIdentifier, |
| 128 | + isStatic: false, |
| 129 | + isValid: isValid, |
| 130 | + }, |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func (p *PrivateIdentifierInstanceFieldInfo) Kind() PrivateIdentifierKind { |
| 135 | + return p.kind |
| 136 | +} |
| 137 | +func (p *PrivateIdentifierInstanceFieldInfo) IsValid() bool { |
| 138 | + return p.PrivateIdentifierInfoBase.isValid |
| 139 | +} |
| 140 | +func (p *PrivateIdentifierInstanceFieldInfo) IsStatic() bool { |
| 141 | + return p.PrivateIdentifierInfoBase.isStatic |
| 142 | +} |
| 143 | +func (p *PrivateIdentifierInstanceFieldInfo) BrandCheckIdentifier() *ast.Identifier { |
| 144 | + return p.PrivateIdentifierInfoBase.brandCheckIdentifier |
| 145 | +} |
| 146 | + |
| 147 | +type PrivateIdentifierStaticFieldInfo struct { |
| 148 | + PrivateIdentifierInfoBase |
| 149 | + kind PrivateIdentifierKind |
| 150 | + // Contains the variable that will serve as the storage for the field. |
| 151 | + VariableName *ast.Identifier |
| 152 | +} |
| 153 | + |
| 154 | +func NewPrivateIdentifierStaticFieldInfo(brandCheckIdentifier *ast.Identifier, variableName *ast.Identifier, isValid bool) *PrivateIdentifierStaticFieldInfo { |
| 155 | + return &PrivateIdentifierStaticFieldInfo{ |
| 156 | + kind: PrivateIdentifierKindField, |
| 157 | + PrivateIdentifierInfoBase: PrivateIdentifierInfoBase{ |
| 158 | + brandCheckIdentifier: brandCheckIdentifier, |
| 159 | + isStatic: true, |
| 160 | + isValid: isValid, |
| 161 | + }, |
| 162 | + VariableName: variableName, |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func (p *PrivateIdentifierStaticFieldInfo) Kind() PrivateIdentifierKind { |
| 167 | + return p.kind |
| 168 | +} |
| 169 | +func (p *PrivateIdentifierStaticFieldInfo) IsValid() bool { |
| 170 | + return p.PrivateIdentifierInfoBase.isValid |
| 171 | +} |
| 172 | +func (p *PrivateIdentifierStaticFieldInfo) IsStatic() bool { |
| 173 | + return p.PrivateIdentifierInfoBase.isStatic |
| 174 | +} |
| 175 | +func (p *PrivateIdentifierStaticFieldInfo) BrandCheckIdentifier() *ast.Identifier { |
| 176 | + return p.PrivateIdentifierInfoBase.brandCheckIdentifier |
| 177 | +} |
| 178 | + |
| 179 | +type PrivateIdentifierUntransformedInfo struct { |
| 180 | + PrivateIdentifierInfoBase |
| 181 | + kind PrivateIdentifierKind |
| 182 | +} |
| 183 | + |
| 184 | +func NewPrivateIdentifierUntransformedInfo(brandCheckIdentifier *ast.Identifier, isValid bool, isStatic bool) *PrivateIdentifierUntransformedInfo { |
| 185 | + return &PrivateIdentifierUntransformedInfo{ |
| 186 | + kind: PrivateIdentifierKindUntransformed, |
| 187 | + PrivateIdentifierInfoBase: PrivateIdentifierInfoBase{ |
| 188 | + brandCheckIdentifier: brandCheckIdentifier, |
| 189 | + isStatic: isStatic, |
| 190 | + isValid: isValid, |
| 191 | + }, |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +func (p *PrivateIdentifierUntransformedInfo) Kind() PrivateIdentifierKind { |
| 196 | + return p.kind |
| 197 | +} |
| 198 | +func (p *PrivateIdentifierUntransformedInfo) IsValid() bool { |
| 199 | + return p.PrivateIdentifierInfoBase.isValid |
| 200 | +} |
| 201 | +func (p *PrivateIdentifierUntransformedInfo) IsStatic() bool { |
| 202 | + return p.PrivateIdentifierInfoBase.isStatic |
| 203 | +} |
| 204 | +func (p *PrivateIdentifierUntransformedInfo) BrandCheckIdentifier() *ast.Identifier { |
| 205 | + return p.PrivateIdentifierInfoBase.brandCheckIdentifier |
| 206 | +} |
0 commit comments