Repository: sveltejs/language-tools
Pull request #1632 (2022-09-13 10:55:30)

(fix) style directive with multiple values
#1626


Patch:
diff --git a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/MustacheTag.ts b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/MustacheTag.ts
index 28e44eb3..9e888b90 100644
--- a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/MustacheTag.ts
+++ b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/MustacheTag.ts
@@ -6,8 +6,8 @@ import { BaseNode } from '../../interfaces';
  * {a}  -->  a;
  */
 export function handleMustacheTag(str: MagicString, node: BaseNode, parent: BaseNode) {
-    if (parent.type === 'Attribute') {
-        // handles inside Attribute.ts
+    if (parent.type === 'Attribute' || parent.type === 'StyleDirective') {
+        // handled inside Attribute.ts / StyleDirective.ts
         return;
     }
     str.overwrite(node.start, node.start + 1, '', { contentOnly: true });
diff --git a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/StyleDirective.ts b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/StyleDirective.ts
index 1583106d..4e23532e 100644
--- a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/StyleDirective.ts
+++ b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/StyleDirective.ts
@@ -24,16 +24,6 @@ export function handleStyleDirective(
         return;
     }
 
-    let start = style.value[0].start;
-    if (style.value[0].type === 'MustacheTag') {
-        start++;
-    }
-    const last = style.value[style.value.length - 1];
-    let end = last.end;
-    if (last.type === 'MustacheTag') {
-        end--;
-    }
-
     if (style.value.length > 1) {
         // We have multiple attribute values, so we build a template string out of them.
         for (const n of style.value) {
@@ -41,7 +31,11 @@ export function handleStyleDirective(
                 str.appendRight(n.start, '$');
             }
         }
-        element.appendToStartEnd([ensureType + '`', [start, end], '`);']);
+        element.appendToStartEnd([
+            ensureType + '`',
+            [style.value[0].start, style.value[style.value.length - 1].end],
+            '`);'
+        ]);
         return;
     }
 
@@ -50,9 +44,13 @@ export function handleStyleDirective(
         const quote = ['"', "'"].includes(str.original[styleVal.start - 1])
             ? str.original[styleVal.start - 1]
             : '"';
-        element.appendToStartEnd([`${ensureType}${quote}`, [start, end], `${quote});`]);
+        element.appendToStartEnd([
+            `${ensureType}${quote}`,
+            [styleVal.start, styleVal.end],
+            `${quote});`
+        ]);
     } else {
         // MustacheTag
-        element.appendToStartEnd([ensureType, [start, end], ');']);
+        element.appendToStartEnd([ensureType, [styleVal.start + 1, styleVal.end - 1], ');']);
     }
 }


Test patch:
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/style-directive/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/style-directive/expectedv2.js
index 0dbaab60..6c5ffe68 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/style-directive/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/style-directive/expectedv2.js
@@ -4,10 +4,10 @@
   { svelteHTML.createElement("div", { });__sveltets_2_ensureType(String, Number, value);  }
  { svelteHTML.createElement("div", { });__sveltets_2_ensureType(String, Number, " ");  }
  { svelteHTML.createElement("div", { });__sveltets_2_ensureType(String, Number, " ");  }
-  { svelteHTML.createElement("div", { });__sveltets_2_ensureType(String, Number, ` $mixed`);  }
- { svelteHTML.createElement("div", { });__sveltets_2_ensureType(String, Number, ` $mixed`);  }
- { svelteHTML.createElement("div", { });__sveltets_2_ensureType(String, Number, `mixed; `);  }
- { svelteHTML.createElement("div", { });__sveltets_2_ensureType(String, Number, ` $mixed; `);  }
+ { svelteHTML.createElement("div", { });__sveltets_2_ensureType(String, Number, ` ${mixed}`);  }
+ { svelteHTML.createElement("div", { });__sveltets_2_ensureType(String, Number, ` ${mixed}`);  }
+ { svelteHTML.createElement("div", { });__sveltets_2_ensureType(String, Number, `${mixed} `);  }
+ { svelteHTML.createElement("div", { });__sveltets_2_ensureType(String, Number, ` ${mixed} `);  }
  { svelteHTML.createElement("div", { });__sveltets_2_ensureType(String, Number, `template${literal}`);  }
 
   { svelteHTML.createElement("div", { });__sveltets_2_ensureType(String, Number, shorthand);}
\ No newline at end of file