Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Microsoft/TypeScript into…
Browse files Browse the repository at this point in the history
… removeSubtypesRecursion
  • Loading branch information
JsonFreeman committed May 7, 2015
2 parents bb7f617 + 5129d7c commit 2425310
Show file tree
Hide file tree
Showing 20 changed files with 563 additions and 65 deletions.
56 changes: 31 additions & 25 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,10 +857,11 @@ module ts {
return symbol;
}
}
let fileName: string;
let sourceFile: SourceFile;
while (true) {
let fileName = normalizePath(combinePaths(searchPath, moduleName));
sourceFile = host.getSourceFile(fileName + ".ts") || host.getSourceFile(fileName + ".d.ts");
fileName = normalizePath(combinePaths(searchPath, moduleName));
sourceFile = forEach(supportedExtensions, extension => host.getSourceFile(fileName + extension));
if (sourceFile || isRelative) {
break;
}
Expand Down Expand Up @@ -5398,38 +5399,43 @@ module ts {
if (!isTypeSubtypeOf(rightType, globalFunctionType)) {
return type;
}
// Target type is type of prototype property

let targetType: Type;
let prototypeProperty = getPropertyOfType(rightType, "prototype");
if (prototypeProperty) {
let targetType = getTypeOfSymbol(prototypeProperty);
if (targetType !== anyType) {
// Narrow to the target type if it's a subtype of the current type
if (isTypeSubtypeOf(targetType, type)) {
return targetType;
}
// If the current type is a union type, remove all constituents that aren't subtypes of the target.
if (type.flags & TypeFlags.Union) {
return getUnionType(filter((<UnionType>type).types, t => isTypeSubtypeOf(t, targetType)));
}
// Target type is type of the protoype property
let prototypePropertyType = getTypeOfSymbol(prototypeProperty);
if (prototypePropertyType !== anyType) {
targetType = prototypePropertyType;
}
}
// Target type is type of construct signature
let constructSignatures: Signature[];
if (rightType.flags & TypeFlags.Interface) {
constructSignatures = resolveDeclaredMembers(<InterfaceType>rightType).declaredConstructSignatures;
}
else if (rightType.flags & TypeFlags.Anonymous) {
constructSignatures = getSignaturesOfType(rightType, SignatureKind.Construct);

if (!targetType) {
// Target type is type of construct signature
let constructSignatures: Signature[];
if (rightType.flags & TypeFlags.Interface) {
constructSignatures = resolveDeclaredMembers(<InterfaceType>rightType).declaredConstructSignatures;
}
else if (rightType.flags & TypeFlags.Anonymous) {
constructSignatures = getSignaturesOfType(rightType, SignatureKind.Construct);
}

if (constructSignatures && constructSignatures.length) {
targetType = getUnionType(map(constructSignatures, signature => getReturnTypeOfSignature(getErasedSignature(signature))));
}
}

if (constructSignatures && constructSignatures.length !== 0) {
let instanceType = getUnionType(map(constructSignatures, signature => getReturnTypeOfSignature(getErasedSignature(signature))));
// Pickup type from union types
if (targetType) {
// Narrow to the target type if it's a subtype of the current type
if (isTypeSubtypeOf(targetType, type)) {
return targetType;
}
// If the current type is a union type, remove all constituents that aren't subtypes of the target.
if (type.flags & TypeFlags.Union) {
return getUnionType(filter((<UnionType>type).types, t => isTypeSubtypeOf(t, instanceType)));
return getUnionType(filter((<UnionType>type).types, t => isTypeSubtypeOf(t, targetType)));
}
return instanceType;
}

return type;
}

Expand Down
10 changes: 6 additions & 4 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,16 +640,18 @@ module ts {
return pathLen > extLen && path.substr(pathLen - extLen, extLen) === extension;
}

let supportedExtensions = [".d.ts", ".ts", ".js"];
/**
* List of supported extensions in order of file resolution precedence.
*/
export const supportedExtensions = [".ts", ".d.ts"];

const extensionsToRemove = [".d.ts", ".ts", ".js"];
export function removeFileExtension(path: string): string {
for (let ext of supportedExtensions) {

for (let ext of extensionsToRemove) {
if (fileExtensionIs(path, ext)) {
return path.substr(0, path.length - ext.length);
}
}

return path;
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ module ts {
Corrupted_locale_file_0: { code: 6051, category: DiagnosticCategory.Error, key: "Corrupted locale file {0}." },
Raise_error_on_expressions_and_declarations_with_an_implied_any_type: { code: 6052, category: DiagnosticCategory.Message, key: "Raise error on expressions and declarations with an implied 'any' type." },
File_0_not_found: { code: 6053, category: DiagnosticCategory.Error, key: "File '{0}' not found." },
File_0_must_have_extension_ts_or_d_ts: { code: 6054, category: DiagnosticCategory.Error, key: "File '{0}' must have extension '.ts' or '.d.ts'." },
File_0_has_unsupported_extension_The_only_supported_extensions_are_1: { code: 6054, category: DiagnosticCategory.Error, key: "File '{0}' has unsupported extension. The only supported extensions are {1}." },
Suppress_noImplicitAny_errors_for_indexing_objects_lacking_index_signatures: { code: 6055, category: DiagnosticCategory.Message, key: "Suppress noImplicitAny errors for indexing objects lacking index signatures." },
Do_not_emit_declarations_for_code_that_has_an_internal_annotation: { code: 6056, category: DiagnosticCategory.Message, key: "Do not emit declarations for code that has an '@internal' annotation." },
Preserve_new_lines_when_emitting_code: { code: 6057, category: DiagnosticCategory.Message, key: "Preserve new-lines when emitting code." },
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1982,7 +1982,7 @@
"category": "Error",
"code": 6053
},
"File '{0}' must have extension '.ts' or '.d.ts'.": {
"File '{0}' has unsupported extension. The only supported extensions are {1}.": {
"category": "Error",
"code": 6054
},
Expand Down
27 changes: 16 additions & 11 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,38 +307,45 @@ module ts {
function processSourceFile(fileName: string, isDefaultLib: boolean, refFile?: SourceFile, refPos?: number, refEnd?: number) {
let start: number;
let length: number;
let extensions: string;
let diagnosticArgument: string[];
if (refEnd !== undefined && refPos !== undefined) {
start = refPos;
length = refEnd - refPos;
}
let diagnostic: DiagnosticMessage;
if (hasExtension(fileName)) {
if (!options.allowNonTsExtensions && !fileExtensionIs(host.getCanonicalFileName(fileName), ".ts")) {
diagnostic = Diagnostics.File_0_must_have_extension_ts_or_d_ts;
if (!options.allowNonTsExtensions && !forEach(supportedExtensions, extension => fileExtensionIs(host.getCanonicalFileName(fileName), extension))) {
diagnostic = Diagnostics.File_0_has_unsupported_extension_The_only_supported_extensions_are_1;
diagnosticArgument = [fileName, "'" + supportedExtensions.join("', '") + "'"];
}
else if (!findSourceFile(fileName, isDefaultLib, refFile, refPos, refEnd)) {
diagnostic = Diagnostics.File_0_not_found;
diagnosticArgument = [fileName];
}
else if (refFile && host.getCanonicalFileName(fileName) === host.getCanonicalFileName(refFile.fileName)) {
diagnostic = Diagnostics.A_file_cannot_have_a_reference_to_itself;
diagnosticArgument = [fileName];
}
}
else {
if (options.allowNonTsExtensions && !findSourceFile(fileName, isDefaultLib, refFile, refPos, refEnd)) {
diagnostic = Diagnostics.File_0_not_found;
diagnosticArgument = [fileName];
}
else if (!findSourceFile(fileName + ".ts", isDefaultLib, refFile, refPos, refEnd) && !findSourceFile(fileName + ".d.ts", isDefaultLib, refFile, refPos, refEnd)) {
else if (!forEach(supportedExtensions, extension => findSourceFile(fileName + extension, isDefaultLib, refFile, refPos, refEnd))) {
diagnostic = Diagnostics.File_0_not_found;
fileName += ".ts";
diagnosticArgument = [fileName];
}
}

if (diagnostic) {
if (refFile) {
diagnostics.add(createFileDiagnostic(refFile, start, length, diagnostic, fileName));
diagnostics.add(createFileDiagnostic(refFile, start, length, diagnostic, ...diagnosticArgument));
}
else {
diagnostics.add(createCompilerDiagnostic(diagnostic, fileName));
diagnostics.add(createCompilerDiagnostic(diagnostic, ...diagnosticArgument));
}
}
}
Expand Down Expand Up @@ -417,9 +424,10 @@ module ts {
let moduleNameText = (<LiteralExpression>moduleNameExpr).text;
if (moduleNameText) {
let searchPath = basePath;
let searchName: string;
while (true) {
let searchName = normalizePath(combinePaths(searchPath, moduleNameText));
if (findModuleSourceFile(searchName + ".ts", moduleNameExpr) || findModuleSourceFile(searchName + ".d.ts", moduleNameExpr)) {
searchName = normalizePath(combinePaths(searchPath, moduleNameText));
if (forEach(supportedExtensions, extension => findModuleSourceFile(searchName + extension, moduleNameExpr))) {
break;
}
let parentPath = getDirectoryPath(searchPath);
Expand Down Expand Up @@ -448,10 +456,7 @@ module ts {
// An ExternalImportDeclaration in anAmbientExternalModuleDeclaration may reference other external modules
// only through top - level external module names. Relative external module names are not permitted.
let searchName = normalizePath(combinePaths(basePath, moduleName));
let tsFile = findModuleSourceFile(searchName + ".ts", nameLiteral);
if (!tsFile) {
findModuleSourceFile(searchName + ".d.ts", nameLiteral);
}
forEach(supportedExtensions, extension => findModuleSourceFile(searchName + extension, nameLiteral));
}
}
});
Expand Down
3 changes: 0 additions & 3 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,6 @@ module ts {
case SyntaxKind.IndexSignature:
case SyntaxKind.FunctionType:
case SyntaxKind.ConstructorType:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrowFunction:
case SyntaxKind.FunctionDeclaration:
return true;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/es6.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3580,6 +3580,7 @@ interface PromiseLike<T> {
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): PromiseLike<TResult>;
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): PromiseLike<TResult>;
}

/**
Expand All @@ -3593,6 +3594,7 @@ interface Promise<T> {
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>;
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): Promise<TResult>;

/**
* Attaches a callback for only the rejection of the Promise.
Expand Down
53 changes: 53 additions & 0 deletions tests/baselines/reference/narrowTypeByInstanceof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//// [narrowTypeByInstanceof.ts]
class Match {
public range(): any {
return undefined;
}
}

class FileMatch {
public resource(): any {
return undefined;
}
}

type FileMatchOrMatch = FileMatch | Match;


let elementA: FileMatchOrMatch, elementB: FileMatchOrMatch;

if (elementA instanceof FileMatch && elementB instanceof FileMatch) {
let a = elementA.resource().path;
let b = elementB.resource().path;
} else if (elementA instanceof Match && elementB instanceof Match) {
let a = elementA.range();
let b = elementB.range();
}


//// [narrowTypeByInstanceof.js]
var Match = (function () {
function Match() {
}
Match.prototype.range = function () {
return undefined;
};
return Match;
})();
var FileMatch = (function () {
function FileMatch() {
}
FileMatch.prototype.resource = function () {
return undefined;
};
return FileMatch;
})();
var elementA, elementB;
if (elementA instanceof FileMatch && elementB instanceof FileMatch) {
var a = elementA.resource().path;
var b = elementB.resource().path;
}
else if (elementA instanceof Match && elementB instanceof Match) {
var a = elementA.range();
var b = elementB.range();
}
72 changes: 72 additions & 0 deletions tests/baselines/reference/narrowTypeByInstanceof.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
=== tests/cases/compiler/narrowTypeByInstanceof.ts ===
class Match {
>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0))

public range(): any {
>range : Symbol(range, Decl(narrowTypeByInstanceof.ts, 0, 17))

return undefined;
>undefined : Symbol(undefined)
}
}

class FileMatch {
>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5))

public resource(): any {
>resource : Symbol(resource, Decl(narrowTypeByInstanceof.ts, 6, 21))

return undefined;
>undefined : Symbol(undefined)
}
}

type FileMatchOrMatch = FileMatch | Match;
>FileMatchOrMatch : Symbol(FileMatchOrMatch, Decl(narrowTypeByInstanceof.ts, 10, 5))
>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5))
>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0))


let elementA: FileMatchOrMatch, elementB: FileMatchOrMatch;
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
>FileMatchOrMatch : Symbol(FileMatchOrMatch, Decl(narrowTypeByInstanceof.ts, 10, 5))
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
>FileMatchOrMatch : Symbol(FileMatchOrMatch, Decl(narrowTypeByInstanceof.ts, 10, 5))

if (elementA instanceof FileMatch && elementB instanceof FileMatch) {
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5))
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5))

let a = elementA.resource().path;
>a : Symbol(a, Decl(narrowTypeByInstanceof.ts, 18, 7))
>elementA.resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21))
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
>resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21))

let b = elementB.resource().path;
>b : Symbol(b, Decl(narrowTypeByInstanceof.ts, 19, 7))
>elementB.resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21))
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
>resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21))

} else if (elementA instanceof Match && elementB instanceof Match) {
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0))
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0))

let a = elementA.range();
>a : Symbol(a, Decl(narrowTypeByInstanceof.ts, 21, 7))
>elementA.range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17))
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
>range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17))

let b = elementB.range();
>b : Symbol(b, Decl(narrowTypeByInstanceof.ts, 22, 7))
>elementB.range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17))
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
>range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17))
}

Loading

0 comments on commit 2425310

Please sign in to comment.