Skip to content

Commit 147de9c

Browse files
committed
refactor: use createReactClass instead
1 parent 40e9be1 commit 147de9c

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"dependencies": {
6060
"@babel/helper-plugin-utils": "^7.0.0",
6161
"@babel/parser": "^7.0.0",
62+
"create-react-class": "^15.7.0",
6263
"lodash.isplainobject": "^4.0.6",
6364
"resolve": "^2.0.0-next.5",
6465
"svgo": "^2.8.0"

src/index.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,18 @@ export default declare(({
2727
SVG_DEFAULT_PROPS_CODE,
2828
}) => {
2929
const namedTemplate = `
30-
var SVG_NAME = function SVG_NAME(props) { React.PureComponent.call(this, props); };
31-
SVG_NAME.prototype = Object.create(React.PureComponent.prototype);
32-
SVG_NAME.prototype.constructor = SVG_NAME;
33-
SVG_NAME.prototype.render = function render() { var props = this.props; return SVG_CODE; };
34-
${SVG_DEFAULT_PROPS_CODE ? 'SVG_NAME.defaultProps = SVG_DEFAULT_PROPS_CODE;' : ''}
30+
var SVG_NAME = createReactClass({
31+
render: function() { var props = this.props; return SVG_CODE; },
32+
${SVG_DEFAULT_PROPS_CODE ? 'getDefaultProps: function() { return SVG_DEFAULT_PROPS_CODE; }' : ''}
33+
});
3534
${IS_EXPORT ? 'export { SVG_NAME };' : ''}
3635
`;
3736
const anonymousTemplate = `
38-
var Component = function (props) { React.PureComponent.call(this, props); };
39-
Component.prototype = Object.create(React.PureComponent.prototype);
40-
Component.prototype.constructor = Component;
41-
Component.prototype.render = function render() { var props = this.props; return SVG_CODE; };
42-
${SVG_DEFAULT_PROPS_CODE ? 'Component.defaultProps = SVG_DEFAULT_PROPS_CODE;' : ''}
43-
Component.displayName = 'EXPORT_FILENAME';
37+
var Component = createReactClass({
38+
displayName: EXPORT_FILENAME,
39+
render: function() { var props = this.props; return SVG_CODE; },
40+
${SVG_DEFAULT_PROPS_CODE ? 'getDefaultProps: function() { return SVG_DEFAULT_PROPS_CODE; }' : ''}
41+
});
4442
export default Component;
4543
`;
4644

@@ -130,6 +128,8 @@ export default declare(({
130128

131129
file.get('ensureReact')();
132130
file.set('ensureReact', () => {});
131+
file.get('ensureCreateReactClass')();
132+
file.set('ensureCreateReactClass', () => {});
133133
}
134134
return newPath;
135135
}
@@ -144,6 +144,20 @@ export default declare(({
144144
if (typeof filename === 'undefined' && typeof opts.filename !== 'string') {
145145
throw new TypeError('the "filename" option is required when transforming code');
146146
}
147+
148+
if (!path.scope.hasBinding('create-react-class')) {
149+
const assignDeclaration = t.importDeclaration([
150+
t.importDefaultSpecifier(t.identifier('createReactClass')),
151+
], t.stringLiteral('create-react-class'));
152+
153+
file.set('ensureCreateReactClass', () => {
154+
const [newPath] = path.unshiftContainer('body', assignDeclaration);
155+
newPath.get('specifiers').forEach((specifier) => { path.scope.registerBinding('module', specifier); });
156+
});
157+
} else {
158+
file.set('ensureCreateReactClass', () => {});
159+
}
160+
147161
if (!path.scope.hasBinding('React')) {
148162
const reactImportDeclaration = t.importDeclaration([
149163
t.importDefaultSpecifier(t.identifier('React')),

test/sanity.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ function assertMatchImport(name, matchRegex) {
1818

1919
const assertReactImport = assertMatchImport('React', () => /import React from ['"]react['"]/g);
2020

21+
const assertCreateReactClassImport = assertMatchImport('createReactClass', () => /import createReactClass from ['"]create-react-class['"]/g);
22+
2123
function assertDefaultProps(shouldExist, result) {
22-
const exists = (/\.defaultProps = /g).test(result.code);
24+
const exists = (/getDefaultProps:/g).test(result.code);
2325

2426
if (!exists && shouldExist) {
25-
throw new Error('defaultProps needs to be present');
27+
throw new Error('getDefaultProps needs to be present');
2628
}
2729

2830
if (exists && !shouldExist) {
29-
throw new Error('defaultProps shouldn\'t be present');
31+
throw new Error('getDefaultProps shouldn\'t be present');
3032
}
3133
}
3234

@@ -45,6 +47,7 @@ transformFile('test/fixtures/test-import.jsx', {
4547
}, (err, result) => {
4648
if (err) throw err;
4749
assertReactImport(result);
50+
assertCreateReactClassImport(result);
4851
assertDefaultProps(true, result);
4952
validateDefaultProps(result);
5053
console.log('test/fixtures/test-import.jsx\n', result.code);
@@ -59,6 +62,7 @@ transformFile('test/fixtures/test-multiple-svg.jsx', {
5962
}, (err, result) => {
6063
if (err) throw err;
6164
assertReactImport(result);
65+
assertCreateReactClassImport(result);
6266
assertDefaultProps(true, result);
6367
validateDefaultProps(result);
6468
console.log('test/fixtures/test-multiple-svg.jsx\n', result.code);
@@ -74,6 +78,7 @@ transformFile('test/fixtures/test-no-react.jsx', {
7478
if (err) throw err;
7579
console.log('test/fixtures/test-no-react.jsx\n', result.code);
7680
assertReactImport(result);
81+
assertCreateReactClassImport(result);
7782
assertDefaultProps(true, result);
7883
validateDefaultProps(result);
7984
});
@@ -99,6 +104,7 @@ transformFile('test/fixtures/test-no-duplicate-react.jsx', {
99104
if (err) throw err;
100105
console.log('test/fixtures/test-no-duplicate-react.jsx\n', result.code);
101106
assertReactImport(result);
107+
assertCreateReactClassImport(result);
102108
assertDefaultProps(true, result);
103109
validateDefaultProps(result);
104110
});

0 commit comments

Comments
 (0)