@travetto/transformer 8.0.0-alpha.12 → 8.0.0-alpha.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/transformer",
3
- "version": "8.0.0-alpha.12",
3
+ "version": "8.0.0-alpha.13",
4
4
  "type": "module",
5
5
  "description": "Functionality for AST transformations, with transformer registration, and general utils",
6
6
  "keywords": [
@@ -25,9 +25,9 @@
25
25
  "directory": "module/transformer"
26
26
  },
27
27
  "dependencies": {
28
- "@travetto/manifest": "^8.0.0-alpha.9",
28
+ "@travetto/manifest": "^8.0.0-alpha.10",
29
29
  "tslib": "^2.8.1",
30
- "typescript": "^6.0.3"
30
+ "typescript": "npm:@typescript/typescript6@^6.0.3"
31
31
  },
32
32
  "travetto": {
33
33
  "displayName": "Transformation",
@@ -102,4 +102,66 @@ export class DeclarationUtil {
102
102
  }
103
103
  return false;
104
104
  }
105
+
106
+ /**
107
+ * Ensures a constructor exists in the class declaration, synthesizing one if necessary
108
+ */
109
+ static ensureConstructor(factory: ts.NodeFactory, node: ts.ClassDeclaration): ts.ClassDeclaration {
110
+ const hasCons = node.members.some(member => ts.isConstructorDeclaration(member));
111
+ if (hasCons) {
112
+ return node;
113
+ }
114
+
115
+ const hasParent = node.heritageClauses?.some(clause => clause.token === ts.SyntaxKind.ExtendsKeyword);
116
+ const newConsStatements: ts.Statement[] = [];
117
+ let parameters: ts.ParameterDeclaration[] = [];
118
+ if (hasParent) {
119
+ parameters = [
120
+ factory.createParameterDeclaration(
121
+ undefined,
122
+ factory.createToken(ts.SyntaxKind.DotDotDotToken),
123
+ factory.createIdentifier('args'),
124
+ undefined,
125
+ factory.createArrayTypeNode(factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)),
126
+ undefined
127
+ )
128
+ ];
129
+ newConsStatements.push(
130
+ factory.createExpressionStatement(
131
+ factory.createCallExpression(
132
+ factory.createSuper(),
133
+ undefined,
134
+ [factory.createSpreadElement(factory.createIdentifier('args'))]
135
+ )
136
+ )
137
+ );
138
+ }
139
+ const newCons = factory.createConstructorDeclaration(
140
+ undefined,
141
+ parameters,
142
+ factory.createBlock(newConsStatements, true)
143
+ );
144
+ return factory.updateClassDeclaration(
145
+ node,
146
+ node.modifiers,
147
+ node.name,
148
+ node.typeParameters,
149
+ node.heritageClauses,
150
+ [...node.members, newCons]
151
+ );
152
+ }
153
+
154
+ /**
155
+ * Calculates the insertion index for constructor statements (after super call, if exists)
156
+ */
157
+ static getConstructorInsertIndex(node: ts.ConstructorDeclaration): number {
158
+ if (node.body) {
159
+ const first = node.body.statements[0];
160
+ if (first && ts.isExpressionStatement(first) && ts.isCallExpression(first.expression) &&
161
+ first.expression.expression.kind === ts.SyntaxKind.SuperKeyword) {
162
+ return 1;
163
+ }
164
+ }
165
+ return 0;
166
+ }
105
167
  }