From abac29d841d9938518ca9caa21d3b6e15370ba9b Mon Sep 17 00:00:00 2001
From: Simon H <5968653+dummdidumm@users.noreply.github.com>
Date: Fri, 13 May 2022 17:10:01 +0200
Subject: [PATCH] (fix) better mapping for empty text attribute (#1483)
new transformation
#1352
---
.../typescript/features/CompletionProvider.test.ts | 13 +++++++++++++
.../testfiles/completions/emptytext-imported.svelte | 3 +++
.../testfiles/completions/emptytext-importer.svelte | 5 +++++
.../svelte2tsx/src/htmlxtojsx_v2/nodes/Attribute.ts | 8 +++++++-
.../component-slot-infer-props/expectedv2.js | 2 +-
.../expectedv2.js | 2 +-
.../style-after-selfclosing-iframe/expectedv2.ts | 2 +-
7 files changed, 31 insertions(+), 4 deletions(-)
create mode 100644 packages/language-server/test/plugins/typescript/testfiles/completions/emptytext-imported.svelte
create mode 100644 packages/language-server/test/plugins/typescript/testfiles/completions/emptytext-importer.svelte
diff --git a/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts b/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts
index 039e52826..77c4e92d3 100644
--- a/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts
+++ b/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts
@@ -1306,6 +1306,19 @@ function test(useNewTransformation: boolean) {
}
});
+ it('handles completion in empty text attribute', async () => {
+ const { completionProvider, document } = setup('emptytext-importer.svelte');
+
+ const completions = await completionProvider.getCompletions(
+ document,
+ Position.create(4, 14)
+ );
+ assert.deepStrictEqual(
+ completions?.items.map((item) => item.label),
+ ['s', 'm', 'l']
+ );
+ });
+
// Hacky, but it works. Needed due to testing both new and old transformation
after(() => {
__resetCache();
diff --git a/packages/language-server/test/plugins/typescript/testfiles/completions/emptytext-imported.svelte b/packages/language-server/test/plugins/typescript/testfiles/completions/emptytext-imported.svelte
new file mode 100644
index 000000000..3f65cd7dd
--- /dev/null
+++ b/packages/language-server/test/plugins/typescript/testfiles/completions/emptytext-imported.svelte
@@ -0,0 +1,3 @@
+
diff --git a/packages/language-server/test/plugins/typescript/testfiles/completions/emptytext-importer.svelte b/packages/language-server/test/plugins/typescript/testfiles/completions/emptytext-importer.svelte
new file mode 100644
index 000000000..1ba6a4d96
--- /dev/null
+++ b/packages/language-server/test/plugins/typescript/testfiles/completions/emptytext-importer.svelte
@@ -0,0 +1,5 @@
+
+
+
diff --git a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Attribute.ts b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Attribute.ts
index 15881f83b..d8a82ea5d 100644
--- a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Attribute.ts
+++ b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Attribute.ts
@@ -139,7 +139,7 @@ export function handleAttribute(
return;
}
if (attr.value.length == 0) {
- // attr=""
+ // shouldn't happen
addAttribute(attributeName, ['""']);
return;
}
@@ -148,6 +148,12 @@ export function handleAttribute(
const attrVal = attr.value[0];
if (attrVal.type == 'Text') {
+ // Handle the attr="" special case with a transformation that allows mapping of the position
+ if (attrVal.start === attrVal.end) {
+ addAttribute(attributeName, [[attrVal.start - 1, attrVal.end + 1]]);
+ return;
+ }
+
const hasBrackets =
str.original.lastIndexOf('}', attrVal.end) === attrVal.end - 1 ||
str.original.lastIndexOf('}"', attrVal.end) === attrVal.end - 1 ||
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/component-slot-infer-props/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/component-slot-infer-props/expectedv2.js
index 3d2fe0f55..7f5ea7326 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/component-slot-infer-props/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/component-slot-infer-props/expectedv2.js
@@ -1,4 +1,4 @@
- { const $$_Parent0C = __sveltets_2_ensureComponent(Parent); const $$_Parent0 = new $$_Parent0C({ target: __sveltets_2_any(), props: { "bare":true,shorthand,"text1":`val1`,"text2":`val2`,"text3":`a${a}b${b}`,"textEmpty":``,"literal":true,"strLiteral":'foo',"complex":{a},"a-dashed-complex":{a},...__sveltets_2_cssProp({"--custom-cssprop":`foo`}),}});{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,foo,} = $$_Parent0.$$slot_def.default;$$_$$;
+ { const $$_Parent0C = __sveltets_2_ensureComponent(Parent); const $$_Parent0 = new $$_Parent0C({ target: __sveltets_2_any(), props: { "bare":true,shorthand,"text1":`val1`,"text2":`val2`,"text3":`a${a}b${b}`,"textEmpty":"","literal":true,"strLiteral":'foo',"complex":{a},"a-dashed-complex":{a},...__sveltets_2_cssProp({"--custom-cssprop":`foo`}),}});{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,foo,} = $$_Parent0.$$slot_def.default;$$_$$;
{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,bar,} = $$_Parent0.$$slot_def["named"];$$_$$;{ const $$_Component1C = __sveltets_2_ensureComponent(Component); new $$_Component1C({ target: __sveltets_2_any(), props: { }});
foo; bar;
}Component}
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/event-handler-component-infer-props/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/event-handler-component-infer-props/expectedv2.js
index a414fcc67..f880af8de 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/event-handler-component-infer-props/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/event-handler-component-infer-props/expectedv2.js
@@ -1 +1 @@
- { const $$_Component0C = __sveltets_2_ensureComponent(Component); const $$_Component0 = new $$_Component0C({ target: __sveltets_2_any(), props: { "bare":true,shorthand,"text1":`val1`,"text2":`val2`,"text3":`a${a}b${b}`,"textEmpty":``,"literal":true,"strLiteral":'foo',"complex":{a},"a-dashed-complex":{a},...__sveltets_2_cssProp({"--custom-cssprop":`foo`}),}});$$_Component0.$on("click", e => e);}
\ No newline at end of file
+ { const $$_Component0C = __sveltets_2_ensureComponent(Component); const $$_Component0 = new $$_Component0C({ target: __sveltets_2_any(), props: { "bare":true,shorthand,"text1":`val1`,"text2":`val2`,"text3":`a${a}b${b}`,"textEmpty":"","literal":true,"strLiteral":'foo',"complex":{a},"a-dashed-complex":{a},...__sveltets_2_cssProp({"--custom-cssprop":`foo`}),}});$$_Component0.$on("click", e => e);}
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/style-after-selfclosing-iframe/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/style-after-selfclosing-iframe/expectedv2.ts
index 93e85b4e5..a1ea21c33 100644
--- a/packages/svelte2tsx/test/svelte2tsx/samples/style-after-selfclosing-iframe/expectedv2.ts
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/style-after-selfclosing-iframe/expectedv2.ts
@@ -1,6 +1,6 @@
///
;function render() {
-async () => { { svelteHTML.createElement("iframe", { "src":``,});}
+async () => { { svelteHTML.createElement("iframe", {"src":"",});}
};
return { props: {}, slots: {}, getters: {}, events: {} }}