Repository: microsoft/TypeScript
File: src/services/codefixes/addNameToNamelessParameter.ts (TypeScript)

Code before:
    const token = getTokenAtPosition(sourceFile, start);
    const param = token.parent;
    if (!isParameter(param)) {
        return Debug.fail("Tried to add a parameter name to a non-parameter: " + Debug.formatSyntaxKind(token.kind));
    }

    const parameters = param.parent.parameters;
    const i = parameters.indexOf(param);
    Debug.assert(!param.type, "Tried to add a parameter name to a parameter that already had one.");
    Debug.assert(i > -1, "Parameter not found in parent parameter list.");

    let end = param.name.getEnd();
    let typeNode: TypeNode = factory.createTypeReferenceNode(param.name as Identifier, /*typeArguments*/ undefined);
    let nextParam = tryGetNextParam(sourceFile, param);
    while (nextParam) {
        typeNode = factory.createArrayTypeNode(typeNode);
        end = nextParam.getEnd();
        nextParam = tryGetNextParam(sourceFile, nextParam);
    }

    const replacement = factory.createParameterDeclaration(
        param.modifiers,
        param.dotDotDotToken,
        "arg" + i,
        param.questionToken,
        param.dotDotDotToken ? isArrayTypeNode(typeNode) ? typeNode : factory.createArrayTypeNode(typeNode) : typeNode,
        param.initializer,
    );
    changeTracker.replaceRange(sourceFile, createRange(param.getStart(sourceFile), end), replacement);
}

function tryGetNextParam(sourceFile: SourceFile, param: ParameterDeclaration) {
    const nextToken = findNextToken(param.name, param.parent, sourceFile);
    if (nextToken && nextToken.kind === SyntaxKind.OpenBracketToken
        && isArrayBindingPattern(nextToken.parent) && isParameter(nextToken.parent.parent)) {
        return nextToken.parent.parent;
    }
    return undefined;
}


Review comment (security):
```suggestion
        param.dotDotDotToken && !isArrayTypeNode(typeNode) ? factory.createArrayTypeNode(typeNode) : typeNode,
```

Although I'm not really convinced it's the job of this codefix to fix the type -- it's supposed to introduce a name, not necessarily fix any types. I think it would be fine to replace this line with just `typeNode,` and let the author fix the type error afterward.

However, changing the codefix is a separate issue than this PR.

Code after:
    const token = getTokenAtPosition(sourceFile, start);
    const param = token.parent;
    if (!isParameter(param)) {
        return Debug.fail("Tried to add a parameter name to a non-parameter: " + Debug.formatSyntaxKind(token.kind));
    }

    const i = param.parent.parameters.indexOf(param);
    Debug.assert(!param.type, "Tried to add a parameter name to a parameter that already had one.");
    Debug.assert(i > -1, "Parameter not found in parent parameter list.");

    let end = param.name.getEnd();
    let typeNode: TypeNode = factory.createTypeReferenceNode(param.name as Identifier, /*typeArguments*/ undefined);
    let nextParam = tryGetNextParam(sourceFile, param);
    while (nextParam) {
        typeNode = factory.createArrayTypeNode(typeNode);
        end = nextParam.getEnd();
        nextParam = tryGetNextParam(sourceFile, nextParam);
    }

    const replacement = factory.createParameterDeclaration(
        param.modifiers,
        param.dotDotDotToken,
        "arg" + i,
        param.questionToken,
        param.dotDotDotToken && !isArrayTypeNode(typeNode) ? factory.createArrayTypeNode(typeNode) : typeNode,
        param.initializer,
    );
    changeTracker.replaceRange(sourceFile, createRange(param.getStart(sourceFile), end), replacement);
}

function tryGetNextParam(sourceFile: SourceFile, param: ParameterDeclaration) {
    const nextToken = findNextToken(param.name, param.parent, sourceFile);
    if (
        nextToken && nextToken.kind === SyntaxKind.OpenBracketToken
        && isArrayBindingPattern(nextToken.parent) && isParameter(nextToken.parent.parent)
    ) {
        return nextToken.parent.parent;
    }
    return undefined;
}