Skip to content

Commit 9afb590

Browse files
authored
feat: support comments in templates (#661)
1 parent 7582d31 commit 9afb590

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

packages/babel-plugin-transform-svg-component/src/__snapshots__/index.test.ts.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ const MyComponent = (props: React.SVGProps<SVGSVGElement>) => <svg><g /></svg>;
6565
export default MyComponent;"
6666
`;
6767
68+
exports[`plugin javascript custom templates supports comments in templates 1`] = `
69+
"/**
70+
* Comment
71+
*/
72+
const MyComponent = () => <svg><g /></svg>;
73+
74+
export default MyComponent;"
75+
`;
76+
6877
exports[`plugin javascript custom templates supports template that does not return an array 1`] = `"<svg><g /></svg>;"`;
6978
7079
exports[`plugin javascript custom templates supports type annotation on component 1`] = `
@@ -284,6 +293,15 @@ const MyComponent = (props: React.SVGProps<SVGSVGElement>) => <svg><g /></svg>;
284293
export default MyComponent;"
285294
`;
286295
296+
exports[`plugin typescript custom templates supports comments in templates 1`] = `
297+
"/**
298+
* Comment
299+
*/
300+
const MyComponent = () => <svg><g /></svg>;
301+
302+
export default MyComponent;"
303+
`;
304+
287305
exports[`plugin typescript custom templates supports template that does not return an array 1`] = `"<svg><g /></svg>;"`;
288306
289307
exports[`plugin typescript custom templates supports type annotation on component 1`] = `

packages/babel-plugin-transform-svg-component/src/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,21 @@ describe('plugin', () => {
248248
})
249249
expect(code).toMatchSnapshot()
250250
})
251+
252+
it('supports comments in templates', () => {
253+
const { code } = testPlugin(language)('<svg><g /></svg>', {
254+
template: ({ jsx }, { tpl }) => tpl`
255+
/**
256+
* Comment
257+
*/
258+
const MyComponent = () => ${jsx}
259+
260+
export default MyComponent;
261+
`,
262+
state: { componentName: 'SvgComponent' },
263+
})
264+
expect(code).toMatchSnapshot()
265+
})
251266
})
252267

253268
describe('#jsxRuntime', () => {

packages/babel-plugin-transform-svg-component/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const plugin = (_: ConfigAPI, opts: Options) => {
1717
const plugins: ParserOptions['plugins'] = opts.typescript
1818
? ['jsx', 'typescript']
1919
: ['jsx']
20-
const tpl = babelTemplate.smart({ plugins }).ast
20+
const tpl = babelTemplate.smart({ plugins, preserveComments: true }).ast
2121
return {
2222
visitor: {
2323
Program(path: NodePath<t.Program>) {

0 commit comments

Comments
 (0)