Skip to content

Commit 7c7f9d7

Browse files
authored
(feat) support new const tag (#1072)
#1067
1 parent e4c2194 commit 7c7f9d7

File tree

32 files changed

+717
-56
lines changed

32 files changed

+717
-56
lines changed

packages/language-server/src/plugins/svelte/features/SvelteTags.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type SvelteLogicTag = 'each' | 'if' | 'await' | 'key';
88
/**
99
* Special svelte syntax tags.
1010
*/
11-
export type SvelteTag = SvelteLogicTag | 'html' | 'debug';
11+
export type SvelteTag = SvelteLogicTag | 'html' | 'debug' | 'const';
1212

1313
/**
1414
* For each tag, a documentation in markdown format.
@@ -74,10 +74,15 @@ It logs the values of specific variables whenever they change, ` +
7474
`and pauses code execution if you have devtools open.
7575
It accepts a comma-separated list of variable names (not arbitrary expressions).
7676
#### Usage:
77-
\`{@debug\`}
77+
\`{@debug}\`
7878
\`{@debug var1, var2, ..., varN}\`\\
7979
\\
8080
https://svelte.dev/docs#debug
81+
`,
82+
const: `\`{@const ...}\`\\
83+
TODO
84+
#### Usage:
85+
\`{@const a = b + c}\`\\
8186
`
8287
};
8388

packages/language-server/src/plugins/svelte/features/getCompletions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ function getCompletionsWithRegardToTriggerCharacter(
124124
if (triggerCharacter === '@') {
125125
return createCompletionItems([
126126
{ tag: 'html', label: 'html' },
127-
{ tag: 'debug', label: 'debug' }
127+
{ tag: 'debug', label: 'debug' },
128+
{ tag: 'const', label: 'const' }
128129
]);
129130
}
130131

packages/language-server/src/plugins/svelte/features/getHoverInfo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ const tagPossibilities: Array<{ tag: SvelteTag | ':else'; values: string[] }> =
112112
// @
113113
{ tag: 'html' as const, values: ['@html'] },
114114
{ tag: 'debug' as const, values: ['@debug'] },
115+
{ tag: 'const' as const, values: ['@const'] },
115116
// this tag has multiple possibilities
116117
{ tag: ':else' as const, values: [':else'] }
117118
];

packages/language-server/test/plugins/svelte/features/getCompletions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('SveltePlugin#getCompletions', () => {
5656
});
5757

5858
it('should return completions for @', () => {
59-
expectCompletionsFor('{@').toEqual(['html', 'debug']);
59+
expectCompletionsFor('{@').toEqual(['html', 'debug', 'const']);
6060
});
6161

6262
describe('should return no completions for :', () => {
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
[
2+
{
3+
"range": {
4+
"start": { "line": 28, "character": 21 },
5+
"end": { "line": 28, "character": 27 }
6+
},
7+
"severity": 4,
8+
"source": "ts",
9+
"message": "'result' is declared but its value is never read.",
10+
"code": 6133,
11+
"tags": [1]
12+
},
13+
{
14+
"range": {
15+
"start": { "line": 29, "character": 12 },
16+
"end": { "line": 29, "character": 18 }
17+
},
18+
"severity": 4,
19+
"source": "ts",
20+
"message": "'unused' is declared but its value is never read.",
21+
"code": 6133,
22+
"tags": [1]
23+
},
24+
{
25+
"range": { "start": { "line": 32, "character": 8 }, "end": { "line": 32, "character": 9 } },
26+
"severity": 4,
27+
"source": "ts",
28+
"message": "'e' is declared but its value is never read.",
29+
"code": 6133,
30+
"tags": [1]
31+
},
32+
{
33+
"range": {
34+
"start": { "line": 33, "character": 12 },
35+
"end": { "line": 33, "character": 18 }
36+
},
37+
"severity": 4,
38+
"source": "ts",
39+
"message": "'unused' is declared but its value is never read.",
40+
"code": 6133,
41+
"tags": [1]
42+
},
43+
{
44+
"range": {
45+
"start": { "line": 39, "character": 12 },
46+
"end": { "line": 39, "character": 18 }
47+
},
48+
"severity": 4,
49+
"source": "ts",
50+
"message": "'unused' is declared but its value is never read.",
51+
"code": 6133,
52+
"tags": [1]
53+
},
54+
{
55+
"range": {
56+
"start": { "line": 29, "character": 21 },
57+
"end": { "line": 29, "character": 32 }
58+
},
59+
"severity": 1,
60+
"source": "ts",
61+
"message": "Cannot find name 'doesntExist'.",
62+
"code": 2304,
63+
"tags": []
64+
},
65+
{
66+
"range": {
67+
"start": { "line": 31, "character": 5 },
68+
"end": { "line": 31, "character": 17 }
69+
},
70+
"severity": 1,
71+
"source": "ts",
72+
"message": "This condition will always return 'false' since the types 'string' and 'boolean' have no overlap.",
73+
"code": 2367,
74+
"tags": []
75+
},
76+
{
77+
"range": {
78+
"start": { "line": 33, "character": 21 },
79+
"end": { "line": 33, "character": 32 }
80+
},
81+
"severity": 1,
82+
"source": "ts",
83+
"message": "Cannot find name 'doesntExist'.",
84+
"code": 2304,
85+
"tags": []
86+
},
87+
{
88+
"range": {
89+
"start": { "line": 35, "character": 5 },
90+
"end": { "line": 35, "character": 17 }
91+
},
92+
"severity": 1,
93+
"source": "ts",
94+
"message": "This condition will always return 'false' since the types 'string' and 'boolean' have no overlap.",
95+
"code": 2367,
96+
"tags": []
97+
},
98+
{
99+
"range": {
100+
"start": { "line": 39, "character": 21 },
101+
"end": { "line": 39, "character": 32 }
102+
},
103+
"severity": 1,
104+
"source": "ts",
105+
"message": "Cannot find name 'doesntExist'.",
106+
"code": 2304,
107+
"tags": []
108+
},
109+
{
110+
"range": {
111+
"start": { "line": 41, "character": 5 },
112+
"end": { "line": 41, "character": 16 }
113+
},
114+
"severity": 1,
115+
"source": "ts",
116+
"message": "This condition will always return 'false' since the types 'number' and 'string' have no overlap.",
117+
"code": 2367,
118+
"tags": []
119+
}
120+
]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<script lang="ts">
2+
const promise = Promise.resolve({foo: true});
3+
const shadowed = true;
4+
shadowed;
5+
</script>
6+
7+
<!-- valid -->
8+
{#await promise then result}
9+
{@const bar = result}
10+
{@const str = "hello"}
11+
{@const shadowed = "shadowed"}
12+
{bar === result}
13+
{str === "hello"}
14+
{shadowed === "shadowed"}
15+
{:catch e}
16+
{@const bar = e}
17+
{@const str = "hello"}
18+
{bar}
19+
{str === "hello"}
20+
{/await}
21+
22+
{#each [1, 2] as item}
23+
{@const x = item * 2}
24+
{@const shadowed = item * 3}
25+
{x === shadowed}
26+
{/each}
27+
28+
<!-- invalid -->
29+
{#await promise then result}
30+
{@const unused = doesntExist}
31+
{@const str = "hello"}
32+
{str === true}
33+
{:catch e}
34+
{@const unused = doesntExist}
35+
{@const str = "hello"}
36+
{str === true}
37+
{/await}
38+
39+
{#each [1, 2] as item}
40+
{@const unused = doesntExist}
41+
{@const x = item * 2}
42+
{x === "asd"}
43+
{/each}

packages/svelte2tsx/rollup.config.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ function repl() {
2727
this.addWatchFile(INPUT);
2828
},
2929
writeBundle() {
30-
const BUILD = require.resolve('./index.js');
31-
const BUILD_TEST = require.resolve('./test/build.ts');
30+
try {
31+
const BUILD = require.resolve('./index.js');
32+
const BUILD_TEST = require.resolve('./test/build.ts');
3233

33-
delete require.cache[BUILD];
34-
const { svelte2tsx } = require('./index.js');
34+
delete require.cache[BUILD];
35+
const svelte2tsx = require('./index.js');
3536

36-
delete require.cache[BUILD_TEST];
37-
require.cache[BUILD_TEST] = require.cache[BUILD];
38-
const { process_transformed_text } = require('./test/sourcemaps/process');
37+
delete require.cache[BUILD_TEST];
38+
require.cache[BUILD_TEST] = require.cache[BUILD];
39+
const { process_transformed_text } = require('./test/sourcemaps/process');
3940

40-
const input_content = fs.readFileSync(INPUT, 'utf-8');
41+
const input_content = fs.readFileSync(INPUT, 'utf-8');
4142

42-
try {
4343
const { code, map } = svelte2tsx(input_content);
4444

4545
map.file = 'code.tsx';

packages/svelte2tsx/src/htmlxtojsx/index.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import { handleSvelteTag } from './nodes/svelte-tag';
2626
import { TemplateScopeManager } from './nodes/template-scope';
2727
import { handleText } from './nodes/text';
2828
import { handleTransitionDirective } from './nodes/transition-directive';
29-
import { usesLet } from './utils/node-utils';
3029

3130
type Walker = (node: TemplateNode, parent: BaseNode, prop: string, index: number) => void;
3231

@@ -170,17 +169,15 @@ export function convertHtmlxToJsx(
170169
case 'SlotTemplate':
171170
handleSvelteTag(htmlx, str, node);
172171
templateScopeManager.componentOrSlotTemplateOrElementEnter(node);
173-
if (usesLet(node)) {
174-
handleSlot(
175-
htmlx,
176-
str,
177-
node,
178-
parent,
179-
getSlotName(node) || 'default',
180-
ifScope,
181-
templateScopeManager.value
182-
);
183-
}
172+
handleSlot(
173+
htmlx,
174+
str,
175+
node,
176+
parent,
177+
getSlotName(node) || 'default',
178+
ifScope,
179+
templateScopeManager.value
180+
);
184181
break;
185182
case 'Text':
186183
handleText(str, node as Text);

0 commit comments

Comments
 (0)