Skip to content

Commit

Permalink
Remove defaultProps which are deprecated in React
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vieira committed Aug 30, 2024
1 parent 5efa467 commit 0038070
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 40 deletions.
33 changes: 3 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,21 @@ export default declare(({
EXPORT_FILENAME,
SVG_NAME,
SVG_CODE,
SVG_DEFAULT_PROPS_CODE,
}) => {
const namedTemplate = `
var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
${SVG_DEFAULT_PROPS_CODE ? 'SVG_NAME.defaultProps = SVG_DEFAULT_PROPS_CODE;' : ''}
${IS_EXPORT ? 'export { SVG_NAME };' : ''}
`;
const anonymousTemplate = `
var Component = function (props) { return SVG_CODE; };
${SVG_DEFAULT_PROPS_CODE ? 'Component.defaultProps = SVG_DEFAULT_PROPS_CODE;' : ''}
Component.displayName = 'EXPORT_FILENAME';
export default Component;
`;

if (SVG_NAME !== 'default') {
return template(namedTemplate)({ SVG_NAME, SVG_CODE, SVG_DEFAULT_PROPS_CODE });
return template(namedTemplate)({ SVG_NAME, SVG_CODE });
}
return template(anonymousTemplate)({ SVG_CODE, SVG_DEFAULT_PROPS_CODE, EXPORT_FILENAME });
return template(anonymousTemplate)({ SVG_CODE, EXPORT_FILENAME });
};

function applyPlugin(importIdentifier, importPath, path, state, isExport, exportFilename) {
Expand Down Expand Up @@ -95,32 +92,8 @@ export default declare(({
EXPORT_FILENAME: exportFilename,
};

// Move props off of element and into defaultProps
if (svgCode.openingElement.attributes.length > 1) {
const keepProps = [];
const defaultProps = [];

svgCode.openingElement.attributes.forEach((prop) => {
if (prop.type === 'JSXSpreadAttribute') {
keepProps.push(prop);
} else if (prop.value.type === 'JSXExpressionContainer') {
const objectExpression = t.objectExpression(prop.value.expression.properties);
defaultProps.push(t.objectProperty(t.identifier(prop.name.name), objectExpression));
} else {
defaultProps.push(t.objectProperty(t.identifier(prop.name.name), prop.value));
}
});

svgCode.openingElement.attributes = keepProps;
opts.SVG_DEFAULT_PROPS_CODE = t.objectExpression(defaultProps);
}

const svgReplacement = buildSvg(opts);
if (opts.SVG_DEFAULT_PROPS_CODE) {
[newPath] = path.replaceWithMultiple(svgReplacement);
} else {
newPath = path.replaceWith(svgReplacement);
}
[newPath] = path.replaceWithMultiple(svgReplacement);

file.get('ensureReact')();
file.set('ensureReact', () => {});
Expand Down
10 changes: 0 additions & 10 deletions test/sanity.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ function assertReactImport(result) {
}
}

function validateDefaultProps(result) {
if (!(/'data-name':/g).test(result.code)) {
throw new Error('data-* props need to be quoted');
}
}

transformFile('test/fixtures/test-import.jsx', {
babelrc: false,
presets: ['@babel/preset-react'],
Expand All @@ -29,7 +23,6 @@ transformFile('test/fixtures/test-import.jsx', {
}, (err, result) => {
if (err) throw err;
assertReactImport(result);
validateDefaultProps(result);
console.log('test/fixtures/test-import.jsx', result.code);
});

Expand All @@ -42,7 +35,6 @@ transformFile('test/fixtures/test-multiple-svg.jsx', {
}, (err, result) => {
if (err) throw err;
assertReactImport(result);
validateDefaultProps(result);
console.log('test/fixtures/test-multiple-svg.jsx', result.code);
});

Expand All @@ -56,7 +48,6 @@ transformFile('test/fixtures/test-no-react.jsx', {
if (err) throw err;
console.log('test/fixtures/test-no-react.jsx', result.code);
assertReactImport(result);
validateDefaultProps(result);
});

transformFile('test/fixtures/test-no-duplicate-react.jsx', {
Expand All @@ -80,7 +71,6 @@ transformFile('test/fixtures/test-no-duplicate-react.jsx', {
if (err) throw err;
console.log('test/fixtures/test-no-duplicate-react.jsx', result.code);
assertReactImport(result);
validateDefaultProps(result);
});

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

0 comments on commit 0038070

Please sign in to comment.