-
Notifications
You must be signed in to change notification settings - Fork 12.9k
feat(51870): Add a Quick Fix to add an additional parameter to a method or function #56411
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9338f6a
feat(51870): add a quick fix to add an additional parameters
a-tarasyuk 31e7122
add more tests
a-tarasyuk 975769f
fix typos and code style
a-tarasyuk 529db34
add additional comments
a-tarasyuk b08da55
add optional parameters quick fix
a-tarasyuk 6498ce1
simplify resolving signature declarations
a-tarasyuk de404b4
disallow adding optional parameters between two required
a-tarasyuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,327 @@ | ||
import { | ||
append, | ||
ArrowFunction, | ||
CodeFixAction, | ||
declarationNameToString, | ||
Diagnostics, | ||
factory, | ||
filter, | ||
findAncestor, | ||
first, | ||
forEach, | ||
FunctionDeclaration, | ||
FunctionExpression, | ||
FunctionLikeDeclaration, | ||
getNameOfAccessExpression, | ||
getNameOfDeclaration, | ||
getTokenAtPosition, | ||
isAccessExpression, | ||
isCallExpression, | ||
isIdentifier, | ||
isParameter, | ||
isPropertyDeclaration, | ||
isSourceFileFromLibrary, | ||
isVariableDeclaration, | ||
last, | ||
lastOrUndefined, | ||
length, | ||
map, | ||
MethodDeclaration, | ||
Node, | ||
NodeBuilderFlags, | ||
ParameterDeclaration, | ||
Program, | ||
QuestionToken, | ||
some, | ||
SourceFile, | ||
SyntaxKind, | ||
textChanges, | ||
Type, | ||
TypeChecker, | ||
TypeNode, | ||
} from "../_namespaces/ts"; | ||
import { | ||
codeFixAll, | ||
createCodeFixAction, | ||
registerCodeFix, | ||
} from "../_namespaces/ts.codefix"; | ||
|
||
const addMissingParamFixId = "addMissingParam"; | ||
const addOptionalParamFixId = "addOptionalParam"; | ||
const errorCodes = [Diagnostics.Expected_0_arguments_but_got_1.code]; | ||
|
||
registerCodeFix({ | ||
errorCodes, | ||
fixIds: [addMissingParamFixId, addOptionalParamFixId], | ||
getCodeActions(context) { | ||
const info = getInfo(context.sourceFile, context.program, context.span.start); | ||
if (info === undefined) return undefined; | ||
|
||
const { name, declarations, newParameters, newOptionalParameters } = info; | ||
const actions: CodeFixAction[] = []; | ||
|
||
if (length(newParameters)) { | ||
append( | ||
actions, | ||
createCodeFixAction( | ||
addMissingParamFixId, | ||
textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, declarations, newParameters)), | ||
[length(newParameters) > 1 ? Diagnostics.Add_missing_parameters_to_0 : Diagnostics.Add_missing_parameter_to_0, name], | ||
addMissingParamFixId, | ||
Diagnostics.Add_all_missing_parameters, | ||
), | ||
); | ||
} | ||
|
||
if (length(newOptionalParameters)) { | ||
append( | ||
actions, | ||
createCodeFixAction( | ||
addOptionalParamFixId, | ||
textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, declarations, newOptionalParameters)), | ||
[length(newOptionalParameters) > 1 ? Diagnostics.Add_optional_parameters_to_0 : Diagnostics.Add_optional_parameter_to_0, name], | ||
addOptionalParamFixId, | ||
Diagnostics.Add_all_optional_parameters, | ||
), | ||
); | ||
} | ||
|
||
return actions; | ||
}, | ||
getAllCodeActions: context => | ||
codeFixAll(context, errorCodes, (changes, diag) => { | ||
const info = getInfo(context.sourceFile, context.program, diag.start); | ||
if (info) { | ||
const { declarations, newParameters, newOptionalParameters } = info; | ||
if (context.fixId === addMissingParamFixId) { | ||
doChange(changes, context.sourceFile, declarations, newParameters); | ||
} | ||
if (context.fixId === addOptionalParamFixId) { | ||
doChange(changes, context.sourceFile, declarations, newOptionalParameters); | ||
} | ||
} | ||
}), | ||
}); | ||
|
||
type ConvertibleSignatureDeclaration = | ||
| FunctionDeclaration | ||
| FunctionExpression | ||
| ArrowFunction | ||
| MethodDeclaration; | ||
|
||
interface SignatureInfo { | ||
readonly newParameters: ParameterInfo[]; | ||
readonly newOptionalParameters: ParameterInfo[]; | ||
readonly name: string; | ||
readonly declarations: ConvertibleSignatureDeclaration[]; | ||
} | ||
|
||
interface ParameterInfo { | ||
readonly pos: number; | ||
readonly declaration: ParameterDeclaration; | ||
} | ||
|
||
function getInfo(sourceFile: SourceFile, program: Program, pos: number): SignatureInfo | undefined { | ||
const token = getTokenAtPosition(sourceFile, pos); | ||
const callExpression = findAncestor(token, isCallExpression); | ||
if (callExpression === undefined || length(callExpression.arguments) === 0) { | ||
return undefined; | ||
} | ||
|
||
const checker = program.getTypeChecker(); | ||
const type = checker.getTypeAtLocation(callExpression.expression); | ||
const convertibleSignatureDeclarations = filter(type.symbol.declarations, isConvertibleSignatureDeclaration); | ||
if (convertibleSignatureDeclarations === undefined) { | ||
return undefined; | ||
} | ||
|
||
const nonOverloadDeclaration = lastOrUndefined(convertibleSignatureDeclarations); | ||
if ( | ||
nonOverloadDeclaration === undefined || | ||
nonOverloadDeclaration.body === undefined || | ||
isSourceFileFromLibrary(program, nonOverloadDeclaration.getSourceFile()) | ||
) { | ||
return undefined; | ||
} | ||
|
||
const name = tryGetName(nonOverloadDeclaration); | ||
if (name === undefined) { | ||
return undefined; | ||
} | ||
|
||
const newParameters: ParameterInfo[] = []; | ||
const newOptionalParameters: ParameterInfo[] = []; | ||
const parametersLength = length(nonOverloadDeclaration.parameters); | ||
const argumentsLength = length(callExpression.arguments); | ||
if (parametersLength > argumentsLength) { | ||
return undefined; | ||
} | ||
|
||
const declarations = [nonOverloadDeclaration, ...getOverloads(nonOverloadDeclaration, convertibleSignatureDeclarations)]; | ||
for (let i = 0, pos = 0, paramIndex = 0; i < argumentsLength; i++) { | ||
const arg = callExpression.arguments[i]; | ||
const expr = isAccessExpression(arg) ? getNameOfAccessExpression(arg) : arg; | ||
const type = checker.getWidenedType(checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(arg))); | ||
const parameter = pos < parametersLength ? nonOverloadDeclaration.parameters[pos] : undefined; | ||
if ( | ||
parameter && | ||
checker.isTypeAssignableTo(type, checker.getTypeAtLocation(parameter)) | ||
) { | ||
pos++; | ||
continue; | ||
} | ||
|
||
const name = expr && isIdentifier(expr) ? expr.text : `p${paramIndex++}`; | ||
const typeNode = typeToTypeNode(checker, type, nonOverloadDeclaration); | ||
append(newParameters, { | ||
pos: i, | ||
declaration: createParameter(name, typeNode, /*questionToken*/ undefined), | ||
}); | ||
|
||
if (isOptionalPos(declarations, pos)) { | ||
continue; | ||
} | ||
|
||
append(newOptionalParameters, { | ||
pos: i, | ||
declaration: createParameter(name, typeNode, factory.createToken(SyntaxKind.QuestionToken)), | ||
}); | ||
} | ||
|
||
return { | ||
newParameters, | ||
newOptionalParameters, | ||
name: declarationNameToString(name), | ||
declarations, | ||
}; | ||
} | ||
|
||
function tryGetName(node: FunctionLikeDeclaration) { | ||
sandersn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const name = getNameOfDeclaration(node); | ||
if (name) { | ||
return name; | ||
} | ||
|
||
if ( | ||
isVariableDeclaration(node.parent) && isIdentifier(node.parent.name) || | ||
isPropertyDeclaration(node.parent) || | ||
isParameter(node.parent) | ||
) { | ||
return node.parent.name; | ||
} | ||
} | ||
|
||
function typeToTypeNode(checker: TypeChecker, type: Type, enclosingDeclaration: Node) { | ||
return checker.typeToTypeNode(checker.getWidenedType(type), enclosingDeclaration, NodeBuilderFlags.NoTruncation) | ||
?? factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword); | ||
} | ||
|
||
function doChange( | ||
changes: textChanges.ChangeTracker, | ||
sourceFile: SourceFile, | ||
declarations: ConvertibleSignatureDeclaration[], | ||
newParameters: ParameterInfo[], | ||
) { | ||
forEach(declarations, declaration => { | ||
if (length(declaration.parameters)) { | ||
changes.replaceNodeRangeWithNodes( | ||
sourceFile, | ||
first(declaration.parameters), | ||
last(declaration.parameters), | ||
updateParameters(declaration, newParameters), | ||
{ | ||
joiner: ", ", | ||
indentation: 0, | ||
leadingTriviaOption: textChanges.LeadingTriviaOption.IncludeAll, | ||
trailingTriviaOption: textChanges.TrailingTriviaOption.Include, | ||
}, | ||
); | ||
} | ||
else { | ||
forEach(updateParameters(declaration, newParameters), (parameter, index) => { | ||
if (length(declaration.parameters) === 0 && index === 0) { | ||
changes.insertNodeAt(sourceFile, declaration.parameters.end, parameter); | ||
} | ||
else { | ||
changes.insertNodeAtEndOfList(sourceFile, declaration.parameters, parameter); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
function isConvertibleSignatureDeclaration(node: Node): node is ConvertibleSignatureDeclaration { | ||
switch (node.kind) { | ||
case SyntaxKind.FunctionDeclaration: | ||
case SyntaxKind.FunctionExpression: | ||
case SyntaxKind.MethodDeclaration: | ||
case SyntaxKind.ArrowFunction: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
function updateParameters(node: ConvertibleSignatureDeclaration, newParameters: readonly ParameterInfo[]) { | ||
const parameters = map(node.parameters, p => | ||
factory.createParameterDeclaration( | ||
p.modifiers, | ||
p.dotDotDotToken, | ||
p.name, | ||
p.questionToken, | ||
p.type, | ||
p.initializer, | ||
)); | ||
for (const { pos, declaration } of newParameters) { | ||
const prev = pos > 0 ? parameters[pos - 1] : undefined; | ||
parameters.splice( | ||
pos, | ||
0, | ||
factory.updateParameterDeclaration( | ||
declaration, | ||
declaration.modifiers, | ||
declaration.dotDotDotToken, | ||
declaration.name, | ||
prev && prev.questionToken ? factory.createToken(SyntaxKind.QuestionToken) : declaration.questionToken, | ||
declaration.type, | ||
declaration.initializer, | ||
), | ||
); | ||
} | ||
return parameters; | ||
} | ||
|
||
function getOverloads(implementation: ConvertibleSignatureDeclaration, declarations: readonly ConvertibleSignatureDeclaration[]): ConvertibleSignatureDeclaration[] { | ||
const overloads: ConvertibleSignatureDeclaration[] = []; | ||
for (const declaration of declarations) { | ||
if (isOverload(declaration)) { | ||
if (length(declaration.parameters) === length(implementation.parameters)) { | ||
overloads.push(declaration); | ||
continue; | ||
} | ||
if (length(declaration.parameters) > length(implementation.parameters)) { | ||
return []; | ||
} | ||
} | ||
} | ||
return overloads; | ||
} | ||
|
||
function isOverload(declaration: ConvertibleSignatureDeclaration) { | ||
return isConvertibleSignatureDeclaration(declaration) && declaration.body === undefined; | ||
} | ||
|
||
function createParameter(name: string, type: TypeNode, questionToken: QuestionToken | undefined) { | ||
return factory.createParameterDeclaration( | ||
/*modifiers*/ undefined, | ||
/*dotDotDotToken*/ undefined, | ||
name, | ||
questionToken, | ||
type, | ||
/*initializer*/ undefined, | ||
); | ||
} | ||
|
||
function isOptionalPos(declarations: ConvertibleSignatureDeclaration[], pos: number) { | ||
return length(declarations) && some(declarations, d => pos < length(d.parameters) && !!d.parameters[pos] && d.parameters[pos].questionToken === undefined); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
////[|function f() {}|] | ||
//// | ||
////const a = 1; | ||
////f(a); | ||
|
||
verify.codeFix({ | ||
description: [ts.Diagnostics.Add_missing_parameter_to_0.message, "f"], | ||
index: 0, | ||
newRangeContent: "function f(a: number) {}" | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.