Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/svelte2tsx/src/htmlxtojsx/nodes/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ export function handleAttribute(
}
}

// Custom property -> remove completely due to JSX incompatibility
if (parent.type === 'InlineComponent' && attr.name.startsWith('--')) {
str.overwrite(attr.start, attr.end, '', { contentOnly: true });
// Custom CSS property
if (parent.type === 'InlineComponent' && attr.name.startsWith('--') && attr.value !== true) {
str.prependRight(attr.start, '{...__sveltets_cssProp({"');
buildTemplateString(attr, str, htmlx, '": `', '`})}');
return;
}

Expand Down Expand Up @@ -169,23 +170,30 @@ export function handleAttribute(
return;
}

// we have multiple attribute values, so we build a string out of them.
// technically the user can do something funky like attr="text "{value} or even attr=text{value}
// so instead of trying to maintain a nice sourcemap with prepends etc, we just overwrite the whole thing
// We have multiple attribute values, so we build a template string out of them.
buildTemplateString(attr, str, htmlx, '={`', '`}');
}

function buildTemplateString(
attr: Attribute,
str: MagicString,
htmlx: string,
leadingOverride: string,
trailingOverride: string
) {
const equals = htmlx.lastIndexOf('=', attr.value[0].start);
str.overwrite(equals, attr.value[0].start, '={`');
str.overwrite(equals, attr.value[0].start, leadingOverride);

for (const n of attr.value) {
for (const n of attr.value as BaseNode[]) {
if (n.type == 'MustacheTag') {
str.appendRight(n.start, '$');
}
}

if (isQuote(htmlx[attr.end - 1])) {
str.overwrite(attr.end - 1, attr.end, '`}');
str.overwrite(attr.end - 1, attr.end, trailingOverride);
} else {
str.appendLeft(attr.end, '`}');
str.appendLeft(attr.end, trailingOverride);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/svelte2tsx/svelte-shims.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ declare function __sveltets_ensureAction(actionCall: SvelteActionReturnType): {}
declare function __sveltets_ensureTransition(transitionCall: SvelteTransitionReturnType): {};
declare function __sveltets_ensureFunction(expression: (e: Event & { detail?: any }) => unknown ): {};
declare function __sveltets_ensureType<T>(type: AConstructorTypeOf<T>, el: T): {};
declare function __sveltets_cssProp(prop: Record<string, any>): {};
declare function __sveltets_ctorOf<T>(type: T): AConstructorTypeOf<T>;
declare function __sveltets_instanceOf<T = any>(type: AConstructorTypeOf<T>): T;
declare function __sveltets_allPropsType(): SvelteAllProps
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<><Parent bare shorthand={shorthand} text1="val1" text2="val2" text3={`a${a}b${b}`} textEmpty="" literal={true} strLiteral={'foo'} complex={{a}} a-dashed-complex={{a}} >{() => {/*Ωignore_startΩ*/const Ψcomplex={a},Ψa_dashed_complex={a};/*Ωignore_endΩ*/() => { let {foo} = /*Ωignore_startΩ*/new Parent({target: __sveltets_any(''), props: {'bare':true, 'shorthand':shorthand, 'text1':"", 'text2':"", 'text3':"", 'textEmpty':"", 'literal':true, 'strLiteral':"", 'complex':Ψcomplex, 'a-dashed-complex':Ψa_dashed_complex}})/*Ωignore_endΩ*/.$$slot_def['default'];<>
<><Parent bare shorthand={shorthand} text1="val1" text2="val2" text3={`a${a}b${b}`} textEmpty="" literal={true} strLiteral={'foo'} complex={{a}} a-dashed-complex={{a}} {...__sveltets_cssProp({"--custom-cssprop": `foo`})} >{() => {/*Ωignore_startΩ*/const Ψcomplex={a},Ψa_dashed_complex={a};/*Ωignore_endΩ*/() => { let {foo} = /*Ωignore_startΩ*/new Parent({target: __sveltets_any(''), props: {'bare':true, 'shorthand':shorthand, 'text1':"", 'text2':"", 'text3':"", 'textEmpty':"", 'literal':true, 'strLiteral':"", 'complex':Ψcomplex, 'a-dashed-complex':Ψa_dashed_complex}})/*Ωignore_endΩ*/.$$slot_def['default'];<>
{() => { let {bar} = /*Ωignore_startΩ*/new Parent({target: __sveltets_any(''), props: {'bare':true, 'shorthand':shorthand, 'text1':"", 'text2':"", 'text3':"", 'textEmpty':"", 'literal':true, 'strLiteral':"", 'complex':Ψcomplex, 'a-dashed-complex':Ψa_dashed_complex}})/*Ωignore_endΩ*/.$$slot_def['named'];<><Component >
{foo} {bar}
</Component></>}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
<><Component betweenprop /></>
<><Component
{...__sveltets_cssProp({"--custom-css-property1": `${'hi'}`})}
{...__sveltets_cssProp({"--custom-css-property2": `hi`})}
betweenprop
{...__sveltets_cssProp({"--custom-css-property3": `hi${jo}hi`})}
{...__sveltets_cssProp({"--custom-css-property4": `hi${jo}hi`})}
{...__sveltets_cssProp({"--custom-css-property5": `hi${jo}hi`})} /></>
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
<Component --custom-css-property1={'hi'} --custom-css-property2="hi" betweenprop --custom-css-property3="hi{jo}hi" />
<Component
--custom-css-property1={'hi'}
--custom-css-property2="hi"
betweenprop
--custom-css-property3="hi{jo}hi"
--custom-css-property4='hi{jo}hi'
--custom-css-property5=hi{jo}hi />
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<><Component bare shorthand={shorthand} text1="val1" text2="val2" text3={`a${a}b${b}`} textEmpty="" literal={true} strLiteral={'foo'} complex={{a}} a-dashed-complex={{a}} />{/*Ωignore_startΩ*/new Component({target: __sveltets_any(''), props: {'bare':true, 'shorthand':shorthand, 'text1':"", 'text2':"", 'text3':"", 'textEmpty':"", 'literal':true, 'strLiteral':"", 'complex':{a}, 'a-dashed-complex':{a}}})/*Ωignore_endΩ*/.$on('click', e => e)}</>
<><Component bare shorthand={shorthand} text1="val1" text2="val2" text3={`a${a}b${b}`} textEmpty="" literal={true} strLiteral={'foo'} complex={{a}} a-dashed-complex={{a}} {...__sveltets_cssProp({"--custom-cssprop": `foo`})} />{/*Ωignore_startΩ*/new Component({target: __sveltets_any(''), props: {'bare':true, 'shorthand':shorthand, 'text1':"", 'text2':"", 'text3':"", 'textEmpty':"", 'literal':true, 'strLiteral':"", 'complex':{a}, 'a-dashed-complex':{a}}})/*Ωignore_endΩ*/.$on('click', e => e)}</>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
///<reference types="svelte" />
<></>;function render() {
<><Component
{...__sveltets_cssProp({"--custom-css-property1": `${(__sveltets_store_get(jo), $jo)}`})}
{...__sveltets_cssProp({"--custom-css-property2": `hi${(__sveltets_store_get(jo), $jo)}hi`})}
{...__sveltets_cssProp({"--custom-css-property3": `hi${(__sveltets_store_get(jo), $jo)}hi`})}
{...__sveltets_cssProp({"--custom-css-property4": `hi${(__sveltets_store_get(jo), $jo)}hi`})} /></>
return { props: {}, slots: {}, getters: {}, events: {} }}

export default class Input__SvelteComponent_ extends createSvelte2TsxComponent(__sveltets_partial(__sveltets_with_any_event(render))) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Component
--custom-css-property1={$jo}
--custom-css-property2="hi{$jo}hi"
--custom-css-property3='hi{$jo}hi'
--custom-css-property4=hi{$jo}hi />