Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm authored Jun 24, 2024
1 parent c203a33 commit 0f08748
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
37 changes: 14 additions & 23 deletions packages/svelte2tsx/src/htmlxtojsx_v2/nodes/SnippetBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,20 @@ export function handleSnippet(
}
);

const last_parameter = snippetBlock.parameters?.at(-1);
const lastParameter = snippetBlock.parameters?.at(-1);

const startEnd =
str.original.indexOf(
'}',
// context was the first iteration in a .next release, remove at some point
(last_parameter?.typeAnnotation
? // if it has a type annotation use the end of the type annotation
// else the end of the parameter
last_parameter?.typeAnnotation.end
: last_parameter?.end) || snippetBlock.expression.end
lastParameter?.typeAnnotation.end ?? lastParameter?.end ?? snippetBlock.expression.end
) + 1;

if (isImplicitProp) {
str.overwrite(snippetBlock.start, snippetBlock.expression.start, '', { contentOnly: true });
const transforms: TransformationArray = ['('];
if (snippetBlock.parameters?.length) {
const start = snippetBlock.parameters?.[0].start;
const end = last_parameter.typeAnnotation
? last_parameter?.typeAnnotation.end
: last_parameter.end;
const start = snippetBlock.parameters[0].start;
const end = lastParameter.typeAnnotation?.end ?? lastParameter.end;
transforms.push([start, end]);
str.overwrite(snippetBlock.expression.end, start, '', {
contentOnly: true
Expand All @@ -82,18 +75,18 @@ export function handleSnippet(
if (snippetBlock.parameters?.length) {
generic = `<[${snippetBlock.parameters
.map((p) => {
let type_annotation = p.typeAnnotation;
if (!type_annotation && p.type === 'AssignmentPattern') {
type_annotation = p.left?.typeAnnotation;
if (!type_annotation) {
type_annotation = p.right?.typeAnnotation;
let typeAnnotation = p.typeAnnotation;
if (!typeAnnotation && p.type === 'AssignmentPattern') {
typeAnnotation = p.left?.typeAnnotation;
if (!typeAnnotation) {
typeAnnotation = p.right?.typeAnnotation;
}
}
if (!type_annotation) return 'any';
return type_annotation.typeAnnotation
if (!typeAnnotation) return 'any';
return typeAnnotation.typeAnnotation
? str.original.slice(
type_annotation.typeAnnotation.start,
type_annotation.typeAnnotation.end
typeAnnotation.typeAnnotation.start,
typeAnnotation.typeAnnotation.end
)
: // slap any on to it to silence "implicit any" errors; JSDoc people can't add types to snippets
'any';
Expand All @@ -110,9 +103,7 @@ export function handleSnippet(

if (snippetBlock.parameters?.length) {
const start = snippetBlock.parameters[0].start;
const end = last_parameter.typeAnnotation
? last_parameter?.typeAnnotation.end
: last_parameter.end;
const end = lastParameter.typeAnnotation?.end ?? lastParameter.end;
transforms.push([start, end]);
}

Expand Down
2 changes: 0 additions & 2 deletions packages/svelte2tsx/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ const enum TestError {

const isSvelte5Plus = Number(VERSION[0]) >= 5;

console.log('IS SVELTE 5 +', isSvelte5Plus);

export function test_samples(dir: string, transform: TransformSampleFn, js: 'js' | 'ts') {
for (const sample of each_sample(dir)) {
if (sample.name.endsWith('.v5') && !isSvelte5Plus) continue;
Expand Down

0 comments on commit 0f08748

Please sign in to comment.