simplify rest param handling
This commit is contained in:
parent
18723d938f
commit
e185ebb8d5
3 changed files with 19 additions and 16 deletions
|
@ -2340,8 +2340,8 @@ namespace ts {
|
|||
// The type is an object literal type.
|
||||
if (!type.symbol) {
|
||||
// Anonymous types without symbols are literals.
|
||||
// mapToTypeDeclarationsArray(type)
|
||||
throw new Error("unknown case.");
|
||||
// TODO: handle this case correctly.
|
||||
noop();
|
||||
}
|
||||
|
||||
return createTypeLiteralNodeFromType(<ObjectType>type);
|
||||
|
@ -2471,7 +2471,7 @@ namespace ts {
|
|||
case SyntaxKind.IndexSignature:
|
||||
throw new Error("type literal constituent not implemented.");
|
||||
default:
|
||||
throw new Error("Unknown case.");
|
||||
throw new Error("Unknown resolved member kind.");
|
||||
}
|
||||
});
|
||||
return typeElements.length ? typeElements : undefined;
|
||||
|
|
|
@ -52,7 +52,7 @@ namespace ts.codefix {
|
|||
, /*initializer*/ undefined);
|
||||
// TODO: make index signature.
|
||||
const propertyChangeTracker = textChanges.ChangeTracker.fromCodeFixContext(context);
|
||||
propertyChangeTracker.insertNodeAfter(sourceFile, openBrace, property, { insertTrailingNewLine: true });
|
||||
propertyChangeTracker.insertNodeAfter(sourceFile, openBrace, property, { suffix: context.newLineCharacter });
|
||||
|
||||
const stringTypeNode = createKeywordTypeNode(SyntaxKind.StringKeyword);
|
||||
const indexingParameter = createParameter(
|
||||
|
@ -70,7 +70,7 @@ namespace ts.codefix {
|
|||
, /*modifiers*/ undefined);
|
||||
|
||||
const indexSignatureChangeTracker = textChanges.ChangeTracker.fromCodeFixContext(context);
|
||||
indexSignatureChangeTracker.insertNodeAfter(sourceFile, openBrace, indexSignature, { insertTrailingNewLine: true });
|
||||
indexSignatureChangeTracker.insertNodeAfter(sourceFile, openBrace, indexSignature, { suffix: context.newLineCharacter });
|
||||
|
||||
return [{
|
||||
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Add_declaration_for_missing_property_0), [token.getText()]),
|
||||
|
|
|
@ -11,7 +11,7 @@ namespace ts.codefix {
|
|||
const changeTracker = textChanges.ChangeTracker.fromCodeFixContext(context);
|
||||
|
||||
for (const newNode of newNodes) {
|
||||
changeTracker.insertNodeAfter(sourceFile, insertAfter, newNode, { insertTrailingNewLine: true });
|
||||
changeTracker.insertNodeAfter(sourceFile, insertAfter, newNode, { suffix: context.newLineCharacter });
|
||||
}
|
||||
return changeTracker.getChanges();
|
||||
}
|
||||
|
@ -137,20 +137,23 @@ namespace ts.codefix {
|
|||
function createMethodImplementingSignatures(signatures: Signature[], name: PropertyName, modifiers: Modifier[] | undefined): MethodDeclaration {
|
||||
Debug.assert(signatures && signatures.length > 0);
|
||||
|
||||
let maxNonRestArgs = -1;
|
||||
let maxArgsIndex = 0;
|
||||
/** This is *a* signature with the maximal number of arguments,
|
||||
* such that if there is a "maximal" signature without rest arguments,
|
||||
* this is one of them.
|
||||
*/
|
||||
let maxArgsSignature = signatures[0];
|
||||
let minArgumentCount = signatures[0].minArgumentCount;
|
||||
let hasRestParameter = false;
|
||||
let someSigHasRestParameter = false;
|
||||
for (let i = 0; i < signatures.length; i++) {
|
||||
const sig = signatures[i];
|
||||
minArgumentCount = Math.min(sig.minArgumentCount, minArgumentCount);
|
||||
hasRestParameter = hasRestParameter || sig.hasRestParameter;
|
||||
const nonRestLength = sig.parameters.length - (sig.hasRestParameter ? 1 : 0);
|
||||
if (nonRestLength >= maxNonRestArgs) {
|
||||
maxNonRestArgs = nonRestLength;
|
||||
maxArgsIndex = i;
|
||||
someSigHasRestParameter = someSigHasRestParameter || sig.hasRestParameter;
|
||||
if (sig.parameters.length >= maxArgsSignature.parameters.length && (!sig.hasRestParameter || maxArgsSignature.hasRestParameter)) {
|
||||
maxArgsSignature = sig;
|
||||
}
|
||||
}
|
||||
const maxNonRestArgs = maxArgsSignature.parameters.length - (maxArgsSignature.hasRestParameter ? 1 : 0);
|
||||
const maxArgsParameterSymbolNames = signatures[maxArgsIndex].getParameters().map(symbol => symbol.getName());
|
||||
|
||||
const parameters: ParameterDeclaration[] = [];
|
||||
|
@ -167,15 +170,15 @@ namespace ts.codefix {
|
|||
parameters.push(newParameter);
|
||||
}
|
||||
|
||||
if (hasRestParameter) {
|
||||
const anyType = createKeywordTypeNode(SyntaxKind.AnyKeyword);
|
||||
if (someSigHasRestParameter) {
|
||||
const anyArrayType = createArrayTypeNode(createKeywordTypeNode(SyntaxKind.AnyKeyword));
|
||||
const restParameter = createParameter(
|
||||
/*decorators*/ undefined
|
||||
, /*modifiers*/ undefined
|
||||
, createToken(SyntaxKind.DotDotDotToken)
|
||||
, maxArgsParameterSymbolNames[maxNonRestArgs] || "rest"
|
||||
, /*questionToken*/ maxNonRestArgs >= minArgumentCount ? createToken(SyntaxKind.QuestionToken) : undefined
|
||||
, anyType
|
||||
, anyArrayType
|
||||
, /*initializer*/ undefined);
|
||||
parameters.push(restParameter);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue