Skip to content

Commit b1b3810

Browse files
authored
(fix) allow null and undefined for style and class directives (#1432)
#1423
1 parent fcea9ce commit b1b3810

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed

packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/style-directive/expected.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
[
22
{
33
"range": {
4-
"start": { "line": 14, "character": 11 },
5-
"end": { "line": 14, "character": 16 }
4+
"start": { "line": 16, "character": 11 },
5+
"end": { "line": 16, "character": 16 }
66
},
77
"severity": 1,
88
"source": "ts",
9-
"message": "Argument of type 'boolean' is not assignable to parameter of type 'String | Number'.",
9+
"message": "Argument of type 'boolean' is not assignable to parameter of type 'String | Number | null | undefined'.",
1010
"code": 2345,
1111
"tags": []
1212
},
1313
{
1414
"range": {
15-
"start": { "line": 15, "character": 18 },
16-
"end": { "line": 15, "character": 23 }
15+
"start": { "line": 17, "character": 18 },
16+
"end": { "line": 17, "character": 23 }
1717
},
1818
"severity": 1,
1919
"source": "ts",
20-
"message": "Argument of type 'boolean' is not assignable to parameter of type 'String | Number'.",
20+
"message": "Argument of type 'boolean' is not assignable to parameter of type 'String | Number | null | undefined'.",
2121
"code": 2345,
2222
"tags": []
2323
}

packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/style-directive/expectedv2.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
[
22
{
33
"range": {
4-
"start": { "line": 14, "character": 11 },
5-
"end": { "line": 14, "character": 16 }
4+
"start": { "line": 16, "character": 11 },
5+
"end": { "line": 16, "character": 16 }
66
},
77
"severity": 1,
88
"source": "ts",
9-
"message": "Argument of type 'boolean' is not assignable to parameter of type 'String | Number'.",
9+
"message": "Argument of type 'boolean' is not assignable to parameter of type 'String | Number | null | undefined'.",
1010
"code": 2345,
1111
"tags": []
1212
},
1313
{
1414
"range": {
15-
"start": { "line": 15, "character": 18 },
16-
"end": { "line": 15, "character": 23 }
15+
"start": { "line": 17, "character": 18 },
16+
"end": { "line": 17, "character": 23 }
1717
},
1818
"severity": 1,
1919
"source": "ts",
20-
"message": "Argument of type 'boolean' is not assignable to parameter of type 'String | Number'.",
20+
"message": "Argument of type 'boolean' is not assignable to parameter of type 'String | Number | null | undefined'.",
2121
"code": 2345,
2222
"tags": []
2323
}

packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/style-directive/input.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<div style:right="right" />
1111
<div style:right={`right${right}`} />
1212
<div style:right=right{right} />
13+
<div style:undefined={undefined} />
14+
<div style:null={null} />
1315

1416
<!-- error -->
1517
<div style:wrong />

packages/svelte2tsx/src/htmlxtojsx_v2/nodes/StyleDirective.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ export function handleStyleDirective(
1414
element: Element
1515
): void {
1616
const htmlx = str.original;
17+
const ensureType = '__sveltets_2_ensureType(String, Number, ';
1718
if (style.value === true || style.value.length === 0) {
1819
element.appendToStartEnd([
19-
'__sveltets_2_ensureType(String, Number, ',
20+
ensureType,
2021
[htmlx.indexOf(':', style.start) + 1, style.end],
2122
');'
2223
]);
@@ -40,11 +41,7 @@ export function handleStyleDirective(
4041
str.appendRight(n.start, '$');
4142
}
4243
}
43-
element.appendToStartEnd([
44-
'__sveltets_2_ensureType(String, Number, `',
45-
[start, end],
46-
'`);'
47-
]);
44+
element.appendToStartEnd([ensureType + '`', [start, end], '`);']);
4845
return;
4946
}
5047

@@ -53,13 +50,9 @@ export function handleStyleDirective(
5350
const quote = ['"', "'"].includes(str.original[styleVal.start - 1])
5451
? str.original[styleVal.start - 1]
5552
: '"';
56-
element.appendToStartEnd([
57-
`__sveltets_2_ensureType(String, Number, ${quote}`,
58-
[start, end],
59-
`${quote});`
60-
]);
53+
element.appendToStartEnd([`${ensureType}${quote}`, [start, end], `${quote});`]);
6154
} else {
6255
// MustacheTag
63-
element.appendToStartEnd(['__sveltets_2_ensureType(String, Number, ', [start, end], ');']);
56+
element.appendToStartEnd([ensureType, [start, end], ');']);
6457
}
6558
}

packages/svelte2tsx/svelte-shims.d.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ declare function __sveltets_1_ensureAnimation(animationCall: SvelteAnimationRetu
119119
declare function __sveltets_1_ensureAction(actionCall: SvelteActionReturnType): {};
120120
declare function __sveltets_1_ensureTransition(transitionCall: SvelteTransitionReturnType): {};
121121
declare function __sveltets_1_ensureFunction(expression: (e: Event & { detail?: any }) => unknown ): {};
122-
declare function __sveltets_1_ensureType<T>(type: AConstructorTypeOf<T>, el: T): {};
123-
declare function __sveltets_1_ensureType<T1, T2>(type1: AConstructorTypeOf<T1>, type2: AConstructorTypeOf<T2>, el: T1 | T2): {};
122+
// Includes undefined and null for all types as all usages also allow these
123+
declare function __sveltets_1_ensureType<T>(type: AConstructorTypeOf<T>, el: T | undefined | null): {};
124+
declare function __sveltets_1_ensureType<T1, T2>(type1: AConstructorTypeOf<T1>, type2: AConstructorTypeOf<T2>, el: T1 | T2 | undefined | null): {};
125+
124126
declare function __sveltets_1_createEnsureSlot<Slots = Record<string, Record<string, any>>>(): <K1 extends keyof Slots, K2 extends keyof Slots[K1]>(k1: K1, k2: K2, val: Slots[K1][K2]) => Slots[K1][K2];
125127
declare function __sveltets_1_ensureRightProps<Props>(props: Props): {};
126128
declare function __sveltets_1_cssProp(prop: Record<string, any>): {};
@@ -253,8 +255,9 @@ type __sveltets_2_SvelteTransitionConfig = {
253255
type __sveltets_2_SvelteTransitionReturnType = __sveltets_2_SvelteTransitionConfig | (() => __sveltets_2_SvelteTransitionConfig)
254256
declare function __sveltets_2_ensureTransition(transitionCall: __sveltets_2_SvelteTransitionReturnType): {};
255257

256-
declare function __sveltets_2_ensureType<T>(type: AConstructorTypeOf<T>, el: T): {};
257-
declare function __sveltets_2_ensureType<T1, T2>(type1: AConstructorTypeOf<T1>, type2: AConstructorTypeOf<T2>, el: T1 | T2): {};
258+
// Includes undefined and null for all types as all usages also allow these
259+
declare function __sveltets_2_ensureType<T>(type: AConstructorTypeOf<T>, el: T | undefined | null): {};
260+
declare function __sveltets_2_ensureType<T1, T2>(type1: AConstructorTypeOf<T1>, type2: AConstructorTypeOf<T2>, el: T1 | T2 | undefined | null): {};
258261

259262
// The following is necessary because there are two clashing errors that can't be solved at the same time
260263
// when using Svelte2TsxComponent, more precisely the event typings in

0 commit comments

Comments
 (0)