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

source macro: preserve new lines #1372

Merged
merged 4 commits into from
Oct 12, 2023
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
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