Skip to content

Commit 0038070

Browse files
committed
Remove defaultProps which are deprecated in React
Drop defaultProps from the generated component as they are now deprecated and will be removed in React 19. They were added in #8 due to concerns about the overhead of `_extends` although no benchmarks were provided and it is likely that difference would be marginal where it matters. Fixes: #126
1 parent 5efa467 commit 0038070

File tree

2 files changed

+3
-40
lines changed

2 files changed

+3
-40
lines changed

src/index.js

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,21 @@ export default declare(({
2424
EXPORT_FILENAME,
2525
SVG_NAME,
2626
SVG_CODE,
27-
SVG_DEFAULT_PROPS_CODE,
2827
}) => {
2928
const namedTemplate = `
3029
var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
31-
${SVG_DEFAULT_PROPS_CODE ? 'SVG_NAME.defaultProps = SVG_DEFAULT_PROPS_CODE;' : ''}
3230
${IS_EXPORT ? 'export { SVG_NAME };' : ''}
3331
`;
3432
const anonymousTemplate = `
3533
var Component = function (props) { return SVG_CODE; };
36-
${SVG_DEFAULT_PROPS_CODE ? 'Component.defaultProps = SVG_DEFAULT_PROPS_CODE;' : ''}
3734
Component.displayName = 'EXPORT_FILENAME';
3835
export default Component;
3936
`;
4037

4138
if (SVG_NAME !== 'default') {
42-
return template(namedTemplate)({ SVG_NAME, SVG_CODE, SVG_DEFAULT_PROPS_CODE });
39+
return template(namedTemplate)({ SVG_NAME, SVG_CODE });
4340
}
44-
return template(anonymousTemplate)({ SVG_CODE, SVG_DEFAULT_PROPS_CODE, EXPORT_FILENAME });
41+
return template(anonymousTemplate)({ SVG_CODE, EXPORT_FILENAME });
4542
};
4643

4744
function applyPlugin(importIdentifier, importPath, path, state, isExport, exportFilename) {
@@ -95,32 +92,8 @@ export default declare(({
9592
EXPORT_FILENAME: exportFilename,
9693
};
9794

98-
// Move props off of element and into defaultProps
99-
if (svgCode.openingElement.attributes.length > 1) {
100-
const keepProps = [];
101-
const defaultProps = [];
102-
103-
svgCode.openingElement.attributes.forEach((prop) => {
104-
if (prop.type === 'JSXSpreadAttribute') {
105-
keepProps.push(prop);
106-
} else if (prop.value.type === 'JSXExpressionContainer') {
107-
const objectExpression = t.objectExpression(prop.value.expression.properties);
108-
defaultProps.push(t.objectProperty(t.identifier(prop.name.name), objectExpression));
109-
} else {
110-
defaultProps.push(t.objectProperty(t.identifier(prop.name.name), prop.value));
111-
}
112-
});
113-
114-
svgCode.openingElement.attributes = keepProps;
115-
opts.SVG_DEFAULT_PROPS_CODE = t.objectExpression(defaultProps);
116-
}
117-
11895
const svgReplacement = buildSvg(opts);
119-
if (opts.SVG_DEFAULT_PROPS_CODE) {
120-
[newPath] = path.replaceWithMultiple(svgReplacement);
121-
} else {
122-
newPath = path.replaceWith(svgReplacement);
123-
}
96+
[newPath] = path.replaceWithMultiple(svgReplacement);
12497

12598
file.get('ensureReact')();
12699
file.set('ensureReact', () => {});

test/sanity.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ function assertReactImport(result) {
1414
}
1515
}
1616

17-
function validateDefaultProps(result) {
18-
if (!(/'data-name':/g).test(result.code)) {
19-
throw new Error('data-* props need to be quoted');
20-
}
21-
}
22-
2317
transformFile('test/fixtures/test-import.jsx', {
2418
babelrc: false,
2519
presets: ['@babel/preset-react'],
@@ -29,7 +23,6 @@ transformFile('test/fixtures/test-import.jsx', {
2923
}, (err, result) => {
3024
if (err) throw err;
3125
assertReactImport(result);
32-
validateDefaultProps(result);
3326
console.log('test/fixtures/test-import.jsx', result.code);
3427
});
3528

@@ -42,7 +35,6 @@ transformFile('test/fixtures/test-multiple-svg.jsx', {
4235
}, (err, result) => {
4336
if (err) throw err;
4437
assertReactImport(result);
45-
validateDefaultProps(result);
4638
console.log('test/fixtures/test-multiple-svg.jsx', result.code);
4739
});
4840

@@ -56,7 +48,6 @@ transformFile('test/fixtures/test-no-react.jsx', {
5648
if (err) throw err;
5749
console.log('test/fixtures/test-no-react.jsx', result.code);
5850
assertReactImport(result);
59-
validateDefaultProps(result);
6051
});
6152

6253
transformFile('test/fixtures/test-no-duplicate-react.jsx', {
@@ -80,7 +71,6 @@ transformFile('test/fixtures/test-no-duplicate-react.jsx', {
8071
if (err) throw err;
8172
console.log('test/fixtures/test-no-duplicate-react.jsx', result.code);
8273
assertReactImport(result);
83-
validateDefaultProps(result);
8474
});
8575

8676
if (fs.existsSync(path.resolve('./PACKAGE.JSON'))) {

0 commit comments

Comments
 (0)