Skip to content

Commit abac29d

Browse files
authored
(fix) better mapping for empty text attribute (#1483)
new transformation #1352
1 parent db20058 commit abac29d

File tree

7 files changed

+31
-4
lines changed

7 files changed

+31
-4
lines changed

packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,19 @@ function test(useNewTransformation: boolean) {
13061306
}
13071307
});
13081308

1309+
it('handles completion in empty text attribute', async () => {
1310+
const { completionProvider, document } = setup('emptytext-importer.svelte');
1311+
1312+
const completions = await completionProvider.getCompletions(
1313+
document,
1314+
Position.create(4, 14)
1315+
);
1316+
assert.deepStrictEqual(
1317+
completions?.items.map((item) => item.label),
1318+
['s', 'm', 'l']
1319+
);
1320+
});
1321+
13091322
// Hacky, but it works. Needed due to testing both new and old transformation
13101323
after(() => {
13111324
__resetCache();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script lang="ts">
2+
export let size: 's' | 'm' | 'l';
3+
</script>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import Button from './emptytext-imported.svelte';
3+
</script>
4+
5+
<Button size="" />

packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Attribute.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export function handleAttribute(
139139
return;
140140
}
141141
if (attr.value.length == 0) {
142-
// attr=""
142+
// shouldn't happen
143143
addAttribute(attributeName, ['""']);
144144
return;
145145
}
@@ -148,6 +148,12 @@ export function handleAttribute(
148148
const attrVal = attr.value[0];
149149

150150
if (attrVal.type == 'Text') {
151+
// Handle the attr="" special case with a transformation that allows mapping of the position
152+
if (attrVal.start === attrVal.end) {
153+
addAttribute(attributeName, [[attrVal.start - 1, attrVal.end + 1]]);
154+
return;
155+
}
156+
151157
const hasBrackets =
152158
str.original.lastIndexOf('}', attrVal.end) === attrVal.end - 1 ||
153159
str.original.lastIndexOf('}"', attrVal.end) === attrVal.end - 1 ||

packages/svelte2tsx/test/htmlx2jsx/samples/component-slot-infer-props/expectedv2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ 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;$$_$$;
1+
{ 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;$$_$$;
22
{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,bar,} = $$_Parent0.$$slot_def["named"];$$_$$;{ const $$_Component1C = __sveltets_2_ensureComponent(Component); new $$_Component1C({ target: __sveltets_2_any(), props: { }});
33
foo; bar;
44
}Component}

packages/svelte2tsx/test/htmlx2jsx/samples/event-handler-component-infer-props/expectedv2.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/svelte2tsx/test/svelte2tsx/samples/style-after-selfclosing-iframe/expectedv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
///<reference types="svelte" />
22
;function render() {
3-
async () => { { svelteHTML.createElement("iframe", { "src":``,});}
3+
async () => { { svelteHTML.createElement("iframe", {"src":"",});}
44
};
55
return { props: {}, slots: {}, getters: {}, events: {} }}
66

0 commit comments

Comments
 (0)