Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
JSX semantic highlight wrong for non-nested self-closing components . F…
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli committed Jan 27, 2020
1 parent 3ec91d8 commit 9954e2f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
18 changes: 17 additions & 1 deletion src/test/semanticTokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++];
Expand All @@ -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);

}

Expand Down Expand Up @@ -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) => <div></div>',
/*1*/'const ItemPrice = (props) => {',
/*2*/' return <MyComponent { ...props } />;',
/*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')
]);
});
});

0 comments on commit 9954e2f

Please sign in to comment.