Skip to content

Commit 225a5bf

Browse files
set expression end to typeAnnotation end where appropriate (#9566)
* set expression end to typeAnnotation end where appropriate * fix * woops * lint --------- Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Simon H <[email protected]>
1 parent 459e4ff commit 225a5bf

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

packages/svelte/src/compiler/phases/1-parse/read/context.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ export default function read_context(parser) {
2222
const code = full_char_code_at(parser.template, i);
2323
if (isIdentifierStart(code, true)) {
2424
const name = /** @type {string} */ (parser.read_identifier());
25+
const annotation = read_type_annotation(parser);
26+
2527
return {
2628
type: 'Identifier',
2729
name,
2830
start,
2931
end: parser.index,
30-
typeAnnotation: read_type_annotation(parser)
32+
typeAnnotation: annotation
3133
};
3234
}
3335

@@ -81,6 +83,10 @@ export default function read_context(parser) {
8183
).left;
8284

8385
expression.typeAnnotation = read_type_annotation(parser);
86+
if (expression.typeAnnotation) {
87+
expression.end = expression.typeAnnotation.end;
88+
}
89+
8490
return expression;
8591
} catch (error) {
8692
parser.acorn_error(error);
@@ -92,6 +98,7 @@ export default function read_context(parser) {
9298
* @returns {any}
9399
*/
94100
function read_type_annotation(parser) {
101+
const index = parser.index;
95102
parser.allow_whitespace();
96103

97104
if (parser.eat(':')) {
@@ -103,5 +110,7 @@ function read_type_annotation(parser) {
103110

104111
parser.index = /** @type {number} */ (expression.end);
105112
return /** @type {any} */ (expression).typeAnnotation;
113+
} else {
114+
parser.index = index;
106115
}
107116
}

packages/svelte/tests/parser-modern/samples/snippets/output.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"type": "Identifier",
2929
"name": "msg",
3030
"start": 43,
31-
"end": 46,
31+
"end": 54,
3232
"typeAnnotation": {
3333
"type": "TSStringKeyword",
3434
"start": 48,

0 commit comments

Comments
 (0)