Skip to content

Commit a032ff2

Browse files
authored
(rework) extend from svelte2tsx base class (#386)
Things like `$$prop_def` live in a `Svelte2TsxComponent` base class now. Saves some characters per transformation and opens up the door for easier manual `d.ts` creation.
1 parent ddaa8e9 commit a032ff2

File tree

71 files changed

+149
-296
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+149
-296
lines changed

packages/svelte2tsx/src/svelte2tsx.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ function processInstanceScriptContent(str: MagicString, script: Node): InstanceS
534534
if (
535535
(ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent)) &&
536536
parent.operator !==
537-
ts.SyntaxKind.ExclamationToken /* `!$store` does not need processing */
537+
ts.SyntaxKind.ExclamationToken /* `!$store` does not need processing */
538538
) {
539539
let simpleOperator: string;
540540
if (parent.operator === ts.SyntaxKind.PlusPlusToken) {
@@ -854,18 +854,17 @@ function addComponentExport(
854854
// the prop is optional
855855
strictMode && isTsFile
856856
? uses$$propsOr$$restProps
857-
? '__sveltets_with_any(render().props)'
858-
: 'render().props'
859-
: `__sveltets_partial${uses$$propsOr$$restProps ? '_with_any' : ''}(render().props)`;
857+
? '__sveltets_with_any(render)'
858+
: 'render'
859+
: `__sveltets_partial${uses$$propsOr$$restProps ? '_with_any' : ''}(render)`;
860860

861861
const doc = formatComponentDocumentation(componentDocumentation);
862862

863863
const statement =
864-
`\n\n${doc}export default class ${
865-
className ? `${className} ` : ''
866-
}{\n $$prop_def = ${propDef}\n $$slot_def = render().slots` +
864+
`\n\n${doc}export default class${
865+
className ? ` ${className}` : ''
866+
} extends createSvelte2TsxComponent(${propDef}) {` +
867867
createClassGetters(getters) +
868-
`\n $on = __sveltets_eventDef(render().events)` +
869868
'\n}';
870869

871870
str.append(statement);
@@ -949,7 +948,7 @@ function createRenderFunction({
949948

950949
const returnString =
951950
`\nreturn { props: ${exportedNames.createPropsStr(
952-
isTsFile
951+
isTsFile,
953952
)}, slots: ${slotsAsDef}, getters: ${createRenderFunctionGetterStr(getters)}` +
954953
`, events: ${eventMapToString(events)} }}`;
955954

@@ -1043,6 +1042,6 @@ export function svelte2tsx(
10431042
return {
10441043
code: str.toString(),
10451044
map: str.generateMap({ hires: true, source: options?.filename }),
1046-
exportedNames
1045+
exportedNames,
10471046
};
10481047
}

packages/svelte2tsx/svelte-shims.d.ts

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,62 @@
11
declare module '*.svelte' {
2-
export default class {
3-
$$prop_def: any;
4-
$$slot_def: any;
2+
export default Svelte2TsxComponent
3+
}
54

6-
$on(event: string, handler: (e: Event) => any): () => void
7-
}
5+
declare class Svelte2TsxComponent<
6+
Props extends {} = {},
7+
Events extends {} = {},
8+
Slots extends {} = {},
9+
StrictEvents extends boolean = true
10+
> {
11+
// svelte2tsx-specific
12+
/**
13+
* @internal This is for type checking capabilities only
14+
* and does not exist at runtime. Don't use this property.
15+
*/
16+
$$prop_def: Props;
17+
/**
18+
* @internal This is for type checking capabilities only
19+
* and does not exist at runtime. Don't use this property.
20+
*/
21+
$$slot_def: Slots;
22+
// https://svelte.dev/docs#Client-side_component_API
23+
constructor(options: {
24+
/**
25+
* An HTMLElement to render to. This option is required.
26+
*/
27+
target: Element;
28+
/**
29+
* A child of `target` to render the component immediately before.
30+
*/
31+
anchor?: Element;
32+
/**
33+
* An object of properties to supply to the component.
34+
*/
35+
props?: Props;
36+
hydrate?: boolean;
37+
intro?: boolean;
38+
$$inline?: boolean;
39+
});
40+
/**
41+
* Causes the callback function to be called whenever the component dispatches an event.
42+
* A function is returned that will remove the event listener when called.
43+
*/
44+
$on<K extends keyof Events>(event: K, handler: (e: SvelteExtractEvent<Events[K]>) => any): void;
45+
/**
46+
* Causes the callback function to be called whenever the component dispatches an event.
47+
* A function is returned that will remove the event listener when called.
48+
*/
49+
$on(event: StrictEvents extends true ? never : string, handler: (e: CustomEvent) => any): void;
50+
/**
51+
* Removes a component from the DOM and triggers any `onDestroy` handlers.
52+
*/
53+
$destroy(): void;
54+
/**
55+
* Programmatically sets props on an instance.
56+
* `component.$set({ x: 1 })` is equivalent to `x = 1` inside the component's `<script>` block.
57+
* Calling this method schedules an update for the next microtask — the DOM is __not__ updated synchronously.
58+
*/
59+
$set(props: Partial<Props>): void;
860
}
961

1062
type AConstructorTypeOf<T> = new (...args: any[]) => T;
@@ -35,7 +87,6 @@ type SvelteAnimation<U extends any[]> = (node: Element, move: { from: DOMRect, t
3587
type SvelteAllProps = { [index: string]: any }
3688
type SvelteRestProps = { [index: string]: any }
3789
type SvelteStore<T> = { subscribe: (run: (value: T) => any, invalidate?: any) => any }
38-
type SvelteComponent = import('*.svelte').default
3990
type SvelteEventRecord = Record<string, Event | Event[]>
4091
type SvelteExtractEvent<T> = T extends any[] ? T[number] : T;
4192
type SvelteOnEvent<T, K extends keyof T> = (
@@ -59,13 +110,19 @@ declare function __sveltets_ensureType<T>(type: AConstructorTypeOf<T>, el: T): {
59110
declare function __sveltets_instanceOf<T>(type: AConstructorTypeOf<T>): T;
60111
declare function __sveltets_allPropsType(): SvelteAllProps
61112
declare function __sveltets_restPropsType(): SvelteRestProps
62-
declare function __sveltets_partial<T>(obj: T): Partial<T>;
63-
declare function __sveltets_partial_with_any<T>(obj: T): Partial<T> & SvelteAllProps
64-
declare function __sveltets_with_any<T>(obj: T): T & SvelteAllProps
113+
declare function __sveltets_partial<Props = {}, Events = {}, Slots = {}>(
114+
render: () => {props?: Props, events?: Events, slots?: Slots }
115+
): () => {props?: Partial<Props>, events?: Events, slots?: Slots }
116+
declare function __sveltets_partial_with_any<Props = {}, Events = {}, Slots = {}>(
117+
render: () => {props?: Props, events?: Events, slots?: Slots }
118+
): () => {props?: Partial<Props> & SvelteAllProps, events?: Events, slots?: Slots }
119+
declare function __sveltets_with_any<Props = {}, Events = {}, Slots = {}>(
120+
render: () => {props?: Props, events?: Events, slots?: Slots }
121+
): () => {props?: Props & SvelteAllProps, events?: Events, slots?: Slots }
65122
declare function __sveltets_store_get<T = any>(store: SvelteStore<T>): T
66123
declare function __sveltets_any(dummy: any): any;
67124
declare function __sveltets_empty(dummy: any): {};
68-
declare function __sveltets_componentType(): AConstructorTypeOf<SvelteComponent>
125+
declare function __sveltets_componentType(): AConstructorTypeOf<Svelte2TsxComponent>
69126
declare function __sveltets_invalidate<T>(getValue: () => T): T
70127
declare function __sveltets_eventDef<T extends SvelteEventRecord>(def: T): SvelteOnAllEvent<T>
71128
declare function __sveltets_mapWindowEvent<K extends keyof HTMLBodyElementEventMap>(
@@ -107,3 +164,7 @@ declare function __sveltets_each<T>(
107164
array: ArrayLike<T>,
108165
callbackfn: (value: T, index: number) => any
109166
): any;
167+
168+
declare function createSvelte2TsxComponent<Props, Events, Slots>(
169+
render: () => {props?: Props, events?: Events, slots?: Slots }
170+
): AConstructorTypeOf<Svelte2TsxComponent<Props, Events, Slots, false>>;

packages/svelte2tsx/test/sourcemaps/event-binding.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
3==== 4==================
66
return { props: {}, slots: {}, getters: {}, events: {} }}
77

8-
export default class {
9-
$$prop_def = __sveltets_partial(render().props)
10-
$$slot_def = render().slots
11-
$on = __sveltets_eventDef(render().events)
8+
export default class extends createSvelte2TsxComponent(__sveltets_partial(render)) {
129
}
1310
!Expected
1411
<Component on:click={$check ? method1 : method2} />

packages/svelte2tsx/test/sourcemaps/let.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
</>);
77
return { props: {}, slots: {}, getters: {}, events: {} }}
88

9-
export default class {
10-
$$prop_def = __sveltets_partial(render().props)
11-
$$slot_def = render().slots
12-
$on = __sveltets_eventDef(render().events)
9+
export default class extends createSvelte2TsxComponent(__sveltets_partial(render)) {
1310
}
1411
!Expected
1512
<script>

packages/svelte2tsx/test/sourcemaps/repl.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,7 @@
179179
</>);
180180
return { props: {slug: slug , chapter: chapter}, slots: {}, getters: {}, events: {} }}
181181

182-
export default class {
183-
$$prop_def = __sveltets_partial(render().props)
184-
$$slot_def = render().slots
185-
$on = __sveltets_eventDef(render().events)
182+
export default class extends createSvelte2TsxComponent(__sveltets_partial(render)) {
186183
}
187184
!Expected
188185
<script context="module">

packages/svelte2tsx/test/svelte2tsx/samples/$store-index/expected.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@
44
{someObject.$store}</>
55
return { props: {}, slots: {}, getters: {}, events: {} }}
66

7-
export default class Input__SvelteComponent_ {
8-
$$prop_def = __sveltets_partial(render().props)
9-
$$slot_def = render().slots
10-
$on = __sveltets_eventDef(render().events)
7+
export default class Input__SvelteComponent_ extends createSvelte2TsxComponent(__sveltets_partial(render)) {
118
}

packages/svelte2tsx/test/svelte2tsx/samples/array-binding-export/expected.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,5 @@
55
() => (<></>);
66
return { props: {a: a , b: b , c: c}, slots: {}, getters: {}, events: {} }}
77

8-
export default class Input__SvelteComponent_ {
9-
$$prop_def = __sveltets_partial(render().props)
10-
$$slot_def = render().slots
11-
$on = __sveltets_eventDef(render().events)
8+
export default class Input__SvelteComponent_ extends createSvelte2TsxComponent(__sveltets_partial(render)) {
129
}

packages/svelte2tsx/test/svelte2tsx/samples/ast-offset-none/expected.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@ __sveltets_store_get(var);
33
() => (<></>);
44
return { props: {}, slots: {}, getters: {}, events: {} }}
55

6-
export default class Input__SvelteComponent_ {
7-
$$prop_def = __sveltets_partial(render().props)
8-
$$slot_def = render().slots
9-
$on = __sveltets_eventDef(render().events)
6+
export default class Input__SvelteComponent_ extends createSvelte2TsxComponent(__sveltets_partial(render)) {
107
}

packages/svelte2tsx/test/svelte2tsx/samples/ast-offset-some/expected.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@
33
() => (<></>);
44
return { props: {}, slots: {}, getters: {}, events: {} }}
55

6-
export default class Input__SvelteComponent_ {
7-
$$prop_def = __sveltets_partial(render().props)
8-
$$slot_def = render().slots
9-
$on = __sveltets_eventDef(render().events)
6+
export default class Input__SvelteComponent_ extends createSvelte2TsxComponent(__sveltets_partial(render)) {
107
}

packages/svelte2tsx/test/svelte2tsx/samples/await-with-$store/expected.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,5 @@ function render() {
1414
</>})}}</>);
1515
return { props: {}, slots: {}, getters: {}, events: {} }}
1616

17-
export default class Input__SvelteComponent_ {
18-
$$prop_def = __sveltets_partial(render().props)
19-
$$slot_def = render().slots
20-
$on = __sveltets_eventDef(render().events)
17+
export default class Input__SvelteComponent_ extends createSvelte2TsxComponent(__sveltets_partial(render)) {
2118
}

0 commit comments

Comments
 (0)