Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lang-ts-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: improve error message for TS-like snippet annotations without lang="ts"
Empty file added .vs/slnx.sqlite
Empty file.
2 changes: 2 additions & 0 deletions packages/svelte/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
/transition.d.ts

/scripts/_bundle.js
*.Zone.Identifier

15 changes: 14 additions & 1 deletion packages/svelte/src/compiler/phases/1-parse/read/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@
const pattern_string = parser.template.slice(start, i);

try {
// Check for TypeScript parameter annotations in the pattern string
if (!parser.ts) {
const parameterRegex = /\w+\s*:\s*\w+/;

Check failure on line 50 in packages/svelte/src/compiler/phases/1-parse/read/context.js

View workflow job for this annotation

GitHub Actions / Lint

Identifier 'parameterRegex' does not match for svelte's naming conventions
if (parameterRegex.test(pattern_string)) {

Check failure on line 51 in packages/svelte/src/compiler/phases/1-parse/read/context.js

View workflow job for this annotation

GitHub Actions / Lint

Identifier 'parameterRegex' does not match for svelte's naming conventions
e.typescript_invalid_feature(
start,
'type annotations in snippets. Did you forget to add lang="ts" to your script tag?'
);
}
}

// the length of the `space_with_newline` has to be start - 1
// because we added a `(` in front of the pattern_string,
// which shifted the entire string to right by 1
Expand All @@ -67,7 +78,9 @@
)
).left;

expression.typeAnnotation = read_type_annotation(parser);
const annotation = read_type_annotation(parser);

expression.typeAnnotation = annotation;
if (expression.typeAnnotation) {
expression.end = expression.typeAnnotation.end;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/svelte/src/compiler/phases/1-parse/state/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ function open(parser) {
const prelude = parser.template.slice(0, params_start).replace(/\S/g, ' ');
const params = parser.template.slice(params_start, parser.index);

// Check for TypeScript parameter annotations without lang="ts"
if (!parser.ts && matched && /\w+\s*:\s*\w+/.test(params)) {
e.typescript_invalid_feature(
parser.index,
'type annotations in snippets. Did you forget to add lang="ts" to your script tag?'
);
}

let function_expression = matched
? /** @type {ArrowFunctionExpression} */ (
parse_expression_at(
Expand Down
Loading