Skip to content

Commit

Permalink
(fix) better mapping for empty text attribute (#1483)
Browse files Browse the repository at this point in the history
new transformation
#1352
  • Loading branch information
dummdidumm committed May 13, 2022
1 parent db20058 commit abac29d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 4 deletions.
Expand Up @@ -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();
Expand Down
@@ -0,0 +1,3 @@
<script lang="ts">
export let size: 's' | 'm' | 'l';
</script>
@@ -0,0 +1,5 @@
<script>
import Button from './emptytext-imported.svelte';
</script>

<Button size="" />
8 changes: 7 additions & 1 deletion packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Attribute.ts
Expand Up @@ -139,7 +139,7 @@ export function handleAttribute(
return;
}
if (attr.value.length == 0) {
// attr=""
// shouldn't happen
addAttribute(attributeName, ['""']);
return;
}
Expand All @@ -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 ||
Expand Down
@@ -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}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -1,6 +1,6 @@
///<reference types="svelte" />
;function render() {
async () => { { svelteHTML.createElement("iframe", { "src":``,});}
async () => { { svelteHTML.createElement("iframe", {"src":"",});}
};
return { props: {}, slots: {}, getters: {}, events: {} }}

Expand Down

0 comments on commit abac29d

Please sign in to comment.