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

Add new option: attributeAlias #122

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 14 additions & 28 deletions src/__tests__/__snapshots__/attributes-transformation.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ module.exports = (
<div>
<div className=\\"one two\\" />
<div className=\\"one two\\" />
<div className={100} />
<div className=\\"100\\" />
<div className={array} />
<div className={object} />
<div className={[\\"one\\", \\"two\\"]} />
<div className=\\"one two\\" />
<div className={[\\"one\\", \\"two\\"].join(\\" \\")} />
<div
className={{
Expand All @@ -24,18 +24,15 @@ module.exports = (
<div before=\\"before\\" {...object} />
<div before=\\"before\\" {...object} after=\\"after\\" />
<div className=\\"mix one two\\" />
<div className={\\"mix \\" + 100} />
<div className={\\"mix \\" + array} />
<div className={\\"mix \\" + object} />
<div className={[\\"mix\\", \\"one\\", \\"two\\"]} />
<div className={\\"mix \\" + [\\"one\\", \\"two\\"].join(\\" \\")} />
<div className=\\"mix 100\\" />
<div className={\`mix \${array}\`} />
<div className={\`mix \${object}\`} />
<div className=\\"mix one two\\" />
<div className={\`mix \${[\\"one\\", \\"two\\"].join(\\" \\")}\`} />
<div
className={
\\"mix \\" +
{
first: true
}
}
className={\`mix \${{
first: true
}}\`}
/>
<div {...object} after=\\"after\\" className=\\"mix\\" />
<div before=\\"before\\" {...object} className=\\"mix\\" />
Expand All @@ -54,7 +51,7 @@ exports[`html output: generated html 1`] = `
className="one two"
/>
<div
className={100}
className="100"
/>
<div
className={
Expand All @@ -73,12 +70,7 @@ exports[`html output: generated html 1`] = `
}
/>
<div
className={
Array [
"one",
"two",
]
}
className="one two"
/>
<div
className="one two"
Expand Down Expand Up @@ -119,13 +111,7 @@ exports[`html output: generated html 1`] = `
className="mix [object Object]"
/>
<div
className={
Array [
"mix",
"one",
"two",
]
}
className="mix one two"
/>
<div
className="mix one two"
Expand Down Expand Up @@ -155,4 +141,4 @@ exports[`html output: generated html 1`] = `
</div>
`;

exports[`static html output: static html 1`] = `"<div><div class=\\"one two\\"></div><div class=\\"one two\\"></div><div class=\\"100\\"></div><div class=\\"arr-one,arr-two\\"></div><div class=\\"[object Object]\\"></div><div class=\\"one,two\\"></div><div class=\\"one two\\"></div><div class=\\"[object Object]\\"></div><div first=\\"one-in-object\\" second=\\"two-in-object\\" after=\\"after\\"></div><div before=\\"before\\" first=\\"one-in-object\\" second=\\"two-in-object\\"></div><div before=\\"before\\" first=\\"one-in-object\\" second=\\"two-in-object\\" after=\\"after\\"></div><div class=\\"mix one two\\"></div><div class=\\"mix 100\\"></div><div class=\\"mix arr-one,arr-two\\"></div><div class=\\"mix [object Object]\\"></div><div class=\\"mix,one,two\\"></div><div class=\\"mix one two\\"></div><div class=\\"mix [object Object]\\"></div><div first=\\"one-in-object\\" second=\\"two-in-object\\" after=\\"after\\" class=\\"mix\\"></div><div before=\\"before\\" first=\\"one-in-object\\" second=\\"two-in-object\\" class=\\"mix\\"></div><div before=\\"before\\" first=\\"one-in-object\\" second=\\"two-in-object\\" after=\\"after\\" class=\\"mix\\"></div></div>"`;
exports[`static html output: static html 1`] = `"<div><div class=\\"one two\\"></div><div class=\\"one two\\"></div><div class=\\"100\\"></div><div class=\\"arr-one,arr-two\\"></div><div class=\\"[object Object]\\"></div><div class=\\"one two\\"></div><div class=\\"one two\\"></div><div class=\\"[object Object]\\"></div><div first=\\"one-in-object\\" second=\\"two-in-object\\" after=\\"after\\"></div><div before=\\"before\\" first=\\"one-in-object\\" second=\\"two-in-object\\"></div><div before=\\"before\\" first=\\"one-in-object\\" second=\\"two-in-object\\" after=\\"after\\"></div><div class=\\"mix one two\\"></div><div class=\\"mix 100\\"></div><div class=\\"mix arr-one,arr-two\\"></div><div class=\\"mix [object Object]\\"></div><div class=\\"mix one two\\"></div><div class=\\"mix one two\\"></div><div class=\\"mix [object Object]\\"></div><div first=\\"one-in-object\\" second=\\"two-in-object\\" after=\\"after\\" class=\\"mix\\"></div><div before=\\"before\\" first=\\"one-in-object\\" second=\\"two-in-object\\" class=\\"mix\\"></div><div before=\\"before\\" first=\\"one-in-object\\" second=\\"two-in-object\\" after=\\"after\\" class=\\"mix\\"></div></div>"`;
118 changes: 118 additions & 0 deletions src/__tests__/__snapshots__/option-attribute-alias.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Expect an error to be thrown 1`] = `
"<basedir>/src/__tests__/option-attribute-alias.input.js:13
11| p.b1.b2(class=\\"b3 b4\\")
12| p.c1(className=\\"c2 c3\\")
> 13| p.d1(class=classes)
14| p.e1(class=['e2', 'e3'])
15| p.f1(class=['f2', ...classesArray])
16| p(class=$.Red)

We can't use expressions in shorthands, use \\"className\\" instead of \\"class\\""
`;

exports[`JavaScript output: transformed source code 1`] = `
"const $ = {
Red: \\"color-red\\"
};
const classes = \\"d2 d3\\";
const classesArray = [\\"f3\\", $.Red];
const showK = true;
const showL = false;

const handleClick = () => {};

const svgGroup = \\"<g></g>\\";
module.exports = (
<div className=\\"a1\\">
<p className=\\"b1 b2 b3 b4\\" />
<p className=\\"c1 c2 c3\\" />
<p className={\`d1 \${classes}\`} />
<p className=\\"e1 e2 e3\\" />
<p className=\\"f1 f2\\" />
<p className={$.Red} />
<p className={\`g1 \${$.Red}\`} />
<p className={\`i1 \${\`i2 \${$.Red}\`}\`} />
<p className=\\"j1 j2 j3 j4 j5\\" />
<p className={showK && \\"k1\\"} />
<p className={\`l1 \${showL ? \\"l2\\" : \\"\\"}\`} />
<a onClick={handleClick} className=\\"m1\\" />
<svg
dangerouslySetInnerHTML={{
__html: \\"<g></g>\\"
}}
className=\\"n1\\"
/>
<svg
dangerouslySetInnerHTML={{
__html: svgGroup
}}
className=\\"o1\\"
/>
</div>
);
"
`;

exports[`html output: generated html 1`] = `
<div
className="a1"
>
<p
className="b1 b2 b3 b4"
/>
<p
className="c1 c2 c3"
/>
<p
className="d1 d2 d3"
/>
<p
className="e1 e2 e3"
/>
<p
className="f1 f2"
/>
<p
className="color-red"
/>
<p
className="g1 color-red"
/>
<p
className="i1 i2 color-red"
/>
<p
className="j1 j2 j3 j4 j5"
/>
<p
className="k1"
/>
<p
className="l1 "
/>
<a
className="m1"
onClick={[Function]}
/>
<svg
className="n1"
dangerouslySetInnerHTML={
Object {
"__html": "<g></g>",
}
}
/>
<svg
className="o1"
dangerouslySetInnerHTML={
Object {
"__html": "<g></g>",
}
}
/>
</div>
`;

exports[`static html output: static html 1`] = `"<div class=\\"a1\\"><p class=\\"b1 b2 b3 b4\\"></p><p class=\\"c1 c2 c3\\"></p><p class=\\"d1 d2 d3\\"></p><p class=\\"e1 e2 e3\\"></p><p class=\\"f1 f2\\"></p><p class=\\"color-red\\"></p><p class=\\"g1 color-red\\"></p><p class=\\"i1 i2 color-red\\"></p><p class=\\"j1 j2 j3 j4 j5\\"></p><p class=\\"k1\\"></p><p class=\\"l1 \\"></p><a class=\\"m1\\"></a><svg class=\\"n1\\"><g></g></svg><svg class=\\"o1\\"><g></g></svg></div>"`;
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = (
return (
<p
key={item}
className={\\"seventh-a seventh-b \\" + (\\"seventh-i-\\" + item)}
className={\`seventh-a seventh-b \${\\"seventh-i-\\" + item}\`}
/>
);
})}
Expand Down
27 changes: 27 additions & 0 deletions src/__tests__/option-attribute-alias.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const $ = {Red: 'color-red'};
const classes = 'd2 d3';
const classesArray = ['f3', $.Red];
const showK = true;
const showL = false;
const handleClick = () => {};
const svgGroup = '<g></g>';

module.exports = pug`
div.a1
p.b1.b2(class="b3 b4")
p.c1(className="c2 c3")
p.d1(class=classes)
p.e1(class=['e2', 'e3'])
p.f1(class=['f2', ...classesArray])
p(class=$.Red)
p(class=${`g1 ${$.Red}`})
p.i1(class=${`i2 ${$.Red}`})
p.j1(
class="j2 j3",
className="j4 j5")
p(class=(showK && "k1"))
p.l1(class=(showL ? "l2" : ""))
a.m1(@click=handleClick)
svg.n1(@html={ __html: "<g></g>" })
svg.o1(@html={ __html: svgGroup })
`;
11 changes: 11 additions & 0 deletions src/__tests__/option-attribute-alias.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import testHelper, {testCompileError} from './test-helper';

testCompileError(__dirname + '/option-attribute-alias.input.js');

testHelper(__dirname + '/option-attribute-alias.input.js', {
attributeAlias: {
class: 'className',
'@click': 'onClick',
'@html': 'dangerouslySetInnerHTML',
},
});
3 changes: 3 additions & 0 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type Variable = {

type Options = {
classAttribute: string,
attributeAlias: {
[string]: string,
},
};

class Context {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {setBabelTypes} from './lib/babel-types';

const DEFAULT_OPTIONS = {
classAttribute: 'className',
attributeAlias: {},
};

export default function(babel) {
Expand Down
Loading