-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support type annotations in {@const ...}
tag
#9609
Changes from 8 commits
5e30745
fe4542f
731b8cf
4d89c84
bf4e7aa
a7d253a
dc5c19d
87c1ba3
cba043c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': minor | ||
--- | ||
|
||
feat: support type definition in {@const} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,8 @@ import read_context from '../read/context.js'; | |
import read_expression from '../read/expression.js'; | ||
import { error } from '../../../errors.js'; | ||
import { create_fragment } from '../utils/create.js'; | ||
import { parse_expression_at } from '../acorn.js'; | ||
import { walk } from 'zimmerframe'; | ||
import { parse } from '../acorn.js'; | ||
|
||
const regex_whitespace_with_closing_curly_brace = /^\s*}/; | ||
|
||
|
@@ -532,21 +532,54 @@ function special(parser) { | |
// {@const a = b} | ||
parser.require_whitespace(); | ||
|
||
const expression = read_expression(parser); | ||
const CONST_LENGTH = 'const '.length; | ||
parser.index = parser.index - CONST_LENGTH; | ||
|
||
let end_index = parser.index; | ||
/** @type {import('estree').VariableDeclaration | undefined} */ | ||
let declaration = undefined; | ||
|
||
if (!(expression.type === 'AssignmentExpression' && expression.operator === '=')) { | ||
const dummy_spaces = parser.template.substring(0, parser.index).replace(/[^\n]/g, ' '); | ||
while (true) { | ||
end_index = parser.template.indexOf('}', end_index + 1); | ||
if (end_index === -1) break; | ||
try { | ||
const node = parse( | ||
dummy_spaces + parser.template.substring(parser.index, end_index), | ||
parser.ts | ||
).body[0]; | ||
if (node?.type === 'VariableDeclaration') { | ||
declaration = node; | ||
break; | ||
} | ||
} catch (e) { | ||
continue; | ||
} | ||
} | ||
|
||
if ( | ||
declaration === undefined || | ||
declaration.declarations.length !== 1 || | ||
declaration.declarations[0].init === undefined | ||
) { | ||
error(start, 'invalid-const'); | ||
} | ||
|
||
parser.allow_whitespace(); | ||
parser.index = end_index; | ||
parser.eat('}', true); | ||
|
||
const id = declaration.declarations[0].id; | ||
if (id.type === 'Identifier') { | ||
// Tidy up some stuff left behind by acorn-typescript | ||
id.end = (id.start ?? 0) + id.name.length; | ||
} | ||
|
||
parser.append( | ||
/** @type {import('#compiler').ConstTag} */ ({ | ||
type: 'ConstTag', | ||
start, | ||
end: parser.index, | ||
expression | ||
declaration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mhhm it probably makes sense to make this a VariableDeclaration instead of an AssignmentExpression, and as such rename the property. For backwards compatibility this should be handled in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohhhhh I forgot to consider about the legacy mode. I think I can copy this. (Once already I did simmilar thing) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}) | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
html: '<p>10 * 10 = 100</p><p>20 * 20 = 400</p>' | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<script lang="ts"> | ||
const boxes = [ { width: 10, height: 10 }, { width: 20, height: 20 } ]; | ||
</script> | ||
|
||
{#each boxes as box} | ||
{@const area: number = box.width * box.height} | ||
<p>{box.width} * {box.height} = {area}</p> | ||
{/each} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
html: '<p>{}</p>' | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script lang="ts"> | ||
</script> | ||
|
||
{@const name: string = "{}"} | ||
<p>{name}</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we revert the doc changes? We're going to rewrite most of these anyway, so no point in doing this now and risk merge conflicts in case we want to port some V4 changes to the main branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also want to revert this but I can not pass the lint ci.
Is it better to add this file to .prettierignore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once I tried to update it.
cba043c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll look into on Monday more closely. If it's only related to prettier then we might need to add it. Although I don't understand how this wasn't failing before then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think because Svelte compiler without this PR can not parse the code due to type in
{@const}
tag.