Skip to content

Commit 2137c0d

Browse files
authored
(fix) svelte2tsx accessors config (#1129)
* support accessors config for svelte2tsx * format
1 parent d056640 commit 2137c0d

File tree

15 files changed

+149
-5
lines changed

15 files changed

+149
-5
lines changed

packages/language-server/src/plugins/typescript/DocumentSnapshot.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ function preprocessSvelteFile(document: Document, options: SvelteSnapshotOptions
166166
filename: document.getFilePath() ?? undefined,
167167
isTsFile: scriptKind === ts.ScriptKind.TSX,
168168
emitOnTemplateError: options.transformOnTemplateError,
169-
namespace: document.config?.compilerOptions?.namespace
169+
namespace: document.config?.compilerOptions?.namespace,
170+
accessors:
171+
document.config?.compilerOptions?.accessors ??
172+
document.config?.compilerOptions?.customElement
170173
});
171174
text = tsx.code;
172175
tsxMap = tsx.map;

packages/language-server/test/plugins/typescript/features/DiagnosticsProvider.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,4 +1777,64 @@ describe('DiagnosticsProvider', () => {
17771777
const diagnostics = await plugin.getDiagnostics(document);
17781778
assert.deepStrictEqual(diagnostics, []);
17791779
});
1780+
1781+
it('checks component with accessors when configured in svelte.config.js', async () => {
1782+
const { plugin, document } = setup('with-svelte-config/accessors-consumer.svelte');
1783+
1784+
const diagnostics = await plugin.getDiagnostics(document);
1785+
assert.deepStrictEqual(diagnostics, <typeof diagnostics>[
1786+
{
1787+
code: 2322,
1788+
message: "Type '\"\"' is not assignable to type 'number | undefined'.",
1789+
range: {
1790+
end: {
1791+
character: 12,
1792+
line: 9
1793+
},
1794+
start: {
1795+
character: 7,
1796+
line: 9
1797+
}
1798+
},
1799+
severity: 1,
1800+
source: 'ts',
1801+
tags: []
1802+
},
1803+
{
1804+
code: 2322,
1805+
message: "Type '\"\"' is not assignable to type 'number | undefined'.",
1806+
range: {
1807+
end: {
1808+
character: 20,
1809+
line: 9
1810+
},
1811+
start: {
1812+
character: 15,
1813+
line: 9
1814+
}
1815+
},
1816+
severity: 1,
1817+
source: 'ts',
1818+
tags: []
1819+
},
1820+
{
1821+
code: 2339,
1822+
message:
1823+
"Property 'abc' does not exist on type 'AccessorsAndOption__SvelteComponent_'.",
1824+
range: {
1825+
end: {
1826+
character: 29,
1827+
line: 9
1828+
},
1829+
start: {
1830+
character: 26,
1831+
line: 9
1832+
}
1833+
},
1834+
severity: 1,
1835+
source: 'ts',
1836+
tags: []
1837+
}
1838+
]);
1839+
});
17801840
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script lang="ts">
2+
export let abc = 1;
3+
</script>
4+
<svelte:options accessors={false} />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script lang="ts">
2+
export let abc = 1;
3+
</script>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
/**
3+
* @type {import('svelte/types/compiler/interfaces').CompileOptions}
4+
*/
5+
compilerOptions: {
6+
accessors: true
7+
}
8+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script lang="ts">
2+
import AccessorsAndOption from './acceesors/accessors-and-option.svelte';
3+
import Accessors from './acceesors/accessors.svelte';
4+
import CustomElement from './customElement/customElement.svelte';
5+
6+
let c: CustomElement;
7+
let a: Accessors;
8+
let ao: AccessorsAndOption;
9+
10+
$: c.abc = a.abc = ao.abc = '';
11+
</script>
12+
13+
<CustomElement />
14+
<Accessors />
15+
<AccessorsAndOption />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script lang="ts">
2+
export let abc = 1;
3+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
/**
3+
* @type {import('svelte/types/compiler/interfaces').CompileOptions}
4+
*/
5+
compilerOptions: {
6+
customElement: true
7+
}
8+
};

packages/svelte2tsx/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function svelte2tsx(
3434
emitOnTemplateError?: boolean;
3535
/**
3636
* The namespace option from svelte config
37+
* see https://svelte.dev/docs#svelte_compile for more info
3738
*/
3839
namespace?: string;
3940
/**
@@ -44,7 +45,13 @@ export function svelte2tsx(
4445
* The shims need to be provided by the user ambient-style,
4546
* for example through `filenames.push(require.resolve('svelte2tsx/svelte-shims.d.ts'))`.
4647
*/
47-
mode?: 'tsx' | 'dts'
48+
mode?: 'tsx' | 'dts',
49+
/**
50+
* The accessor option from svelte config.
51+
* Would be overridden by the same config in the svelte:option element if exist
52+
* see https://svelte.dev/docs#svelte_compile for more info
53+
*/
54+
accessors?: boolean
4855
}
4956
): SvelteCompiledToTsx
5057

packages/svelte2tsx/src/svelte2tsx/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ type TemplateProcessResult = {
3838

3939
function processSvelteTemplate(
4040
str: MagicString,
41-
options?: { emitOnTemplateError?: boolean; namespace?: string }
41+
options?: { emitOnTemplateError?: boolean; namespace?: string; accessors?: boolean }
4242
): TemplateProcessResult {
4343
const { htmlxAst, tags } = parseHtmlx(str.original, options);
4444

4545
let uses$$props = false;
4646
let uses$$restProps = false;
4747
let uses$$slots = false;
48-
let usesAccessors = false;
48+
let usesAccessors = !!options.accessors;
4949

5050
const componentDocumentation = new ComponentDocumentation();
5151

@@ -288,6 +288,7 @@ export function svelte2tsx(
288288
emitOnTemplateError?: boolean;
289289
namespace?: string;
290290
mode?: 'tsx' | 'dts';
291+
accessors?: boolean;
291292
} = {}
292293
) {
293294
const str = new MagicString(svelte);

0 commit comments

Comments
 (0)