Skip to content

Commit 6f029ec

Browse files
authored
Merge pull request #27 from rwjblue/typescript-support
Add typescript support to the default generated codemod.
2 parents 0f25cf5 + e573b65 commit 6f029ec

6 files changed

Lines changed: 44 additions & 9 deletions

File tree

commands/local/generate/codemod.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@ module.exports.handler = function handler(options) {
2020
fs.outputFileSync(
2121
`${codemodDir}/index.js`,
2222
stripIndent`
23+
const { getParser } = require('codemod-cli').jscodeshift;
24+
2325
module.exports = function transformer(file, api) {
24-
const j = api.jscodeshift;
26+
const j = getParser(api);
2527
2628
return j(file.source)
2729
.find(j.Identifier)
2830
.forEach(path => {
29-
j(path).replaceWith(
30-
j.identifier(path.node.name.split('').reverse().join(''))
31-
);
31+
path.node.name = path.node.name
32+
.split('')
33+
.reverse()
34+
.join('');
3235
})
3336
.toSource();
3437
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"test": "qunit tests/**/*-test.js"
1818
},
1919
"dependencies": {
20+
"@babel/parser": "^7.0.0-beta.51",
2021
"chalk": "^2.4.1",
2122
"common-tags": "^1.8.0",
2223
"execa": "^0.10.0",
@@ -26,6 +27,7 @@
2627
"import-local": "^1.0.0",
2728
"jscodeshift": "^0.5.1",
2829
"pkg-up": "^2.0.0",
30+
"recast": "^0.15.0",
2931
"yargs": "^11.0.0"
3032
},
3133
"devDependencies": {

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
const TestSupport = require('./test-support');
44
const BinSupport = require('./bin-support');
5+
const TransformSupport = require('./transform-support');
56

67
module.exports = {
78
runTransformTest: TestSupport.runTransformTest,
89
runTransform: BinSupport.runTransform,
10+
jscodeshift: TransformSupport.jscodeshift,
911
};

src/transform-support.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
function getJSCodeshiftParser(api) {
4+
try {
5+
let parser = require('recast/parsers/typescript');
6+
7+
return api.jscodeshift.withParser(parser);
8+
} catch (e) {
9+
// eslint-disable-next-line
10+
console.log(
11+
'could not load typescript aware parser, falling back to standard recast parser...'
12+
);
13+
14+
return api.jscodeshift;
15+
}
16+
}
17+
18+
module.exports = {
19+
jscodeshift: {
20+
getParser: getJSCodeshiftParser,
21+
},
22+
};

tests/cli-test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,19 @@ QUnit.module('codemod-cli', function(hooks) {
256256
wrap(function*(assert) {
257257
userProject.write({
258258
foo: {
259-
'something.js': 'let blah = bar',
260-
'other.js': 'let blah = bar',
259+
'something.js': 'let blah = bar;',
260+
'other.js': 'let blah = bar;',
261+
'otherthing.ts': 'let blah: Map = bar;',
261262
},
262263
});
263264

264-
yield CodemodCLI.runTransform(codemodProject.path('bin'), 'main', 'foo/*thing.js');
265+
yield CodemodCLI.runTransform(codemodProject.path('bin'), 'main', 'foo/*thing.[jt]s');
265266

266267
assert.deepEqual(userProject.read(), {
267268
foo: {
268-
'something.js': 'let halb = rab',
269-
'other.js': 'let blah = bar',
269+
'something.js': 'let halb = rab;',
270+
'other.js': 'let blah = bar;',
271+
'otherthing.ts': 'let halb: paM = rab;',
270272
},
271273
});
272274
})

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
esutils "^2.0.2"
1717
js-tokens "^3.0.0"
1818

19+
"@babel/parser@^7.0.0-beta.51":
20+
version "7.0.0-beta.51"
21+
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.0.0-beta.51.tgz#27cec2df409df60af58270ed8f6aa55409ea86f6"
22+
1923
"@mrmlnc/readdir-enhanced@^2.2.1":
2024
version "2.2.1"
2125
resolved "https://registry.yarnpkg.com/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz#524af240d1a360527b730475ecfa1344aa540dde"

0 commit comments

Comments
 (0)