Skip to content

Commit

Permalink
🚀 codemod created for adding try catch, test suite added
Browse files Browse the repository at this point in the history
  • Loading branch information
vivek12345 committed Jan 31, 2019
0 parents commit 5b95c25
Show file tree
Hide file tree
Showing 16 changed files with 5,091 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 2 space indentation
[*.js]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
27 changes: 27 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
parser: "babel-eslint",
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {
jsx: true,
arrowFunctions: true,
module: true,
experimentalObjectRestSpread: true
}
},
env: {
browser: true,
node: true,
jest: true,
es6: true
},
extends: ["eslint:recommended"],
rules: {
indent: ["error", 2, { SwitchCase: 1 }],
"linebreak-style": ["error", "unix"],
quotes: ["error", "single"],
semi: ["error", "always"],
"no-console": ["off"]
}
};
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
env/properties.env
test-report.xml
*.swp
*.idea
11 changes: 11 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"parser": "babylon",
"printWidth": 120,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": true,
"semi": true,
"useTabs": false,
"jsxBracketSameLine": false
}
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- "node"
- "v8.10.0"
cache: yarn
branches:
only:
- master
script:
- yarn test
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"editor.fontSize": 12,
"editor.formatOnPaste": true,
"editor.formatOnSave": false,
"prettier.eslintIntegration": false
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Vivek Nayyar

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
## async-await-codemod [![Build Status](https://travis-ci.org/vivek12345/async-await-codemod.svg)](https://travis-ci.org/vivek12345/async-await-codemod)

This repository contains a codemod script for use with
[JSCodeshift](https://github.com/facebook/jscodeshift) that help add try catch statements to asyc await calls.

### Setup & Run

1. `yarn global add jscodeshift`
1. `git clone https://github.com/vivek12345/async-await-codemod.git`
1. Run `yarn install` in the async-await-codemod directory
1. `jscodeshift -t <codemod-script> <path>`
* `codemod-script` - path to the transform file, see available scripts below;
* `path` - files or directory to transform;
* use the `-d` option for a dry-run and use `-p` to print the output for comparison;
* use the `--extensions` option if your files have different extensions than `.js` (for example, `--extensions js,jsx`);
* see all available [jscodeshift options](https://github.com/facebook/jscodeshift#usage-cli).

### Included Script

#### `async-await-with-try-catch`

Converts async await functions like below:-

```javascript
async function completeApplicationFlow() {
// wait for get session status api to check the status
let response;
response = await getSessionStatusApi();
// wait for getting next set of questions api
response = await getNextQuestionsApi();
// finally submit application
response = await submitApplication();
}

```

To the following with try catch statements:-

```javascript
async function completeApplicationFlow() {
// wait for get session status api to check the status
let response;
try {
response = await getSessionStatusApi();
} catch(e) {
console.log(e);
}
// wait for getting next set of questions api
try {
response = await getNextQuestionsApi();
} catch(e) {
console.log(e);
}
// finally submit application
try {
response = await submitApplication();
} catch(e) {
console.log(e);
}
}

```


```sh
jscodeshift -t async-await-codemod/transforms/async-await-with-try-catch.js <path>
```

### Recast Options

Options to [recast](https://github.com/benjamn/recast)'s printer can be provided
through the `printOptions` command line argument

```sh
jscodeshift -t transform.js <path> --printOptions='{"quote":"double"}'
```

## 👍 Contribute

Show your ❤️ and support by giving a ⭐. Any suggestions and pull request are welcome !

### 📝 License

MIT © [viveknayyar](https://github.com/vivek12345)
69 changes: 69 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "async-await-codemod",
"version": "1.0.0",
"description": "codemod for adding try catch statements to async await code",
"main": "index.js",
"author": "Vivek Nayyar",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/vivek12345/async-await-codemod.git"
},
"scripts": {
"test": "jest",
"eslint:fix": "eslint --ignore-path .gitignore --fix"
},
"lint-staged": {
"src/**/*.{js}": [
"npm run eslint:fix",
"git add"
],
"src/**/*.{js,json,css}": [
"prettier --config .prettierrc --write",
"git add"
]
},
"jest": {
"collectCoverageFrom": [
"transforms/**/*.{js,jsx,mjs}"
],
"testMatch": [
"<rootDir>/transforms/**/__tests__/**/*.{js,jsx,mjs}",
"<rootDir>/transforms/**/?(*.)(spec|test).{js,jsx,mjs}"
],
"testEnvironment": "jsdom",
"testURL": "http://localhost",
"transform": {
"^.+\\.(js|jsx|mjs)$": "<rootDir>/node_modules/babel-jest"
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"
],
"moduleFileExtensions": [
"web.js",
"js",
"json",
"web.jsx",
"jsx",
"node",
"mjs"
]
},
"keywords": [
"codemod",
"recast"
],
"dependencies": {
"jscodeshift": "^0.6.3"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.3.1",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.0.0",
"eslint": "^5.12.1",
"jest": "^24.0.0",
"prettier": "^1.16.3"
}
}
28 changes: 28 additions & 0 deletions transforms/__testfixtures__/async-await-with-try-catch.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable */
async function getData() {
const b = a + await getSessionStatusApi();
}
async function getApiData() {
await getApiData();
}

async function getQuestionsData() {
const c = b + await new Api().getQuestionsData();
}

async function getNormalData() {
const c = await getData;
}

async function reduce(array, reducer, accumulator) {
for (let i = 0; i < array.length; i++) {
accumulator = await reducer(accumulator, array[i], i, array);
}
return accumulator;
}
async function testData() {
await every([2, 3], async v => (await fetch(v)).ok);
}
const a = async function(req, res) {
const tasks = await repository.get();
};
56 changes: 56 additions & 0 deletions transforms/__testfixtures__/async-await-with-try-catch.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* eslint-disable */
async function getData() {
try {
const b = a + await getSessionStatusApi();
} catch (e) {
console.log(e);
}
}
async function getApiData() {
try {
await getApiData();
} catch (e) {
console.log(e);
};
}

async function getQuestionsData() {
try {
const c = b + await new Api().getQuestionsData();
} catch (e) {
console.log(e);
}
}

async function getNormalData() {
try {
const c = await getData;
} catch (e) {
console.log(e);
}
}

async function reduce(array, reducer, accumulator) {
for (let i = 0; i < array.length; i++) {
try {
accumulator = await reducer(accumulator, array[i], i, array);
} catch (e) {
console.log(e);
};
}
return accumulator;
}
async function testData() {
try {
await every([2, 3], async v => (await fetch(v)).ok);
} catch (e) {
console.log(e);
};
}
const a = async function(req, res) {
try {
const tasks = await repository.get();
} catch (e) {
console.log(e);
}
};
3 changes: 3 additions & 0 deletions transforms/__tests__/async-await-with-try-catch-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
jest.autoMockOff();
const defineTest = require('jscodeshift/dist/testUtils').defineTest;
defineTest(__dirname, 'async-await-with-try-catch');
Loading

0 comments on commit 5b95c25

Please sign in to comment.