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

feat: support comments in templates #661

Merged
merged 1 commit into from
Jan 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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