-
Notifications
You must be signed in to change notification settings - Fork 98
Open
Description
TypeScript type assertions lose opening parenthesis in bind:value expressions, causing syntax errors
Bug Description
When using Prettier with prettier-plugin-svelte
, TypeScript type assertions in bind:value
expressions lose their opening parenthesis, resulting in invalid syntax that breaks parsing.
Example
Before formatting (correct syntax):
<script lang="ts">
interface Config {
value: string;
}
let config: Config | Record<string, never> = { value: 'test' };
</script>
<input bind:value={(config as Config).value} />
After prettier --write (broken syntax):
<script lang="ts">
interface Config {
value: string;
}
let config: Config | Record<string, never> = { value: 'test' };
</script>
<input bind:value={config as Config).value} />
Notice the missing opening parenthesis: (config as Config)
becomes config as Config)
.
Impact
This creates a cascade of problems:
- Syntax Error: The code becomes unparseable due to mismatched parentheses
- ESLint Failure:
eslint --fix
breaks the file, making it impossible to lint on subsequent runs - Build Failure: Svelte compiler cannot parse the invalid syntax
Environment
- prettier: 3.6.2
- prettier-plugin-svelte: 3.4.0 (latest)
- svelte: 5.0.0
- Node.js: 20.19.4
- npm: 10.8.2
Reproduction
- Create a
.svelte
file with TypeScript type assertion inbind:value
- Run
prettier --write file.svelte
- Observe the missing opening parenthesis
Minimal Reproduction Case
<script lang="ts">
interface Config { value: string; }
let config: Config | Record<string, never> = { value: 'test' };
</script>
<input bind:value={(config as Config).value} />
Run: prettier --write file.svelte
Result: bind:value={config as Config).value}
(missing opening parenthesis)
Related Issues
This appears related to:
- Prettier adding surrounding parentheses on new function bindings #477 (opposite problem: unwanted parentheses addition in function bindings)
Expected Behavior
Prettier should preserve the parentheses around TypeScript type assertions in bind:value
expressions to maintain valid syntax.
Workaround
Use <!-- prettier-ignore -->
comments before affected lines:
<!-- prettier-ignore -->
<input bind:value={(config as Config).value} />
Metadata
Metadata
Assignees
Labels
No labels