Skip to content

Commit dffa26c

Browse files
authored
Merge pull request #41 from smooth-code/refactor-emSize
Various improvements
2 parents 06743dc + 3c9d8b4 commit dffa26c

9 files changed

Lines changed: 783 additions & 380 deletions

File tree

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CHANGELOG.md

__fixtures__/two.svg

Lines changed: 0 additions & 20 deletions
This file was deleted.

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,34 @@
1616
"license": "MIT",
1717
"devDependencies": {
1818
"babel-cli": "^6.26.0",
19-
"babel-eslint": "^8.0.3",
19+
"babel-eslint": "^8.2.1",
2020
"babel-loader": "^7.1.2",
2121
"babel-plugin-transform-class-properties": "^6.24.1",
2222
"babel-plugin-transform-object-rest-spread": "^6.26.0",
2323
"babel-preset-env": "^1.6.0",
2424
"babel-preset-react": "^6.24.1",
2525
"codecov": "^3.0.0",
2626
"conventional-github-releaser": "^2.0.0",
27-
"eslint": "^4.12.1",
27+
"eslint": "^4.16.0",
2828
"eslint-config-airbnb-base": "^12.0.1",
2929
"eslint-config-prettier": "^2.9.0",
3030
"eslint-plugin-import": "^2.7.0",
31-
"jest": "^21.2.1",
31+
"jest": "^22.1.4",
3232
"react": "^16.2.0",
33-
"standard-version": "^4.2.0",
33+
"standard-version": "^4.3.0",
3434
"webpack": "^3.10.0"
3535
},
3636
"dependencies": {
3737
"chalk": "^2.1.0",
38-
"commander": "^2.12.2",
38+
"commander": "^2.13.0",
3939
"glob": "^7.1.2",
4040
"h2x-core": "^0.1.9",
4141
"h2x-plugin-jsx": "^0.1.9",
4242
"loader-utils": "^1.1.0",
4343
"lodash": "^4.17.4",
4444
"mz": "^2.6.0",
4545
"output-file-sync": "^2.0.0",
46-
"prettier": "^1.9.1",
46+
"prettier": "^1.10.2",
4747
"recursive-readdir": "^2.2.1",
4848
"svgo": "^1.0.3"
4949
},

src/cli/__snapshots__/index.test.js.snap

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,6 @@ export default One;
1414
"
1515
`;
1616

17-
exports[`cli --icon 2`] = `
18-
"import React from \\"react\\";
19-
20-
const Two = props => (
21-
<svg viewBox=\\"0 0 48 1\\" width=\\"1em\\" height=\\"1em\\" {...props}>
22-
<path d=\\"M0 0h48v1H0z\\" fill=\\"#063855\\" fillRule=\\"evenodd\\" />
23-
</svg>
24-
);
25-
26-
export default Two;
27-
28-
"
29-
`;
30-
3117
exports[`cli --ids 1`] = `
3218
"import React from \\"react\\";
3319

src/cli/index.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ describe('cli', () => {
3232
it('--icon', async () => {
3333
const [stdout] = await exec('bin/svgr --icon __fixtures__/one.svg')
3434
expect(stdout).toMatchSnapshot()
35-
const [stdout2] = await exec('bin/svgr --icon __fixtures__/two.svg')
36-
expect(stdout2).toMatchSnapshot()
3735
})
3836

3937
it('--ref', async () => {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`emSize should add width & height if not present 1`] = `
4+
"<svg width=\\"1em\\" height=\\"1em\\">
5+
<g stroke=\\"#063855\\" strokeWidth={2} />
6+
</svg>
7+
"
8+
`;
9+
10+
exports[`emSize should keep other attributes 1`] = `
11+
"<svg viewBox=\\"0 0 10 10\\" width=\\"1em\\" height=\\"1em\\">
12+
<g stroke=\\"#063855\\" strokeWidth={2} />
13+
</svg>
14+
"
15+
`;
16+
17+
exports[`emSize should replace width and height value by 1em 1`] = `
18+
"<svg width=\\"1em\\" height=\\"1em\\">
19+
<g stroke=\\"#063855\\" strokeWidth={2} />
20+
</svg>
21+
"
22+
`;

src/h2x/emSize.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,31 @@ const emSize = () => ({
1212
visitor: {
1313
JSXElement: {
1414
enter(path) {
15-
if (path.node.name === 'svg' && !path.node.attributes.some(attr => attr && attr.name === 'width' && attr.value === '1em') && !path.node.attributes.some(attr => attr && attr.name === 'height' && attr.value === '1em')) {
16-
const nextAttrs = path.node.attributes.filter(attr => attr.name !== 'width' && attr.name !== 'height');
17-
nextAttrs.push(makeSizeAttr('width'));
18-
nextAttrs.push(makeSizeAttr('height'));
19-
path.node.attributes = nextAttrs;
20-
path.replace(path.node);
21-
}
15+
// Skip if not svg node
16+
if (path.node.name !== 'svg') return
17+
18+
// Split attributes into two arrays
19+
const sizeAttributes = []
20+
const otherAttributes = []
21+
path.node.attributes.forEach(attr => {
22+
if (attr.name === 'width' || attr.name === 'height')
23+
sizeAttributes.push(attr)
24+
else otherAttributes.push(attr)
25+
})
26+
27+
// Skip if size attributes are correctly set
28+
if (
29+
sizeAttributes.length === 2 &&
30+
sizeAttributes.every(attr => attr.value === '1em')
31+
)
32+
return
33+
34+
path.node.attributes = [
35+
...otherAttributes,
36+
makeSizeAttr('width'),
37+
makeSizeAttr('height'),
38+
]
39+
path.replace(path.node)
2240
},
2341
},
2442
},

src/h2x/emSize.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import jsx from 'h2x-plugin-jsx'
2+
import h2x from '../plugins/h2x'
3+
import emSize from './emSize'
4+
5+
describe('emSize', () => {
6+
it('should add width & height if not present', () => {
7+
const result = h2x(
8+
`<svg>
9+
<g stroke="#063855" stroke-width="2" />
10+
</svg>`,
11+
{ plugins: [jsx, emSize] },
12+
)
13+
14+
expect(result).toMatchSnapshot()
15+
})
16+
17+
it('should replace width and height value by 1em', () => {
18+
const result = h2x(
19+
`<svg width="10px" height="10px">
20+
<g stroke="#063855" stroke-width="2" />
21+
</svg>`,
22+
{ plugins: [jsx, emSize] },
23+
)
24+
25+
expect(result).toMatchSnapshot()
26+
})
27+
28+
it('should keep other attributes', () => {
29+
const result = h2x(
30+
`<svg viewbox="0 0 10 10" width="10px" height="10px">
31+
<g stroke="#063855" stroke-width="2" />
32+
</svg>`,
33+
{ plugins: [jsx, emSize] },
34+
)
35+
36+
expect(result).toMatchSnapshot()
37+
})
38+
})

0 commit comments

Comments
 (0)