This repository contains jscodeshift
tranformation scripts used by DataFox.
Tranformation scripts in the decaffeinate
directory are scripts meant to be run on decaffeinate
tranpiled CoffeeScript -> ES6 code.
These scripts are mainly meant to fix style rather than correctness.
The style issues are mainly those imposed by the Airbnb JavaScript style guide that were unable to be fixed by eslint --fix
in the bulk-decaffeinate
process.
These scripts are operating on files run with decaffeinate's --keep-commonjs
and --prefer-const
flags on.
module.exports = class Foo
let Foo;
module.exports = Foo = class Foo {};
module.exports = class Foo {};
module.exports = class Foo
new Foo()
let Foo;
module.exports = Foo = class Foo {};
module.exports = class Foo {};
class Foo
setBar: (bar) ->
this.bar = 42
class Foo {
setBar(bar) {
return this.bar = 42;
}
}
class Foo {
setBar(bar) {
this.bar = 42;
}
}
bar = foo() ? ''
let left;
const bar = (left = foo()) != null ? left : '';
const bar = foo() || '';
Note: This will skip over any for..of
statements that contain a break
or return
since it cannot easily be converted into a forEach
.
for x in [1, 2, 3, 4, 5]
continue if x % 2 == 0
console.log(x)
for (let x of [1, 2, 3, 4, 5]) {
if ((x % 2) === 0) { continue; }
console.log(x);
}
[1, 2, 3, 4, 5].forEach((x) => {
if ((x % 2) === 0) {
return;
}
console.log(x);
});
describe('SearchListService', () =>
# declare queryBuilderObj
### coffeelint-variable-scope-ignore ###
queryBuilderObj = null
# ...
)
describe('SearchListService', () => {
// declare queryBuilderObj
/* coffeelint-variable-scope-ignore */
let queryBuilderObj;
return queryBuilderObj = null;
// ...
});
describe('SearchListService', () => {
// declare queryBuilderObj
let queryBuilderObj;
return queryBuilderObj = null;
// ...
});
Works with common Array.prototype
, underscore _
, async
, and StreamWorker
functions.
Does not work with tasks in async
control flow functions.
NOTE: This assumes that you have unbound all your bound methods prior to running this transform.
You can do so by running find . -name "*.coffee" | xargs perl -pi -e 's/^( \w+:.*)=>$/\1->/g'
on all your CoffeeScript source files.
class MultiplierService
constructor: ->
this.multiplier = 42
# Originally a bound method:
# multiply: (x) =>
multiply: (x) ->
return x * @multiplier
multiplyAll: (xs) ->
return xs.map(@multiply)
xs = [1, 2, 3]
ms = new MultiplierService()
# Direct usage
xs.map(ms.multiply)
# Assigning to a variable prior to usage
doWork = ms.multiply
xs.map(multiply)
multiply(1)
class MultiplierService {
constructor() {
this.multiplier = 42;
}
// Originally a bound method:
// multiply: (x) =>
multiply(x) {
return x * this.multiplier;
}
multiplyAll(xs) {
return xs.map(this.multiply);
}
}
const xs = [1, 2, 3];
const ms = new MultiplierService();
// Direct usage
xs.map(ms.multiply);
// Assigning to a variable prior to usage
const doWork = ms.multiply;
xs.map(doWork);
doWork(1);
class MultiplierService {
constructor() {
this.multiplier = 42;
}
// Originally a bound method:
// multiply: (x) =>
multiply(x) {
return x * this.multiplier;
}
multiplyAll(xs) {
return xs.map(this.multiply.bind(this));
}
}
const xs = [1, 2, 3];
const ms = new MultiplierService();
// Direct usage
xs.map(ms.multiply.bind(ms));
// Assigning to a variable prior to usage
const doWork = ms.multiply.bind(ms);
xs.map(doWork);
doWork(1);
Tranformation scripts in the ember-styleguide
directory are scripts meant to be run on ES6 ember-cli code.
These scripts are mainly meant to fix style rather than correctness.
The style issues are mainly those imposed by the NetGuru Ember Style Guide.
import Ember from 'ember';
export default Ember.Component.extend({
foo: Ember.computed(function() {}),
});
import Ember from 'ember';
const {Component, computed} = Ember;
export default Component.extend({
foo: computed(function() {}),
});
import Ember from 'ember';
export default Ember.Component.extend({
foo: Ember.computed('bar', function() {
return this.get('bar');
}),
});
import Ember from 'ember';
const {get} = Ember;
export default Ember.Component.extend({
foo: Ember.computed('bar', function() {
return get(this, 'bar');
}),
});
These are some miscellaneous transforms.
This should be run if you have classes that are assigned to some identifier and then the identifier is used in the file to construct the class.
module.exports = (class FooScript { });
if (!module.parent) {
const script = new module.exports();
}
class FooScript { };
module.exports = FooScript;
if (!module.parent) {
const script = new FooScript();
}
Adds a top-level 'use strict' statement to JavaScript files.
If you are doing this with bulk-decaffeinate, you should run eslint --fix
afterwards since bulk-decaffeinate puts a comment directly before the 'use strict';
directive.
Borrowed from cpojer/jscodemod;