Skip to content

Commit

Permalink
source macro: preserve new lines (#1372)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Taranto <michaeltaranto@users.noreply.github.com>
  • Loading branch information
mrm007 and michaeltaranto authored Oct 12, 2023
1 parent 5345268 commit 3f8a82c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
33 changes: 33 additions & 0 deletions .changeset/red-melons-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
'@braid-design-system/source.macro': patch
---

Preserve new lines in the output code

Before:
```ts
const responsiveValue = useResponsiveValue()
const isMobile = responsiveValue({
mobile: true,
tablet: false,
})
const isDesktopOrAbove = responsiveValue({
mobile: false,
desktop: true,
})
```

After:
```ts
const responsiveValue = useResponsiveValue()

const isMobile = responsiveValue({
mobile: true,
tablet: false,
})

const isDesktopOrAbove = responsiveValue({
mobile: false,
desktop: true,
})
```
43 changes: 42 additions & 1 deletion packages/source.macro/__snapshots__/source.macro.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,48 @@ const foo = 'bar';
`;

exports[`source.macro 4. code only: 4. code only 1`] = `
exports[`source.macro 4. with new lines: 4. with new lines 1`] = `
import source from './source.macro';
const options = source(() => {
sideEffect();
return [
{ value: "1", label: "Option 1" },
{ value: "2", label: "Option 2" },
{ value: "3", label: "Option 3" },
];
});
↓ ↓ ↓ ↓ ↓ ↓
const options = {
code: '() => {\\n sideEffect();\\n\\n return [\\n { value: "1", label: "Option 1" },\\n { value: "2", label: "Option 2" },\\n { value: "3", label: "Option 3" }];\\n\\n}',
value: () => {
sideEffect();
return [
{
value: '1',
label: 'Option 1',
},
{
value: '2',
label: 'Option 2',
},
{
value: '3',
label: 'Option 3',
},
];
},
};
`;

exports[`source.macro 5. code only: 5. code only 1`] = `
import source from './source.macro';
Expand Down
4 changes: 3 additions & 1 deletion packages/source.macro/source.macro.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ module.exports = createMacro(

references.default.forEach(({ parentPath }) => {
const value = parentPath.node.arguments[0] || t.identifier('undefined');
const code = t.stringLiteral(generate(value).code);
const code = t.stringLiteral(
generate(value, { retainLines: true }).code.replace(/^\n+/, ''),
);

return parentPath.replaceWith(
codeOnly
Expand Down
15 changes: 15 additions & 0 deletions packages/source.macro/source.macro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ pluginTester({
const foo = 'bar';
`,
},
'with new lines': {
code: /* ts */ `
import source from './source.macro';
const options = source(() => {
sideEffect();
return [
{ value: "1", label: "Option 1" },
{ value: "2", label: "Option 2" },
{ value: "3", label: "Option 3" },
];
});
`,
},
'code only': {
pluginOptions: { source: { codeOnly: true } },
code: /* ts */ `
Expand Down

0 comments on commit 3f8a82c

Please sign in to comment.