Skip to content

Commit

Permalink
Handle 'undefined' output from transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomontalbano committed Sep 16, 2022
1 parent acd6cef commit 0fd83ee
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/lib/figma.ts
Expand Up @@ -11,6 +11,7 @@ import {
promiseSequentially,
fromEntries,
chunk,
emptySvg,
} from './utils';

const getComponents = (
Expand Down Expand Up @@ -164,7 +165,7 @@ const enrichPagesWithSvg = async (
...page,
components: page.components.map((component) => ({
...component,
svg: svgs[component.id],
svg: svgs[component.id] || emptySvg,
})),
}));
};
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/lib/utils.ts
Expand Up @@ -2,6 +2,8 @@ import axios from 'axios';

export const toArray = <T>(any: T): T[] => (Array.isArray(any) ? any : [any]);

export const emptySvg = '<svg></svg>';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const fromEntries = (iterable: any[][]): { [key: string]: any } => {
return [...iterable].reduce((obj: { [key: string]: unknown }, [key, val]) => {
Expand Down Expand Up @@ -40,7 +42,7 @@ export const fetchAsSvgXml = (url: string): Promise<string> => {
'Content-Type': 'images/svg+xml',
},
}).then((response) => {
if (response.data.length === 0) return '<svg></svg>';
if (response.data.length === 0) return emptySvg;

return response.data;
}).catch((error: Error) => {
Expand Down
23 changes: 23 additions & 0 deletions packages/output-components-as-es6/src/index.test.ts
Expand Up @@ -157,6 +157,29 @@ describe('outputter as es6', () => {
);
});

it('should not break when transformers return "undefined"', async () => {
const writeFileSync = sinon.stub(fs, 'writeFileSync');
const document = figmaDocument.createDocument({ children: [figmaDocument.page1] });
const pages: FigmaExport.PageNode[] = figma.getPages(document);
const pagesWithSvg = await figma.enrichPagesWithSvg(client, 'fileABCD', pages, {
transformers: [async () => undefined],
});

nockScope.done();

await outputter({
output: 'output',
})(pagesWithSvg);

expect(writeFileSync).to.be.calledOnce;
expect(writeFileSync).to.be.calledWithMatch(
'output/page1.js',

// eslint-disable-next-line max-len
'export const figmaLogo = `<svg></svg>`;',
);
});

it('should throw an error if component starts with a number', async () => {
const page = {
...figmaDocument.page1,
Expand Down

0 comments on commit 0fd83ee

Please sign in to comment.