-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) enable actions to enhance typings on applied element (#1553)
- Loading branch information
1 parent
7d6d2e6
commit 9e1f5d3
Showing
15 changed files
with
249 additions
and
92 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...plugins/typescript/features/diagnostics/fixtures/actions-enhance-types.only/expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} | ||
] |
24 changes: 24 additions & 0 deletions
24
...ugins/typescript/features/diagnostics/fixtures/actions-enhance-types.only/expectedv2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} | ||
] |
27 changes: 27 additions & 0 deletions
27
.../plugins/typescript/features/diagnostics/fixtures/actions-enhance-types.only/input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
*/ | ||
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); | ||
} |
Oops, something went wrong.