Skip to content

Commit 59ef3da

Browse files
committed
Merge remote-tracking branch 'refs/remotes/Microsoft/master'
2 parents 383cbf0 + 87036fe commit 59ef3da

3,810 files changed

Lines changed: 24160 additions & 10954 deletions

File tree

Some content is hidden

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

src/compiler/binder.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,8 @@ namespace ts {
11891189
case SyntaxKind.ThisType:
11901190
seenThisKeyword = true;
11911191
return;
1192-
1192+
case SyntaxKind.TypePredicate:
1193+
return checkTypePredicate(node as TypePredicateNode);
11931194
case SyntaxKind.TypeParameter:
11941195
return declareSymbolAndAddToSymbolTable(<Declaration>node, SymbolFlags.TypeParameter, SymbolFlags.TypeParameterExcludes);
11951196
case SyntaxKind.Parameter:
@@ -1275,6 +1276,17 @@ namespace ts {
12751276
}
12761277
}
12771278

1279+
function checkTypePredicate(node: TypePredicateNode) {
1280+
const { parameterName, type } = node;
1281+
if (parameterName && parameterName.kind === SyntaxKind.Identifier) {
1282+
checkStrictModeIdentifier(parameterName as Identifier);
1283+
}
1284+
if (parameterName && parameterName.kind === SyntaxKind.ThisType) {
1285+
seenThisKeyword = true;
1286+
}
1287+
bind(type);
1288+
}
1289+
12781290
function bindSourceFileIfExternalModule() {
12791291
setExportContextFlag(file);
12801292
if (isExternalModule(file)) {

src/compiler/checker.ts

Lines changed: 364 additions & 240 deletions
Large diffs are not rendered by default.

src/compiler/declarationEmitter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,9 @@ namespace ts {
13091309
}
13101310

13111311
function emitSignatureDeclaration(node: SignatureDeclaration) {
1312+
const prevEnclosingDeclaration = enclosingDeclaration;
1313+
enclosingDeclaration = node;
1314+
13121315
// Construct signature or constructor type write new Signature
13131316
if (node.kind === SyntaxKind.ConstructSignature || node.kind === SyntaxKind.ConstructorType) {
13141317
write("new ");
@@ -1321,9 +1324,6 @@ namespace ts {
13211324
write("(");
13221325
}
13231326

1324-
const prevEnclosingDeclaration = enclosingDeclaration;
1325-
enclosingDeclaration = node;
1326-
13271327
// Parameters
13281328
emitCommaList(node.parameters, emitParameterDeclaration);
13291329

src/compiler/diagnosticMessages.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@
867867
"category": "Error",
868868
"code": 2312
869869
},
870-
"Constraint of a type parameter cannot reference any type parameter from the same type parameter list.": {
870+
"Type parameter '{0}' has a circular constraint.": {
871871
"category": "Error",
872872
"code": 2313
873873
},
@@ -1647,6 +1647,14 @@
16471647
"category": "Error",
16481648
"code": 2517
16491649
},
1650+
"A 'this'-based type guard is not compatible with a parameter-based type guard.": {
1651+
"category": "Error",
1652+
"code": 2518
1653+
},
1654+
"A 'this'-based type predicate is only allowed within a class or interface's members, get accessors, or return type positions for functions and methods.": {
1655+
"category": "Error",
1656+
"code": 2519
1657+
},
16501658
"Duplicate identifier '{0}'. Compiler uses declaration '{1}' to support async functions.": {
16511659
"category": "Error",
16521660
"code": 2520

src/compiler/emitter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5260,11 +5260,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
52605260
writeLine();
52615261
emitToken(SyntaxKind.CloseBraceToken, node.members.end);
52625262
emitStart(node);
5263-
write(")(");
5263+
write("(");
52645264
if (baseTypeNode) {
52655265
emit(baseTypeNode.expression);
52665266
}
5267-
write(")");
5267+
write("))");
52685268
if (node.kind === SyntaxKind.ClassDeclaration) {
52695269
write(";");
52705270
}

src/compiler/parser.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,11 +1963,7 @@ namespace ts {
19631963
function parseTypeReferenceOrTypePredicate(): TypeReferenceNode | TypePredicateNode {
19641964
const typeName = parseEntityName(/*allowReservedWords*/ false, Diagnostics.Type_expected);
19651965
if (typeName.kind === SyntaxKind.Identifier && token === SyntaxKind.IsKeyword && !scanner.hasPrecedingLineBreak()) {
1966-
nextToken();
1967-
const node = <TypePredicateNode>createNode(SyntaxKind.TypePredicate, typeName.pos);
1968-
node.parameterName = <Identifier>typeName;
1969-
node.type = parseType();
1970-
return finishNode(node);
1966+
return parseTypePredicate(typeName as Identifier);
19711967
}
19721968
const node = <TypeReferenceNode>createNode(SyntaxKind.TypeReference, typeName.pos);
19731969
node.typeName = typeName;
@@ -1977,8 +1973,16 @@ namespace ts {
19771973
return finishNode(node);
19781974
}
19791975

1980-
function parseThisTypeNode(): TypeNode {
1981-
const node = <TypeNode>createNode(SyntaxKind.ThisType);
1976+
function parseTypePredicate(lhs: Identifier | ThisTypeNode): TypePredicateNode {
1977+
nextToken();
1978+
const node = createNode(SyntaxKind.TypePredicate, lhs.pos) as TypePredicateNode;
1979+
node.parameterName = lhs;
1980+
node.type = parseType();
1981+
return finishNode(node);
1982+
}
1983+
1984+
function parseThisTypeNode(): ThisTypeNode {
1985+
const node = createNode(SyntaxKind.ThisType) as ThisTypeNode;
19821986
nextToken();
19831987
return finishNode(node);
19841988
}
@@ -2424,8 +2428,15 @@ namespace ts {
24242428
return parseStringLiteralTypeNode();
24252429
case SyntaxKind.VoidKeyword:
24262430
return parseTokenNode<TypeNode>();
2427-
case SyntaxKind.ThisKeyword:
2428-
return parseThisTypeNode();
2431+
case SyntaxKind.ThisKeyword: {
2432+
const thisKeyword = parseThisTypeNode();
2433+
if (token === SyntaxKind.IsKeyword && !scanner.hasPrecedingLineBreak()) {
2434+
return parseTypePredicate(thisKeyword);
2435+
}
2436+
else {
2437+
return thisKeyword;
2438+
}
2439+
}
24292440
case SyntaxKind.TypeOfKeyword:
24302441
return parseTypeQuery();
24312442
case SyntaxKind.OpenBraceToken:

src/compiler/sys.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ namespace ts {
5151
args: string[];
5252
currentDirectory: string;
5353
executingFile: string;
54+
newLine?: string;
55+
useCaseSensitiveFileNames?: boolean;
5456
echo(s: string): void;
5557
quit(exitCode?: number): void;
5658
fileExists(path: string): boolean;
@@ -60,6 +62,8 @@ namespace ts {
6062
readFile(path: string): string;
6163
writeFile(path: string, contents: string): void;
6264
readDirectory(path: string, extension?: string, exclude?: string[]): string[];
65+
watchFile?(path: string, callback: (path: string, removed?: boolean) => void): FileWatcher;
66+
watchDirectory?(path: string, callback: (path: string) => void, recursive?: boolean): FileWatcher;
6367
};
6468

6569
export var sys: System = (function () {
@@ -411,11 +415,6 @@ namespace ts {
411415
// and is more efficient than `fs.watchFile` (ref: https://github.com/nodejs/node/pull/2649
412416
// and https://github.com/Microsoft/TypeScript/issues/4643), therefore
413417
// if the current node.js version is newer than 4, use `fs.watch` instead.
414-
if (isNode4OrLater()) {
415-
// Note: in node the callback of fs.watch is given only the relative file name as a parameter
416-
return _fs.watch(fileName, (eventName: string, relativeFileName: string) => callback(fileName));
417-
}
418-
419418
const watchedFile = watchedFileSet.addFile(fileName, callback);
420419
return {
421420
close: () => watchedFileSet.removeFile(watchedFile)
@@ -474,9 +473,9 @@ namespace ts {
474473
function getChakraSystem(): System {
475474

476475
return {
477-
newLine: "\r\n",
476+
newLine: ChakraHost.newLine || "\r\n",
478477
args: ChakraHost.args,
479-
useCaseSensitiveFileNames: false,
478+
useCaseSensitiveFileNames: !!ChakraHost.useCaseSensitiveFileNames,
480479
write: ChakraHost.echo,
481480
readFile(path: string, encoding?: string) {
482481
// encoding is automatically handled by the implementation in ChakraHost

src/compiler/types.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -733,11 +733,15 @@ namespace ts {
733733
// @kind(SyntaxKind.StringKeyword)
734734
// @kind(SyntaxKind.SymbolKeyword)
735735
// @kind(SyntaxKind.VoidKeyword)
736-
// @kind(SyntaxKind.ThisType)
737736
export interface TypeNode extends Node {
738737
_typeNodeBrand: any;
739738
}
740739

740+
// @kind(SyntaxKind.ThisType)
741+
export interface ThisTypeNode extends TypeNode {
742+
_thisTypeNodeBrand: any;
743+
}
744+
741745
export interface FunctionOrConstructorTypeNode extends TypeNode, SignatureDeclaration {
742746
_functionOrConstructorTypeNodeBrand: any;
743747
}
@@ -756,7 +760,7 @@ namespace ts {
756760

757761
// @kind(SyntaxKind.TypePredicate)
758762
export interface TypePredicateNode extends TypeNode {
759-
parameterName: Identifier;
763+
parameterName: Identifier | ThisTypeNode;
760764
type: TypeNode;
761765
}
762766

@@ -1820,10 +1824,25 @@ namespace ts {
18201824
CannotBeNamed
18211825
}
18221826

1827+
export const enum TypePredicateKind {
1828+
This,
1829+
Identifier
1830+
}
1831+
18231832
export interface TypePredicate {
1833+
kind: TypePredicateKind;
1834+
type: Type;
1835+
}
1836+
1837+
// @kind (TypePredicateKind.This)
1838+
export interface ThisTypePredicate extends TypePredicate {
1839+
_thisTypePredicateBrand: any;
1840+
}
1841+
1842+
// @kind (TypePredicateKind.Identifier)
1843+
export interface IdentifierTypePredicate extends TypePredicate {
18241844
parameterName: string;
18251845
parameterIndex: number;
1826-
type: Type;
18271846
}
18281847

18291848
/* @internal */
@@ -2047,7 +2066,6 @@ namespace ts {
20472066
resolvedSymbol?: Symbol; // Cached name resolution result
20482067
flags?: NodeCheckFlags; // Set of flags specific to Node
20492068
enumMemberValue?: number; // Constant value of enum member
2050-
isIllegalTypeReferenceInConstraint?: boolean; // Is type reference in constraint refers to the type parameter from the same list
20512069
isVisible?: boolean; // Is this node visible
20522070
generatedName?: string; // Generated name for module, enum, or import declaration
20532071
generatedNames?: Map<string>; // Generated names table for source file
@@ -2091,6 +2109,7 @@ namespace ts {
20912109
ESSymbol = 0x01000000, // Type of symbol primitive introduced in ES6
20922110
ThisType = 0x02000000, // This type
20932111
ObjectLiteralPatternWithComputedProperties = 0x04000000, // Object literal type implied by binding pattern has computed properties
2112+
PredicateType = 0x08000000, // Predicate types are also Boolean types, but should not be considered Intrinsics - there's no way to capture this with flags
20942113

20952114
/* @internal */
20962115
Intrinsic = Any | String | Number | Boolean | ESSymbol | Void | Undefined | Null,
@@ -2102,7 +2121,7 @@ namespace ts {
21022121
UnionOrIntersection = Union | Intersection,
21032122
StructuredType = ObjectType | Union | Intersection,
21042123
/* @internal */
2105-
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral,
2124+
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral | PredicateType,
21062125
/* @internal */
21072126
PropagatingFlags = ContainsUndefinedOrNull | ContainsObjectLiteral | ContainsAnyFunctionType
21082127
}
@@ -2123,6 +2142,11 @@ namespace ts {
21232142
intrinsicName: string; // Name of intrinsic type
21242143
}
21252144

2145+
// Predicate types (TypeFlags.Predicate)
2146+
export interface PredicateType extends Type {
2147+
predicate: ThisTypePredicate | IdentifierTypePredicate;
2148+
}
2149+
21262150
// String literal types (TypeFlags.StringLiteral)
21272151
export interface StringLiteralType extends Type {
21282152
text: string; // Text of string literal
@@ -2239,7 +2263,6 @@ namespace ts {
22392263
declaration: SignatureDeclaration; // Originating declaration
22402264
typeParameters: TypeParameter[]; // Type parameters (undefined if non-generic)
22412265
parameters: Symbol[]; // Parameters
2242-
typePredicate?: TypePredicate; // Type predicate
22432266
/* @internal */
22442267
resolvedReturnType: Type; // Resolved return type
22452268
/* @internal */
@@ -2288,6 +2311,7 @@ namespace ts {
22882311
inferUnionTypes: boolean; // Infer union types for disjoint candidates (otherwise undefinedType)
22892312
inferences: TypeInferences[]; // Inferences made for each type parameter
22902313
inferredTypes: Type[]; // Inferred type for each type parameter
2314+
mapper?: TypeMapper; // Type mapper for this inference context
22912315
failedTypeParameterIndex?: number; // Index of type parameter for which inference failed
22922316
// It is optional because in contextual signature instantiation, nothing fails
22932317
}

src/compiler/utilities.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ namespace ts {
342342
case SyntaxKind.EnumMember:
343343
case SyntaxKind.FunctionDeclaration:
344344
case SyntaxKind.FunctionExpression:
345+
case SyntaxKind.MethodDeclaration:
345346
errorNode = (<Declaration>node).name;
346347
break;
347348
}
@@ -692,6 +693,10 @@ namespace ts {
692693
return node && node.kind === SyntaxKind.MethodDeclaration && node.parent.kind === SyntaxKind.ObjectLiteralExpression;
693694
}
694695

696+
export function isIdentifierTypePredicate(predicate: TypePredicate): predicate is IdentifierTypePredicate {
697+
return predicate && predicate.kind === TypePredicateKind.Identifier;
698+
}
699+
695700
export function getContainingFunction(node: Node): FunctionLikeDeclaration {
696701
while (true) {
697702
node = node.parent;

tests/baselines/reference/2dArrays.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ var Cell = (function () {
2020
function Cell() {
2121
}
2222
return Cell;
23-
})();
23+
}());
2424
var Ship = (function () {
2525
function Ship() {
2626
}
2727
return Ship;
28-
})();
28+
}());
2929
var Board = (function () {
3030
function Board() {
3131
}
3232
Board.prototype.allShipsSunk = function () {
3333
return this.ships.every(function (val) { return val.isSunk; });
3434
};
3535
return Board;
36-
})();
36+
}());

0 commit comments

Comments
 (0)