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

Fix check for type imports in transform-react-native-svg #907

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
25 changes: 25 additions & 0 deletions packages/babel-plugin-transform-react-native-svg/src/index.test.ts
Original file line number Diff line number Diff line change
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>;"
`)
})
})
Original file line number Diff line number Diff line change
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