├── reverse-identifiers.input.ts ├── package.json ├── README.md ├── .vscode └── launch.json ├── reverse-identifiers.test.js ├── reverse-identifiers.js ├── LICENSE ├── .gitignore └── .travis.yml /reverse-identifiers.input.ts: -------------------------------------------------------------------------------- 1 | type NameOrNameArray = string | string[]; 2 | 3 | function createName(name: NameOrNameArray) { 4 | if (typeof name === "string") { 5 | return name; 6 | } 7 | else { 8 | return name.join(" "); 9 | } 10 | } 11 | 12 | var greetingMessage = `Greetings, ${ createName(["Sam", "Smith"]) }`; 13 | alert(greetingMessage); 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jscodeshift-transforms", 3 | "version": "0.0.0", 4 | "description": "A collection of jscodeshift transforms", 5 | "main": "index.js", 6 | "repository": "git@github.com:elliottsj/jscodeshift-transforms.git", 7 | "author": "Spencer Elliott ", 8 | "license": "MIT", 9 | "scripts": { 10 | "test": "jest" 11 | }, 12 | "devDependencies": { 13 | "jest": "^23.6.0", 14 | "jscodeshift": "^0.6.1" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jscodeshift-typescript-example 2 | Example of using jscodeshift with TypeScript. 3 | 4 | ### Usage 5 | First install: 6 | ```shell 7 | npm install 8 | ``` 9 | 10 | Try the [reverse-identifiers](reverse-identifiers.js) transform: 11 | ```shell 12 | ./node_modules/.bin/jscodeshift -t ./reverse-identifiers.js --extensions=ts --parser=ts ./reverse-identifiers.input.ts --print --dry 13 | ``` 14 | 15 | _Omit `--dry` to write the transformed source back to disk._ 16 | 17 | ### Test 18 | ```shell 19 | npm test 20 | ``` 21 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Jest Tests", 11 | "program": "${workspaceFolder}/node_modules/.bin/jest", 12 | "args": [], 13 | "internalConsoleOptions": "openOnSessionStart" 14 | } 15 | ] 16 | } -------------------------------------------------------------------------------- /reverse-identifiers.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const child_process = require('child_process'); 4 | const path = require('path'); 5 | const transform = require.resolve('./reverse-identifiers'); 6 | 7 | it('transforms correctly', () => { 8 | const result = child_process.spawnSync( 9 | path.join(__dirname, 'node_modules', '.bin', 'jscodeshift'), 10 | [ 11 | '--dry', 12 | '--print', 13 | '--run-in-band', 14 | '-t', transform, 15 | '--extensions=ts', 16 | '--parser=ts', 17 | path.join(__dirname, './reverse-identifiers.input.ts'), 18 | ], 19 | { 20 | encoding: 'utf8', 21 | } 22 | ); 23 | 24 | expect(result.stdout).toEqual(expect.stringContaining("type yarrAemaNrOemaN = string | string[];")); 25 | }); 26 | -------------------------------------------------------------------------------- /reverse-identifiers.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016-present, Facebook, Inc. 3 | * All rights reserved. 4 | * 5 | * This source code is licensed under the BSD-style license found in the 6 | * LICENSE file in the root directory of this source tree. An additional grant 7 | * of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | /** 11 | * Example jscodeshift transformer. Simply reverses the names of all 12 | * identifiers. Stolen from: 13 | * https://github.com/facebook/jscodeshift/blob/7be2557f369794e915afe7f91ab81b1215e66857/sample/reverse-identifiers.js 14 | */ 15 | function transform(file, api) { 16 | const j = api.jscodeshift; 17 | return j(file.source) 18 | .find(j.Identifier) 19 | .replaceWith( 20 | p => Object.assign({}, p.node, { 21 | name: p.node.name.split('').reverse().join('') 22 | }) 23 | ) 24 | .toSource(); 25 | } 26 | 27 | module.exports = transform; 28 | module.exports.parser = 'ts'; 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Spencer Elliott 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Use Trusty beta environment: https://docs.travis-ci.com/user/reference/trusty/ 2 | dist: trusty 3 | sudo: false 4 | 5 | language: node_js 6 | node_js: 7 | - '6' 8 | - '8' 9 | - 'stable' 10 | 11 | # Make Greenkeeper work with lockfiles: 12 | # https://github.com/greenkeeperio/greenkeeper-lockfile/tree/d675ee5ccd4b035bbe135e63b38e397af261373f#npm 13 | env: 14 | global: 15 | # GH_TOKEN 16 | - secure: vZhLVggarP4P8xRtoB3yvEJNo2R9NrE6U1e7wVpdhgUcUEIA7XW8bAUfc3pxrnmZTa1Hpl0/zrOOMtR0LYrgqolK6HkvL8fziA+LWIu+2G2Ub1g7IY55Do3E2eKCLXUz0kGSSFwAG4mGn6ycF2HN9UP9ipci7z8nSoboWvAl0i9lO1fDI4RWg1Zjdy0giAbGHSw/mxh3U91CaLSZC/c1Xx0UuZB/j4fvzinF+XLez/w9kobc6swoKEAmn6l3jF05vZlnNcU0ASwH3dFEExY9pX6FELVxkEHUOFXbueUplUWe4VBeNQ3DxI+X/BSBUXq1wMuwaXQAzl+EJ14Hgp9ZZhb3TGAoqYya2pH+ROVJuf9b7BSg13XfKyNonDAC9CsU1R9XVcyqJvD/vNgr32OiIy6jj2U3GAx19vGdxkI9GmSaRnNmabHGbboec7zlLAWSM7AAhIibE5Tvfiyswn3fdTA6gQnR/NTjFnCKscEJ2lbtTbM9mT2Kzlp6vc0pJB72zJtleOAWyJaYh+CUdYPsDkCfTdd9wiwfwmXn94ExrCLAy4kdLZn72ZbM4ZPuBCMv3AqpY+ge1ngMKNectgYktrIalV5o0xLGUCrTmYBIYsD1MHlAWKcuOVSNTc4aFMdVxeHVBbH3/BpFIGTH4eUDeni7oSGfJ1Vza4svXYhdz40= 17 | 18 | install: npm install 19 | before_install: 20 | # package-lock.json was introduced in npm@5 21 | - '[[ $(node -v) =~ ^v9.*$ ]] || npm install -g npm@latest' # skipped when using node 9 22 | - npm install -g greenkeeper-lockfile@1 23 | before_script: greenkeeper-lockfile-update 24 | after_script: greenkeeper-lockfile-upload 25 | --------------------------------------------------------------------------------