microsoft / TypeScript-TmLanguage

TextMate grammar files for TypeScript for VS Code, Sublime Text, and Atom.
MIT License
415 stars 130 forks source link

Code Highlighting Broken for Generic Functions with Default Values in .tsx Files #1042

Open jinliming2 opened 1 month ago

jinliming2 commented 1 month ago

Does this issue occur when all extensions are disabled?: Yes

Describe the bug In VSCode, when writing a generic function with default values in a .tsx file, code highlighting is broken and colors are displayed incorrectly. The same code works fine in a .ts file.

To Reproduce

  1. Open VSCode and create a new .tsx file.
  2. Write the following code:
    
    const func = <T = string,>(args: T): string => {
    const t = typeof args;
    if (typeof t === 'string') {
    return t.split(',').join('_');
    }
    return '';
    };
    func('');

const add = (a: number, b: number) => { return a + b; };


3. Observe the code highlighting.

**Expected behavior**
Code highlighting should work correctly and match the behavior in .ts files.

**Screenshots**
In `.tsx` file:
![Image](https://github.com/user-attachments/assets/9cceb590-fd21-467c-8ab7-2300095fee43)
Pay attention to the screenshot where the generic default parameter definitions, colons, and arrow function arrows are all highlighted in red, while the colors of keywords such as const, if, and return are incorrect.

In `.ts` file:
![Image](https://github.com/user-attachments/assets/381bc1ea-0829-41fb-8bc5-40846b928895)
RedCMD commented 1 month ago

workaround can be to remove the space before = Image

jinliming2 commented 1 month ago

workaround can be to remove the space before =

@RedCMD Yes, but not all scenarios allow removing =, for example:

const func = <T = string,>(args?: T): T[] => {
  return args === undefined ? [] : [args];
};
const emptyStringArr = func();
const emptyNumberArr = func<number>();

Image

RedCMD commented 1 month ago

but not all scenarios allow removing =

@jinliming2 removing the space , not the equals however TS will auto format the space back in

caused by #jsx being higher than #arrow-function Image

jsx-tag-in-expression: Image

arrow-function: Image

jinliming2 commented 1 month ago

@RedCMD Yes, it seems that removing the space after the generic parameter can eliminate the error, but the lint rule will automatically format it and add the space back. I have to disable the lint rule for this line.