Skip to content

Commit d23e150

Browse files
author
Joseph Watts
committed
Create property declarations for parameters with property assignments
Signed-off-by: Joseph Watts <jwatts43@bloomberg.net>
1 parent 0c0518d commit d23e150

File tree

1 file changed

+25
-27
lines changed
  • src/compiler/transformers

1 file changed

+25
-27
lines changed

src/compiler/transformers/ts.ts

+25-27
Original file line numberDiff line numberDiff line change
@@ -904,12 +904,30 @@ namespace ts {
904904
const members: ClassElement[] = [];
905905
const existingMembers = visitNodes(node.members, classElementVisitor, isClassElement);
906906
const constructor = getFirstConstructorWithBody(node);
907-
currentClassHasParameterProperties = constructor &&
908-
!!(constructor.transformFlags & TransformFlags.ContainsTypeScriptClassSyntax) &&
909-
forEach(constructor.parameters, isParameterWithPropertyAssignment);
910-
if (currentClassHasParameterProperties && constructor) {
907+
const parametersWithPropertyAssignments =
908+
constructor && !!(constructor.transformFlags & TransformFlags.ContainsTypeScriptClassSyntax)
909+
? filter(constructor.parameters, isParameterPropertyDeclaration)
910+
: undefined;
911+
if (some(parametersWithPropertyAssignments) && constructor) {
912+
currentClassHasParameterProperties = true;
913+
914+
// Create property declarations for constructor parameter properties.
915+
addRange(
916+
members,
917+
parametersWithPropertyAssignments.map(param =>
918+
createProperty(
919+
/*decorators*/ undefined,
920+
/*modifiers*/ undefined,
921+
param.name,
922+
/*questionOrExclamationToken*/ undefined,
923+
/*type*/ undefined,
924+
/*initializer*/ undefined
925+
)
926+
)
927+
);
928+
911929
const parameters = transformConstructorParameters(constructor);
912-
const body = transformConstructorBody(node.members, constructor);
930+
const body = transformConstructorBody(node.members, constructor, parametersWithPropertyAssignments);
913931
members.push(startOnNewLine(
914932
setOriginalNode(
915933
setTextRange(
@@ -990,7 +1008,7 @@ namespace ts {
9901008
* @param node The current class.
9911009
* @param constructor The current class constructor.
9921010
*/
993-
function transformConstructorBody(members: NodeArray<ClassElement>, constructor: ConstructorDeclaration) {
1011+
function transformConstructorBody(members: NodeArray<ClassElement>, constructor: ConstructorDeclaration, propertyAssignments: ReadonlyArray<ParameterPropertyDeclaration>) {
9941012
let statements: Statement[] = [];
9951013
let indexOfFirstStatement = 0;
9961014

@@ -1010,7 +1028,6 @@ namespace ts {
10101028
// this.y = y;
10111029
// }
10121030
//
1013-
const propertyAssignments = getParametersWithPropertyAssignments(constructor);
10141031
addRange(statements, map(propertyAssignments, transformParameterWithPropertyAssignment));
10151032

10161033
// Get property initializers.
@@ -1049,31 +1066,12 @@ namespace ts {
10491066
);
10501067
}
10511068

1052-
/**
1053-
* Gets all parameters of a constructor that should be transformed into property assignments.
1054-
*
1055-
* @param node The constructor node.
1056-
*/
1057-
function getParametersWithPropertyAssignments(node: ConstructorDeclaration): ReadonlyArray<ParameterDeclaration> {
1058-
return filter(node.parameters, isParameterWithPropertyAssignment);
1059-
}
1060-
1061-
/**
1062-
* Determines whether a parameter should be transformed into a property assignment.
1063-
*
1064-
* @param parameter The parameter node.
1065-
*/
1066-
function isParameterWithPropertyAssignment(parameter: ParameterDeclaration) {
1067-
return hasModifier(parameter, ModifierFlags.ParameterPropertyModifier)
1068-
&& isIdentifier(parameter.name);
1069-
}
1070-
10711069
/**
10721070
* Transforms a parameter into a property assignment statement.
10731071
*
10741072
* @param node The parameter declaration.
10751073
*/
1076-
function transformParameterWithPropertyAssignment(node: ParameterDeclaration) {
1074+
function transformParameterWithPropertyAssignment(node: ParameterPropertyDeclaration) {
10771075
Debug.assert(isIdentifier(node.name));
10781076
const name = node.name as Identifier;
10791077
const propertyName = getMutableClone(name);

0 commit comments

Comments
 (0)