@@ -904,12 +904,30 @@ namespace ts {
904
904
const members : ClassElement [ ] = [ ] ;
905
905
const existingMembers = visitNodes ( node . members , classElementVisitor , isClassElement ) ;
906
906
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
+
911
929
const parameters = transformConstructorParameters ( constructor ) ;
912
- const body = transformConstructorBody ( node . members , constructor ) ;
930
+ const body = transformConstructorBody ( node . members , constructor , parametersWithPropertyAssignments ) ;
913
931
members . push ( startOnNewLine (
914
932
setOriginalNode (
915
933
setTextRange (
@@ -990,7 +1008,7 @@ namespace ts {
990
1008
* @param node The current class.
991
1009
* @param constructor The current class constructor.
992
1010
*/
993
- function transformConstructorBody ( members : NodeArray < ClassElement > , constructor : ConstructorDeclaration ) {
1011
+ function transformConstructorBody ( members : NodeArray < ClassElement > , constructor : ConstructorDeclaration , propertyAssignments : ReadonlyArray < ParameterPropertyDeclaration > ) {
994
1012
let statements : Statement [ ] = [ ] ;
995
1013
let indexOfFirstStatement = 0 ;
996
1014
@@ -1010,7 +1028,6 @@ namespace ts {
1010
1028
// this.y = y;
1011
1029
// }
1012
1030
//
1013
- const propertyAssignments = getParametersWithPropertyAssignments ( constructor ) ;
1014
1031
addRange ( statements , map ( propertyAssignments , transformParameterWithPropertyAssignment ) ) ;
1015
1032
1016
1033
// Get property initializers.
@@ -1049,31 +1066,12 @@ namespace ts {
1049
1066
) ;
1050
1067
}
1051
1068
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
-
1071
1069
/**
1072
1070
* Transforms a parameter into a property assignment statement.
1073
1071
*
1074
1072
* @param node The parameter declaration.
1075
1073
*/
1076
- function transformParameterWithPropertyAssignment ( node : ParameterDeclaration ) {
1074
+ function transformParameterWithPropertyAssignment ( node : ParameterPropertyDeclaration ) {
1077
1075
Debug . assert ( isIdentifier ( node . name ) ) ;
1078
1076
const name = node . name as Identifier ;
1079
1077
const propertyName = getMutableClone ( name ) ;
0 commit comments