Skip to content

Commit 0169d0d

Browse files
authored
Upgrade to TypeScript 2.4 (lyft#1)
* Upgrade to TypeScript 2.4 as well as other dependencies * Update test according to the new printer * Remove createTypeDeclarationStatement hack * Clean before lint * 1.1.0 * Remove I prefix from props and state props * Fix readme
1 parent 78bce8b commit 0169d0d

File tree

19 files changed

+64
-109
lines changed

19 files changed

+64
-109
lines changed

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ Transforms React code written in JavaScript to TypeScript.
1818
```jsx
1919
class MyComponent extends React.Component {
2020
static propTypes = {
21-
p: React.PropTypes.string.isRequired,
22-
r: React.PropTypes.number
21+
prop1: React.PropTypes.string.isRequired,
22+
prop2: React.PropTypes.number
2323
}
2424
constructor() {
2525
super();
26-
this.state = { foo: 1, bar: 2 };
26+
this.state = { foo: 1, bar: 'str' };
2727
}
2828
render() {
2929
return <div>{this.state.foo}, {this.state.bar}, {this.state.baz}</div>
@@ -36,18 +36,18 @@ class MyComponent extends React.Component {
3636

3737
**output**
3838
```tsx
39-
interface IMyComponentProps {
40-
p: string;
41-
r: number | undefined;
39+
type MyComponentProps = {
40+
prop1: string;
41+
prop2: number | undefined;
4242
}
4343

44-
interface IMyComponentState {
44+
type MyComponentState = {
4545
foo: number;
4646
bar: string;
4747
baz: number | undefined;
4848
}
4949

50-
class MyComponent extends React.Component<IMyComponentProps, IMyComponentState> {
50+
class MyComponent extends React.Component<MyComponentProps, MyComponentState> {
5151
constructor() {
5252
super();
5353
this.state = { foo: 1, bar: 'str' };

package.json

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "react-js-to-ts",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "Convert React code from JavaScript to TypeScript",
55
"main": "dist/index.js",
66
"scripts": {
77
"pretest": "npm run build",
88
"test": "jest",
99
"coverage": "jest --coverage",
1010
"posttest": "npm run lint",
11+
"prelint": "npm run clean",
1112
"lint": "tslint --type-check --project tsconfig.json --format codeFrame --exclude test/**/*.tsx",
1213
"prepublish": "npm run build",
1314
"clean": "rm -rf dist",
@@ -30,20 +31,20 @@
3031
"license": "Apache-2.0",
3132
"dependencies": {
3233
"chalk": "^1.1.3",
33-
"commander": "^2.9.0",
34+
"commander": "^2.10.0",
3435
"glob": "^7.1.2",
35-
"typescript": "^2.3.2"
36+
"typescript": "^2.4.0"
3637
},
3738
"devDependencies": {
3839
"@types/chalk": "^0.4.31",
39-
"@types/commander": "^2.9.0",
40+
"@types/commander": "^2.9.1",
4041
"@types/glob": "^5.0.30",
41-
"@types/jest": "^19.2.3",
42-
"@types/node": "^7.0.12",
43-
"@types/react": "^15.0.21",
44-
"jest": "^20.0.0",
45-
"ts-jest": "^20.0.1",
46-
"ts-node": "^3.0.2",
42+
"@types/jest": "^20.0.2",
43+
"@types/node": "^8.0.2",
44+
"@types/react": "^15.0.31",
45+
"jest": "^20.0.4",
46+
"ts-jest": "^20.0.6",
47+
"ts-node": "^3.1.0",
4748
"tslint": "^5.2.0"
4849
}
4950
}

src/compiler.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ export function compile(filePath: string, factoryFactories: TransformFactoryFact
2828
======================= Diagnostics for ${filePath} =======================
2929
`));
3030
for (const diag of result.diagnostics) {
31-
const pos = diag.file.getLineAndCharacterOfPosition(diag.start);
32-
console.log(`(${pos.line}, ${pos.character}) ${diag.messageText}`)
31+
if (diag.file && diag.start) {
32+
const pos = diag.file.getLineAndCharacterOfPosition(diag.start);
33+
console.log(`(${pos.line}, ${pos.character}) ${diag.messageText}`)
34+
}
3335
}
3436
}
3537

src/helpers/index.ts

-16
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,6 @@ export function isReactComponent(classDeclaration: ts.ClassDeclaration, typeChec
4343
return true;
4444
}
4545

46-
/**
47-
* A temporary hack until ts allows creating type alias declaration nodes
48-
* Creates a TypeAliasDeclaration
49-
* @param name Name of type node
50-
* @param type Type node
51-
* @param sourceFile SourceFile for context
52-
*/
53-
export function createTypeDeclarationStatement(name: string, type: ts.TypeNode, sourceFile: ts.SourceFile) {
54-
const printer = ts.createPrinter()
55-
const typeString = printer.printNode(ts.EmitHint.Unspecified, type, sourceFile);
56-
const newSource = ts.createSourceFile('temp.ts', `type ${name} = ${typeString}`, sourceFile.languageVersion);
57-
58-
return newSource.statements[0];
59-
}
60-
61-
6246
/**
6347
* Determine if a ts.HeritageClause is React HeritageClause
6448
*

src/transforms/collapse-intersection-interfaces-transform.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ export function collapseIntersectionInterfacesTransformFactoryFactory(
3939
.map((type: ts.TypeLiteralNode) => type.members)
4040
.reduce((all, members) => ts.createNodeArray(all.concat(members)), ts.createNodeArray([]));
4141

42-
return helpers.createTypeDeclarationStatement(
42+
return ts.createTypeAliasDeclaration(
43+
[],
44+
[],
4345
node.name.text,
46+
[],
4447
ts.createTypeLiteralNode(allMembers),
45-
sourceFile,
4648
);
4749
}
4850

src/transforms/react-hoist-generics-transform.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ function hoist(reactClass: ts.ClassDeclaration, sourceFile: ts.SourceFile) {
5454
}
5555

5656
const [propType, stateType] = reactType.typeArguments;
57-
const propTypeName = `I${className}Props`;
58-
const stateTypeName = `I${className}State`;
59-
const propTypeDeclaration = helpers.createTypeDeclarationStatement(propTypeName, propType, sourceFile);
60-
const stateTypeDeclaration = helpers.createTypeDeclarationStatement(stateTypeName, stateType, sourceFile);
57+
const propTypeName = `${className}Props`;
58+
const stateTypeName = `${className}State`;
59+
const propTypeDeclaration = ts.createTypeAliasDeclaration([], [], propTypeName, [], propType);
60+
const stateTypeDeclaration = ts.createTypeAliasDeclaration([], [], stateTypeName, [], stateType);
6161
const propTypeRef = ts.createTypeReferenceNode(propTypeName, []);
6262
const stateTypeRef = ts.createTypeReferenceNode(stateTypeName, []);
6363
const newClassStatement = insertTypeRefs(reactClass, propTypeRef, stateTypeRef);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ export function reactJSMakePropsAndStateInterfaceTransformFactoryFactory(typeChe
222222
console.warn('Bad value for propType', name, 'at', propertyAssignment.getStart());
223223
return result;
224224
}
225-
const typeValue = getTypeFromReactPropTypeExpression(propertyAssignment.initializer)
226-
const propertySignature = ts.createPropertySignature(name, undefined, typeValue, undefined);
225+
const typeValue = getTypeFromReactPropTypeExpression(propertyAssignment.initializer);
226+
const propertySignature = ts.createPropertySignature([], name, undefined, typeValue, undefined);
227227
result.members.push(propertySignature)
228228
return result;
229229
}, ts.createTypeLiteralNode([]));
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
type Foo = {
2-
3-
};
1+
type Foo = {};

test/end-to-end/basic/output.tsx

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import * as React from 'react';
22

3-
type IMyComponentProps = {
3+
type MyComponentProps = {};
4+
type MyComponentState = void;
45

5-
};
6-
type IMyComponentState = void;
7-
8-
export default class MyComponent extends React.Component<IMyComponentProps, IMyComponentState> {
6+
export default class MyComponent extends React.Component<MyComponentProps, MyComponentState> {
97
render() {
108
return <div />;
119
}
12-
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import * as React from 'react';
22

3-
type IMyComponentProps = {
3+
type MyComponentProps = {
44
baz: string;
55
};
6-
type IMyComponentState = {
6+
type MyComponentState = {
77
dynamicState: number;
88
foo: number;
99
bar: string;
1010
};
11-
export default class MyComponent extends React.Component<IMyComponentProps, IMyComponentState> {
11+
export default class MyComponent extends React.Component<MyComponentProps, MyComponentState> {
1212
state = { foo: 1, bar: 'str' };
1313
render() {
1414
return <div />;
1515
}
1616
otherFn() {
1717
this.setState({ dynamicState: 42 });
1818
}
19-
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import * as React from 'react';
22

3-
type IMyComponentProps = {
3+
type MyComponentProps = {
44
baz: string;
55
};
6-
type IMyComponentState = {
6+
type MyComponentState = {
77
foo: number;
88
bar: string;
99
};
10-
export default class MyComponent extends React.Component<IMyComponentProps, IMyComponentState> {
10+
export default class MyComponent extends React.Component<MyComponentProps, MyComponentState> {
1111
state = { foo: 1, bar: 'str' };
1212
render() {
1313
return <div />;
1414
}
15-
}
15+
}

test/react-hoist-generics-transform/propless-stateless/output.tsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import * as React from 'react';
22

3-
type IMyComponentProps = {
4-
5-
};
6-
type IMyComponentState = void;
7-
export default class MyComponent extends React.Component<IMyComponentProps, IMyComponentState> {
3+
type MyComponentProps = {};
4+
type MyComponentState = void;
5+
export default class MyComponent extends React.Component<MyComponentProps, MyComponentState> {
86
render() {
97
return <div />;
108
}

test/react-hoist-generics-transform/props-and-state/output.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as React from 'react';
22

3-
type IMyComponentProps = {
3+
type MyComponentProps = {
44
foo: string;
55
bar: object;
66
};
7-
type IMyComponentState = {
7+
type MyComponentState = {
88
baz: string;
99
[k: string]: string;
1010
};
11-
export default class MyComponent extends React.Component<IMyComponentProps, IMyComponentState> {
11+
export default class MyComponent extends React.Component<MyComponentProps, MyComponentState> {
1212
render() {
1313
return <div />;
1414
}

test/react-js-make-props-and-state-transform/propless-stateless/output.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as React from 'react';
22

3-
export default class MyComponent extends React.Component<{
4-
5-
}, void> {
3+
export default class MyComponent extends React.Component<{}, void> {
64
render() {
75
return <div />;
86
}

test/react-js-make-props-and-state-transform/set-state-advanced/output.tsx

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
import * as React from 'react';
22

3-
export default class MyComponent extends React.Component<{
4-
}, {
5-
foo: number;
6-
bar: number;
7-
} & {
8-
baz: number;
9-
} & {
10-
something: {
11-
big: number;
12-
here: string;
13-
of: {
14-
a: number;
15-
}[];
16-
};
17-
}> {
3+
export default class MyComponent extends React.Component<{}, { foo: number; bar: number; } & { baz: number; } & { something: { big: number; here: string; of: { a: number; }[]; }; }> {
184
render() {
195
return <button onClick={this.onclick.bind(this)}/>;
206
}
@@ -43,4 +29,4 @@ export default class MyComponent extends React.Component<{
4329
}
4430
});
4531
}
46-
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import * as React from 'react';
22

3-
export default class MyComponent extends React.Component<{
4-
}, {
5-
foo: number;
6-
bar: number;
7-
}> {
8-
3+
export default class MyComponent extends React.Component<{}, { foo: number; bar: number; }> {
94
render() {
105
return <button onClick={this.onclick.bind(this)}/>;
116
}
127

138
onclick() {
149
this.setState({ foo: 1, bar: 2 });
1510
}
16-
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import * as React from 'react';
22

3-
export default class MyComponent extends React.Component<{
4-
}, {
5-
foo: number;
6-
}> {
3+
export default class MyComponent extends React.Component<{}, { foo: number; }> {
74
state = { foo: 1 };
85
render() {
96
return <div />;
107
}
11-
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import * as React from 'react';
22

3-
export default class MyComponent extends React.Component<{
4-
}, {
5-
foo: number;
6-
}> {
7-
3+
export default class MyComponent extends React.Component<{}, { foo: number; }> {
84
constructor(props, context) {
95
super(props, context);
106
this.state = { foo: 1 };
@@ -13,4 +9,4 @@ export default class MyComponent extends React.Component<{
139
render() {
1410
return <div />;
1511
}
16-
}
12+
}

test/react-js-make-props-and-state-transform/static-proptypes-many-props/output.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ export default class MyComponent extends React.Component<{
44
any: any | undefined;
55
array: any[] | undefined;
66
bool: boolean | undefined;
7-
func: (...args: any[]) => any | undefined;
7+
func: ((...args: any[]) => any) | undefined;
88
number: number | undefined;
99
object: object | undefined;
1010
string: string | undefined;
11-
node: number | string | JSX.Element | undefined;
11+
node: (number | string | JSX.Element) | undefined;
1212
element: JSX.Element | undefined;
1313
anyRequired: any;
1414
arrayRequired: any[];
@@ -43,4 +43,4 @@ export default class MyComponent extends React.Component<{
4343
render() {
4444
return <div />;
4545
}
46-
}
46+
}

0 commit comments

Comments
 (0)