Skip to content
This repository was archived by the owner on Sep 21, 2019. It is now read-only.

Commit 293f840

Browse files
committed
Add prettier precommit hook for source
1 parent ca89c56 commit 293f840

8 files changed

+652
-308
lines changed

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test/
2+
dist/

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"singleQuote": true,
3+
"printWidth": 120,
4+
"tabWidth": 4,
5+
"trailingComma": "all"
6+
}

LICENSE.md

Lines changed: 183 additions & 184 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,62 @@ Transforms React code written in JavaScript to TypeScript.
1111
* Hoist large interfaces for props and state out of `React.Component<P, S>` into declared types
1212
* Convert functional components with `PropTypes` property to TypeScript and uses propTypes to generate function type declaration
1313

14-
1514
## Example
1615

1716
**input**
17+
1818
```jsx
1919
class MyComponent extends React.Component {
20-
static propTypes = {
21-
prop1: React.PropTypes.string.isRequired,
22-
prop2: React.PropTypes.number
23-
}
24-
constructor() {
25-
super();
26-
this.state = { foo: 1, bar: 'str' };
27-
}
28-
render() {
29-
return <div>{this.state.foo}, {this.state.bar}, {this.state.baz}</div>
30-
}
31-
onClick() {
32-
this.setState({ baz: 3 })
33-
}
20+
static propTypes = {
21+
prop1: React.PropTypes.string.isRequired,
22+
prop2: React.PropTypes.number,
23+
};
24+
constructor() {
25+
super();
26+
this.state = { foo: 1, bar: 'str' };
27+
}
28+
render() {
29+
return (
30+
<div>
31+
{this.state.foo}, {this.state.bar}, {this.state.baz}
32+
</div>
33+
);
34+
}
35+
onClick() {
36+
this.setState({ baz: 3 });
37+
}
3438
}
3539
```
3640

3741
**output**
42+
3843
```tsx
3944
type MyComponentProps = {
40-
prop1: string;
41-
prop2?: number;
42-
}
45+
prop1: string;
46+
prop2?: number;
47+
};
4348

4449
type MyComponentState = {
45-
foo: number;
46-
bar: string;
47-
baz: number;
48-
}
50+
foo: number;
51+
bar: string;
52+
baz: number;
53+
};
4954

5055
class MyComponent extends React.Component<MyComponentProps, MyComponentState> {
51-
constructor() {
52-
super();
53-
this.state = { foo: 1, bar: 'str' };
54-
}
55-
render() {
56-
return <div>{this.state.foo}, {this.state.bar}, {this.state.baz}</div>
57-
}
58-
onClick() {
59-
this.setState({ baz: 3 })
60-
}
56+
constructor() {
57+
super();
58+
this.state = { foo: 1, bar: 'str' };
59+
}
60+
render() {
61+
return (
62+
<div>
63+
{this.state.foo}, {this.state.bar}, {this.state.baz}
64+
</div>
65+
);
66+
}
67+
onClick() {
68+
this.setState({ baz: 3 });
69+
}
6170
}
6271
```
6372

@@ -73,8 +82,8 @@ npm install -g react-js-to-ts
7382
react-js-to-ts my-react-js-file.js
7483
```
7584

76-
7785
### VSCode plugin
86+
7887
details
7988
[Download from VSCode Marketplace](https://marketplace.visualstudio.com/items?itemName=mohsen1.react-javascript-to-typescript-transform-vscode#overview)
8089

@@ -91,13 +100,14 @@ npm test
91100
#### Watch mode
92101

93102
Pass `-w` to `npm test`
103+
94104
```
95105
npm test -- -w
96106
```
97107

98108
#### Only a single test case
99109

100-
Pass `-t` with transform name and case name space separated to `npm test`
110+
Pass `-t` with transform name and case name space separated to `npm test`
101111

102112
```
103113
npm test -- -t "react-js-make-props-and-state-transform propless-stateless"

package.json

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,57 @@
11
{
2-
"name": "react-js-to-ts",
3-
"version": "1.2.0",
4-
"description": "Convert React code from JavaScript to TypeScript",
5-
"main": "dist/index.js",
6-
"scripts": {
7-
"pretest": "npm run build",
8-
"test": "jest",
9-
"coverage": "jest --coverage",
10-
"posttest": "npm run lint",
11-
"prelint": "npm run clean",
12-
"lint": "tslint --type-check --project tsconfig.json --format codeFrame --exclude test/**/*.tsx",
13-
"prepublish": "npm run build",
14-
"clean": "rm -rf dist",
15-
"prebuild": "npm run clean",
16-
"build": "tsc --pretty"
17-
},
18-
"jest": {
19-
"mapCoverage": true,
20-
"transform": {
21-
".ts": "<rootDir>/node_modules/ts-jest/preprocessor.js"
2+
"name": "react-js-to-ts",
3+
"version": "1.2.0",
4+
"description": "Convert React code from JavaScript to TypeScript",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"pretest": "npm run build",
8+
"test": "jest",
9+
"coverage": "jest --coverage",
10+
"posttest": "npm run lint",
11+
"prelint": "npm run clean",
12+
"lint": "tslint --type-check --project tsconfig.json --format codeFrame --exclude test/**/*.tsx",
13+
"prepublish": "npm run build",
14+
"clean": "rm -rf dist",
15+
"prebuild": "npm run clean",
16+
"build": "tsc --pretty",
17+
"precommit": "lint-staged",
18+
"prettier": "prettier --write *.{js,json,css,md,ts,tsx}"
2219
},
23-
"testRegex": "test/runner.ts",
24-
"moduleFileExtensions": [
25-
"ts",
26-
"js"
27-
]
28-
},
29-
"bin": "dist/cli.js",
30-
"author": "Mohsen Azimi <me@azimi.me>",
31-
"license": "Apache-2.0",
32-
"dependencies": {
33-
"chalk": "^1.1.3",
34-
"commander": "^2.10.0",
35-
"glob": "^7.1.2",
36-
"lodash": "^4.17.4",
37-
"typescript": "^2.6.2"
38-
},
39-
"devDependencies": {
40-
"@types/chalk": "^0.4.31",
41-
"@types/commander": "^2.9.1",
42-
"@types/glob": "^5.0.30",
43-
"@types/jest": "^20.0.2",
44-
"@types/lodash": "^4.14.93",
45-
"@types/node": "^8.0.2",
46-
"@types/react": "^15.0.31",
47-
"jest": "^20.0.4",
48-
"ts-jest": "^20.0.6",
49-
"ts-node": "^3.1.0",
50-
"tslint": "^5.2.0"
51-
}
20+
"jest": {
21+
"mapCoverage": true,
22+
"transform": {
23+
".ts": "<rootDir>/node_modules/ts-jest/preprocessor.js"
24+
},
25+
"testRegex": "test/runner.ts",
26+
"moduleFileExtensions": ["ts", "js"]
27+
},
28+
"lint-staged": {
29+
"*.{js,json,css,md,ts,tsx}": ["npm run prettier", "git add"]
30+
},
31+
"bin": "dist/cli.js",
32+
"author": "Mohsen Azimi <me@azimi.me>",
33+
"license": "Apache-2.0",
34+
"dependencies": {
35+
"chalk": "^1.1.3",
36+
"commander": "^2.10.0",
37+
"glob": "^7.1.2",
38+
"lodash": "^4.17.4",
39+
"typescript": "^2.6.2"
40+
},
41+
"devDependencies": {
42+
"@types/chalk": "^0.4.31",
43+
"@types/commander": "^2.9.1",
44+
"@types/glob": "^5.0.30",
45+
"@types/jest": "^20.0.2",
46+
"@types/lodash": "^4.14.93",
47+
"@types/node": "^8.0.2",
48+
"@types/react": "^15.0.31",
49+
"husky": "^0.14.3",
50+
"jest": "^20.0.4",
51+
"lint-staged": "^6.0.1",
52+
"prettier": "^1.10.2",
53+
"ts-jest": "^20.0.6",
54+
"ts-node": "^3.1.0",
55+
"tslint": "^5.2.0"
56+
}
5257
}

tsconfig.json

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,7 @@
1111
"outDir": "dist",
1212
"sourceRoot": "../src"
1313
},
14-
"exclude": [
15-
"node_modules",
16-
"test"
17-
],
18-
"types": [
19-
"node",
20-
"jest"
21-
],
22-
"lib": [
23-
"es2017"
24-
]
14+
"exclude": ["node_modules", "test"],
15+
"types": ["node", "jest"],
16+
"lib": ["es2017"]
2517
}

tslint.json

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,11 @@
44
},
55
"defaultSeverity": "warning",
66
"rules": {
7-
"quotemark": [
8-
true,
9-
"single",
10-
"avoid-escape"
11-
],
12-
"max-line-length": [
13-
true,
14-
120
15-
],
16-
"indent": [
17-
"error",
18-
4
19-
],
20-
"only-arrow-functions": [
21-
false
22-
],
7+
"quotemark": [true, "single", "avoid-escape"],
8+
"max-line-length": [true, 120],
9+
"indent": ["error", 4],
10+
"only-arrow-functions": [false],
2311
// Disabled rules
24-
"typedef": [
25-
false
26-
]
12+
"typedef": [false]
2713
}
28-
}
14+
}

0 commit comments

Comments
 (0)