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

Add prettier precommit hook for source #21

Merged
merged 1 commit into from
Jan 20, 2018
Merged
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
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/
dist/
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"printWidth": 120,
"tabWidth": 4,
"trailingComma": "all"
}
6 changes: 2 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
"preLaunchTask": "compile",
"program": "${workspaceRoot}/node_modules/.bin/jest",
"request": "launch",
"runtimeArgs": [

],
"runtimeArgs": [],
"runtimeExecutable": null,
"sourceMaps": true,
"stopOnEntry": false,
Expand All @@ -28,4 +26,4 @@
"port": 5858
}
]
}
}
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
"args": ["-p", "."],
"showOutput": "silent",
"problemMatcher": "$tsc"
}
}
367 changes: 183 additions & 184 deletions LICENSE.md

Large diffs are not rendered by default.

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


## Example

**input**

```jsx
class MyComponent extends React.Component {
static propTypes = {
prop1: React.PropTypes.string.isRequired,
prop2: React.PropTypes.number
}
constructor() {
super();
this.state = { foo: 1, bar: 'str' };
}
render() {
return <div>{this.state.foo}, {this.state.bar}, {this.state.baz}</div>
}
onClick() {
this.setState({ baz: 3 })
}
static propTypes = {
prop1: React.PropTypes.string.isRequired,
prop2: React.PropTypes.number,
};
constructor() {
super();
this.state = { foo: 1, bar: 'str' };
}
render() {
return (
<div>
{this.state.foo}, {this.state.bar}, {this.state.baz}
</div>
);
}
onClick() {
this.setState({ baz: 3 });
}
}
```

**output**

```tsx
type MyComponentProps = {
prop1: string;
prop2?: number;
}
prop1: string;
prop2?: number;
};

type MyComponentState = {
foo: number;
bar: string;
baz: number;
}
foo: number;
bar: string;
baz: number;
};

class MyComponent extends React.Component<MyComponentProps, MyComponentState> {
constructor() {
super();
this.state = { foo: 1, bar: 'str' };
}
render() {
return <div>{this.state.foo}, {this.state.bar}, {this.state.baz}</div>
}
onClick() {
this.setState({ baz: 3 })
}
constructor() {
super();
this.state = { foo: 1, bar: 'str' };
}
render() {
return (
<div>
{this.state.foo}, {this.state.bar}, {this.state.baz}
</div>
);
}
onClick() {
this.setState({ baz: 3 });
}
}
```

Expand All @@ -73,8 +82,8 @@ npm install -g react-js-to-ts
react-js-to-ts my-react-js-file.js
```


### VSCode plugin

details
[Download from VSCode Marketplace](https://marketplace.visualstudio.com/items?itemName=mohsen1.react-javascript-to-typescript-transform-vscode#overview)

Expand All @@ -91,13 +100,14 @@ npm test
#### Watch mode

Pass `-w` to `npm test`

```
npm test -- -w
```

#### Only a single test case

Pass `-t` with transform name and case name space separated to `npm test`
Pass `-t` with transform name and case name space separated to `npm test`

```
npm test -- -t "react-js-make-props-and-state-transform propless-stateless"
Expand Down
103 changes: 54 additions & 49 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
{
"name": "react-js-to-ts",
"version": "1.2.0",
"description": "Convert React code from JavaScript to TypeScript",
"main": "dist/index.js",
"scripts": {
"pretest": "npm run build",
"test": "jest",
"coverage": "jest --coverage",
"posttest": "npm run lint",
"prelint": "npm run clean",
"lint": "tslint --type-check --project tsconfig.json --format codeFrame --exclude test/**/*.tsx",
"prepublish": "npm run build",
"clean": "rm -rf dist",
"prebuild": "npm run clean",
"build": "tsc --pretty"
},
"jest": {
"mapCoverage": true,
"transform": {
".ts": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"name": "react-js-to-ts",
"version": "1.2.0",
"description": "Convert React code from JavaScript to TypeScript",
"main": "dist/index.js",
"scripts": {
"pretest": "npm run build",
"test": "jest",
"coverage": "jest --coverage",
"posttest": "npm run lint",
"prelint": "npm run clean",
"lint": "tslint --type-check --project tsconfig.json --format codeFrame --exclude test/**/*.tsx",
"prepublish": "npm run build",
"clean": "rm -rf dist",
"prebuild": "npm run clean",
"build": "tsc --pretty",
"precommit": "lint-staged",
"prettier": "prettier --write *.{js,json,css,md,ts,tsx}"
},
"testRegex": "test/runner.ts",
"moduleFileExtensions": [
"ts",
"js"
]
},
"bin": "dist/cli.js",
"author": "Mohsen Azimi <me@azimi.me>",
"license": "Apache-2.0",
"dependencies": {
"chalk": "^1.1.3",
"commander": "^2.10.0",
"glob": "^7.1.2",
"lodash": "^4.17.4",
"typescript": "^2.6.2"
},
"devDependencies": {
"@types/chalk": "^0.4.31",
"@types/commander": "^2.9.1",
"@types/glob": "^5.0.30",
"@types/jest": "^20.0.2",
"@types/lodash": "^4.14.93",
"@types/node": "^8.0.2",
"@types/react": "^15.0.31",
"jest": "^20.0.4",
"ts-jest": "^20.0.6",
"ts-node": "^3.1.0",
"tslint": "^5.2.0"
}
"jest": {
"mapCoverage": true,
"transform": {
".ts": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "test/runner.ts",
"moduleFileExtensions": ["ts", "js"]
},
"lint-staged": {
"*.{js,json,css,md,ts,tsx}": ["npm run prettier", "git add"]
},
"bin": "dist/cli.js",
"author": "Mohsen Azimi <me@azimi.me>",
"license": "Apache-2.0",
"dependencies": {
"chalk": "^1.1.3",
"commander": "^2.10.0",
"glob": "^7.1.2",
"lodash": "^4.17.4",
"typescript": "^2.6.2"
},
"devDependencies": {
"@types/chalk": "^0.4.31",
"@types/commander": "^2.9.1",
"@types/glob": "^5.0.30",
"@types/jest": "^20.0.2",
"@types/lodash": "^4.14.93",
"@types/node": "^8.0.2",
"@types/react": "^15.0.31",
"husky": "^0.14.3",
"jest": "^20.0.4",
"lint-staged": "^6.0.1",
"prettier": "^1.10.2",
"ts-jest": "^20.0.6",
"ts-node": "^3.1.0",
"tslint": "^5.2.0"
}
}
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ program
.version('1.0.0')
.usage('[options] <filename or glob>')
.command('* <glob>')
.action((globPattern) => {
.action(globPattern => {
if (!globPattern) {
throw new Error('You must provide a file name or glob pattern to transform');
}
Expand All @@ -24,7 +24,7 @@ program
fs.renameSync(filePath, newPath);
const result = run(newPath);
fs.writeFileSync(newPath, result);
} catch(error) {
} catch (error) {
console.warn(`Failed to convert ${file}`);
console.warn(error);
}
Expand Down
11 changes: 6 additions & 5 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@ export function compile(filePath: string, factoryFactories: TransformFactoryFact
factoryFactories.map(factoryFactory => factoryFactory(typeChecker), compilerOptions),
);


if (result.diagnostics && result.diagnostics.length) {
console.log(chalk.yellow(`
console.log(
chalk.yellow(`
======================= Diagnostics for ${filePath} =======================
`));
`),
);
for (const diag of result.diagnostics) {
if (diag.file && diag.start) {
const pos = diag.file.getLineAndCharacterOfPosition(diag.start);
console.log(`(${pos.line}, ${pos.character}) ${diag.messageText}`)
console.log(`(${pos.line}, ${pos.character}) ${diag.messageText}`);
}
}
}

const printer = ts.createPrinter()
const printer = ts.createPrinter();

// TODO: fix the index 0 access... What if program have multiple source files?
return printer.printNode(ts.EmitHint.SourceFile, result.transformed[0], sourceFiles[0]);
Expand Down
24 changes: 6 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import * as ts from 'typescript';

import { compile } from './compiler';
import {
reactJSMakePropsAndStateInterfaceTransformFactoryFactory,
} from './transforms/react-js-make-props-and-state-transform';
import {
reactHoistGenericsTransformFactoryFactory,
} from './transforms/react-hoist-generics-transform';
import {
reactRemovePropTypesAssignmentTransformFactoryFactory,
} from './transforms/react-remove-prop-types-assignment-transform';
import {
reactMovePropTypesToClassTransformFactoryFactory,
} from './transforms/react-move-prop-types-to-class-transform';
import {
collapseIntersectionInterfacesTransformFactoryFactory,
} from './transforms/collapse-intersection-interfaces-transform';
import {
reactRemoveStaticPropTypesMemberTransformFactoryFactory,
} from './transforms/react-remove-static-prop-types-member-transform';
import { reactJSMakePropsAndStateInterfaceTransformFactoryFactory } from './transforms/react-js-make-props-and-state-transform';
import { reactHoistGenericsTransformFactoryFactory } from './transforms/react-hoist-generics-transform';
import { reactRemovePropTypesAssignmentTransformFactoryFactory } from './transforms/react-remove-prop-types-assignment-transform';
import { reactMovePropTypesToClassTransformFactoryFactory } from './transforms/react-move-prop-types-to-class-transform';
import { collapseIntersectionInterfacesTransformFactoryFactory } from './transforms/collapse-intersection-interfaces-transform';
import { reactRemoveStaticPropTypesMemberTransformFactoryFactory } from './transforms/react-remove-static-prop-types-member-transform';

export {
reactMovePropTypesToClassTransformFactoryFactory,
Expand Down
14 changes: 3 additions & 11 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@
"outDir": "dist",
"sourceRoot": "../src"
},
"exclude": [
"node_modules",
"test"
],
"types": [
"node",
"jest"
],
"lib": [
"es2017"
]
"exclude": ["node_modules", "test"],
"types": ["node", "jest"],
"lib": ["es2017"]
}
26 changes: 6 additions & 20 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,11 @@
},
"defaultSeverity": "warning",
"rules": {
"quotemark": [
true,
"single",
"avoid-escape"
],
"max-line-length": [
true,
120
],
"indent": [
"error",
4
],
"only-arrow-functions": [
false
],
"quotemark": [true, "single", "avoid-escape"],
"max-line-length": [true, 120],
"indent": ["error", 4],
"only-arrow-functions": [false],
// Disabled rules
"typedef": [
false
]
"typedef": [false]
}
}
}
Loading