Skip to content

Commit 0655c67

Browse files
authored
fix: support module script attribute (#2473)
fixes #2472
1 parent 815a1ff commit 0655c67

File tree

4 files changed

+80
-47
lines changed

4 files changed

+80
-47
lines changed

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

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -483,60 +483,66 @@ describe('SveltePlugin#getDiagnostics', () => {
483483
const { plugin, document } = setupFromFile('diagnostics-module.svelte');
484484
const diagnostics = await plugin.getDiagnostics(document);
485485

486-
assert.deepStrictEqual(diagnostics, [
487-
{
488-
range: { start: { line: 1, character: 4 }, end: { line: 1, character: 26 } },
489-
message: isSvelte5Plus
490-
? 'Reactive declarations only exist at the top level of the instance script'
491-
: '$: has no effect in a module script',
492-
severity: 2,
493-
source: 'svelte',
494-
code: isSvelte5Plus
495-
? 'reactive_declaration_invalid_placement'
496-
: 'module-script-reactive-declaration'
497-
}
498-
]);
486+
assert.deepStrictEqual(
487+
diagnostics.filter((d) => d.code !== 'script_context_deprecated'),
488+
[
489+
{
490+
range: { start: { line: 1, character: 4 }, end: { line: 1, character: 26 } },
491+
message: isSvelte5Plus
492+
? 'Reactive declarations only exist at the top level of the instance script'
493+
: '$: has no effect in a module script',
494+
severity: 2,
495+
source: 'svelte',
496+
code: isSvelte5Plus
497+
? 'reactive_declaration_invalid_placement'
498+
: 'module-script-reactive-declaration'
499+
}
500+
]
501+
);
499502
});
500503

501504
it('should correctly determine diagnostic position for script when theres also context="module"', async () => {
502505
const { plugin, document } = setupFromFile('diagnostics-module-and-instance.svelte');
503506
const diagnostics = await plugin.getDiagnostics(document);
504507

505-
assert.deepStrictEqual(diagnostics, [
506-
{
507-
code: isSvelte5Plus ? 'export_let_unused' : 'unused-export-let',
508-
message:
509-
"Component has unused export property 'unused1'. If it is for external reference only, please consider using `export const unused1`",
510-
range: {
511-
start: {
512-
line: 5,
513-
character: 13
508+
assert.deepStrictEqual(
509+
diagnostics.filter((d) => d.code !== 'script_context_deprecated'),
510+
[
511+
{
512+
code: isSvelte5Plus ? 'export_let_unused' : 'unused-export-let',
513+
message:
514+
"Component has unused export property 'unused1'. If it is for external reference only, please consider using `export const unused1`",
515+
range: {
516+
start: {
517+
line: 5,
518+
character: 13
519+
},
520+
end: {
521+
line: 5,
522+
character: isSvelte5Plus ? 20 : 27
523+
}
514524
},
515-
end: {
516-
line: 5,
517-
character: isSvelte5Plus ? 20 : 27
518-
}
525+
severity: 2,
526+
source: 'svelte'
519527
},
520-
severity: 2,
521-
source: 'svelte'
522-
},
523-
{
524-
code: isSvelte5Plus ? 'export_let_unused' : 'unused-export-let',
525-
message:
526-
"Component has unused export property 'unused2'. If it is for external reference only, please consider using `export const unused2`",
527-
range: {
528-
start: {
529-
line: 6,
530-
character: 13
528+
{
529+
code: isSvelte5Plus ? 'export_let_unused' : 'unused-export-let',
530+
message:
531+
"Component has unused export property 'unused2'. If it is for external reference only, please consider using `export const unused2`",
532+
range: {
533+
start: {
534+
line: 6,
535+
character: 13
536+
},
537+
end: {
538+
line: 6,
539+
character: isSvelte5Plus ? 20 : 27
540+
}
531541
},
532-
end: {
533-
line: 6,
534-
character: isSvelte5Plus ? 20 : 27
535-
}
536-
},
537-
severity: 2,
538-
source: 'svelte'
539-
}
540-
]);
542+
severity: 2,
543+
source: 'svelte'
544+
}
545+
]
546+
);
541547
});
542548
});

packages/svelte2tsx/src/svelte2tsx/nodes/Scripts.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ export class Scripts {
3535
if (
3636
tag.attributes &&
3737
tag.attributes.find(
38-
(a) => a.name == 'context' && a.value.length == 1 && a.value[0].raw == 'module'
38+
(a) =>
39+
(a.name == 'context' &&
40+
a.value.length == 1 &&
41+
a.value[0].raw == 'module') ||
42+
a.name === 'module'
3943
)
4044
) {
4145
moduleScriptTag = tag;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
///<reference types="svelte" />
2+
;
3+
export function preload() {}
4+
let b = 5;
5+
;;function render() {
6+
7+
let world = "name"
8+
;
9+
async () => {
10+
11+
{ svelteHTML.createElement("h1", {}); world; }};
12+
return { props: {world: world}, slots: {}, events: {} }}
13+
14+
export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(['world'], __sveltets_2_with_any_event(render()))) {
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
export let world = "name"
3+
</script>
4+
<script module>
5+
export function preload() {}
6+
let b = 5;
7+
</script>
8+
<h1>hello {world}</h1>

0 commit comments

Comments
 (0)