Skip to content

Commit de23e2f

Browse files
committed
Merge branch 'master' into constructorAccessibility
Conflicts: src/compiler/checker.ts
2 parents ba8b168 + 3820dfa commit de23e2f

File tree

105 files changed

+1944
-1016
lines changed

Some content is hidden

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

105 files changed

+1944
-1016
lines changed

src/compiler/checker.ts

+189-151
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@
687687
"category": "Error",
688688
"code": 1218
689689
},
690-
"Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning.": {
690+
"Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.": {
691691
"category": "Error",
692692
"code": 1219
693693
},
@@ -723,6 +723,10 @@
723723
"category": "Error",
724724
"code": 1227
725725
},
726+
"A type predicate is only allowed in return type position for functions and methods.": {
727+
"category": "Error",
728+
"code": 1228
729+
},
726730
"A type predicate cannot reference a rest parameter.": {
727731
"category": "Error",
728732
"code": 1229
@@ -2745,11 +2749,6 @@
27452749
"category": "Error",
27462750
"code": 8016
27472751
},
2748-
"'decorators' can only be used in a .ts file.": {
2749-
"category": "Error",
2750-
"code": 8017
2751-
},
2752-
27532752
"Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses.": {
27542753
"category": "Error",
27552754
"code": 9002

src/compiler/parser.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,7 @@ namespace ts {
19371937
return finishNode(node);
19381938
}
19391939

1940-
function parseTypePredicate(lhs: Identifier | ThisTypeNode): TypePredicateNode {
1940+
function parseThisTypePredicate(lhs: ThisTypeNode): TypePredicateNode {
19411941
nextToken();
19421942
const node = createNode(SyntaxKind.TypePredicate, lhs.pos) as TypePredicateNode;
19431943
node.parameterName = lhs;
@@ -2362,7 +2362,7 @@ namespace ts {
23622362
case SyntaxKind.ThisKeyword: {
23632363
const thisKeyword = parseThisTypeNode();
23642364
if (token === SyntaxKind.IsKeyword && !scanner.hasPrecedingLineBreak()) {
2365-
return parseTypePredicate(thisKeyword);
2365+
return parseThisTypePredicate(thisKeyword);
23662366
}
23672367
else {
23682368
return thisKeyword;

src/compiler/program.ts

+20-11
Original file line numberDiff line numberDiff line change
@@ -533,18 +533,25 @@ namespace ts {
533533
}
534534

535535
let referencedSourceFile: string;
536-
while (true) {
537-
const searchName = normalizePath(combinePaths(containingDirectory, moduleName));
538-
referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state);
539-
if (referencedSourceFile) {
540-
break;
541-
}
542-
const parentPath = getDirectoryPath(containingDirectory);
543-
if (parentPath === containingDirectory) {
544-
break;
536+
if (moduleHasNonRelativeName(moduleName)) {
537+
while (true) {
538+
const searchName = normalizePath(combinePaths(containingDirectory, moduleName));
539+
referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state);
540+
if (referencedSourceFile) {
541+
break;
542+
}
543+
const parentPath = getDirectoryPath(containingDirectory);
544+
if (parentPath === containingDirectory) {
545+
break;
546+
}
547+
containingDirectory = parentPath;
545548
}
546-
containingDirectory = parentPath;
547549
}
550+
else {
551+
const candidate = normalizePath(combinePaths(containingDirectory, moduleName));
552+
referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state);
553+
}
554+
548555

549556
return referencedSourceFile
550557
? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations }
@@ -1169,7 +1176,9 @@ namespace ts {
11691176
diagnostics.push(createDiagnosticForNode(typeAssertionExpression.type, Diagnostics.type_assertion_expressions_can_only_be_used_in_a_ts_file));
11701177
return true;
11711178
case SyntaxKind.Decorator:
1172-
diagnostics.push(createDiagnosticForNode(node, Diagnostics.decorators_can_only_be_used_in_a_ts_file));
1179+
if (!options.experimentalDecorators) {
1180+
diagnostics.push(createDiagnosticForNode(node, Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalDecorators_option_to_remove_this_warning));
1181+
}
11731182
return true;
11741183
}
11751184

src/compiler/tsconfig.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
{
22
"compilerOptions": {
3-
"module": "commonjs",
43
"noImplicitAny": true,
54
"removeComments": true,
65
"preserveConstEnums": true,
76
"out": "../../built/local/tsc.js",
87
"sourceMap": true
98
},
109
"files": [
10+
"types.ts",
1111
"core.ts",
1212
"sys.ts",
13-
"types.ts",
1413
"diagnosticInformationMap.generated.ts",
1514
"scanner.ts",
1615
"parser.ts",

src/compiler/types.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,6 @@ namespace ts {
426426
IntrinsicElement = IntrinsicNamedElement | IntrinsicIndexedElement,
427427
}
428428

429-
430429
/* @internal */
431430
export const enum RelationComparisonResult {
432431
Succeeded = 1, // Should be truthy
@@ -1749,6 +1748,7 @@ namespace ts {
17491748
buildSignatureDisplay(signatures: Signature, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags, kind?: SignatureKind): void;
17501749
buildParameterDisplay(parameter: Symbol, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void;
17511750
buildTypeParameterDisplay(tp: TypeParameter, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void;
1751+
buildTypePredicateDisplay(predicate: TypePredicate, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void;
17521752
buildTypeParameterDisplayFromSymbol(symbol: Symbol, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void;
17531753
buildDisplayForParametersAndDelimiters(parameters: Symbol[], writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void;
17541754
buildDisplayForTypeParametersAndDelimiters(typeParameters: TypeParameter[], writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void;
@@ -1814,22 +1814,24 @@ namespace ts {
18141814
Identifier
18151815
}
18161816

1817-
export interface TypePredicate {
1817+
export interface TypePredicateBase {
18181818
kind: TypePredicateKind;
18191819
type: Type;
18201820
}
18211821

18221822
// @kind (TypePredicateKind.This)
1823-
export interface ThisTypePredicate extends TypePredicate {
1823+
export interface ThisTypePredicate extends TypePredicateBase {
18241824
_thisTypePredicateBrand: any;
18251825
}
18261826

18271827
// @kind (TypePredicateKind.Identifier)
1828-
export interface IdentifierTypePredicate extends TypePredicate {
1828+
export interface IdentifierTypePredicate extends TypePredicateBase {
18291829
parameterName: string;
18301830
parameterIndex: number;
18311831
}
18321832

1833+
export type TypePredicate = IdentifierTypePredicate | ThisTypePredicate;
1834+
18331835
/* @internal */
18341836
export type AnyImportSyntax = ImportDeclaration | ImportEqualsDeclaration;
18351837

@@ -2037,10 +2039,9 @@ namespace ts {
20372039
LoopWithCapturedBlockScopedBinding = 0x00010000, // Loop that contains block scoped variable captured in closure
20382040
CapturedBlockScopedBinding = 0x00020000, // Block-scoped binding that is captured in some function
20392041
BlockScopedBindingInLoop = 0x00040000, // Block-scoped binding with declaration nested inside iteration statement
2040-
HasSeenSuperCall = 0x00080000, // Set during the binding when encounter 'super'
2041-
ClassWithBodyScopedClassBinding = 0x00100000, // Decorated class that contains a binding to itself inside of the class body.
2042-
BodyScopedClassBinding = 0x00200000, // Binding to a decorated class inside of the class's body.
2043-
NeedsLoopOutParameter = 0x00400000, // Block scoped binding whose value should be explicitly copied outside of the converted loop
2042+
ClassWithBodyScopedClassBinding = 0x00080000, // Decorated class that contains a binding to itself inside of the class body.
2043+
BodyScopedClassBinding = 0x00100000, // Binding to a decorated class inside of the class's body.
2044+
NeedsLoopOutParameter = 0x00200000, // Block scoped binding whose value should be explicitly copied outside of the converted loop
20442045
}
20452046

20462047
/* @internal */
@@ -2060,6 +2061,8 @@ namespace ts {
20602061
importOnRightSide?: Symbol; // for import declarations - import that appear on the right side
20612062
jsxFlags?: JsxFlags; // flags for knowing what kind of element/attributes we're dealing with
20622063
resolvedJsxType?: Type; // resolved element attributes type of a JSX openinglike element
2064+
hasSuperCall?: boolean; // recorded result when we try to find super-call. We only try to find one if this flag is undefined, indicating that we haven't made an attempt.
2065+
superCall?: ExpressionStatement; // Cached first super-call found in the constructor. Used in checking whether super is called before this-accessing
20632066
}
20642067

20652068
export const enum TypeFlags {
@@ -2095,7 +2098,6 @@ namespace ts {
20952098
ESSymbol = 0x01000000, // Type of symbol primitive introduced in ES6
20962099
ThisType = 0x02000000, // This type
20972100
ObjectLiteralPatternWithComputedProperties = 0x04000000, // Object literal type implied by binding pattern has computed properties
2098-
PredicateType = 0x08000000, // Predicate types are also Boolean types, but should not be considered Intrinsics - there's no way to capture this with flags
20992101

21002102
/* @internal */
21012103
Intrinsic = Any | String | Number | Boolean | ESSymbol | Void | Undefined | Null,
@@ -2107,7 +2109,7 @@ namespace ts {
21072109
UnionOrIntersection = Union | Intersection,
21082110
StructuredType = ObjectType | Union | Intersection,
21092111
/* @internal */
2110-
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral | PredicateType,
2112+
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral,
21112113
/* @internal */
21122114
PropagatingFlags = ContainsUndefinedOrNull | ContainsObjectLiteral | ContainsAnyFunctionType
21132115
}
@@ -2128,11 +2130,6 @@ namespace ts {
21282130
intrinsicName: string; // Name of intrinsic type
21292131
}
21302132

2131-
// Predicate types (TypeFlags.Predicate)
2132-
export interface PredicateType extends Type {
2133-
predicate: ThisTypePredicate | IdentifierTypePredicate;
2134-
}
2135-
21362133
// String literal types (TypeFlags.StringLiteral)
21372134
export interface StringLiteralType extends Type {
21382135
text: string; // Text of string literal
@@ -2267,6 +2264,8 @@ namespace ts {
22672264
erasedSignatureCache?: Signature; // Erased version of signature (deferred)
22682265
/* @internal */
22692266
isolatedSignatureType?: ObjectType; // A manufactured type that just contains the signature for purposes of signature comparison
2267+
/* @internal */
2268+
typePredicate?: TypePredicate;
22702269
}
22712270

22722271
export const enum IndexKind {

src/compiler/utilities.ts

+4
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,10 @@ namespace ts {
748748
return predicate && predicate.kind === TypePredicateKind.Identifier;
749749
}
750750

751+
export function isThisTypePredicate(predicate: TypePredicate): predicate is ThisTypePredicate {
752+
return predicate && predicate.kind === TypePredicateKind.This;
753+
}
754+
751755
export function getContainingFunction(node: Node): FunctionLikeDeclaration {
752756
while (true) {
753757
node = node.parent;

src/server/tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"compilerOptions": {
3-
"module": "commonjs",
43
"noImplicitAny": true,
54
"removeComments": true,
65
"preserveConstEnums": true,

src/services/tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"compilerOptions": {
3-
"module": "commonjs",
43
"noImplicitAny": true,
54
"removeComments": true,
65
"preserveConstEnums": true,

tests/baselines/reference/arrayBufferIsViewNarrowsType.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var obj: Object;
44
>Object : Object
55

66
if (ArrayBuffer.isView(obj)) {
7-
>ArrayBuffer.isView(obj) : arg is ArrayBufferView
7+
>ArrayBuffer.isView(obj) : boolean
88
>ArrayBuffer.isView : (arg: any) => arg is ArrayBufferView
99
>ArrayBuffer : ArrayBufferConstructor
1010
>isView : (arg: any) => arg is ArrayBufferView

tests/baselines/reference/classExtendsNull.errors.txt

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
tests/cases/compiler/classExtendsNull.ts(2,5): error TS17005: A constructor cannot contain a 'super' call when its class extends 'null'
1+
tests/cases/compiler/classExtendsNull.ts(3,9): error TS17005: A constructor cannot contain a 'super' call when its class extends 'null'
22

33

44
==== tests/cases/compiler/classExtendsNull.ts (1 errors) ====
55
class C extends null {
66
constructor() {
7-
~~~~~~~~~~~~~~~
87
super();
9-
~~~~~~~~~~~~~~~~
8+
~~~~~~~
9+
!!! error TS17005: A constructor cannot contain a 'super' call when its class extends 'null'
1010
return Object.create(null);
11-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1211
}
13-
~~~~~
14-
!!! error TS17005: A constructor cannot contain a 'super' call when its class extends 'null'
1512
}
1613

1714
class D extends null {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [declarationEmitIdentifierPredicates01.ts]
2+
3+
export function f(x: any): x is number {
4+
return typeof x === "number";
5+
}
6+
7+
//// [declarationEmitIdentifierPredicates01.js]
8+
"use strict";
9+
function f(x) {
10+
return typeof x === "number";
11+
}
12+
exports.f = f;
13+
14+
15+
//// [declarationEmitIdentifierPredicates01.d.ts]
16+
export declare function f(x: any): x is number;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicates01.ts ===
2+
3+
export function f(x: any): x is number {
4+
>f : Symbol(f, Decl(declarationEmitIdentifierPredicates01.ts, 0, 0))
5+
>x : Symbol(x, Decl(declarationEmitIdentifierPredicates01.ts, 1, 18))
6+
>x : Symbol(x, Decl(declarationEmitIdentifierPredicates01.ts, 1, 18))
7+
8+
return typeof x === "number";
9+
>x : Symbol(x, Decl(declarationEmitIdentifierPredicates01.ts, 1, 18))
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicates01.ts ===
2+
3+
export function f(x: any): x is number {
4+
>f : (x: any) => x is number
5+
>x : any
6+
>x : any
7+
8+
return typeof x === "number";
9+
>typeof x === "number" : boolean
10+
>typeof x : string
11+
>x : any
12+
>"number" : string
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicatesWithPrivateName01.ts(6,33): error TS4060: Return type of exported function has or is using private name 'I'.
2+
3+
4+
==== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicatesWithPrivateName01.ts (1 errors) ====
5+
6+
interface I {
7+
a: number;
8+
}
9+
10+
export function f(x: any): x is I {
11+
~
12+
!!! error TS4060: Return type of exported function has or is using private name 'I'.
13+
return typeof x.a === "number";
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [declarationEmitIdentifierPredicatesWithPrivateName01.ts]
2+
3+
interface I {
4+
a: number;
5+
}
6+
7+
export function f(x: any): x is I {
8+
return typeof x.a === "number";
9+
}
10+
11+
//// [declarationEmitIdentifierPredicatesWithPrivateName01.js]
12+
"use strict";
13+
function f(x) {
14+
return typeof x.a === "number";
15+
}
16+
exports.f = f;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//// [declarationEmitThisPredicates01.ts]
2+
3+
export class C {
4+
m(): this is D {
5+
return this instanceof D;
6+
}
7+
}
8+
9+
export class D extends C {
10+
}
11+
12+
//// [declarationEmitThisPredicates01.js]
13+
"use strict";
14+
var __extends = (this && this.__extends) || function (d, b) {
15+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
16+
function __() { this.constructor = d; }
17+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
18+
};
19+
var C = (function () {
20+
function C() {
21+
}
22+
C.prototype.m = function () {
23+
return this instanceof D;
24+
};
25+
return C;
26+
}());
27+
exports.C = C;
28+
var D = (function (_super) {
29+
__extends(D, _super);
30+
function D() {
31+
_super.apply(this, arguments);
32+
}
33+
return D;
34+
}(C));
35+
exports.D = D;
36+
37+
38+
//// [declarationEmitThisPredicates01.d.ts]
39+
export declare class C {
40+
m(): this is D;
41+
}
42+
export declare class D extends C {
43+
}

0 commit comments

Comments
 (0)