Skip to content

feat: transform block to attribute #153

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

Open
wants to merge 1 commit 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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ const Component = props => pug` //- const Component = props => (
`;
```

#### Transform block to attribute

```jsx
const Component = props => pug` //- const Component = props => (
Menu //- <Menu icon={<img />}>
button //- <button />
block icon //- </Menu>
img //- )
`;
```

#### Interpolation

If you'd prefer to use interpolation, you can. This is possible by using `${}` within your template.
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/__snapshots__/block-attribute.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`JavaScript output: transformed source code 1`] = `
"module.exports = (
<menu icon=<img />>
<button />
</menu>
);
"
`;

exports[`html output: generated html 1`] = `
<menu
icon={<img />}
>
<button />
</menu>
`;

exports[`static html output: static html 1`] = `"<menu icon=\\"[object Object]\\"><button></button></menu>"`;
6 changes: 6 additions & 0 deletions src/__tests__/block-attribute.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = pug`
menu
button
block icon
img
`;
3 changes: 3 additions & 0 deletions src/__tests__/block-attribute.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import testHelper from './test-helper';

testHelper(__dirname + '/block-attribute.input.js');
15 changes: 15 additions & 0 deletions src/visitors/NamedBlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @flow

import type Context from '../context';
import {visitJsxExpressions} from '../visitors';
import {buildJSXFragment} from '../utils/jsx';

const NamedBlockVisitor = {
jsx(node: Object, context: Context): JSXValue {
const nodes = visitJsxExpressions(node.nodes, context);
node.nodes = nodes.length === 1 ? nodes : [buildJSXFragment(nodes)];
return node;
},
};

export default NamedBlockVisitor;
13 changes: 12 additions & 1 deletion src/visitors/Tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,24 @@ function getAttributesAndChildren(
attrs: Array<JSXAttribute | JSXSpreadAttribute>,
children: Array<JSXValue>,
} {
const children = getChildren(node, context);
let children = getChildren(node, context);

if (node.attributeBlocks.length) {
throw new Error('Attribute blocks are not yet supported in react-pug');
}

const attrs = getAttributes(node, context);

const blocks = children.filter(({type}) => type === 'NamedBlock');
if (blocks.length) {
children = children.filter(({type}) => type !== 'NamedBlock');
attrs.push(
...blocks.map(block =>
t.jSXAttribute(t.jSXIdentifier(block.name), block.nodes[0]),
),
);
}

context.key.handleAttributes(attrs);

return {attrs, children};
Expand Down