Skip to content

Commit

Permalink
(fix) negative length and wrong order semantic token of event handler (
Browse files Browse the repository at this point in the history
…#811)

#808
In svelte2tsx transformation, component event handler code is moved, Thus the semantic token would have the wrong order with other attributes and a negative length($on). Filter out the negative one and then sort the token
  • Loading branch information
jasonlyu123 authored Feb 10, 2021
1 parent 00d2e06 commit ecfb759
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class SemanticTokensProviderImpl implements SemanticTokensProvider {
ts.SemanticClassificationFormat.TwentyTwenty
);

const builder = new SemanticTokensBuilder();
const data: Array<[number, number, number, number, number]> = [];
let index = 0;

while (index < spans.length) {
Expand All @@ -61,17 +61,27 @@ export class SemanticTokensProviderImpl implements SemanticTokensProvider {

const [line, character, length] = originalPosition;

// remove identifers whose start and end mapped to the same location
// like the svelte2tsx inserted render function
if (!length) {
// remove identifiers whose start and end mapped to the same location,
// like the svelte2tsx inserted render function,
// or reversed like Component.$on
if (length <= 0) {
continue;
}

const modifier = this.getTokenModifierFromClassification(encodedClassification);

builder.push(line, character, length, classificationType, modifier);
data.push([line, character, length, classificationType, modifier]);
}

const sorted = data.sort((a, b) => {
const [lineA, charA] = a;
const [lineB, charB] = b;

return lineA - lineB || charA - charB;
});

const builder = new SemanticTokensBuilder();
sorted.forEach((tokenData) => builder.push(...tokenData));
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,42 +62,42 @@ describe('SemanticTokensProvider', () => {
modifiers: number[];
}> = [
{
line: 1,
line: 2,
character: 14,
length: 'TextContent'.length,
type: TokenType.interface,
modifiers: [TokenModifier.declaration]
},
{
line: 2,
line: 3,
character: 8,
length: 'text'.length,
type: TokenType.property,
modifiers: [TokenModifier.declaration]
},
{
line: 5,
line: 6,
character: 15,
length: 'textPromise'.length,
type: TokenType.variable,
modifiers: [TokenModifier.declaration, TokenModifier.local]
},
{
line: 5,
line: 6,
character: 28,
length: 'Promise'.length,
type: TokenType.interface,
modifiers: [TokenModifier.defaultLibrary]
},
{
line: 5,
line: 6,
character: 36,
length: 'TextContent'.length,
type: TokenType.interface,
modifiers: []
},
{
line: 7,
line: 8,
character: 19,
length: 'blurHandler'.length,
type: TokenType.function,
Expand All @@ -107,49 +107,49 @@ describe('SemanticTokensProvider', () => {
const tokenDataAll = [
...tokenDataScript,
{
line: 10,
line: 11,
character: 8,
length: 'textPromise'.length,
type: TokenType.variable,
modifiers: [TokenModifier.local]
},
{
line: 10,
line: 11,
character: 25,
length: 'text'.length,
type: TokenType.parameter,
modifiers: [TokenModifier.declaration]
},
{
line: 11,
line: 12,
character: 23,
length: 'blurHandler'.length,
type: TokenType.function,
modifiers: [TokenModifier.async, TokenModifier.local]
},
{
line: 11,
line: 12,
character: 43,
length: 'text'.length,
type: TokenType.parameter,
modifiers: []
},
{
line: 11,
line: 12,
character: 48,
length: 'text'.length,
type: TokenType.property,
modifiers: []
},
{
line: 13,
line: 14,
character: 16,
length: 1,
type: TokenType.parameter,
modifiers: [TokenModifier.declaration]
},
{
line: 14,
line: 15,
character: 5,
length: 1,
type: TokenType.parameter,
Expand Down Expand Up @@ -187,7 +187,7 @@ describe('SemanticTokensProvider', () => {

let index = 0;
while (index < tokens.length) {
result.push(tokens.splice(index, (index += 5)));
result.push(tokens.slice(index, (index += 5)));
}

return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
export let value;
</script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import Imported from './imported.svelte';
interface TextContent {
text: string,
}
Expand All @@ -9,7 +10,7 @@
</script>

{#await textPromise then text}
<textarea on:blur={blurHandler} value={text.text} />
<Imported on:blur={blurHandler} value={text.text} />
{/await}
{#each ['a'] as a}
{a}
Expand Down

0 comments on commit ecfb759

Please sign in to comment.