Skip to content

Commit e573b65

Browse files
author
Robert Jackson
committed
Add typescript support to jscodeshift based transforms.
1 parent eaf519b commit e573b65

4 files changed

Lines changed: 38 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
}

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
@@ -213,17 +213,19 @@ QUnit.module('codemod-cli', function(hooks) {
213213
wrap(function*(assert) {
214214
userProject.write({
215215
foo: {
216-
'something.js': 'let blah = bar',
217-
'other.js': 'let blah = bar',
216+
'something.js': 'let blah = bar;',
217+
'other.js': 'let blah = bar;',
218+
'otherthing.ts': 'let blah: Map = bar;',
218219
},
219220
});
220221

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

223224
assert.deepEqual(userProject.read(), {
224225
foo: {
225-
'something.js': 'let halb = rab',
226-
'other.js': 'let blah = bar',
226+
'something.js': 'let halb = rab;',
227+
'other.js': 'let blah = bar;',
228+
'otherthing.ts': 'let halb: paM = rab;',
227229
},
228230
});
229231
})

0 commit comments

Comments
 (0)