@svgr/babel-plugin-svg-dynamic-title
Advanced tools
Comparing version 6.0.0 to 6.3.0
import { NodePath, types } from '@babel/core'; | ||
declare type tag = 'title' | 'desc'; | ||
interface Options { | ||
tag: tag | null; | ||
} | ||
interface State { | ||
opts: Options; | ||
} | ||
declare const plugin: () => { | ||
visitor: { | ||
JSXElement(path: NodePath<types.JSXElement>): void; | ||
JSXElement(path: NodePath<types.JSXElement>, state: State): void; | ||
}; | ||
}; | ||
export { plugin as default }; | ||
export { Options, plugin as default }; |
@@ -6,13 +6,13 @@ 'use strict'; | ||
const elements = ["svg", "Svg"]; | ||
const createTitleElement = (children = [], attributes = []) => { | ||
const title = core.types.jsxIdentifier("title"); | ||
return core.types.jsxElement(core.types.jsxOpeningElement(title, attributes), core.types.jsxClosingElement(title), children); | ||
const createTagElement = (tag, children = [], attributes = []) => { | ||
const eleName = core.types.jsxIdentifier(tag); | ||
return core.types.jsxElement(core.types.jsxOpeningElement(eleName, attributes), core.types.jsxClosingElement(eleName), children); | ||
}; | ||
const createTitleIdAttribute = () => core.types.jsxAttribute(core.types.jsxIdentifier("id"), core.types.jsxExpressionContainer(core.types.identifier("titleId"))); | ||
const addTitleIdAttribute = (attributes) => { | ||
const createTagIdAttribute = (tag) => core.types.jsxAttribute(core.types.jsxIdentifier("id"), core.types.jsxExpressionContainer(core.types.identifier(`${tag}Id`))); | ||
const addTagIdAttribute = (tag, attributes) => { | ||
const existingId = attributes.find((attribute) => core.types.isJSXAttribute(attribute) && attribute.name.name === "id"); | ||
if (!existingId) { | ||
return [...attributes, createTitleIdAttribute()]; | ||
return [...attributes, createTagIdAttribute(tag)]; | ||
} | ||
existingId.value = core.types.jsxExpressionContainer(core.types.isStringLiteral(existingId.value) ? core.types.logicalExpression("||", core.types.identifier("titleId"), existingId.value) : core.types.identifier("titleId")); | ||
existingId.value = core.types.jsxExpressionContainer(core.types.isStringLiteral(existingId.value) ? core.types.logicalExpression("||", core.types.identifier(`${tag}Id`), existingId.value) : core.types.identifier(`${tag}Id`)); | ||
return attributes; | ||
@@ -22,3 +22,4 @@ }; | ||
visitor: { | ||
JSXElement(path) { | ||
JSXElement(path, state) { | ||
const tag = state.opts.tag || "title"; | ||
if (!elements.length) | ||
@@ -31,17 +32,17 @@ return; | ||
} | ||
const getTitleElement = (existingTitle) => { | ||
const getTagElement = (existingTitle) => { | ||
var _a; | ||
const titleExpression = core.types.identifier("title"); | ||
const tagExpression = core.types.identifier(tag); | ||
if (existingTitle) { | ||
existingTitle.openingElement.attributes = addTitleIdAttribute(existingTitle.openingElement.attributes); | ||
existingTitle.openingElement.attributes = addTagIdAttribute(tag, existingTitle.openingElement.attributes); | ||
} | ||
const conditionalTitle = core.types.conditionalExpression(titleExpression, createTitleElement([core.types.jsxExpressionContainer(titleExpression)], existingTitle ? existingTitle.openingElement.attributes : [createTitleIdAttribute()]), core.types.nullLiteral()); | ||
const conditionalTitle = core.types.conditionalExpression(tagExpression, createTagElement(tag, [core.types.jsxExpressionContainer(tagExpression)], existingTitle ? existingTitle.openingElement.attributes : [createTagIdAttribute(tag)]), core.types.nullLiteral()); | ||
if ((_a = existingTitle == null ? void 0 : existingTitle.children) == null ? void 0 : _a.length) { | ||
return core.types.jsxExpressionContainer(core.types.conditionalExpression(core.types.binaryExpression("===", titleExpression, core.types.identifier("undefined")), existingTitle, conditionalTitle)); | ||
return core.types.jsxExpressionContainer(core.types.conditionalExpression(core.types.binaryExpression("===", tagExpression, core.types.identifier("undefined")), existingTitle, conditionalTitle)); | ||
} | ||
return core.types.jsxExpressionContainer(conditionalTitle); | ||
}; | ||
let titleElement = null; | ||
let tagElement = null; | ||
const hasTitle = path.get("children").some((childPath) => { | ||
if (childPath.node === titleElement) | ||
if (childPath.node === tagElement) | ||
return false; | ||
@@ -53,11 +54,11 @@ if (!childPath.isJSXElement()) | ||
return false; | ||
if (name.node.name !== "title") | ||
if (name.node.name !== tag) | ||
return false; | ||
titleElement = getTitleElement(childPath.node); | ||
childPath.replaceWith(titleElement); | ||
tagElement = getTagElement(childPath.node); | ||
childPath.replaceWith(tagElement); | ||
return true; | ||
}); | ||
titleElement = titleElement || getTitleElement(); | ||
tagElement = tagElement || getTagElement(); | ||
if (!hasTitle) { | ||
path.node.children.unshift(titleElement); | ||
path.node.children.unshift(tagElement); | ||
path.replaceWith(path.node); | ||
@@ -64,0 +65,0 @@ } |
{ | ||
"name": "@svgr/babel-plugin-svg-dynamic-title", | ||
"description": "Transform SVG by adding a dynamic title element", | ||
"version": "6.0.0", | ||
"version": "6.3.0", | ||
"main": "./dist/index.js", | ||
"exports": "./dist/index.js", | ||
"typings": "./dist/index.d.ts", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.js", | ||
"types": "./dist/index.d.ts" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"repository": "https://github.com/gregberge/svgr/tree/master/packages/babel-plugin-svg-dynamic-title", | ||
@@ -33,3 +39,3 @@ "author": "Greg Bergé <berge.greg@gmail.com>", | ||
}, | ||
"gitHead": "af9a6cbd4387180c7d6683713330a7ffcfb600e9" | ||
"gitHead": "d29884e118288a36fb68bd939983824b0d81fc8a" | ||
} |
@@ -19,4 +19,8 @@ # @svgr/babel-plugin-svg-dynamic-title | ||
## Note | ||
This plugin handles both the titleProp and descProp options. By default, it will handle titleProp only. | ||
## License | ||
MIT |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13527
78
26