Skip to content

Commit

Permalink
Create property declarations for parameters with property assignments
Browse files Browse the repository at this point in the history
Signed-off-by: Joseph Watts <jwatts43@bloomberg.net>
  • Loading branch information
Joseph Watts committed Oct 23, 2018
1 parent 04aa477 commit 37ae579
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,12 +904,30 @@ namespace ts {
const members: ClassElement[] = [];
const existingMembers = visitNodes(node.members, classElementVisitor, isClassElement);
const constructor = getFirstConstructorWithBody(node);
currentClassHasParameterProperties = constructor &&
!!(constructor.transformFlags & TransformFlags.ContainsTypeScriptClassSyntax) &&
forEach(constructor.parameters, isParameterWithPropertyAssignment);
if (currentClassHasParameterProperties && constructor) {
const parametersWithPropertyAssignments =
constructor && !!(constructor.transformFlags & TransformFlags.ContainsTypeScriptClassSyntax)
? filter(constructor.parameters, isParameterPropertyDeclaration)
: undefined;
if (some(parametersWithPropertyAssignments) && constructor) {
currentClassHasParameterProperties = true;

// Create property declarations for constructor parameter properties.
addRange(
members,
parametersWithPropertyAssignments.map(param =>
createProperty(
/*decorators*/ undefined,
/*modifiers*/ undefined,
param.name,
/*questionOrExclamationToken*/ undefined,
/*type*/ undefined,
/*initializer*/ undefined
)
)
);

const parameters = transformConstructorParameters(constructor);
const body = transformConstructorBody(node.members, constructor);
const body = transformConstructorBody(node.members, constructor, parametersWithPropertyAssignments);
members.push(startOnNewLine(
setOriginalNode(
setTextRange(
Expand Down Expand Up @@ -990,7 +1008,7 @@ namespace ts {
* @param node The current class.
* @param constructor The current class constructor.
*/
function transformConstructorBody(members: NodeArray<ClassElement>, constructor: ConstructorDeclaration) {
function transformConstructorBody(members: NodeArray<ClassElement>, constructor: ConstructorDeclaration, propertyAssignments: ReadonlyArray<ParameterPropertyDeclaration>) {
let statements: Statement[] = [];
let indexOfFirstStatement = 0;

Expand All @@ -1010,7 +1028,6 @@ namespace ts {
// this.y = y;
// }
//
const propertyAssignments = getParametersWithPropertyAssignments(constructor);
addRange(statements, map(propertyAssignments, transformParameterWithPropertyAssignment));

// Get property initializers.
Expand Down Expand Up @@ -1049,31 +1066,12 @@ namespace ts {
);
}

/**
* Gets all parameters of a constructor that should be transformed into property assignments.
*
* @param node The constructor node.
*/
function getParametersWithPropertyAssignments(node: ConstructorDeclaration): ReadonlyArray<ParameterDeclaration> {
return filter(node.parameters, isParameterWithPropertyAssignment);
}

/**
* Determines whether a parameter should be transformed into a property assignment.
*
* @param parameter The parameter node.
*/
function isParameterWithPropertyAssignment(parameter: ParameterDeclaration) {
return hasModifier(parameter, ModifierFlags.ParameterPropertyModifier)
&& isIdentifier(parameter.name);
}

/**
* Transforms a parameter into a property assignment statement.
*
* @param node The parameter declaration.
*/
function transformParameterWithPropertyAssignment(node: ParameterDeclaration) {
function transformParameterWithPropertyAssignment(node: ParameterPropertyDeclaration) {
Debug.assert(isIdentifier(node.name));
const name = node.name as Identifier;
const propertyName = getMutableClone(name);
Expand Down

0 comments on commit 37ae579

Please sign in to comment.