Skip to content

Commit

Permalink
(fix) Don't break on upcoming custom css properties (#963)
Browse files Browse the repository at this point in the history
Remove --custom-css-propeties during Component attribute transformations. They have no property which they can be checked against and the leading dashes are invalid JSX

Related RFC: sveltejs/rfcs#13
  • Loading branch information
dummdidumm committed Apr 24, 2021
1 parent 558ea8b commit 4cd5d58
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 7 deletions.
6 changes: 6 additions & 0 deletions packages/svelte2tsx/src/htmlxtojsx/nodes/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ 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 });
return;
}

//we are a bare attribute
if (attr.value === true) {
if (
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte2tsx/src/htmlxtojsx/utils/node-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function getNameValuePairsFromAttributes(
complexExpression?: true;
}> {
return ((node.attributes as Node[]) || [])
.filter((attr) => attr.type === 'Attribute')
.filter((attr) => attr.type === 'Attribute' && !attr.name.startsWith('--'))
.map((attr) => {
const name: string = attr.name;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<><Hello __custom-prop="foo"></Hello>
<><Hello __wtf="foo"></Hello>
<div __custom-prop="foo"></div></>
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<Hello --custom-prop="foo"></Hello>
<Hello |-wtf="foo"></Hello>
<div --custom-prop="foo"></div>
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}} >{() => {/*Ω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,4 +1,4 @@
<Parent bare {shorthand} text1=val1 text2="val2" text3="a{a}b{b}" textEmpty="" literal={true} strLiteral={'foo'} complex={{a}} a-dashed-complex={{a}} let:foo>
<Parent bare {shorthand} text1=val1 text2="val2" text3="a{a}b{b}" textEmpty="" literal={true} strLiteral={'foo'} complex={{a}} a-dashed-complex={{a}} --custom-cssprop="foo" let:foo>
<Component slot="named" let:bar>
{foo} {bar}
</Component>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<><Component betweenprop /></>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Component --custom-css-property1={'hi'} --custom-css-property2="hi" betweenprop --custom-css-property3="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}} />{/*Ω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
@@ -1 +1 @@
<Component bare {shorthand} text1=val1 text2="val2" text3="a{a}b{b}" textEmpty="" literal={true} strLiteral={'foo'} complex={{a}} a-dashed-complex={{a}} on:click={e => e} />
<Component bare {shorthand} text1=val1 text2="val2" text3="a{a}b{b}" textEmpty="" literal={true} strLiteral={'foo'} complex={{a}} a-dashed-complex={{a}} --custom-cssprop="foo" on:click={e => e} />

0 comments on commit 4cd5d58

Please sign in to comment.