Skip to content

Commit 3bc8c7e

Browse files
committed
Merge branch 'master' into fixMappedTypeCombinedMappers
# Conflicts: # src/compiler/checker.ts
2 parents 80ef89b + ecb2115 commit 3bc8c7e

23 files changed

+934
-1086
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"gulp-insert": "latest",
6161
"gulp-newer": "latest",
6262
"gulp-sourcemaps": "latest",
63-
"gulp-typescript": "latest",
63+
"gulp-typescript": "3.1.3",
6464
"into-stream": "latest",
6565
"istanbul": "latest",
6666
"jake": "latest",

src/compiler/checker.ts

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4606,7 +4606,7 @@ namespace ts {
46064606
// Create a mapper from T to the current iteration type constituent. Then, if the
46074607
// mapped type is itself an instantiated type, combine the iteration mapper with the
46084608
// instantiation mapper.
4609-
const iterationMapper = createUnaryTypeMapper(typeParameter, t);
4609+
const iterationMapper = createTypeMapper([typeParameter], [t]);
46104610
const templateMapper = type.mapper ? combineTypeMappers(type.mapper, iterationMapper) : iterationMapper;
46114611
const propType = instantiateType(templateType, templateMapper);
46124612
// If the current iteration type constituent is a string literal type, create a property.
@@ -4666,7 +4666,7 @@ namespace ts {
46664666
}
46674667

46684668
function getErasedTemplateTypeFromMappedType(type: MappedType) {
4669-
return instantiateType(getTemplateTypeFromMappedType(type), createUnaryTypeMapper(getTypeParameterFromMappedType(type), anyType));
4669+
return instantiateType(getTemplateTypeFromMappedType(type), createTypeEraser([getTypeParameterFromMappedType(type)]));
46704670
}
46714671

46724672
function isGenericMappedType(type: Type) {
@@ -5058,9 +5058,10 @@ namespace ts {
50585058
if (!links.resolvedSignature) {
50595059
const parameters: Symbol[] = [];
50605060
let hasLiteralTypes = false;
5061-
let minArgumentCount = -1;
5061+
let minArgumentCount = 0;
50625062
let thisParameter: Symbol = undefined;
50635063
let hasThisParameter: boolean;
5064+
const iife = getImmediatelyInvokedFunctionExpression(declaration);
50645065
const isJSConstructSignature = isJSDocConstructSignature(declaration);
50655066

50665067
// If this is a JSDoc construct signature, then skip the first parameter in the
@@ -5087,14 +5088,12 @@ namespace ts {
50875088
hasLiteralTypes = true;
50885089
}
50895090

5090-
if (param.initializer || param.questionToken || param.dotDotDotToken || isJSDocOptionalParameter(param)) {
5091-
if (minArgumentCount < 0) {
5092-
minArgumentCount = i - (hasThisParameter ? 1 : 0);
5093-
}
5094-
}
5095-
else {
5096-
// If we see any required parameters, it means the prior ones were not in fact optional.
5097-
minArgumentCount = -1;
5091+
// Record a new minimum argument count if this is not an optional parameter
5092+
const isOptionalParameter = param.initializer || param.questionToken || param.dotDotDotToken ||
5093+
iife && parameters.length > iife.arguments.length && !param.type ||
5094+
isJSDocOptionalParameter(param);
5095+
if (!isOptionalParameter) {
5096+
minArgumentCount = parameters.length;
50985097
}
50995098
}
51005099

@@ -5109,13 +5108,6 @@ namespace ts {
51095108
}
51105109
}
51115110

5112-
if (minArgumentCount < 0) {
5113-
minArgumentCount = declaration.parameters.length - (hasThisParameter ? 1 : 0);
5114-
}
5115-
if (isJSConstructSignature) {
5116-
minArgumentCount--;
5117-
}
5118-
51195111
const classType = declaration.kind === SyntaxKind.Constructor ?
51205112
getDeclaredTypeOfClassOrInterface(getMergedSymbol((<ClassDeclaration>declaration.parent).symbol))
51215113
: undefined;
@@ -6135,7 +6127,7 @@ namespace ts {
61356127
error(accessExpression, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(type));
61366128
return unknownType;
61376129
}
6138-
const mapper = createUnaryTypeMapper(getTypeParameterFromMappedType(type), indexType);
6130+
const mapper = createTypeMapper([getTypeParameterFromMappedType(type)], [indexType]);
61396131
const templateMapper = type.mapper ? combineTypeMappers(type.mapper, mapper) : mapper;
61406132
return instantiateType(getTemplateTypeFromMappedType(type), templateMapper);
61416133
}
@@ -6241,7 +6233,7 @@ namespace ts {
62416233
* this function should be called in a left folding style, with left = previous result of getSpreadType
62426234
* and right = the new element to be spread.
62436235
*/
6244-
function getSpreadType(left: Type, right: Type, isFromObjectLiteral: boolean): Type {
6236+
function getSpreadType(left: Type, right: Type): Type {
62456237
if (left.flags & TypeFlags.Any || right.flags & TypeFlags.Any) {
62466238
return anyType;
62476239
}
@@ -6254,10 +6246,10 @@ namespace ts {
62546246
return left;
62556247
}
62566248
if (left.flags & TypeFlags.Union) {
6257-
return mapType(left, t => getSpreadType(t, right, isFromObjectLiteral));
6249+
return mapType(left, t => getSpreadType(t, right));
62586250
}
62596251
if (right.flags & TypeFlags.Union) {
6260-
return mapType(right, t => getSpreadType(left, t, isFromObjectLiteral));
6252+
return mapType(right, t => getSpreadType(left, t));
62616253
}
62626254

62636255
const members = createMap<Symbol>();
@@ -6276,18 +6268,18 @@ namespace ts {
62766268

62776269
for (const rightProp of getPropertiesOfType(right)) {
62786270
// we approximate own properties as non-methods plus methods that are inside the object literal
6279-
const isOwnProperty = !(rightProp.flags & SymbolFlags.Method) || isFromObjectLiteral;
62806271
const isSetterWithoutGetter = rightProp.flags & SymbolFlags.SetAccessor && !(rightProp.flags & SymbolFlags.GetAccessor);
62816272
if (getDeclarationModifierFlagsFromSymbol(rightProp) & (ModifierFlags.Private | ModifierFlags.Protected)) {
62826273
skippedPrivateMembers[rightProp.name] = true;
62836274
}
6284-
else if (isOwnProperty && !isSetterWithoutGetter) {
6275+
else if (!isClassMethod(rightProp) && !isSetterWithoutGetter) {
62856276
members[rightProp.name] = rightProp;
62866277
}
62876278
}
62886279
for (const leftProp of getPropertiesOfType(left)) {
62896280
if (leftProp.flags & SymbolFlags.SetAccessor && !(leftProp.flags & SymbolFlags.GetAccessor)
6290-
|| leftProp.name in skippedPrivateMembers) {
6281+
|| leftProp.name in skippedPrivateMembers
6282+
|| isClassMethod(leftProp)) {
62916283
continue;
62926284
}
62936285
if (leftProp.name in members) {
@@ -6312,6 +6304,10 @@ namespace ts {
63126304
return createAnonymousType(undefined, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
63136305
}
63146306

6307+
function isClassMethod(prop: Symbol) {
6308+
return prop.flags & SymbolFlags.Method && find(prop.declarations, decl => isClassLike(decl.parent));
6309+
}
6310+
63156311
function createLiteralType(flags: TypeFlags, text: string) {
63166312
const type = <LiteralType>createType(flags);
63176313
type.text = text;
@@ -6502,16 +6498,16 @@ namespace ts {
65026498
return <T>instantiations[type.id] || (instantiations[type.id] = instantiator(type, mapper));
65036499
}
65046500

6505-
function createUnaryTypeMapper(source: Type, target: Type): TypeMapper {
6506-
return t => t === source ? target : t;
6501+
function makeUnaryTypeMapper(source: Type, target: Type) {
6502+
return (t: Type) => t === source ? target : t;
65076503
}
65086504

6509-
function createBinaryTypeMapper(source1: Type, target1: Type, source2: Type, target2: Type): TypeMapper {
6510-
return t => t === source1 ? target1 : t === source2 ? target2 : t;
6505+
function makeBinaryTypeMapper(source1: Type, target1: Type, source2: Type, target2: Type) {
6506+
return (t: Type) => t === source1 ? target1 : t === source2 ? target2 : t;
65116507
}
65126508

6513-
function createArrayTypeMapper(sources: Type[], targets: Type[]): TypeMapper {
6514-
return t => {
6509+
function makeArrayTypeMapper(sources: Type[], targets: Type[]) {
6510+
return (t: Type) => {
65156511
for (let i = 0; i < sources.length; i++) {
65166512
if (t === sources[i]) {
65176513
return targets ? targets[i] : anyType;
@@ -6522,11 +6518,9 @@ namespace ts {
65226518
}
65236519

65246520
function createTypeMapper(sources: Type[], targets: Type[]): TypeMapper {
6525-
const count = sources.length;
6526-
const mapper: TypeMapper =
6527-
count == 1 ? createUnaryTypeMapper(sources[0], targets ? targets[0] : anyType) :
6528-
count == 2 ? createBinaryTypeMapper(sources[0], targets ? targets[0] : anyType, sources[1], targets ? targets[1] : anyType) :
6529-
createArrayTypeMapper(sources, targets);
6521+
const mapper: TypeMapper = sources.length === 1 ? makeUnaryTypeMapper(sources[0], targets ? targets[0] : anyType) :
6522+
sources.length === 2 ? makeBinaryTypeMapper(sources[0], targets ? targets[0] : anyType, sources[1], targets ? targets[1] : anyType) :
6523+
makeArrayTypeMapper(sources, targets);
65306524
mapper.mappedTypes = sources;
65316525
return mapper;
65326526
}
@@ -6560,7 +6554,7 @@ namespace ts {
65606554

65616555
function combineTypeMappers(mapper1: TypeMapper, mapper2: TypeMapper): TypeMapper {
65626556
const mapper: TypeMapper = t => instantiateType(mapper1(t), mapper2);
6563-
mapper.mappedTypes = mapper1.mappedTypes;
6557+
mapper.mappedTypes = concatenate(mapper1.mappedTypes, mapper2.mappedTypes);
65646558
return mapper;
65656559
}
65666560

@@ -10936,23 +10930,23 @@ namespace ts {
1093610930
const func = parameter.parent;
1093710931
if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
1093810932
const iife = getImmediatelyInvokedFunctionExpression(func);
10939-
if (iife) {
10933+
if (iife && iife.arguments) {
1094010934
const indexOfParameter = indexOf(func.parameters, parameter);
10941-
if (iife.arguments && indexOfParameter < iife.arguments.length) {
10942-
if (parameter.dotDotDotToken) {
10943-
const restTypes: Type[] = [];
10944-
for (let i = indexOfParameter; i < iife.arguments.length; i++) {
10945-
restTypes.push(getWidenedLiteralType(checkExpression(iife.arguments[i])));
10946-
}
10947-
return createArrayType(getUnionType(restTypes));
10948-
}
10949-
const links = getNodeLinks(iife);
10950-
const cached = links.resolvedSignature;
10951-
links.resolvedSignature = anySignature;
10952-
const type = getWidenedLiteralType(checkExpression(iife.arguments[indexOfParameter]));
10953-
links.resolvedSignature = cached;
10954-
return type;
10955-
}
10935+
if (parameter.dotDotDotToken) {
10936+
const restTypes: Type[] = [];
10937+
for (let i = indexOfParameter; i < iife.arguments.length; i++) {
10938+
restTypes.push(getWidenedLiteralType(checkExpression(iife.arguments[i])));
10939+
}
10940+
return restTypes.length ? createArrayType(getUnionType(restTypes)) : undefined;
10941+
}
10942+
const links = getNodeLinks(iife);
10943+
const cached = links.resolvedSignature;
10944+
links.resolvedSignature = anySignature;
10945+
const type = indexOfParameter < iife.arguments.length ?
10946+
getWidenedLiteralType(checkExpression(iife.arguments[indexOfParameter])) :
10947+
parameter.initializer ? undefined : undefinedWideningType;
10948+
links.resolvedSignature = cached;
10949+
return type;
1095610950
}
1095710951
const contextualSignature = getContextualSignature(func);
1095810952
if (contextualSignature) {
@@ -11661,7 +11655,7 @@ namespace ts {
1166111655
checkExternalEmitHelpers(memberDecl, ExternalEmitHelpers.Assign);
1166211656
}
1166311657
if (propertiesArray.length > 0) {
11664-
spread = getSpreadType(spread, createObjectLiteralType(), /*isFromObjectLiteral*/ true);
11658+
spread = getSpreadType(spread, createObjectLiteralType());
1166511659
propertiesArray = [];
1166611660
propertiesTable = createMap<Symbol>();
1166711661
hasComputedStringProperty = false;
@@ -11673,7 +11667,7 @@ namespace ts {
1167311667
error(memberDecl, Diagnostics.Spread_types_may_only_be_created_from_object_types);
1167411668
return unknownType;
1167511669
}
11676-
spread = getSpreadType(spread, type, /*isFromObjectLiteral*/ false);
11670+
spread = getSpreadType(spread, type);
1167711671
offset = i + 1;
1167811672
continue;
1167911673
}
@@ -11718,7 +11712,7 @@ namespace ts {
1171811712

1171911713
if (spread !== emptyObjectType) {
1172011714
if (propertiesArray.length > 0) {
11721-
spread = getSpreadType(spread, createObjectLiteralType(), /*isFromObjectLiteral*/ true);
11715+
spread = getSpreadType(spread, createObjectLiteralType());
1172211716
}
1172311717
if (spread.flags & TypeFlags.Object) {
1172411718
// only set the symbol and flags if this is a (fresh) object type

src/compiler/moduleNameResolver.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace ts {
1515
}
1616

1717
/** Array that is only intended to be pushed to, never read. */
18-
interface Push<T> {
18+
export interface Push<T> {
1919
push(value: T): void;
2020
}
2121

@@ -357,7 +357,7 @@ namespace ts {
357357
* Then it computes the set of parent folders for 'directory' that should have the same module resolution result
358358
* and for every parent folder in set it adds entry: parent -> module resolution. .
359359
* Lets say we first directory name: /a/b/c/d/e and resolution result is: /a/b/bar.ts.
360-
* Set of parent folders that should have the same result will be:
360+
* Set of parent folders that should have the same result will be:
361361
* [
362362
* /a/b/c/d, /a/b/c, /a/b
363363
* ]
@@ -391,7 +391,7 @@ namespace ts {
391391
}
392392
}
393393
}
394-
394+
395395
function getCommonPrefix(directory: Path, resolution: string) {
396396
if (resolution === undefined) {
397397
return undefined;
@@ -421,7 +421,7 @@ namespace ts {
421421
trace(host, Diagnostics.Resolving_module_0_from_1, moduleName, containingFile);
422422
}
423423
const containingDirectory = getDirectoryPath(containingFile);
424-
let perFolderCache = cache && cache.getOrCreateCacheForDirectory(containingDirectory);
424+
const perFolderCache = cache && cache.getOrCreateCacheForDirectory(containingDirectory);
425425
let result = perFolderCache && perFolderCache[moduleName];
426426

427427
if (result) {
@@ -1022,15 +1022,15 @@ namespace ts {
10221022

10231023
/**
10241024
* Represents result of search. Normally when searching among several alternatives we treat value `undefined` as indicator
1025-
* that search fails and we should try another option.
1025+
* that search fails and we should try another option.
10261026
* However this does not allow us to represent final result that should be used instead of further searching (i.e. a final result that was found in cache).
10271027
* SearchResult is used to deal with this issue, its values represents following outcomes:
10281028
* - undefined - not found, continue searching
10291029
* - { value: undefined } - not found - stop searching
10301030
* - { value: <some-value> } - found - stop searching
10311031
*/
10321032
type SearchResult<T> = { value: T | undefined } | undefined;
1033-
1033+
10341034
/**
10351035
* Wraps value to SearchResult.
10361036
* @returns undefined if value is undefined or { value } otherwise

0 commit comments

Comments
 (0)