8000 Tuples in rest parameters and spread expressions by ahejlsberg · Pull Request #24897 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content

Tuples in rest parameters and spread expressions #24897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
8cd8edb
Allow generic rest parameters and infer tuples when possible
ahejlsberg Jun 7, 2018
43bfccf
Accept new baselines
ahejlsberg Jun 7, 2018
2e04322
Optional elements in tuple types + empty tuple types + other fixes.
ahejlsberg Jun 9, 2018
346276c
Accept new baselines
ahejlsberg Jun 9, 2018
c69e4e7
Expand rest parameter with tuple types when emitting signatures
ahejlsberg Jun 11, 2018
73e008d
Accept new baselines
ahejlsberg Jun 11, 2018
7aee647
Record parameter names in inferred tuple types
ahejlsberg Jun 11, 2018
9ae9371
Fix lint errors
ahejlsberg Jun 11, 2018
e239f86
Flatten spread expressions of tuple types
ahejlsberg Jun 11, 2018
0b0a91a
Accept new baselines
ahejlsberg Jun 11, 2018
5245642
Accept baseline API changes
ahejlsberg Jun 11, 2018
39099a0
Merge branch 'master' into restTuples
ahejlsberg Jun 12, 2018
dee51ed
Accept baseline changes in APIs
ahejlsberg Jun 12, 2018
f93f6ec
Better algorithm for combined co- and contra-variant inferences.
ahejlsberg Jun 12, 2018
6bdedad
Accept new baselines
ahejlsberg Jun 12, 2018
58d5583
Minor fixes
ahejlsberg Jun 13, 2018
cee75aa
Properly widen type element types in inferred rest parameter types
ahejlsberg Jun 14, 2018
0cc0fad
Add tests
ahejlsberg Jun 14, 2018
09f17bc
Accept new baselines
ahejlsberg Jun 14, 2018
f1efd1d
Parsing and rudimentary checking of tuples with rest elements
ahejlsberg Jun 15, 2018
4f99bc1
Merge branch 'master' into restTuples
ahejlsberg Jun 16, 2018
64aabf2
Accept new baselines
ahejlsberg Jun 16, 2018
82448af
Merge branch 'master' into restTuples
ahejlsberg Jun 16, 2018
3f03a37
Infer union types for rest elements in tuples
ahejlsberg Jun 16, 2018
43bac20
Array literals contextually typed by tuple types with rest elements
ahejlsberg Jun 16, 2018
ae859d6
Update destructuring to support optional and rest elements in tuples
ahejlsberg Jun 22, 2018
c231000
Update tests
ahejlsberg Jun 22, 2018
4037d07
Accept new baselines
ahejlsberg Jun 22, 2018
cffa1dd
Accept new baselines
ahejlsberg Jun 22, 2018
0a94f77
Accept new baselines
ahejlsberg Jun 22, 2018
b0d8c65
Merge branch 'master' into restTuples
ahejlsberg Jun 22, 2018
b650d7d
Fix issue
ahejlsberg Jun 22, 2018
88444fe
Accept new baselines
ahejlsberg Jun 22, 2018
28c9f59
Complete support for rest elements in tuples
ahejlsberg Jun 25, 2018
9cd8ead
Update tests
ahejlsberg Jun 25, 2018
3cc3b49
Accept new baselines
ahejlsberg Jun 25, 2018
d7443f0
Contextual typing by tuple rest elements
ahejlsberg Jun 25, 2018
d869e56
Add tests
ahejlsberg Jun 25, 2018
5ef7e9f
Accept new baselines
ahejlsberg Jun 25, 2018
656ccd8
Revert package.json change
ahejlsberg Jun 26, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Expand rest parameter with tuple types when emitting signatures
  • Loading branch information
ahejlsberg committed Jun 11, 2018
commit c69e4e7aff4c46197dcc55bfbfc677c4dbf4142d
40 changes: 28 additions & 12 deletions src/compiler/checker.ts
BCCB
Original file line number Diff line number Diff line change
Expand Up @@ -3627,7 +3627,7 @@ namespace ts {
typeParameters = signature.typeParameters && signature.typeParameters.map(parameter => typeParameterToDeclaration(parameter, context));
}

const parameters = signature.parameters.map(parameter => symbolToParameterDeclaration(parameter, context, kind === SyntaxKind.Constructor));
const parameters = getExpandedParameters(signature).map(parameter => symbolToParameterDeclaration(parameter, context, kind === SyntaxKind.Constructor));
if (signature.thisParameter) {
const thisParameter = symbolToParameterDeclaration(signature.thisParameter, context);
parameters.unshift(thisParameter);
Expand Down Expand Up @@ -3696,7 +3696,7 @@ namespace ts {
const parameterTypeNode = typeToTypeNodeHelper(parameterType, context);

const modifiers = !(context.flags & NodeBuilderFlags.OmitParameterModifiers) && preserveModifierFlags && parameterDeclaration && parameterDeclaration.modifiers ? parameterDeclaration.modifiers.map(getSynthesizedClone) : undefined;
const isRest = parameterDeclaration ? isRestParameter(parameterDeclaration) : (parameterSymbol as TransientSymbol).isRestParameter;
const isRest = parameterDeclaration && isRestParameter(parameterDeclaration) || getCheckFlags(parameterSymbol) & CheckFlags.RestParameter;
const dotDotDotToken = isRest ? createToken(SyntaxKind.DotDotDotToken) : undefined;
const name = parameterDeclaration
? parameterDeclaration.name ?
Expand All @@ -3705,7 +3705,8 @@ namespace ts {
cloneBindingName(parameterDeclaration.name) :
symbolName(parameterSymbol)
: symbolName(parameterSymbol);
const questionToken = parameterDeclaration && isOptionalParameter(parameterDeclaration) ? createToken(SyntaxKind.QuestionToken) : undefined;
const isOptional = parameterDeclaration && isOptionalParameter(parameterDeclaration) || getCheckFlags(parameterSymbol) & CheckFlags.OptionalParameter;
const questionToken = isOptional ? createToken(SyntaxKind.QuestionToken) : undefined;
const parameterNode = createParameter(
/*decorators*/ undefined,
modifiers,
Expand Down Expand Up @@ -6198,6 +6199,27 @@ namespace ts {
/*resolvedTypePredicate*/ undefined, sig.minArgumentCount, sig.hasRestParameter, sig.hasLiteralTypes);
}

function getExpandedParameters(sig: Signature): Symbol[] {
if (sig.hasRestParameter) {
const restIndex = sig.parameters.length - 1;
const restParameter = sig.parameters[restIndex];
const restType = getTypeOfSymbol(restParameter);
if (isTupleType(restType)) {
const elementTypes = (<TypeReference>restType).typeArguments || emptyArray;
const minLength = (<TupleType>(<TypeReference>restType).target).minLength;
const restParams = map(elementTypes, (t, i) => {
const name = restParameter.escapedName + "_" + i as __String;
const checkFlags = i >= minLength ? CheckFlags.OptionalParameter : 0;
const symbol = createSymbol(SymbolFlags.FunctionScopedVariable, name, checkFlags);
symbol.type = t;
return symbol;
});
return concatenate(sig.parameters.slice(0, restIndex), restParams);
}
}
return sig.parameters;
}

function getDefaultConstructSignatures(classType: InterfaceType): Signature[] {
const baseConstructorType = getBaseConstructorTypeOfClass(classType);
const baseSignatures = getSignaturesOfType(baseConstructorType, SignatureKind.Construct);
Expand Down Expand Up @@ -7376,9 +7398,8 @@ namespace ts {
const lastParamVariadicType = firstDefined(lastParamTags, p =>
p.typeExpression && isJSDocVariadicType(p.typeExpression.type) ? p.typeExpression.type : undefined);

const syntheticArgsSymbol = createSymbol(SymbolFlags.Variable, "args" as __String);
const syntheticArgsSymbol = createSymbol(SymbolFlags.Variable, "args" as __String, CheckFlags.RestParameter);
syntheticArgsSymbol.type = lastParamVariadicType ? createArrayType(getTypeFromTypeNode(lastParamVariadicType.type)) : anyArrayType;
syntheticArgsSymbol.isRestParameter = true;
if (lastParamVariadicType) {
// Replace the last parameter with a rest parameter.
parameters.pop();
Expand Down Expand Up @@ -9716,7 +9737,7 @@ namespace ts {
}
// Keep the flags from the symbol we're instantiating. Mark that is instantiated, and
// also transient so that we can just store data on it directly.
const result = createSymbol(symbol.flags, symbol.escapedName, CheckFlags.Instantiated | (getCheckFlags(symbol) & CheckFlags.Late));
const result = createSymbol(symbol.flags, symbol.escapedName, CheckFlags.Instantiated | getCheckFlags(symbol) & (CheckFlags.Late | CheckFlags.OptionalParameter | CheckFlags.RestParameter));
result.declarations = symbol.declarations;
result.parent = symbol.parent;
result.target = symbol;
Expand All @@ -9727,11 +9748,6 @@ namespace ts {
if (symbol.nameType) {
result.nameType = symbol.nameType;
}
if (isTransientSymbol(symbol)) {
if (symbol.isRestParameter) {
result.isRestParameter = symbol.isRestParameter;
}
}
return result;
}

Expand Down Expand Up @@ -21034,7 +21050,7 @@ namespace ts {
}

function isRestParameterType(type: Type) {
return isArrayType(type) || type.flags & TypeFlags.TypeParameter && isArrayType(getConstraintOfType(type) || unknownType);
return isArrayType(type) || isTupleType(type) || type.flags & TypeFlags.TypeParameter && isArrayType(getBaseConstraintOfType(type) || unknownType);
}

function checkParameter(node: ParameterDeclaration) {
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3548,14 +3548,15 @@ namespace ts {
ContainsPrivate = 1 << 8, // Synthetic property with private constituent(s)
ContainsStatic = 1 << 9, // Synthetic property with static constituent(s)
Late = 1 << 10, // Late-bound symbol for a computed property with a dynamic name
ReverseMapped = 1 << 11, // property of reverse-inferred homomorphic mapped type.
ReverseMapped = 1 << 11, // Property of reverse-inferred homomorphic mapped type
OptionalParameter = 1 << 12, // Optional parameter
RestParameter = 1 << 13, // Rest parameter
Synthetic = SyntheticProperty | SyntheticMethod
}

/* @internal */
export interface TransientSymbol extends Symbol, SymbolLinks {
checkFlags: CheckFlags;
isRestParameter?: boolean;
}

/* @internal */
Expand Down
0