-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚀 codemod created for adding try catch, test suite added
- Loading branch information
0 parents
commit 5b95c25
Showing
16 changed files
with
5,091 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-env"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# 2 space indentation | ||
[*.js] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
transforms/__testfixtures__/async-await-with-try-catch.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
transforms/__testfixtures__/async-await-with-try-catch.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
Oops, something went wrong.