Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) better mapping for empty text attribute #1483

Merged
merged 2 commits into from May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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