Skip to content

Commit

Permalink
feat: support comments in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
tu4mo committed Jan 2, 2022
1 parent 17507b4 commit a3934a2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ const MyComponent = (props: React.SVGProps<SVGSVGElement>) => <svg><g /></svg>;
export default MyComponent;"
`;
exports[`plugin javascript custom templates supports comments in templates 1`] = `
"/**
* Comment
*/
const MyComponent = () => <svg><g /></svg>;
export default MyComponent;"
`;
exports[`plugin javascript custom templates supports template that does not return an array 1`] = `"<svg><g /></svg>;"`;
exports[`plugin javascript custom templates supports type annotation on component 1`] = `
Expand Down Expand Up @@ -284,6 +293,15 @@ const MyComponent = (props: React.SVGProps<SVGSVGElement>) => <svg><g /></svg>;
export default MyComponent;"
`;
exports[`plugin typescript custom templates supports comments in templates 1`] = `
"/**
* Comment
*/
const MyComponent = () => <svg><g /></svg>;
export default MyComponent;"
`;
exports[`plugin typescript custom templates supports template that does not return an array 1`] = `"<svg><g /></svg>;"`;
exports[`plugin typescript custom templates supports type annotation on component 1`] = `
Expand Down
15 changes: 15 additions & 0 deletions packages/babel-plugin-transform-svg-component/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ describe('plugin', () => {
})
expect(code).toMatchSnapshot()
})

it('supports comments in templates', () => {
const { code } = testPlugin(language)('<svg><g /></svg>', {
template: ({ jsx }, { tpl }) => tpl`
/**
* Comment
*/
const MyComponent = () => ${jsx}
export default MyComponent;
`,
state: { componentName: 'SvgComponent' },
})
expect(code).toMatchSnapshot()
})
})

describe('#jsxRuntime', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-transform-svg-component/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const plugin = (_: ConfigAPI, opts: Options) => {
const plugins: ParserOptions['plugins'] = opts.typescript
? ['jsx', 'typescript']
: ['jsx']
const tpl = babelTemplate.smart({ plugins }).ast
const tpl = babelTemplate.smart({ plugins, preserveComments: true }).ast
return {
visitor: {
Program(path: NodePath<t.Program>) {
Expand Down

0 comments on commit a3934a2

Please sign in to comment.