Skip to content

Commit a92f611

Browse files
author
fpapado
committed
Handle class transform for ['propTypes'] and PropTypes.
1 parent 6af38e1 commit a92f611

File tree

8 files changed

+76
-3
lines changed

8 files changed

+76
-3
lines changed

src/transforms/react-js-make-props-and-state-transform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export function reactJSMakePropsAndStateInterfaceTransformFactoryFactory(typeChe
256256
* @param node React propTypes value
257257
*/
258258
function getTypeFromReactPropTypeExpression(node: ts.PropertyAccessExpression) {
259-
const text = node.getText().replace(/React\.PropTypes\./, '');
259+
const text = node.getText().replace(/(React\.)?PropTypes\./, '');
260260
let result = null;
261261
if (/string/.test(text)) {
262262
result = ts.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
@@ -300,7 +300,7 @@ export function reactJSMakePropsAndStateInterfaceTransformFactoryFactory(typeChe
300300
* @param node React propTypes member node
301301
*/
302302
function isPropTypeOptional(node: ts.PropertyAccessExpression) {
303-
const text = node.getText().replace(/React\.PropTypes\./, '');
303+
const text = node.getText().replace(/(React\.)?PropTypes\./, '');
304304
return !/\.isRequired/.test(text)
305305
}
306306
};

src/transforms/react-move-prop-types-to-class-transform.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,14 @@ function visitSourceFile(sourceFile: ts.SourceFile, typeChecker: ts.TypeChecker)
8787
*/
8888
function getComponentName(propTypeAssignment: ts.Statement, sourceFile: ts.SourceFile) {
8989
const text = propTypeAssignment.getText(sourceFile);
90-
return text.substr(0, text.indexOf('.'));
90+
91+
// if declared as Component.propTypes = ...
92+
if (helpers.isPropertyAccessPropTypeDeclaration(propTypeAssignment)) {
93+
return text.substr(0, text.indexOf('.'));
94+
} else {
95+
// declared as Component['propTypes']
96+
return text.substr(0, text.indexOf('['));
97+
}
9198
}
9299

93100
/**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class SomeComponent extends React.Component<{
2+
foo: number;
3+
}, {
4+
bar: string;
5+
}> {
6+
render() {
7+
return null;
8+
}
9+
}
10+
SomeComponent['propTypes'] = { foo: PropTypes.string };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class SomeComponent extends React.Component<{
2+
foo: number;
3+
}, {
4+
bar: string;
5+
}> {
6+
static propTypes = { foo: PropTypes.string };
7+
render() {
8+
return null;
9+
}
10+
}
11+
12+
SomeComponent['propTypes'] = { foo: PropTypes.string };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class SomeComponent extends React.Component<{
2+
foo: number;
3+
}, {
4+
bar: string;
5+
}> {
6+
render() {
7+
return null;
8+
}
9+
}
10+
SomeComponent.propTypes = { foo: PropTypes.string };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class SomeComponent extends React.Component<{
2+
foo: number;
3+
}, {
4+
bar: string;
5+
}> {
6+
static propTypes = { foo: PropTypes.string };
7+
render() {
8+
return null;
9+
}
10+
}
11+
12+
SomeComponent.propTypes = { foo: PropTypes.string };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class SomeComponent extends React.Component<{
2+
foo: number;
3+
}, {
4+
bar: string;
5+
}> {
6+
render() {
7+
return null;
8+
}
9+
}
10+
SomeComponent['propTypes'] = { foo: React.PropTypes.string };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class SomeComponent extends React.Component<{
2+
foo: number;
3+
}, {
4+
bar: string;
5+
}> {
6+
static propTypes = { foo: React.PropTypes.string };
7+
render() {
8+
return null;
9+
}
10+
}
11+
12+
SomeComponent['propTypes'] = { foo: React.PropTypes.string };

0 commit comments

Comments
 (0)