From 44ba678186dcc690779d417b83e094fa500fcc42 Mon Sep 17 00:00:00 2001 From: sinchang Date: Sun, 24 Jul 2022 15:15:18 +0800 Subject: [PATCH] feat(babel-preset): add attribute role=img to the svg element --- packages/babel-preset/src/index.test.ts | 24 ++++++++++++------------ packages/babel-preset/src/index.ts | 7 ++++++- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/packages/babel-preset/src/index.test.ts b/packages/babel-preset/src/index.test.ts index 8460c9fe..18945f50 100644 --- a/packages/babel-preset/src/index.test.ts +++ b/packages/babel-preset/src/index.test.ts @@ -28,7 +28,7 @@ describe('preset', () => { ).toMatchInlineSnapshot(` "import * as React from \\"react\\"; - const SvgComponent = () => ; + const SvgComponent = () => ; export default SvgComponent;" `) @@ -45,7 +45,7 @@ describe('preset', () => { const SvgComponent = ({ title, titleId - }) => {title ? {title} : null}; + }) => {title ? {title} : null}; export default SvgComponent;" `) @@ -62,7 +62,7 @@ describe('preset', () => { const SvgComponent = ({ title, titleId - }) => {title === undefined ? Hello : title ? {title} : null}; + }) => {title === undefined ? Hello : title ? {title} : null}; export default SvgComponent;" `) @@ -77,7 +77,7 @@ describe('preset', () => { const SvgComponent = ({ title, titleId - }) => {title === undefined ? {\\"Hello\\"} : title ? {title} : null}; + }) => {title === undefined ? {\\"Hello\\"} : title ? {title} : null}; export default SvgComponent;" `) @@ -94,7 +94,7 @@ describe('preset', () => { const SvgComponent = ({ desc, descId - }) => {desc ? {desc} : null}; + }) => {desc ? {desc} : null}; export default SvgComponent;" `) @@ -111,7 +111,7 @@ describe('preset', () => { const SvgComponent = ({ desc, descId - }) => {desc === undefined ? Hello : desc ? {desc} : null}; + }) => {desc === undefined ? Hello : desc ? {desc} : null}; export default SvgComponent;" `) @@ -126,7 +126,7 @@ describe('preset', () => { const SvgComponent = ({ desc, descId - }) => {desc === undefined ? {\\"Hello\\"} : desc ? {desc} : null}; + }) => {desc === undefined ? {\\"Hello\\"} : desc ? {desc} : null}; export default SvgComponent;" `) @@ -146,7 +146,7 @@ describe('preset', () => { titleId, desc, descId - }) => {desc ? {desc} : null}{title ? {title} : null}; + }) => {desc ? {desc} : null}{title ? {title} : null}; export default SvgComponent;" `) @@ -163,7 +163,7 @@ describe('preset', () => { ).toMatchInlineSnapshot(` "import * as React from \\"react\\"; - const SvgComponent = () => ; + const SvgComponent = () => ; export default SvgComponent;" `) @@ -179,7 +179,7 @@ describe('preset', () => { ).toMatchInlineSnapshot(` "import * as React from \\"react\\"; - const SvgComponent = props => ; + const SvgComponent = props => ; export default SvgComponent;" `) @@ -195,7 +195,7 @@ describe('preset', () => { ).toMatchInlineSnapshot(` "import * as React from \\"react\\"; - const SvgComponent = props => ; + const SvgComponent = props => ; export default SvgComponent;" `) @@ -213,7 +213,7 @@ describe('preset', () => { "import * as React from \\"react\\"; import Svg from \\"react-native-svg\\"; - const SvgComponent = props => ; + const SvgComponent = props => ; export default SvgComponent;" `) diff --git a/packages/babel-preset/src/index.ts b/packages/babel-preset/src/index.ts index c77b2396..7a5b1b39 100644 --- a/packages/babel-preset/src/index.ts +++ b/packages/babel-preset/src/index.ts @@ -49,7 +49,12 @@ function replaceMapToValues(replaceMap: { [key: string]: string }): Value[] { const plugin = (_: ConfigAPI, opts: Options) => { let toRemoveAttributes = ['version'] - let toAddAttributes: Attribute[] = [] + let toAddAttributes: Attribute[] = [ + { + name: 'role', + value: 'img', + }, + ] if (opts.svgProps) { toAddAttributes = [...toAddAttributes, ...propsToAttributes(opts.svgProps)]