Skip to content

Commit

Permalink
Merge pull request #9407 from Microsoft/literalTypes
Browse files Browse the repository at this point in the history
Number, enum, and boolean literal types
  • Loading branch information
ahejlsberg authored Jul 28, 2016
2 parents 0783743 + 5ff07dc commit 0c131fa
Show file tree
Hide file tree
Showing 442 changed files with 13,793 additions and 4,854 deletions.
22 changes: 4 additions & 18 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,18 +618,10 @@ namespace ts {
return false;
}

function isNarrowingNullCheckOperands(expr1: Expression, expr2: Expression) {
return (expr1.kind === SyntaxKind.NullKeyword || expr1.kind === SyntaxKind.Identifier && (<Identifier>expr1).text === "undefined") && isNarrowableOperand(expr2);
}

function isNarrowingTypeofOperands(expr1: Expression, expr2: Expression) {
return expr1.kind === SyntaxKind.TypeOfExpression && isNarrowableOperand((<TypeOfExpression>expr1).expression) && expr2.kind === SyntaxKind.StringLiteral;
}

function isNarrowingDiscriminant(expr: Expression) {
return expr.kind === SyntaxKind.PropertyAccessExpression && isNarrowableReference((<PropertyAccessExpression>expr).expression);
}

function isNarrowingBinaryExpression(expr: BinaryExpression) {
switch (expr.operatorToken.kind) {
case SyntaxKind.EqualsToken:
Expand All @@ -638,9 +630,8 @@ namespace ts {
case SyntaxKind.ExclamationEqualsToken:
case SyntaxKind.EqualsEqualsEqualsToken:
case SyntaxKind.ExclamationEqualsEqualsToken:
return isNarrowingNullCheckOperands(expr.right, expr.left) || isNarrowingNullCheckOperands(expr.left, expr.right) ||
isNarrowingTypeofOperands(expr.right, expr.left) || isNarrowingTypeofOperands(expr.left, expr.right) ||
isNarrowingDiscriminant(expr.left) || isNarrowingDiscriminant(expr.right);
return isNarrowableOperand(expr.left) || isNarrowableOperand(expr.right) ||
isNarrowingTypeofOperands(expr.right, expr.left) || isNarrowingTypeofOperands(expr.left, expr.right);
case SyntaxKind.InstanceOfKeyword:
return isNarrowableOperand(expr.left);
case SyntaxKind.CommaToken:
Expand All @@ -664,11 +655,6 @@ namespace ts {
return isNarrowableReference(expr);
}

function isNarrowingSwitchStatement(switchStatement: SwitchStatement) {
const expr = switchStatement.expression;
return expr.kind === SyntaxKind.PropertyAccessExpression && isNarrowableReference((<PropertyAccessExpression>expr).expression);
}

function createBranchLabel(): FlowLabel {
return {
flags: FlowFlags.BranchLabel,
Expand Down Expand Up @@ -718,7 +704,7 @@ namespace ts {
}

function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): FlowNode {
if (!isNarrowingSwitchStatement(switchStatement)) {
if (!isNarrowingExpression(switchStatement.expression)) {
return antecedent;
}
setFlowNodeReferenced(antecedent);
Expand Down Expand Up @@ -1983,7 +1969,7 @@ namespace ts {
function bindThisPropertyAssignment(node: BinaryExpression) {
// Declare a 'member' in case it turns out the container was an ES5 class or ES6 constructor
let assignee: Node;
if (container.kind === SyntaxKind.FunctionDeclaration || container.kind === SyntaxKind.FunctionDeclaration) {
if (container.kind === SyntaxKind.FunctionDeclaration || container.kind === SyntaxKind.FunctionExpression) {
assignee = container;
}
else if (container.kind === SyntaxKind.Constructor) {
Expand Down
1,111 changes: 734 additions & 377 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ namespace ts {
return undefined;
}

export function contains<T>(array: T[], value: T, areEqual?: (a: T, b: T) => boolean): boolean {
export function contains<T>(array: T[], value: T): boolean {
if (array) {
for (const v of array) {
if (areEqual ? areEqual(v, value) : v === value) {
if (v === value) {
return true;
}
}
Expand Down Expand Up @@ -182,10 +182,13 @@ namespace ts {
let result: T[];
if (array) {
result = [];
for (const item of array) {
if (!contains(result, item, areEqual)) {
result.push(item);
loop: for (const item of array) {
for (const res of result) {
if (areEqual ? areEqual(res, item) : res === item) {
continue loop;
}
}
result.push(item);
}
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ namespace ts {
case SyntaxKind.NullKeyword:
case SyntaxKind.NeverKeyword:
case SyntaxKind.ThisType:
case SyntaxKind.StringLiteralType:
case SyntaxKind.LiteralType:
return writeTextOfNode(currentText, type);
case SyntaxKind.ExpressionWithTypeArguments:
return emitExpressionWithTypeArguments(<ExpressionWithTypeArguments>type);
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,10 @@
"category": "Error",
"code": 2534
},
"Enum type '{0}' has members with initializers that are not literals.": {
"category": "Error",
"code": 2535
},
"JSX element attributes type '{0}' may not be a union type.": {
"category": "Error",
"code": 2600
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6045,7 +6045,7 @@ const _super = (function (geti, seti) {
return;

case SyntaxKind.StringKeyword:
case SyntaxKind.StringLiteralType:
case SyntaxKind.LiteralType:
write("String");
return;

Expand Down
Loading

0 comments on commit 0c131fa

Please sign in to comment.