Skip to content
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 @@ -1406,4 +1406,180 @@ describe('DiagnosticsProvider', () => {
}
]);
});

it('checks $$Slots usage', async () => {
const { plugin, document } = setup('$$slots.svelte');
const diagnostics = await plugin.getDiagnostics(document);
assert.deepStrictEqual(diagnostics, [
{
code: 2345,
message:
"Argument of type 'boolean' is not assignable to parameter of type 'string'.",
range: {
start: {
character: 41,
line: 13
},
end: {
character: 45,
line: 13
}
},
severity: 1,
source: 'ts',
tags: []
},
{
code: 2345,
message:
'Argument of type \'"invalidProp1"\' is not assignable to parameter of type \'"valid1" | "validPropWrongType1"\'.',
range: {
start: {
character: 60,
line: 13
},
end: {
character: 60,
line: 13
}
},
severity: 1,
source: 'ts',
tags: []
},
{
code: 2345,
message:
"Argument of type 'boolean' is not assignable to parameter of type 'string'.",
range: {
start: {
character: 52,
line: 14
},
end: {
character: 56,
line: 14
}
},
severity: 1,
source: 'ts',
tags: []
},
{
code: 2345,
message:
'Argument of type \'"invalidProp2"\' is not assignable to parameter of type \'"valid2" | "validPropWrongType2"\'.',
range: {
start: {
character: 71,
line: 14
},
end: {
character: 71,
line: 14
}
},
severity: 1,
source: 'ts',
tags: []
},
{
code: 2345,
message:
"Argument of type '\"invalid\"' is not assignable to parameter of type 'keyof $$Slots'.",
range: {
start: {
character: 26,
line: 15
},
end: {
character: 26,
line: 15
}
},
severity: 1,
source: 'ts',
tags: []
}
]);
});

it('checks $$Slots component usage', async () => {
const { plugin, document } = setup('using-$$slots.svelte');
const diagnostics = await plugin.getDiagnostics(document);
assert.deepStrictEqual(diagnostics, [
{
code: 2339,
message:
"Property 'invalidProp1' does not exist on type '{ valid1: boolean; validPropWrongType1: string; }'.",
range: {
start: {
character: 46,
line: 4
},
end: {
character: 58,
line: 4
}
},
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: 5,
line: 6
},
end: {
character: 33,
line: 6
}
},
severity: 1,
source: 'ts',
tags: []
},
{
code: 2339,
message:
"Property 'invalidProp2' does not exist on type '{ valid2: boolean; validPropWrongType2: string; }'.",
range: {
start: {
character: 59,
line: 8
},
end: {
character: 71,
line: 8
}
},
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: 9,
line: 10
},
end: {
character: 37,
line: 10
}
},
severity: 1,
source: 'ts',
tags: []
}
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script lang="ts">
interface $$Slots {
default: {
valid1: boolean;
validPropWrongType1: string;
},
foo: {
valid2: boolean;
validPropWrongType2: string;
}
}
</script>

<slot valid1={true} validPropWrongType1={true} invalidProp1={true} />
<slot name="foo" valid2={true} validPropWrongType2={true} invalidProp2={true} />
<slot name="invalid" prop={true} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script lang="ts">
import Slots from './$$slots.svelte';
</script>

<Slots let:valid1 let:validPropWrongType1 let:invalidProp1>
{valid1 === true}
{validPropWrongType1 === true}
{invalidProp1}
<div slot="foo" let:valid2 let:validPropWrongType2 let:invalidProp2>
{valid2 === true}
{validPropWrongType2 === true}
{invalidProp2}
</div>
</Slots>
43 changes: 35 additions & 8 deletions packages/svelte2tsx/src/htmlxtojsx/nodes/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export function handleAttribute(
parent: BaseNode,
preserveCase: boolean
): void {
const shouldApplySlotCheck = parent.type === 'Slot' && attr.name !== 'name';
const slotName = shouldApplySlotCheck
? parent.attributes?.find((a: BaseNode) => a.name === 'name')?.value[0]?.data || 'default'
: undefined;
const ensureSlotStr = `__sveltets_ensureSlot("${slotName}","${attr.name}",`;
let transformedFromDirectiveOrNamespace = false;

const transformAttributeCase = (name: string) => {
Expand Down Expand Up @@ -118,6 +123,10 @@ export function handleAttribute(
}

str.appendRight(attr.start, `${attrName}=`);
if (shouldApplySlotCheck) {
str.prependRight(attr.start + 1, ensureSlotStr);
str.prependLeft(attr.end - 1, ')');
}
return;
}

Expand Down Expand Up @@ -145,33 +154,51 @@ export function handleAttribute(
!isNaN(attrVal.data);

if (needsNumberConversion) {
const begin = '{' + (shouldApplySlotCheck ? ensureSlotStr : '');
const end = shouldApplySlotCheck ? ')}' : '}';
if (needsQuotes) {
str.prependRight(equals + 1, '{');
str.appendLeft(attr.end, '}');
str.prependRight(equals + 1, begin);
str.appendLeft(attr.end, end);
} else {
str.overwrite(equals + 1, equals + 2, '{');
str.overwrite(attr.end - 1, attr.end, '}');
str.overwrite(equals + 1, equals + 2, begin);
str.overwrite(attr.end - 1, attr.end, end);
}
} else if (needsQuotes) {
str.prependRight(equals + 1, '"');
str.appendLeft(attr.end, '"');
const begin = shouldApplySlotCheck ? `{${ensureSlotStr}"` : '"';
const end = shouldApplySlotCheck ? '")}' : '"';
str.prependRight(equals + 1, begin);
str.appendLeft(attr.end, end);
} else if (shouldApplySlotCheck) {
str.prependRight(equals + 1, `{${ensureSlotStr}`);
str.appendLeft(attr.end, ')}');
}
return;
}

if (attrVal.type == 'MustacheTag') {
const isInQuotes = attrVal.end != attr.end;
//if the end doesn't line up, we are wrapped in quotes
if (attrVal.end != attr.end) {
if (isInQuotes) {
str.remove(attrVal.start - 1, attrVal.start);
str.remove(attr.end - 1, attr.end);
}
if (shouldApplySlotCheck) {
str.prependRight(attrVal.start + 1, ensureSlotStr);
str.appendLeft(attr.end - (isInQuotes ? 2 : 1), ')');
}
return;
}
return;
}

// We have multiple attribute values, so we build a template string out of them.
buildTemplateString(attr, str, htmlx, '={`', '`}');
buildTemplateString(
attr,
str,
htmlx,
shouldApplySlotCheck ? `={${ensureSlotStr}\`` : '={`',
shouldApplySlotCheck ? '`)}' : '`}'
);
}

function buildTemplateString(
Expand Down
3 changes: 3 additions & 0 deletions packages/svelte2tsx/src/htmlxtojsx/nodes/slot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ interface ComponentNode extends BaseNode {
[shadowedPropsSymbol]?: PropsShadowedByLet[];
}

/**
* Transforms the usage of a slot (let:xx)
*/
export function handleSlot(
htmlx: string,
str: MagicString,
Expand Down
Loading