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) enable actions to enhance typings on applied element #1553
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
05995aa
(feat) enable actions to enhance typings on applied element
e155d65
cleanup
dummdidumm 3e84fd7
adjust typings
dummdidumm 74bb725
Merge branch 'master' into action-type
dummdidumm f243d4c
adjust typing
dummdidumm 48c1453
typecheck test
dummdidumm File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?