Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) add experimental $$Events and strictEvents support #1054

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1293,4 +1293,117 @@ describe('DiagnosticsProvider', () => {
}
]);
});

it('checks $$Events usage', async () => {
const { plugin, document } = setup('$$events.svelte');
const diagnostics = await plugin.getDiagnostics(document);
assert.deepStrictEqual(diagnostics, [
{
code: 2345,
message:
"Argument of type 'true' is not assignable to parameter of type 'string | undefined'.",
range: {
start: {
character: 20,
line: 12
},
end: {
character: 24,
line: 12
}
},
severity: 1,
source: 'ts',
tags: []
},
{
code: 2345,
message:
'Argument of type \'"click"\' is not assignable to parameter of type \'"foo"\'.',
range: {
start: {
character: 13,
line: 13
},
end: {
character: 20,
line: 13
}
},
severity: 1,
source: 'ts',
tags: []
}
]);
});

it('checks $$Events component usage', async () => {
const { plugin, document } = setup('diagnostics-$$events.svelte');
const diagnostics = await plugin.getDiagnostics(document);
assert.deepStrictEqual(diagnostics, [
{
code: 2345,
message:
// Note: If you only run this test, the test message is slightly different for some reason
'Argument of type \'"bar"\' is not assignable to parameter of type \'"foo" | "click"\'.',
range: {
start: {
character: 10,
line: 7
},
end: {
character: 15,
line: 7
}
},
severity: 1,
source: 'ts',
tags: []
},
{
code: 2367,
message:
"This condition will always return 'false' since the types 'string' and 'boolean' have no overlap.",
range: {
start: {
character: 37,
line: 7
},
end: {
character: 54,
line: 7
}
},
severity: 1,
source: 'ts',
tags: []
}
]);
});

it('checks strictEvents', async () => {
const { plugin, document } = setup('diagnostics-strictEvents.svelte');
const diagnostics = await plugin.getDiagnostics(document);
assert.deepStrictEqual(diagnostics, [
{
code: 2345,
message:
// Note: If you only run this test, the test message is slightly different for some reason
'Argument of type \'"bar"\' is not assignable to parameter of type \'"foo" | "click"\'.',
range: {
start: {
character: 16,
line: 7
},
end: {
character: 21,
line: 7
}
},
severity: 1,
source: 'ts',
tags: []
}
]);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
interface ComponentEvents {
interface $$Events {
a: CustomEvent<boolean>;
/**
* TEST
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';

interface $$Events {
foo: CustomEvent<string>;
click: MouseEvent;
}

const dispatch = createEventDispatcher();
// valid
dispatch('foo', 'bar');
// invalid
dispatch('foo', true);
dispatch('click');
</script>

<button on:click>click</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script lang="ts">
import Events from './$$events.svelte';
</script>

<!-- valid -->
<Events on:click={e => e} on:foo={e => e.detail === 'bar'} />
<!-- invalid -->
<Events on:bar={e => e} on:foo={e => e.detail === true} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script lang="ts">
import StrictEvents from './strictEvents.svelte';
</script>

<!-- valid -->
<StrictEvents on:foo={e => e} on:click={e => e} />
<!-- invalid -->
<StrictEvents on:bar={e => e} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script strictEvents>
import { createEventDispatcher } from 'svelte';

const dispatch = createEventDispatcher();
dispatch('foo', 'bar');
</script>

<button on:click>click</button>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
interface ComponentEvents {
interface $$Events {
a: CustomEvent<boolean>;
/**
* TEST
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte2tsx/src/htmlxtojsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export function htmlx2jsx(
htmlx: string,
options?: { emitOnTemplateError?: boolean; preserveAttributeCase: boolean }
) {
const ast = parseHtmlx(htmlx, options);
const ast = parseHtmlx(htmlx, options).htmlxAst;
const str = new MagicString(htmlx);

convertHtmlxToJsx(str, ast, null, null, options);
Expand Down
10 changes: 7 additions & 3 deletions packages/svelte2tsx/src/svelte2tsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function processSvelteTemplate(
str: MagicString,
options?: { emitOnTemplateError?: boolean; namespace?: string }
): TemplateProcessResult {
const htmlxAst = parseHtmlx(str.original, options);
const { htmlxAst, tags } = parseHtmlx(str.original, options);

let uses$$props = false;
let uses$$restProps = false;
Expand Down Expand Up @@ -279,7 +279,11 @@ function processSvelteTemplate(
moduleScriptTag,
scriptTag,
slots: slotHandler.getSlotDef(),
events: new ComponentEvents(eventHandler),
events: new ComponentEvents(
eventHandler,
tags.some((tag) => tag.attributes?.some((a) => a.name === 'strictEvents')),
str
),
uses$$props,
uses$$restProps,
uses$$slots,
Expand Down Expand Up @@ -461,7 +465,7 @@ export function svelte2tsx(
addComponentExport({
str,
uses$$propsOr$$restProps: uses$$props || uses$$restProps,
strictEvents: events.hasInterface(),
strictEvents: events.hasStrictEvents(),
isTsFile: options?.isTsFile,
getters,
exportedNames,
Expand Down
Loading