Skip to content

Commit 0a97f5f

Browse files
committedOct 31, 2014
Merge pull request #960 from Microsoft/templates
Support for ES6 Templates
2 parents 33cee0c + 3e8978f commit 0a97f5f

File tree

304 files changed

+3226
-206
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

304 files changed

+3226
-206
lines changed
 

Diff for: ‎src/compiler/checker.ts

+37-9
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ module ts {
427427
if (result.flags & SymbolFlags.BlockScopedVariable) {
428428
// Block-scoped variables cannot be used before their definition
429429
var declaration = forEach(result.declarations, d => d.flags & NodeFlags.BlockScoped ? d : undefined);
430-
Debug.assert(declaration, "Block-scoped variable declaration is undefined");
430+
Debug.assert(declaration !== undefined, "Block-scoped variable declaration is undefined");
431431
var declarationSourceFile = getSourceFileOfNode(declaration);
432432
var referenceSourceFile = getSourceFileOfNode(errorLocation);
433433
if (declarationSourceFile === referenceSourceFile) {
@@ -472,7 +472,7 @@ module ts {
472472
function getSymbolOfPartOfRightHandSideOfImport(entityName: EntityName, importDeclaration?: ImportDeclaration): Symbol {
473473
if (!importDeclaration) {
474474
importDeclaration = getAncestor(entityName, SyntaxKind.ImportDeclaration);
475-
Debug.assert(importDeclaration);
475+
Debug.assert(importDeclaration !== undefined);
476476
}
477477
// There are three things we might try to look for. In the following examples,
478478
// the search term is enclosed in |...|:
@@ -3334,7 +3334,6 @@ module ts {
33343334
}
33353335
if (reportErrors) {
33363336
headMessage = headMessage || Diagnostics.Type_0_is_not_assignable_to_type_1;
3337-
Debug.assert(headMessage);
33383337
reportError(headMessage, typeToString(source), typeToString(target));
33393338
}
33403339
return Ternary.False;
@@ -4912,7 +4911,7 @@ module ts {
49124911
}
49134912
return createArrayType(getUnionType(elementTypes));
49144913
}
4915-
4914+
49164915
function isNumericName(name: string) {
49174916
// The intent of numeric names is that
49184917
// - they are names with text in a numeric form, and that
@@ -4937,7 +4936,7 @@ module ts {
49374936
// with the strings '"Infinity"', '"-Infinity"', and '"NaN"' respectively.
49384937
return (+name).toString() === name;
49394938
}
4940-
4939+
49414940
function checkObjectLiteral(node: ObjectLiteral, contextualMapper?: TypeMapper): Type {
49424941
var members = node.symbol.members;
49434942
var properties: SymbolTable = {};
@@ -5660,6 +5659,13 @@ module ts {
56605659
return getReturnTypeOfSignature(signature);
56615660
}
56625661

5662+
function checkTaggedTemplateExpression(node: TaggedTemplateExpression): Type {
5663+
// TODO (drosen): Make sure substitutions are assignable to the tag's arguments.
5664+
checkExpression(node.tag);
5665+
checkExpression(node.template);
5666+
return anyType;
5667+
}
5668+
56635669
function checkTypeAssertion(node: TypeAssertion): Type {
56645670
var exprType = checkExpression(node.operand);
56655671
var targetType = getTypeFromTypeNode(node.type);
@@ -6170,6 +6176,19 @@ module ts {
61706176
return getUnionType([type1, type2]);
61716177
}
61726178

6179+
function checkTemplateExpression(node: TemplateExpression): Type {
6180+
// We just want to check each expressions, but we are unconcerned with
6181+
// the type of each expression, as any value may be coerced into a string.
6182+
// It is worth asking whether this is what we really want though.
6183+
// A place where we actually *are* concerned with the expressions' types are
6184+
// in tagged templates.
6185+
forEach((<TemplateExpression>node).templateSpans, templateSpan => {
6186+
checkExpression(templateSpan.expression);
6187+
});
6188+
6189+
return stringType;
6190+
}
6191+
61736192
function checkExpressionWithContextualType(node: Expression, contextualType: Type, contextualMapper?: TypeMapper): Type {
61746193
var saveContextualType = node.contextualType;
61756194
node.contextualType = contextualType;
@@ -6223,7 +6242,10 @@ module ts {
62236242
return booleanType;
62246243
case SyntaxKind.NumericLiteral:
62256244
return numberType;
6245+
case SyntaxKind.TemplateExpression:
6246+
return checkTemplateExpression(<TemplateExpression>node);
62266247
case SyntaxKind.StringLiteral:
6248+
case SyntaxKind.NoSubstitutionTemplateLiteral:
62276249
return stringType;
62286250
case SyntaxKind.RegularExpressionLiteral:
62296251
return globalRegExpType;
@@ -6240,6 +6262,8 @@ module ts {
62406262
case SyntaxKind.CallExpression:
62416263
case SyntaxKind.NewExpression:
62426264
return checkCallExpression(<CallExpression>node);
6265+
case SyntaxKind.TaggedTemplateExpression:
6266+
return checkTaggedTemplateExpression(<TaggedTemplateExpression>node);
62436267
case SyntaxKind.TypeAssertion:
62446268
return checkTypeAssertion(<TypeAssertion>node);
62456269
case SyntaxKind.ParenExpression:
@@ -7549,17 +7573,17 @@ module ts {
75497573
errorMessage = Diagnostics.Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_accessor;
75507574
}
75517575
else {
7552-
Debug.assert(derived.flags & SymbolFlags.Property);
7576+
Debug.assert((derived.flags & SymbolFlags.Property) !== 0);
75537577
errorMessage = Diagnostics.Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_property;
75547578
}
75557579
}
75567580
else if (base.flags & SymbolFlags.Property) {
7557-
Debug.assert(derived.flags & SymbolFlags.Method);
7581+
Debug.assert((derived.flags & SymbolFlags.Method) !== 0);
75587582
errorMessage = Diagnostics.Class_0_defines_instance_member_property_1_but_extended_class_2_defines_it_as_instance_member_function;
75597583
}
75607584
else {
7561-
Debug.assert(base.flags & SymbolFlags.Accessor);
7562-
Debug.assert(derived.flags & SymbolFlags.Method);
7585+
Debug.assert((base.flags & SymbolFlags.Accessor) !== 0);
7586+
Debug.assert((derived.flags & SymbolFlags.Method) !== 0);
75637587
errorMessage = Diagnostics.Class_0_defines_instance_member_accessor_1_but_extended_class_2_defines_it_as_instance_member_function;
75647588
}
75657589

@@ -8016,6 +8040,7 @@ module ts {
80168040
case SyntaxKind.IndexedAccess:
80178041
case SyntaxKind.CallExpression:
80188042
case SyntaxKind.NewExpression:
8043+
case SyntaxKind.TaggedTemplateExpression:
80198044
case SyntaxKind.TypeAssertion:
80208045
case SyntaxKind.ParenExpression:
80218046
case SyntaxKind.PrefixOperator:
@@ -8293,6 +8318,9 @@ module ts {
82938318
case SyntaxKind.CallExpression:
82948319
case SyntaxKind.NewExpression:
82958320
return (<CallExpression>parent).typeArguments && (<CallExpression>parent).typeArguments.indexOf(node) >= 0;
8321+
case SyntaxKind.TaggedTemplateExpression:
8322+
// TODO (drosen): TaggedTemplateExpressions may eventually support type arguments.
8323+
return false;
82968324
}
82978325
}
82988326

Diff for: ‎src/compiler/core.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ module ts {
1919
[index: string]: T;
2020
}
2121

22+
export enum Comparison {
23+
LessThan = -1,
24+
EqualTo = 0,
25+
GreaterThan = 1
26+
}
27+
2228
export interface StringSet extends Map<any> { }
2329

2430
export function forEach<T, U>(array: T[], callback: (element: T) => U): U {
@@ -93,6 +99,7 @@ module ts {
9399
export function concatenate<T>(array1: T[], array2: T[]): T[] {
94100
if (!array2 || !array2.length) return array1;
95101
if (!array1 || !array1.length) return array2;
102+
96103
return array1.concat(array2);
97104
}
98105

@@ -326,11 +333,11 @@ module ts {
326333
};
327334
}
328335

329-
export function compareValues<T>(a: T, b: T): number {
330-
if (a === b) return 0;
331-
if (a === undefined) return -1;
332-
if (b === undefined) return 1;
333-
return a < b ? -1 : 1;
336+
export function compareValues<T>(a: T, b: T): Comparison {
337+
if (a === b) return Comparison.EqualTo;
338+
if (a === undefined) return Comparison.LessThan;
339+
if (b === undefined) return Comparison.GreaterThan;
340+
return a < b ? Comparison.LessThan : Comparison.GreaterThan;
334341
}
335342

336343
function getDiagnosticFilename(diagnostic: Diagnostic): string {
@@ -355,7 +362,7 @@ module ts {
355362
var previousDiagnostic = diagnostics[0];
356363
for (var i = 1; i < diagnostics.length; i++) {
357364
var currentDiagnostic = diagnostics[i];
358-
var isDupe = compareDiagnostics(currentDiagnostic, previousDiagnostic) === 0;
365+
var isDupe = compareDiagnostics(currentDiagnostic, previousDiagnostic) === Comparison.EqualTo;
359366
if (!isDupe) {
360367
newDiagnostics.push(currentDiagnostic);
361368
previousDiagnostic = currentDiagnostic;
@@ -644,7 +651,7 @@ module ts {
644651
return currentAssertionLevel >= level;
645652
}
646653

647-
export function assert(expression: any, message?: string, verboseDebugInfo?: () => string): void {
654+
export function assert(expression: boolean, message?: string, verboseDebugInfo?: () => string): void {
648655
if (!expression) {
649656
var verboseDebugString = "";
650657
if (verboseDebugInfo) {

0 commit comments

Comments
 (0)
Please sign in to comment.