mirror of https://github.com/sveltejs/svelte
Detect TypeScript `type` declarations by actually parsing instead of by
character-class blacklists, so that expressions like `{type === 'all' ? a : b}`
or `{type instanceof Foo}` aren't misclassified as malformed declarations.
Closes #18328
pull/18330/head
parent
e027d8bf22
commit
4bf2a373e7
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: don't mistake expressions starting with `type` (e.g. `{type === 'all' ? a : b}`) for TypeScript `type` declarations in tags
|
||||
@ -0,0 +1 @@
|
||||
{type instanceof /* probe */ Object}
|
||||
@ -0,0 +1,92 @@
|
||||
{
|
||||
"css": null,
|
||||
"js": [],
|
||||
"start": 0,
|
||||
"end": 36,
|
||||
"type": "Root",
|
||||
"fragment": {
|
||||
"type": "Fragment",
|
||||
"nodes": [
|
||||
{
|
||||
"type": "ExpressionTag",
|
||||
"start": 0,
|
||||
"end": 36,
|
||||
"expression": {
|
||||
"type": "BinaryExpression",
|
||||
"start": 1,
|
||||
"end": 35,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 35
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
"type": "Identifier",
|
||||
"start": 1,
|
||||
"end": 5,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
}
|
||||
},
|
||||
"name": "type"
|
||||
},
|
||||
"operator": "instanceof",
|
||||
"right": {
|
||||
"type": "Identifier",
|
||||
"start": 29,
|
||||
"end": 35,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 29
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 35
|
||||
}
|
||||
},
|
||||
"name": "Object",
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "Block",
|
||||
"value": " probe ",
|
||||
"start": 17,
|
||||
"end": 28
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"options": null,
|
||||
"comments": [
|
||||
{
|
||||
"type": "Block",
|
||||
"value": " probe ",
|
||||
"start": 17,
|
||||
"end": 28,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 17
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 28
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
html: `<ul><li>a</li><li>b</li><li>All types</li></ul><p data-cast="">cast</p><p data-generic-call="">generic-call</p><p data-generic-call-space="">generic-call-space</p>`
|
||||
});
|
||||
@ -0,0 +1,16 @@
|
||||
<script lang="ts">
|
||||
const assetTypes = ['a', 'b', 'all'] as const;
|
||||
function type<T>(x: T): T {
|
||||
return x;
|
||||
}
|
||||
</script>
|
||||
|
||||
<ul>
|
||||
{#each assetTypes as type (type)}
|
||||
<li>{type === 'all' ? 'All types' : type}</li>
|
||||
{/each}
|
||||
</ul>
|
||||
|
||||
<p data-cast>{'cast' as string}</p>
|
||||
<p data-generic-call>{type<string>('generic-call')}</p>
|
||||
<p data-generic-call-space>{type <string>('generic-call-space')}</p>
|
||||
@ -0,0 +1,14 @@
|
||||
[
|
||||
{
|
||||
"code": "declaration_tag_invalid_type",
|
||||
"message": "Declaration tags must be `let` or `const` declarations",
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 33
|
||||
}
|
||||
}
|
||||
]
|
||||
@ -0,0 +1,2 @@
|
||||
<script lang="ts"></script>
|
||||
{type Foo<T extends string> = T[]}
|
||||
@ -0,0 +1,14 @@
|
||||
[
|
||||
{
|
||||
"code": "declaration_tag_invalid_type",
|
||||
"message": "Declaration tags must be `let` or `const` declarations",
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 16
|
||||
}
|
||||
}
|
||||
]
|
||||
@ -0,0 +1,2 @@
|
||||
<script lang="ts"></script>
|
||||
{type Ω = string}
|
||||
Loading…
Reference in new issue