From 9954e2f25289053973702dfabc53e2efa41aa47c Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Mon, 27 Jan 2020 14:34:06 +0100 Subject: [PATCH] JSX semantic highlight wrong for non-nested self-closing components . Fixes microsoft/vscode#89224 --- src/index.ts | 2 +- src/test/semanticTokens.test.ts | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index f9be465..f42738f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -70,7 +70,7 @@ export = function init(modules: { typescript: typeof import("typescript/lib/tsse return; } const prevInJSXElement = inJSXElement; - if (ts.isJsxElement(node)) { + if (ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node)) { inJSXElement = true; } if (ts.isJsxExpression(node)) { diff --git a/src/test/semanticTokens.test.ts b/src/test/semanticTokens.test.ts index eb38aff..9a15db0 100644 --- a/src/test/semanticTokens.test.ts +++ b/src/test/semanticTokens.test.ts @@ -150,6 +150,7 @@ function assertTokens(mainFileName: string, files: { [name: string]: string } = const sourceFile = languageService.getProgram()?.getSourceFile(mainFilePath)!; let actualRanges = []; + let snippet = ''; let i = 0; while (i < result.spans.length) { const start = result.spans[i++], len = result.spans[i++], classification = result.spans[i++]; @@ -159,8 +160,9 @@ function assertTokens(mainFileName: string, files: { [name: string]: string } = const tokenClassifiction = [tokenTypes[typeIdx], ...tokenModifiers.filter((_, i) => modSet & 1 << i)].join('.'); actualRanges.push(t(lineAndChar.line, lineAndChar.character, len, tokenClassifiction)); + snippet += `t(${lineAndChar.line}, ${lineAndChar.character}, ${len}, '${tokenClassifiction}'), `; } - assert.deepEqual(actualRanges, expected); + assert.deepEqual(actualRanges, expected, snippet); } @@ -212,4 +214,18 @@ suite('HTML Semantic Tokens', () => { t(2, 32, 5, 'namespace'), t(2, 38, 7, 'variable.readonly') ]); }); + + + test('JSX2', () => { + const input = [ + /*0*/'const MyComponent = (props) =>
', + /*1*/'const ItemPrice = (props) => {', + /*2*/' return ;', + /*3*/'}', + ].join('\n'); + assertTokens('main.tsx', { 'main.tsx': input }, [ + t(0, 6, 11, 'variable.declaration.readonly'), t(0, 21, 5, 'parameter.declaration'), + t(1, 6, 9, 'variable.declaration.readonly'), t(1, 19, 5, 'parameter.declaration') + ]); + }); }); \ No newline at end of file