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
jyc.dev 4 days ago committed by GitHub
parent c4daa490bb
commit a6985bcd24
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: strip `?` from optional parameters in `<script lang="ts">` so generated JavaScript is valid

@ -181,7 +181,7 @@
"clsx": "^2.1.1",
"devalue": "^5.8.1",
"esm-env": "^1.2.1",
"esrap": "^2.2.11",
"esrap": "^2.2.12",
"is-reference": "^3.0.3",
"locate-character": "^3.0.0",
"magic-string": "^0.30.11",

@ -30,6 +30,12 @@ const visitors = {
delete n.readonly;
delete n.definite;
delete n.override;
// `optional` is reused by JS optional chaining (`a?.b`, `a?.()`), so only
// strip the TypeScript optional marker (`x?: T`, `m?(): T`, `x?: T` fields)
if (n.type !== 'MemberExpression' && n.type !== 'CallExpression') {
delete n.optional;
}
},
Decorator(node) {
e.typescript_invalid_feature(node, 'decorators (related TSC proposal is not stage 4 yet)');

@ -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}

@ -102,8 +102,8 @@ importers:
specifier: ^1.2.1
version: 1.2.1
esrap:
specifier: ^2.2.11
version: 2.2.11(@typescript-eslint/types@8.59.4)
specifier: ^2.2.12
version: 2.2.12(@typescript-eslint/types@8.59.4)
is-reference:
specifier: ^3.0.3
version: 3.0.3
@ -1418,8 +1418,8 @@ packages:
resolution: {integrity: sha512-Ap6G0WQwcU/LHsvLwON1fAQX9Zp0A2Y6Y/cJBl9r/JbW90Zyg4/zbG6zzKa2OTALELarYHmKu0GhpM5EO+7T0g==}
engines: {node: '>=0.10'}
esrap@2.2.11:
resolution: {integrity: sha512-gPdx+I+BjYEinNMQaBXFjbaJVyoPMU4ZODg5mE+M4DqVG9VusAVHHjcBX+zqyITlI0DIARwDMMzZwAWj36dRoQ==}
esrap@2.2.12:
resolution: {integrity: sha512-On0QbLyaiAkVC4eXtgnXK9Kh2opit+3rcUSOc45DqJ2s/X2eXAHsGOKRSJ6IDagQEW5vPyivANfXUiqgXC67Rw==}
peerDependencies:
'@typescript-eslint/types': ^8.2.0
peerDependenciesMeta:
@ -2886,7 +2886,7 @@ snapshots:
'@jridgewell/gen-mapping@0.3.5':
dependencies:
'@jridgewell/set-array': 1.2.1
'@jridgewell/sourcemap-codec': 1.5.0
'@jridgewell/sourcemap-codec': 1.5.5
'@jridgewell/trace-mapping': 0.3.31
'@jridgewell/remapping@2.3.4':
@ -3695,7 +3695,7 @@ snapshots:
dependencies:
estraverse: 5.3.0
esrap@2.2.11(@typescript-eslint/types@8.59.4):
esrap@2.2.12(@typescript-eslint/types@8.59.4):
dependencies:
'@jridgewell/sourcemap-codec': 1.5.5
optionalDependencies:

Loading…
Cancel
Save