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
@@ -0,0 +1,68 @@
[
{
"range": {
"start": { "line": 22, "character": 9 },
"end": { "line": 22, "character": 14 }
},
"severity": 1,
"source": "ts",
"message": "Argument of type '{ $$_attributes: { foo: string; 'on:bar': (e: CustomEvent<boolean>) => void; }; }' is not assignable to parameter of type 'SvelteActionReturnType'.",
"code": 2345,
"tags": []
},
{
"range": {
"start": { "line": 22, "character": 16 },
"end": { "line": 22, "character": 19 }
},
"severity": 1,
"source": "ts",
"message": "Type '{ foo: string; onbar: (e: CustomEvent<boolean>) => void; }' is not assignable to type 'HTMLProps<HTMLDivElement>'.\n Property 'foo' does not exist on type 'HTMLProps<HTMLDivElement>'.",
"code": 2322,
"tags": []
},
{
"range": {
"start": { "line": 25, "character": 9 },
"end": { "line": 25, "character": 14 }
},
"severity": 1,
"source": "ts",
"message": "Argument of type '{ $$_attributes: { foo: string; 'on:bar': (e: CustomEvent<boolean>) => void; }; }' is not assignable to parameter of type 'SvelteActionReturnType'.",
"code": 2345,
"tags": []
},
{
"range": {
"start": { "line": 25, "character": 16 },
"end": { "line": 25, "character": 19 }
},
"severity": 1,
"source": "ts",
"message": "Type '{ foo: number; onbar: (e: CustomEvent<boolean>) => void; }' is not assignable to type 'HTMLProps<HTMLDivElement>'.\n Property 'foo' does not exist on type 'HTMLProps<HTMLDivElement>'.",
"code": 2322,
"tags": []
},
{
"range": {
"start": { "line": 26, "character": 9 },
"end": { "line": 26, "character": 14 }
},
"severity": 1,
"source": "ts",
"message": "Argument of type '{ $$_attributes: { foo: string; 'on:bar': (e: CustomEvent<boolean>) => void; }; }' is not assignable to parameter of type 'SvelteActionReturnType'.",
"code": 2345,
"tags": []
},
{
"range": {
"start": { "line": 26, "character": 16 },
"end": { "line": 26, "character": 19 }
},
"severity": 1,
"source": "ts",
"message": "Type '{ foo: string; onbar: (e: CustomEvent<string>) => void; }' is not assignable to type 'HTMLProps<HTMLDivElement>'.\n Property 'foo' does not exist on type 'HTMLProps<HTMLDivElement>'.",
"code": 2322,
"tags": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"range": {
"start": { "line": 25, "character": 16 },
"end": { "line": 25, "character": 19 }
},
"severity": 1,
"source": "ts",
"message": "Type 'number' is not assignable to type 'string'.",
"code": 2322,
"tags": []
},
{
"range": {
"start": { "line": 26, "character": 31 },
"end": { "line": 26, "character": 34 }
},
"severity": 1,
"source": "ts",
"message": "Type '(e: CustomEvent<string>) => void' is not assignable to type '(e: CustomEvent<boolean>) => void'.\n Types of parameters 'e' and 'e' are incompatible.\n Type 'CustomEvent<boolean>' is not assignable to type 'CustomEvent<string>'.\n Type 'boolean' is not assignable to type 'string'.",
"code": 2322,
"tags": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script lang="ts">
function action(node: any) {
node;
return {
// TODO: replace this with the Svelte interface generic once it lands in Svelte; this fails for the old transformation
$$_attributes: {
foo: 'string',
'on:bar': (e: CustomEvent<boolean>) => {e;}
}
}
}

function onBar(e: CustomEvent<boolean>) {
e;
}

function onWrongBar(e: CustomEvent<string>) {
e;
}
</script>

<!-- valid for new transformation -->
<div use:action foo="valid" on:bar={onBar} />

<!-- invalid -->
<div use:action foo={1} on:bar={onBar} />
<div use:action foo="valid" on:bar={onWrongBar} />
2 changes: 1 addition & 1 deletion packages/svelte2tsx/src/htmlxtojsx_v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export function convertHtmlxToJsx(
handleStyleDirective(str, node as StyleDirective, element as Element);
break;
case 'Action':
handleActionDirective(str, node as BaseDirective, element as Element);
handleActionDirective(node as BaseDirective, element as Element);
break;
case 'Transition':
handleTransitionDirective(str, node as BaseDirective, element as Element);
Expand Down
27 changes: 2 additions & 25 deletions packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Action.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
import MagicString from 'magic-string';
import { BaseDirective } from '../../interfaces';
import {
getDirectiveNameStartEndIdx,
rangeWithTrailingPropertyAccess,
TransformationArray
} from '../utils/node-utils';
import { Element } from './Element';

/**
* use:xxx={params} ---> __sveltets_2_ensureAction(xxx(svelte.mapElementTag('ParentNodeName'),(params)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this comment is still valid?

*/
export function handleActionDirective(
str: MagicString,
attr: BaseDirective,
element: Element
): void {
const transformations: TransformationArray = [
'__sveltets_2_ensureAction(',
getDirectiveNameStartEndIdx(str, attr),
`(${element.typingsNamespace}.mapElementTag('${element.tagName}')`
];
if (attr.expression) {
transformations.push(
',(',
rangeWithTrailingPropertyAccess(str.original, attr.expression),
')'
);
}
transformations.push('));');
element.appendToStartEnd(transformations);
export function handleActionDirective(attr: BaseDirective, element: Element): void {
element.addAction(attr);
}
Loading