Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions packages/babel-plugin-transform-react-native-svg/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ const elementToComponent: { [key: string]: string } = {
}

const plugin = () => {
function isIdPresentInAst(path: NodePath<t.Program>, id: string): boolean {
let found = false
path.traverse({
JSXAttribute(attrPath: NodePath<t.JSXAttribute>) {
if (attrPath.node.name && attrPath.node.name.name === 'id') {
const valueNode = attrPath.node.value
if (
valueNode &&
valueNode.type === 'StringLiteral' &&
valueNode.value === id
) {
found = true
attrPath.stop()
}
}
},
})
return found
}

function replaceElement(path: NodePath<t.JSXElement>, state: State) {
const namePath = path.get('openingElement').get('name')
if (!namePath.isJSXIdentifier()) return
Expand Down Expand Up @@ -130,6 +150,126 @@ const plugin = () => {

path.traverse(svgElementVisitor, state as State)
path.traverse(importDeclarationVisitor, state as State)

if (state.unsupportedComponents && state.unsupportedComponents.size > 0) {
const unsupportedSet = new Set(state.unsupportedComponents)
path.traverse({
JSXElement(innerPath: NodePath<t.JSXElement>) {
const openingName = innerPath.get('openingElement').get('name')
if (
openingName.isJSXIdentifier() &&
unsupportedSet.has(openingName.node.name)
) {
innerPath.remove()
return
}
if (innerPath.has('closingElement')) {
const closingName = innerPath.get('closingElement').get('name')
if (
!Array.isArray(closingName) &&
closingName.isJSXIdentifier() &&
unsupportedSet.has(closingName.node.name)
) {
innerPath.remove()
}
}
},
JSXIdentifier(innerPath: NodePath<t.JSXIdentifier>) {
if (unsupportedSet.has(innerPath.node.name)) {
if (
innerPath.parentPath.isJSXOpeningElement() ||
innerPath.parentPath.isJSXClosingElement()
) {
innerPath.parentPath.remove()
}
}
},
})
}

if (state.unsupportedComponents && state.unsupportedComponents.size > 0) {
path.traverse({
JSXAttribute(attrPath: NodePath<t.JSXAttribute>) {
const attrName =
attrPath.node.name && attrPath.node.name.name
if (!attrName || typeof attrName !== 'string') return
const relevantAttrs = [
'filter',
'clipPath',
'mask',
'fill',
'stroke',
'href',
'xlinkHref',
'style',
]
if (!relevantAttrs.includes(attrName)) return
const valueNode = attrPath.node.value
if (!valueNode) return
if (
valueNode.type === 'StringLiteral' &&
/^url\(#.+\)$/.test(valueNode.value)
) {
const refId = valueNode.value.match(/^url\(#(.+)\)$/)?.[1]
if (refId && !isIdPresentInAst(path, refId)) {
attrPath.remove()
}
}
if (
valueNode.type === 'JSXExpressionContainer' &&
valueNode.expression.type === 'StringLiteral' &&
/^url\(#.+\)$/.test(valueNode.expression.value)
) {
const refId = valueNode.expression.value.match(
/^url\(#(.+)\)$/,
)?.[1]
if (refId && !isIdPresentInAst(path, refId)) {
attrPath.remove()
}
}
},
})
}

if (state.unsupportedComponents && state.unsupportedComponents.size > 0) {
path.traverse({
JSXElement(innerPath: NodePath<t.JSXElement>) {
const openingName = innerPath.get('openingElement').get('name')
if (
!openingName.isJSXIdentifier() ||
openingName.node.name !== 'use'
)
return
// Check href or xlinkHref attribute for orphaned reference
const attrs = innerPath.get('openingElement').get('attributes')
for (const attr of attrs) {
if (!attr.isJSXAttribute()) continue
const attrName = attr.node.name && attr.node.name.name
if (attrName !== 'href' && attrName !== 'xlinkHref') continue
const valueNode = attr.node.value
let refId: string | null = null
if (
valueNode &&
valueNode.type === 'StringLiteral' &&
/^#.+$/.test(valueNode.value)
) {
refId = valueNode.value.slice(1)
} else if (
valueNode &&
valueNode.type === 'JSXExpressionContainer' &&
valueNode.expression.type === 'StringLiteral' &&
/^#.+$/.test(valueNode.expression.value)
) {
refId = valueNode.expression.value.slice(1)
}
if (refId && !isIdPresentInAst(path, refId)) {
innerPath.remove()
break
}
}
},
})
}
},
},
}
Expand Down