Skip to content

Commit bb6f3ad

Browse files
committed
Add parameter initialisers to control flow
Also a little code to emit them in declarations, but this doesn't work yet.
1 parent caad486 commit bb6f3ad

3 files changed

Lines changed: 19 additions & 14 deletions

File tree

src/compiler/binder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ namespace ts {
810810
};
811811
}
812812

813-
function createFlowAssignment(antecedent: FlowNode, node: Expression | VariableDeclaration | BindingElement): FlowNode {
813+
function createFlowAssignment(antecedent: FlowNode, node: Expression | VariableDeclaration | BindingElement | ParameterDeclaration): FlowNode {
814814
setFlowNodeReferenced(antecedent);
815815
return <FlowAssignment>{
816816
flags: FlowFlags.Assignment,
@@ -2311,6 +2311,9 @@ namespace ts {
23112311
}
23122312
else {
23132313
declareSymbolAndAddToSymbolTable(node, SymbolFlags.FunctionScopedVariable, SymbolFlags.ParameterExcludes);
2314+
if (node.initializer) {
2315+
currentFlow = createFlowAssignment(currentFlow, node);
2316+
}
23142317
}
23152318

23162319
// If this is a property-parameter, then also declare the property symbol into the

src/compiler/checker.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,7 +3245,9 @@ namespace ts {
32453245

32463246
// Use type from type annotation if one is present
32473247
if (declaration.type) {
3248-
return addOptionality(getTypeFromTypeNode(declaration.type), /*optional*/ declaration.questionToken && includeOptionality);
3248+
const isOptional = declaration.questionToken ||
3249+
(declaration.initializer && declaration.kind === SyntaxKind.Parameter && !(getModifierFlags(declaration) & ModifierFlags.ParameterPropertyModifier));
3250+
return addOptionality(getTypeFromTypeNode(declaration.type), /*optional*/ isOptional && includeOptionality);
32493251
}
32503252

32513253
if ((compilerOptions.noImplicitAny || declaration.flags & NodeFlags.JavaScriptFile) &&
@@ -9042,8 +9044,8 @@ namespace ts {
90429044
switch (source.kind) {
90439045
case SyntaxKind.Identifier:
90449046
return target.kind === SyntaxKind.Identifier && getResolvedSymbol(<Identifier>source) === getResolvedSymbol(<Identifier>target) ||
9045-
(target.kind === SyntaxKind.VariableDeclaration || target.kind === SyntaxKind.BindingElement) &&
9046-
getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(<Identifier>source)) === getSymbolOfNode(target);
9047+
(target.kind === SyntaxKind.VariableDeclaration || target.kind === SyntaxKind.BindingElement) && getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(<Identifier>source)) === getSymbolOfNode(target) ||
9048+
target.kind === SyntaxKind.Parameter && getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(source as Identifier)) === getSymbolOfNode(target);
90479049
case SyntaxKind.ThisKeyword:
90489050
return target.kind === SyntaxKind.ThisKeyword;
90499051
case SyntaxKind.PropertyAccessExpression:
@@ -9314,7 +9316,7 @@ namespace ts {
93149316
return links.resolvedType || getTypeOfExpression(node);
93159317
}
93169318

9317-
function getInitialTypeOfVariableDeclaration(node: VariableDeclaration) {
9319+
function getInitialTypeOfVariableDeclaration(node: VariableDeclaration | ParameterDeclaration) {
93189320
if (node.initializer) {
93199321
return getTypeOfInitializer(node.initializer);
93209322
}
@@ -9327,15 +9329,15 @@ namespace ts {
93279329
return unknownType;
93289330
}
93299331

9330-
function getInitialType(node: VariableDeclaration | BindingElement) {
9331-
return node.kind === SyntaxKind.VariableDeclaration ?
9332-
getInitialTypeOfVariableDeclaration(<VariableDeclaration>node) :
9332+
function getInitialType(node: VariableDeclaration | BindingElement | ParameterDeclaration) {
9333+
return node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.Parameter ?
9334+
getInitialTypeOfVariableDeclaration(<VariableDeclaration | ParameterDeclaration>node) :
93339335
getInitialTypeOfBindingElement(<BindingElement>node);
93349336
}
93359337

93369338
function getInitialOrAssignedType(node: VariableDeclaration | BindingElement | Expression) {
9337-
return node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.BindingElement ?
9338-
getInitialType(<VariableDeclaration | BindingElement>node) :
9339+
return node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.BindingElement || node.kind === SyntaxKind.Parameter ?
9340+
getInitialType(<VariableDeclaration | BindingElement | ParameterDeclaration>node) :
93399341
getAssignedType(<Expression>node);
93409342
}
93419343

src/compiler/declarationEmitter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,12 @@ namespace ts {
319319
}
320320
}
321321

322-
function writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, type: TypeNode, getSymbolAccessibilityDiagnostic: GetSymbolAccessibilityDiagnostic) {
322+
function writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, type: TypeNode, getSymbolAccessibilityDiagnostic: GetSymbolAccessibilityDiagnostic, addUndefined?: boolean) {
323323
writer.getSymbolAccessibilityDiagnostic = getSymbolAccessibilityDiagnostic;
324324
write(": ");
325325
if (type) {
326326
// Write the type
327-
emitType(type);
327+
emitType(type, addUndefined);
328328
}
329329
else {
330330
errorNameNode = declaration.name;
@@ -384,7 +384,7 @@ namespace ts {
384384
emitType(type);
385385
}
386386

387-
function emitType(type: TypeNode | Identifier | QualifiedName) {
387+
function emitType(type: TypeNode | Identifier | QualifiedName, addUndefined?: boolean) {
388388
switch (type.kind) {
389389
case SyntaxKind.AnyKeyword:
390390
case SyntaxKind.StringKeyword:
@@ -1593,7 +1593,7 @@ namespace ts {
15931593
emitTypeOfVariableDeclarationFromTypeLiteral(node);
15941594
}
15951595
else if (!hasModifier(node.parent, ModifierFlags.Private)) {
1596-
writeTypeOfDeclaration(node, node.type, getParameterDeclarationTypeVisibilityError);
1596+
writeTypeOfDeclaration(node, node.type, getParameterDeclarationTypeVisibilityError, !!node.initializer && !(getModifierFlags(node) & ModifierFlags.ParameterPropertyModifier));
15971597
}
15981598

15991599
function getParameterDeclarationTypeVisibilityError(symbolAccessibilityResult: SymbolAccessibilityResult): SymbolAccessibilityDiagnostic {

0 commit comments

Comments
 (0)