Skip to content

Commit

Permalink
Support flow types and proposals in transformer (fixes #129)
Browse files Browse the repository at this point in the history
  • Loading branch information
fkling committed Jul 18, 2016
1 parent e03140d commit 28eb515
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 30 deletions.
96 changes: 67 additions & 29 deletions bin/__tests__/jscodeshift-test.js
Expand Up @@ -89,40 +89,78 @@ describe('jscodeshift CLI', () => {
);
});

it('loads transform files with Babel if not disabled', () => {
var source = createTempFileWith('a');
var transform = createTransformWith(
'return (function() { "use strict"; const a = 42; }).toString();'
);
return Promise.all([
run(['-t', transform, source]).then(
describe('Babel', () => {

it('loads transform files with Babel if not disabled', () => {
var source = createTempFileWith('a');
var transform = createTransformWith(
'return (function() { "use strict"; const a = 42; }).toString();'
);
return Promise.all([
run(['-t', transform, source]).then(
() => {
expect(fs.readFileSync(source).toString())
.toMatch(/var\s*a\s*=\s*42/);
}
),
run(['-t', transform, '--no-babel', source]).then(
() => {
expect(fs.readFileSync(source).toString())
.toMatch(/const\s*a\s*=\s*42/);
}
),
]);
});

it('supports proposals in transform files', () => {
var source = createTempFileWith('a');
var transform = createTransformWith(
'return (function() {' +
' "use strict"; ' +
' const spread = {}; ' +
' ({...spread})' +
'}).toString();'
);
return Promise.all([
run(['-t', transform, source]).then(
() => {
expect(fs.readFileSync(source).toString())
.toMatch(/\(\{\},\s*spread\)/);
}
),
]);
});

it('supports flow type annotations in transform files', () => {
var source = createTempFileWith('a');
var transform = createTransformWith(
'return (function() { "use strict"; const a: number = 42; }).toString();'
);
return Promise.all([
run(['-t', transform, source]).then(
() => {
expect(fs.readFileSync(source).toString())
.toMatch(/var\s*a\s*=\s*42/);
}
),
]);
});

it('ignores .babelrc files in the directories of the source files', () => {
var transform = createTransformWith(
'return (function() { "use strict"; const a = 42; }).toString();'
);
var babelrc = createTempFileWith(`{"ignore": ["${transform}"]}`, '.babelrc');
var source = createTempFileWith('a', 'source.js');

return run(['-t', transform, source]).then(
() => {
expect(fs.readFileSync(source).toString())
.toMatch(/var\s*a\s*=\s*42/);
}
),
run(['-t', transform, '--no-babel', source]).then(
() => {
expect(fs.readFileSync(source).toString())
.toMatch(/const\s*a\s*=\s*42/);
}
),
]);
});

it('ignores .babelrc files in the directories of the source files', () => {
var transform = createTransformWith(
'return (function() { "use strict"; const a = 42; }).toString();'
);
var babelrc = createTempFileWith(`{"ignore": ["${transform}"]}`, '.babelrc');
var source = createTempFileWith('a', 'source.js');
);
});

return run(['-t', transform, source]).then(
() => {
expect(fs.readFileSync(source).toString())
.toMatch(/var\s*a\s*=\s*42/);
}
);
});

it('passes jscodeshift and stats the transform function', () => {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -28,7 +28,9 @@
"dependencies": {
"async": "^1.5.0",
"babel-core": "^5",
"babel-plugin-transform-flow-strip-types": "^6.8.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-1": "^6.5.0",
"babel-register": "^6.9.0",
"babylon": "^6.8.1",
"colors": "^1.1.2",
Expand Down
8 changes: 7 additions & 1 deletion src/Worker.js
Expand Up @@ -54,7 +54,13 @@ function setup(tr, babel) {
if (babel === 'babel') {
require('babel-register')({
babelrc: false,
presets: [require('babel-preset-es2015')],
presets: [
require('babel-preset-es2015'),
require('babel-preset-stage-1'),
],
plugins: [
require('babel-plugin-transform-flow-strip-types'),
]
});
}
const module = require(tr);
Expand Down

0 comments on commit 28eb515

Please sign in to comment.