diff --git a/.eslintignore b/.eslintignore index 85490b0ad4..862afe0fb9 100644 --- a/.eslintignore +++ b/.eslintignore @@ -19,4 +19,8 @@ docs/js/ examples/ # Ignore built files. build/ - +# react-codemod +npm-react-codemod/test/ +npm-react-codemod/scripts/ +npm-react-codemod/build/ +npm-react-codemod/node_modules/ diff --git a/npm-react-codemod/.npmignore b/npm-react-codemod/.npmignore new file mode 100644 index 0000000000..651b253185 --- /dev/null +++ b/npm-react-codemod/.npmignore @@ -0,0 +1 @@ +/transforms/ diff --git a/npm-react-codemod/README.md b/npm-react-codemod/README.md new file mode 100644 index 0000000000..5ebb3c21cc --- /dev/null +++ b/npm-react-codemod/README.md @@ -0,0 +1,19 @@ +## react-codemod + +This repository contains a collection of codemod scripts based on +[JSCodeshift](https://github.com/facebook/jscodeshift) that help update React +APIs. + +### Setup & Run + + * `npm install -g react-codemod` + * `react-codemod ` + * Use the `-d` option for a dry-run and use `-p` to print the output + for comparison + +### Recast Options + +Options to [recast](https://github.com/benjamn/recast)'s printer can be provided +through the `printOptions` command line argument + + * `react-codemod class --printOptions='{"quote":"double"}'` diff --git a/npm-react-codemod/package.json b/npm-react-codemod/package.json new file mode 100644 index 0000000000..46b5ceb2be --- /dev/null +++ b/npm-react-codemod/package.json @@ -0,0 +1,32 @@ +{ + "name": "react-codemod", + "version": "1.0.0", + "description": "React codemod scripts", + "license": "BSD-3-Clause", + "repository": { + "type": "git", + "url": "https://github.com/facebook/react" + }, + "scripts": { + "build": "rm -rf build; babel transforms/ --out-dir=build/", + "test": "jest", + "prepublish": "npm run build" + }, + "bin": { + "react-codemod": "./react-codemod" + }, + "dependencies": { + "jscodeshift": "^0.1.0" + }, + "devDependencies": { + "babel": "^4.7.16", + "babel-jest": "^4.0.0", + "jest-cli": "^0.4.0" + }, + "jest": { + "scriptPreprocessor": "./node_modules/babel-jest", + "testPathDirs": [ + "test" + ] + } +} diff --git a/npm-react-codemod/react-codemod b/npm-react-codemod/react-codemod new file mode 100755 index 0000000000..236cca8d32 --- /dev/null +++ b/npm-react-codemod/react-codemod @@ -0,0 +1,7 @@ +#!/bin/bash + +DIR=$(npm root -g)/react-codemod +TRANSFORM=$1 +shift + +$DIR/node_modules/.bin/jscodeshift -t $DIR/build/$TRANSFORM.js $@ diff --git a/npm-react-codemod/test/__tests__/transform-tests.js b/npm-react-codemod/test/__tests__/transform-tests.js new file mode 100644 index 0000000000..7d73c7f060 --- /dev/null +++ b/npm-react-codemod/test/__tests__/transform-tests.js @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +"use strict"; + +jest.autoMockOff(); + +var fs = require('fs'); +var jscodeshift = require('jscodeshift'); + +function read(fileName) { + return fs.readFileSync(__dirname + '/../' + fileName, 'utf8'); +} + +function test(transformName, testFileName, options) { + var path = testFileName + '.js'; + var source = read(testFileName + '.js'); + var output = read(testFileName + '.output.js'); + + var transform = require('../../transforms/' + transformName); + expect( + (transform({path, source}, {jscodeshift}, options || {}) || '').trim() + ).toEqual( + output.trim() + ); +} + +describe('Transform Tests', () => { + + + +});