Skip to content

Commit

Permalink
chore: refactor printJS
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Nov 21, 2024
1 parent cb3ab67 commit 4e88090
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,11 @@ export function embed(path: FastPath, _options: Options) {
const parent: Node = path.getParentNode();
const printJsExpression = () =>
(parent as any).expression
? printJS(
parent,
(options.svelteStrictMode && !options._svelte_is5Plus) ?? false,
false,
false,
'expression',
)
? printJS(parent, 'expression', {
forceSingleQuote: (options.svelteStrictMode && !options._svelte_is5Plus) ?? false,
})
: undefined;
const printSvelteBlockJS = (name: string) => printJS(parent, false, true, false, name);
const printSvelteBlockJS = (name: string) => printJS(parent, name, { forceSingleLine: true });

switch (parent.type) {
case 'IfBlock':
Expand Down Expand Up @@ -116,25 +112,23 @@ export function embed(path: FastPath, _options: Options) {
}
break;
case 'Element':
printJS(
parent,
(options.svelteStrictMode && !options._svelte_is5Plus) ?? false,
false,
false,
'tag',
);
printJS(parent, 'tag', {
forceSingleQuote: (options.svelteStrictMode && !options._svelte_is5Plus) ?? false,
});
break;
case 'MustacheTag':
printJS(parent, isInsideQuotedAttribute(path, options), false, false, 'expression');
printJS(parent, 'expression', {
forceSingleQuote: isInsideQuotedAttribute(path, options),
});
break;
case 'RawMustacheTag':
printJS(parent, false, false, false, 'expression');
printJS(parent, 'expression', {});
break;
case 'Spread':
printJS(parent, false, false, false, 'expression');
printJS(parent, 'expression', {});
break;
case 'ConstTag':
printJS(parent, false, false, true, 'expression');
printJS(parent, 'expression', { removeParentheses: true });
break;
case 'RenderTag':
if (node === parent.expression) {
Expand All @@ -150,7 +144,7 @@ export function embed(path: FastPath, _options: Options) {
parent.argument = null;
parent.arguments = null;
}
printJS(parent, false, false, false, 'expression');
printJS(parent, 'expression', {});
}
break;
case 'EventHandler':
Expand Down Expand Up @@ -409,17 +403,19 @@ async function embedTag(

function printJS(
node: any,
forceSingleQuote: boolean,
forceSingleLine: boolean,
removeParentheses: boolean,
name: string,
options: {
forceSingleQuote?: boolean;
forceSingleLine?: boolean;
removeParentheses?: boolean;
},
) {
const part = node[name] as BaseNode | undefined;
if (!part || typeof part !== 'object') {
return;
}
part.isJS = true;
part.forceSingleQuote = forceSingleQuote;
part.forceSingleLine = forceSingleLine;
part.removeParentheses = removeParentheses;
part.forceSingleQuote = options.forceSingleQuote;
part.forceSingleLine = options.forceSingleLine;
part.removeParentheses = options.removeParentheses;
}

0 comments on commit 4e88090

Please sign in to comment.