microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
99.13k stars 12.3k forks source link

Codefix add missing function declaration inserts function in wrong file #59116

Open hediet opened 1 week ago

hediet commented 1 week ago

Image

main.ts:

import { X } from "./lib";

X.foobar();

lib.ts:

export namespace X {
    export function test() {}
};
mjbvz commented 6 days ago

Confirmed that this seems to be an issue on the TS Side. Here's the returned edit:

    {
        "seq": 0,
        "type": "response",
        "command": "getCodeFixes",
        "request_seq": 61,
        "success": true,
        "body": [
            {
                "fixName": "fixMissingFunctionDeclaration",
                "description": "Add missing function declaration 'foobar'",
                "changes": [
                    {
                        "fileName": "/Users/matb/projects/sandbox/index.ts",
                        "textChanges": [
                            {
                                "start": {
                                    "line": 13,
                                    "offset": 2
                                },
                                "end": {
                                    "line": 13,
                                    "offset": 2
                                },
                                "newText": "\n\nexport function foobar() {\n    throw new Error(\"Function not implemented.\");\n}\n"
                            }
                        ]
                    }
                ]
            }
        ]
    }
andrewbranch commented 3 days ago

I'm seeing a similar response but a different editor behavior. We do have incorrect logic for figuring out where to export the function, but the response shows that it should be inserted two lines after the end of the file being edited. For me, this does not result in X.foobar being split at the cursor when invoked with the cursor in the middle of foo|bar, but rather the function gets added with newlines after X.foobar

image

I think the behavior shown in the gif where the text is apparently inserted at the cursor instead of at the end of the file might be an editor bug, but one that I can't reproduce. That said, is it expected for TS Server to be returning an insertion range that's outside the bounds of the current document length?

andrewbranch commented 3 days ago

Note that the bug still happens when the namespace X is a global in another file, but not when it's in the same file as the call expression.

hediet commented 4 hours ago

I think the behavior shown in the gif where the text is apparently inserted at the cursor instead of at the end of the file might be an editor bug, but one that I can't reproduce. That said, is it expected for TS Server to be returning an insertion range that's outside the bounds of the current document length?

Sorry for the confusion, that was a coincidence. The amount of new-lines roughly equals the characters in the other document before the position of where the method should be inserted. It just happened to be the editor cursor location in this case, but that is unrelated.