Skip to content

[tsserver] Custom Diagnostic errors, don't triggered getCodeFixAtPosition #35553

Closed
@sviat9440

Description

@sviat9440

When must be triggered getCodeFixAtPosition?
I think every time, when i open code fix menu in my IDE.

Note: I use InteljIDEA.

So, when I write code that has an internal (typescript) error (like syntax or type error), and open code fix menu (by ALT + ENTER as default), IDE call getCodeFixAtPosition with tsserver.
For example:

const a: number = ''; // type error: cant assign string to number

then getCodeFixAtPosition was called and works.

image
image

But, when I create custom diagnostic error, in getSemanticDiagnostics or getSyntacticDiagnostics, then getCodeFixAtPosition was not called when I try to open code fix menu.

image
image

Here is code, that create custom error:

export function HooksChecker(base: LanguageServiceConstructor) {
    class HooksCheckerLanguageService extends base {
        private parser = new LanguageServiceParser(this.info.project);

        constructor(private info: ts.server.PluginCreateInfo) {
            super();
        }

        public getSemanticDiagnostics(fileName: string): ts.Diagnostic[] {
            return [
                ...super.getSemanticDiagnostics(fileName),
                {
                    category: DiagnosticCategory.Error,
                    code: 0,
                    file: this.parser.getSourceFile(fileName),
                    start: 0,
                    length: 5,
                    messageText: 'MyCustomError',
                },
            ];
        }

        public getCodeFixesAtPosition(
            fileName: string,
            start: number,
            end: number,
            errorCodes: ReadonlyArray<number>,
            formatOptions: ts.FormatCodeSettings,
            preferences: ts.UserPreferences,
        ): ReadonlyArray<ts.CodeFixAction> {
            return [
                ...super.getCodeFixesAtPosition(fileName, start, end, errorCodes, formatOptions, preferences),
                {
                    description: 'test test',
                    changes: [
                        {
                            fileName: fileName,
                            textChanges: [
                                {
                                    span: {
                                        start: 0,
                                        length: 2,
                                    },
                                    newText: 'test test',
                                }
                            ]
                        }
                    ],
                    fixName: 'test fix name'
                }
            ]
        }
    }
}

I don't know where to look for the problem. Maybe a bug in the IDE, but maybe I did something wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions