From b48dd1f250a8371023386105d9fd427d2a7f01e0 Mon Sep 17 00:00:00 2001 From: Jungzl <13jungzl@gmail.com> Date: Tue, 28 Apr 2026 17:01:34 +0800 Subject: [PATCH 1/3] fix: skip handling of SVG path elements with d attribute or inside svg --- src/__snapshots__/index.test.ts.snap | 12 ++++++++++++ src/index.test.ts | 22 ++++++++++++++++++++++ src/index.ts | 25 +++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/src/__snapshots__/index.test.ts.snap b/src/__snapshots__/index.test.ts.snap index cef0208..bd6a317 100644 --- a/src/__snapshots__/index.test.ts.snap +++ b/src/__snapshots__/index.test.ts.snap @@ -1,5 +1,17 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`do not handle path tag inside svg 1`] = ` +"function Icon(props) { + return ; +}" +`; + +exports[`do not handle path tag with d attribute outside svg 1`] = ` +"function Icon(props) { + return ; +}" +`; + exports[`handles JSX 1`] = ` "import { Mesh as _Mesh, BoxGeometry as _BoxGeometry, MeshBasicMaterial as _MeshBasicMaterial } from \\"three\\"; import { extend as _extend } from \\"@react-three/fiber\\"; diff --git a/src/index.test.ts b/src/index.test.ts index 9e420e3..466e11c 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -75,3 +75,25 @@ it('handles template strings', () => { ) expect(code).toMatchSnapshot() }) + +it('do not handle path tag inside svg',()=>{ + const code = transform( + ` + function Icon(props) { + return ; + } + ` + ) + expect(code).toMatchSnapshot() +}) + +it('do not handle path tag with d attribute outside svg',()=>{ + const code = transform( + ` + function Icon(props) { + return ; + } + ` + ) + expect(code).toMatchSnapshot() +}) diff --git a/src/index.ts b/src/index.ts index 38c956f..0de579a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -79,6 +79,31 @@ export default declare((api) => { // Parse identifiers (e.g. , ) let type = 'property' in name ? name.property.name : name.name + + // Skip SVG path with 'd' attribute (SVG path data) + if (type === 'path') { + const hasDAttribute = path.node.attributes.some( + (attr) => + t.isJSXAttribute(attr) && + t.isJSXIdentifier(attr.name) && + attr.name.name === 'd' + ) + if (hasDAttribute) return + + // fallback + let parent: NodePath | null = path.parentPath + while (parent) { + if ( + t.isJSXElement(parent.node) && + t.isJSXIdentifier(parent.node.openingElement.name) && + parent.node.openingElement.name.name === 'svg' + ) { + return + } + parent = parent.parentPath + } + } + const declaration = path.scope.getBinding(type)?.path.node if (t.isVariableDeclarator(declaration)) { if (t.isStringLiteral(declaration.init)) { From 2b512c63a007c27f081adaa99988f8ccc8608b57 Mon Sep 17 00:00:00 2001 From: Jungzl <13jungzl@gmail.com> Date: Tue, 28 Apr 2026 17:11:30 +0800 Subject: [PATCH 2/3] chore: snapshot --- src/__snapshots__/index.test.ts.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__snapshots__/index.test.ts.snap b/src/__snapshots__/index.test.ts.snap index bd6a317..ac508df 100644 --- a/src/__snapshots__/index.test.ts.snap +++ b/src/__snapshots__/index.test.ts.snap @@ -8,7 +8,7 @@ exports[`do not handle path tag inside svg 1`] = ` exports[`do not handle path tag with d attribute outside svg 1`] = ` "function Icon(props) { - return ; + return ; }" `; From 73f3949a961b5980b30bf902b5602c7d3cd6767a Mon Sep 17 00:00:00 2001 From: Jungzl <13jungzl@gmail.com> Date: Thu, 14 May 2026 11:53:50 +0800 Subject: [PATCH 3/3] chore: test --- src/__snapshots__/index.test.ts.snap | 11 +++++++++++ src/index.test.ts | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/__snapshots__/index.test.ts.snap b/src/__snapshots__/index.test.ts.snap index ac508df..11a33cc 100644 --- a/src/__snapshots__/index.test.ts.snap +++ b/src/__snapshots__/index.test.ts.snap @@ -12,6 +12,17 @@ exports[`do not handle path tag with d attribute outside svg 1`] = ` }" `; +exports[`handle path tag w/o d attribute 1`] = ` +"import { Path as _Path } from \\"three\\"; +import { extend as _extend } from \\"@react-three/fiber\\"; +_extend({ + Path: _Path +}); +function Comp(props) { + return ; +}" +`; + exports[`handles JSX 1`] = ` "import { Mesh as _Mesh, BoxGeometry as _BoxGeometry, MeshBasicMaterial as _MeshBasicMaterial } from \\"three\\"; import { extend as _extend } from \\"@react-three/fiber\\"; diff --git a/src/index.test.ts b/src/index.test.ts index 466e11c..414625a 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -97,3 +97,14 @@ it('do not handle path tag with d attribute outside svg',()=>{ ) expect(code).toMatchSnapshot() }) + +it('handle path tag w/o d attribute',()=>{ + const code = transform( + ` + function Comp(props) { + return ; + } + ` + ) + expect(code).toMatchSnapshot() +})