mirror of https://github.com/sveltejs/svelte
fix: strip `?` from optional parameters in svelte lang ts (#18448)
In `esrap@2.2.12` we fixed the missing `?` in ts files https://github.com/sveltejs/esrap/pull/139 It's good for `ts`, but bad for svelte compiler that was not removing it as it was a esrap issue not printing it :/ This issue came: https://github.com/sveltejs/esrap/issues/141 This PR is fixing the issue in the compiler stripping the `?`pull/16743/merge
parent
c4daa490bb
commit
a6985bcd24
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: strip `?` from optional parameters in `<script lang="ts">` so generated JavaScript is valid
|
||||
@ -0,0 +1,22 @@
|
||||
import 'svelte/internal/disclose-version';
|
||||
import 'svelte/internal/flags/legacy';
|
||||
import * as $ from 'svelte/internal/client';
|
||||
|
||||
export default function Typescript_optional_parameter($$anchor) {
|
||||
// the `?` on the optional parameter must be stripped, but optional chaining
|
||||
// (`x?.length`, `o?.b`) must be preserved
|
||||
function a(x) {
|
||||
return x?.length;
|
||||
}
|
||||
|
||||
const o = {};
|
||||
const v = o?.b?.c;
|
||||
|
||||
a();
|
||||
$.next();
|
||||
|
||||
var text = $.text();
|
||||
|
||||
$.template_effect(() => $.set_text(text, v));
|
||||
$.append($$anchor, text);
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
import * as $ from 'svelte/internal/server';
|
||||
|
||||
export default function Typescript_optional_parameter($$renderer) {
|
||||
// the `?` on the optional parameter must be stripped, but optional chaining
|
||||
// (`x?.length`, `o?.b`) must be preserved
|
||||
function a(x) {
|
||||
return x?.length;
|
||||
}
|
||||
|
||||
const o = {};
|
||||
const v = o?.b?.c;
|
||||
|
||||
a();
|
||||
$$renderer.push(`<!---->${$.escape(v)}`);
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
<script lang="ts">
|
||||
// the `?` on the optional parameter must be stripped, but optional chaining
|
||||
// (`x?.length`, `o?.b`) must be preserved
|
||||
function a(x?: string) {
|
||||
return x?.length;
|
||||
}
|
||||
|
||||
const o: { b?: { c: number } } = {};
|
||||
const v = o?.b?.c;
|
||||
|
||||
a();
|
||||
</script>
|
||||
|
||||
{v}
|
||||
Loading…
Reference in new issue