Skip to content

Commit

Permalink
fix: check for type imports in transform-react-native-svg (#907)
Browse files Browse the repository at this point in the history
Signed-off-by: Yash Srivastav <yashsriv@meta.com>
  • Loading branch information
yashsriv committed Sep 28, 2023
1 parent 308672d commit f73f14d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/babel-plugin-transform-react-native-svg/src/index.test.ts
Expand Up @@ -26,4 +26,29 @@ describe('plugin', () => {
<Svg><G /></Svg>;"
`)
})

it('should add deal with type imports properly', () => {
const code = transform(
`
import Svg from 'react-native-svg';
import type { SvgProps } from "react-native-svg";
const ComponentSvg = () => <svg><g /></svg>;
`,
{
plugins: [
'@babel/plugin-syntax-jsx',
['@babel/plugin-syntax-typescript', { isTSX: true }],
plugin,
],
configFile: false,
},
)?.code

expect(code).toMatchInlineSnapshot(`
"import Svg, { G } from 'react-native-svg';
import type { SvgProps } from "react-native-svg";
const ComponentSvg = () => <Svg><G /></Svg>;"
`)
})
})
Expand Up @@ -79,9 +79,13 @@ const plugin = () => {

const importDeclarationVisitor = {
ImportDeclaration(path: NodePath<t.ImportDeclaration>, state: State) {
const isNotTypeImport =
!path.get('importKind').hasNode() ||
path.node.importKind == null ||
path.node.importKind === 'value'
if (
path.get('source').isStringLiteral({ value: 'react-native-svg' }) &&
!path.get('importKind').hasNode()
isNotTypeImport
) {
state.replacedComponents.forEach((component) => {
if (
Expand Down

1 comment on commit f73f14d

@vercel
Copy link

@vercel vercel bot commented on f73f14d Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

svgr – ./

svgr-gregberge.vercel.app
api.react-svgr.com
svgr-git-main-gregberge.vercel.app

Please sign in to comment.