Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
5,672 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 |
---|---|---|
|
@@ -59,3 +59,9 @@ typings/ | |
|
||
# next.js build output | ||
.next | ||
|
||
|
||
# IntelliJ | ||
|
||
.idea/* | ||
*.iml |
Large diffs are not rendered by default.
Oops, something went wrong.
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,15 @@ | ||
{ | ||
"name": "sample-js-codemod", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"jscodeshift": "^0.6.3", | ||
"jest": "^24.1.0" | ||
} | ||
} |
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 @@ | ||
export default (a, b) => a + b; |
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 @@ | ||
export default function (a, b) { | ||
return a + b; | ||
} |
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 @@ | ||
jest.autoMockOff(); | ||
const defineTest = require('../../src/testUtils').defineTest; | ||
|
||
describe('arrow-to-function', () => { | ||
defineTest(__dirname, 'arrow-to-function', null, 'defaultExportedArrow'); | ||
}); |
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,15 @@ | ||
module.exports = function transform(file, api) { | ||
const j = api.jscodeshift; | ||
|
||
return j(file.source) | ||
.find(j.ArrowFunctionExpression) | ||
.replaceWith( | ||
p => { | ||
const nodeValue = p.value; | ||
return j.functionDeclaration(j.identifier(''), nodeValue.params, | ||
j.blockStatement([j.returnStatement(nodeValue.body)]) | ||
); | ||
} | ||
) | ||
.toSource(); | ||
}; |
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,103 @@ | ||
/** | ||
* Copyright (c) 2016-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. | ||
*/ | ||
|
||
/* global expect, describe, it */ | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
function runInlineTest(module, options, input, expectedOutput) { | ||
// Handle ES6 modules using default export for the transform | ||
const transform = module.default ? module.default : module; | ||
|
||
// Jest resets the module registry after each test, so we need to always get | ||
// a fresh copy of jscodeshift on every test run. | ||
let jscodeshift = require('jscodeshift'); | ||
if (module.parser) { | ||
jscodeshift = jscodeshift.withParser(module.parser); | ||
} | ||
|
||
const output = transform( | ||
input, | ||
{ | ||
jscodeshift, | ||
stats: () => {}, | ||
}, | ||
options || {} | ||
); | ||
expect((output || '').trim()).toEqual(expectedOutput.trim()); | ||
} | ||
exports.runInlineTest = runInlineTest; | ||
|
||
/** | ||
* Utility function to run a jscodeshift script within a unit test. This makes | ||
* several assumptions about the environment: | ||
* | ||
* - `dirName` contains the name of the directory the test is located in. This | ||
* should normally be passed via __dirname. | ||
* - The test should be located in a subdirectory next to the transform itself. | ||
* Commonly tests are located in a directory called __tests__. | ||
* - `transformName` contains the filename of the transform being tested, | ||
* excluding the .js extension. | ||
* - `testFilePrefix` optionally contains the name of the file with the test | ||
* data. If not specified, it defaults to the same value as `transformName`. | ||
* This will be suffixed with ".input.js" for the input file and ".output.js" | ||
* for the expected output. For example, if set to "foo", we will read the | ||
* "foo.input.js" file, pass this to the transform, and expect its output to | ||
* be equal to the contents of "foo.output.js". | ||
* - Test data should be located in a directory called __testfixtures__ | ||
* alongside the transform and __tests__ directory. | ||
*/ | ||
function runTest(dirName, transformName, options, testFilePrefix) { | ||
if (!testFilePrefix) { | ||
testFilePrefix = transformName; | ||
} | ||
|
||
const fixtureDir = path.join(dirName, '..', '__testfixtures__'); | ||
const inputPath = path.join(fixtureDir, testFilePrefix + '.input.js'); | ||
const source = fs.readFileSync(inputPath, 'utf8'); | ||
const expectedOutput = fs.readFileSync( | ||
path.join(fixtureDir, testFilePrefix + '.output.js'), | ||
'utf8' | ||
); | ||
// Assumes transform is one level up from __tests__ directory | ||
const module = require(path.join(dirName, '..', transformName + '.js')); | ||
runInlineTest(module, options, { | ||
path: inputPath, | ||
source | ||
}, expectedOutput); | ||
} | ||
exports.runTest = runTest; | ||
|
||
/** | ||
* Handles some boilerplate around defining a simple jest/Jasmine test for a | ||
* jscodeshift transform. | ||
*/ | ||
function defineTest(dirName, transformName, options, testFilePrefix) { | ||
const testName = testFilePrefix | ||
? `transforms correctly using "${testFilePrefix}" data` | ||
: 'transforms correctly'; | ||
describe(transformName, () => { | ||
it(testName, () => { | ||
runTest(dirName, transformName, options, testFilePrefix); | ||
}); | ||
}); | ||
} | ||
exports.defineTest = defineTest; | ||
|
||
function defineInlineTest(module, options, input, expectedOutput, testName) { | ||
it(testName || 'transforms correctly', () => { | ||
runInlineTest(module, options, { | ||
source: input | ||
}, expectedOutput); | ||
}); | ||
} | ||
exports.defineInlineTest = defineInlineTest; |