Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove defaultProps which are deprecated in React #128

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 1 addition & 3 deletions src/transformSvg.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ export default (t) => ({
// to
// <svg strokeWidth="5">
// don't convert any custom data-* or aria-* attributes just wrap in quotes
if (/^data-|^aria-/.test(originalName.name)) {
originalName.name = `'${originalName.name}'`;
} else {
if (!/^data-|^aria-/.test(originalName.name)) {
originalName.name = hyphenToCamel(originalName.name);
}
}
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
Loading