From 0175b1c74fb25db82a107afe7ae987ded290a828 Mon Sep 17 00:00:00 2001 From: Rajasegar Chandran Date: Tue, 8 Jun 2021 16:35:14 +0530 Subject: [PATCH] chore(ci): add github actions 1. fix tests with new api --- .github/workflows/ci.yml | 67 +++++++++++++++ .gitignore | 1 + README.md | 85 +++++++++++++++++-- collections/CallExpression.js | 2 +- collections/__tests__/CallExpression-test.js | 4 +- .../__tests__/FunctionDeclaration-test.js | 2 +- .../__tests__/ImportDeclaration-test.js | 4 +- package.json | 6 +- 8 files changed, 153 insertions(+), 18 deletions(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..83cd517e --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,67 @@ +name: CI + +on: + push: + branches: + - master + - main + - 'v*' # older version branches + tags: + - '*' + pull_request: {} + schedule: + - cron: '0 6 * * 0' # weekly, on sundays + +jobs: + lint: + name: Linting + runs-on: ubuntu-latest + if: "!contains(github.event.head_commit.message, 'skip ci')" + + steps: + - uses: actions/checkout@v1 + - uses: actions/setup-node@v1 + with: + node-version: 12.x + - name: install dependencies + run: yarn install --frozen-lockfile + - name: linting + run: yarn lint + + test: + name: Tests + runs-on: ubuntu-latest + if: "!contains(github.event.head_commit.message, 'skip ci')" + + strategy: + matrix: + node: ['12', '14', '16'] + + steps: + - uses: actions/checkout@v1 + - uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node }} + - name: install dependencies + run: yarn install --frozen-lockfile + - name: test + run: yarn test --coverage + - name: Coveralls + uses: coverallsapp/github-action@master + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + + floating-test: + name: Floating dependencies + runs-on: ubuntu-latest + if: "!contains(github.event.head_commit.message, 'skip ci')" + + steps: + - uses: actions/checkout@v1 + - uses: actions/setup-node@v1 + with: + node-version: '12.x' + - name: install dependencies + run: yarn install --no-lockfile + - name: test + run: yarn test diff --git a/.gitignore b/.gitignore index 8d3b3d75..1d45f6fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules pnpm-debug.log +coverage/ diff --git a/README.md b/README.md index 4cf3e597..b3fbca8c 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ npm install jscodeshift-collections ``` ## Usage -``` +```js const jscsCollections = require('jscodeshift-collections'); module.exports = function(fileInfo, api) { @@ -27,6 +27,12 @@ module.exports = function(fileInfo, api) { Say for example, if you want to rename a function declaration `foo` to `bar` +### Transform with new Collection API +```js +j.findFunctionDeclarations('foo') + .renameTo('bar') +``` + ### Before ```js @@ -34,11 +40,6 @@ function foo() { } ``` -### Codemod with new Collection API -```js -j.findFunctionDeclarations('foo') - .renameTo('bar') -``` ### After @@ -47,6 +48,72 @@ function bar() { } ``` -## List of Collection: -- FunctionDeclaration -- CallExpression +## List of Collections: + +### FunctionDeclaration + +```js +// Find all function declarations +j.findFunctionDeclarations() + +// Find all function declarations with name=foo and rename them to bar +j.findFunctionDeclarations('foo') + .renameTo('xyz'); + +// Find and remove params + j.findFunctionDeclarations('bar') + .removeParam('a'); + +// Find and add params + j.findFunctionDeclarations('bar') + .addParam('d'); +``` + +### CallExpression + +```js +// Find all call expressions + j.findCallExpressions(); + +// Find all member expressions + j.findCallExpressions('foo.bar'); + + +// Find all nested member expressions + j.findCallExpressions('foo.bar.baz'); + +// Find and rename call expressions + j.findCallExpressions('foo') + .renameTo('xyz'); + +// Find and rename member expressions + j.findCallExpressions('foo') + .renameTo('foo.bar'); + +// Find and remove params + j.findCallExpressions('bar') + .removeParam('a'); + +// Find and add params + j.findCallExpressions('bar') + .addParam('d'); +``` + +### ImportDeclaration + +```js +// Find all import declarations + j.findImportDeclarations(); + +// Find and rename + j.findImportDeclarations('a') + .renameTo('xyz'); + +// Find and remove specifiers + j.findImportDeclarations('a') + .removeSpecifier('a'); + +// Find and add specifiers + j.findImportDeclarations('a') + .addSpecifier('c'); +``` diff --git a/collections/CallExpression.js b/collections/CallExpression.js index 8acf2af4..138db44a 100644 --- a/collections/CallExpression.js +++ b/collections/CallExpression.js @@ -76,7 +76,7 @@ const transformMethods = { removeParam(param) { return this.forEach((path) => { const newParams = path.value.arguments.filter((p) => p.name !== param); - path.value.arguments = newParams; + path.value.arguments = newParams; // eslint-disable-line }); }, }; diff --git a/collections/__tests__/CallExpression-test.js b/collections/__tests__/CallExpression-test.js index bfd969ee..ee164f5f 100644 --- a/collections/__tests__/CallExpression-test.js +++ b/collections/__tests__/CallExpression-test.js @@ -1,11 +1,9 @@ const Collection = require('jscodeshift/src/Collection'); const recast = require('recast'); -const types = recast.types.namedTypes; - const CallExpressionCollection = require('../CallExpression'); -CallExpressionCollection.register(); +CallExpressionCollection.register(Collection); test('find all call expressions', () => { const nodes = [recast.parse( diff --git a/collections/__tests__/FunctionDeclaration-test.js b/collections/__tests__/FunctionDeclaration-test.js index d4cb18d7..479aac2e 100644 --- a/collections/__tests__/FunctionDeclaration-test.js +++ b/collections/__tests__/FunctionDeclaration-test.js @@ -6,7 +6,7 @@ const types = recast.types.namedTypes; const FunctionDeclarationCollection = require('../FunctionDeclaration'); -FunctionDeclarationCollection.register(); +FunctionDeclarationCollection.register(Collection); test('find all function declarations', () => { const nodes = [recast.parse( diff --git a/collections/__tests__/ImportDeclaration-test.js b/collections/__tests__/ImportDeclaration-test.js index b4b42d4b..4e78aab3 100644 --- a/collections/__tests__/ImportDeclaration-test.js +++ b/collections/__tests__/ImportDeclaration-test.js @@ -3,9 +3,9 @@ const recast = require('recast'); const ImportDeclarationCollection = require('../ImportDeclaration'); -ImportDeclarationCollection.register(); +ImportDeclarationCollection.register(Collection); -test('find all function declarations', () => { +test('find all import declarations', () => { const nodes = [recast.parse( 'function foo() {}', 'import a from "a"', diff --git a/package.json b/package.json index ac3f9655..9ebee8c0 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,10 @@ "description": "", "main": "index.js", "scripts": { - "test": "jest", - "release": "release-it" + "test": "jest --coverage", + "release": "release-it", + "deploy": "git push && git push --tags && npm publish", + "lint": "eslint ." }, "keywords": [], "author": "Rajasegar Chandran",