Skip to content

Commit

Permalink
Merge pull request #163 from robz/leetspeak-codemod-demo
Browse files Browse the repository at this point in the history
leetspeak demo let and const transform
  • Loading branch information
fkling authored Oct 25, 2016
2 parents ff26114 + 8f194b1 commit e4fdb02
Show file tree
Hide file tree
Showing 16 changed files with 272 additions and 272 deletions.
44 changes: 22 additions & 22 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

'use strict';

var assert = require('assert');
var recast = require('recast');
var _ = require('lodash');
const assert = require('assert');
const recast = require('recast');
const _ = require('lodash');

var astTypes = recast.types;
const astTypes = recast.types;
var types = astTypes.namedTypes;
var NodePath = astTypes.NodePath;
var Node = types.Node;
const NodePath = astTypes.NodePath;
const Node = types.Node;

/**
* This represents a generic collection of node paths. It only has a generic
Expand Down Expand Up @@ -92,15 +92,15 @@ class Collection {
* @param {Type} type Force the new collection to be of a specific type
*/
map(callback, type) {
var paths = [];
const paths = [];
this.forEach(function(path) {
/*jshint eqnull:true*/
var result = callback.apply(path, arguments);
let result = callback.apply(path, arguments);
if (result == null) return;
if (!Array.isArray(result)) {
result = [result];
}
for (var i = 0; i < result.length; i++) {
for (let i = 0; i < result.length; i++) {
if (paths.indexOf(result[i]) === -1) {
paths.push(result[i]);
}
Expand Down Expand Up @@ -185,7 +185,7 @@ class Collection {
* @param {string|number} ...fields
*/
get() {
var path = this.__paths[0];
const path = this.__paths[0];
if (!path) {
throw Error(
'You cannot call "get" on a collection with no paths. ' +
Expand Down Expand Up @@ -223,11 +223,11 @@ class Collection {
* @return {Type} type An AST type
*/
function _inferTypes(paths) {
var _types = [];
let _types = [];

if (paths.length > 0 && Node.check(paths[0].node)) {
var nodeType = types[paths[0].node.type];
var sameType = paths.length === 1 ||
const nodeType = types[paths[0].node.type];
const sameType = paths.length === 1 ||
paths.every(path => nodeType.check(path.node));

if (sameType) {
Expand Down Expand Up @@ -308,7 +308,7 @@ function fromNodes(nodes, parent, type) {
);
}

var CPt = Collection.prototype;
const CPt = Collection.prototype;

/**
* This function adds the provided methods to the prototype of the corresponding
Expand All @@ -319,12 +319,12 @@ var CPt = Collection.prototype;
* @param {Type=} type Optional type to add the methods to
*/
function registerMethods(methods, type) {
for (var methodName in methods) {
for (const methodName in methods) {
if (!methods.hasOwnProperty(methodName)) {
return;
}
if (hasConflictingRegistration(methodName, type)) {
var msg = `There is a conflicting registration for method with name "${methodName}".\nYou tried to register an additional method with `;
let msg = `There is a conflicting registration for method with name "${methodName}".\nYou tried to register an additional method with `;

if (type) {
msg += `type "${type.toString()}".`
Expand All @@ -334,7 +334,7 @@ function registerMethods(methods, type) {

msg += '\nThere are existing registrations for that method with ';

var conflictingRegistrations = CPt[methodName].typedRegistrations;
const conflictingRegistrations = CPt[methodName].typedRegistrations;

if (conflictingRegistrations) {
msg += `type ${Object.keys(conflictingRegistrations).join(', ')}.`;
Expand Down Expand Up @@ -365,13 +365,13 @@ function installTypedMethod(methodName) {
throw new Error(`Internal Error: "${methodName}" method is already installed`);
}

var registrations = {};
const registrations = {};

function typedMethod() {
var types = Object.keys(registrations);
const types = Object.keys(registrations);

for (var i = 0; i < types.length; i++) {
var currentType = types[i];
for (let i = 0; i < types.length; i++) {
const currentType = types[i];
if (registrations[currentType] && this.isOfType(currentType)) {
return registrations[currentType].apply(this, arguments);
}
Expand All @@ -397,7 +397,7 @@ function hasConflictingRegistration(methodName, type) {
return false;
}

var registrations = CPt[methodName] && CPt[methodName].typedRegistrations;
const registrations = CPt[methodName] && CPt[methodName].typedRegistrations;

if (!registrations) {
return true;
Expand Down
12 changes: 6 additions & 6 deletions src/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ function trimStackTrace(trace) {
return '';
}
// Remove this file from the stack trace of an error thrown in the transformer
var lines = trace.split('\n');
var result = [];
const lines = trace.split('\n');
const result = [];
lines.every(function(line) {
if (line.indexOf(__filename) === -1) {
result.push(line);
Expand All @@ -108,8 +108,8 @@ function trimStackTrace(trace) {
}

function run(data) {
var files = data.files;
var options = data.options || {};
const files = data.files;
const options = data.options || {};
if (!files.length) {
finish();
return;
Expand All @@ -125,8 +125,8 @@ function run(data) {
}
source = source.toString();
try {
var jscodeshift = prepareJscodeshift(options);
var out = transform(
const jscodeshift = prepareJscodeshift(options);
const out = transform(
{
path: file,
source: source,
Expand Down
78 changes: 39 additions & 39 deletions src/__tests__/Collection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@


describe('Collection API', function() {
var nodes;
var Collection;
var recast;
var NodePath;
var types;
var b;
let nodes;
let Collection;
let recast;
let NodePath;
let types;
let b;

beforeEach(function() {
jest.resetModuleRegistry();
Expand All @@ -39,21 +39,21 @@ describe('Collection API', function() {
});

it('should create a collection from an array of paths', function() {
var paths = [
const paths = [
new NodePath(b.identifier('foo')),
new NodePath(b.identifier('bar')),
];
expect(Collection.fromPaths(paths).getTypes()).toContain('Identifier');
});

it('accepts an empty array as input', function() {
var values = [];
const values = [];
expect(() => Collection.fromPaths(values)).not.toThrow();
expect(() => Collection.fromNodes(values)).not.toThrow();
});

it('throws if it is passed an array of mixed values', function() {
var values = [
const values = [
new NodePath(b.identifier('foo')),
b.identifier('bar'),
];
Expand All @@ -62,7 +62,7 @@ describe('Collection API', function() {
});

it('returns a collection of the closest common type', function() {
var nodes = [
let nodes = [
b.identifier('foo'),
b.sequenceExpression([]),
];
Expand All @@ -80,34 +80,34 @@ describe('Collection API', function() {

describe('Method extensions', function() {
it('handles method extensions for types', function() {
var Collection = require('../Collection');
var getNames = jest.genMockFunction().mockImpl(function() {
const Collection = require('../Collection');
const getNames = jest.genMockFunction().mockImpl(function() {
expect(this.nodes()).toEqual(nodes);
});
Collection.registerMethods({getNames: getNames}, types.Identifier);

var collection = Collection.fromNodes(nodes);
const collection = Collection.fromNodes(nodes);

expect(collection.getNames).toBeDefined();
collection.getNames();
expect(getNames).toBeCalled();
});

it('throws if a method is called for the wrong node type', function() {
var Collection = require('../Collection');
var getNames = jest.genMockFunction();
const Collection = require('../Collection');
const getNames = jest.genMockFunction();
Collection.registerMethods({getNames: getNames}, types.Identifier);

var collection = Collection.fromNodes([
const collection = Collection.fromNodes([
b.blockStatement([])
]);

expect(() => collection.getNames()).toThrow();
});

it('ads "global" methods to all types', function() {
var Collection = require('../Collection');
var getNames = jest.genMockFunction();
const Collection = require('../Collection');
const getNames = jest.genMockFunction();
Collection.registerMethods({getNames: getNames});

expect(Collection.fromNodes([b.blockStatement([])])).toBeDefined();
Expand All @@ -116,16 +116,16 @@ describe('Collection API', function() {
});

it('handles type inheritance chains', function() {
var Collection = require('../Collection');
var nodeMethod = function() {};
var identifierMethod = function() {};
const Collection = require('../Collection');
const nodeMethod = function() {};
const identifierMethod = function() {};
Collection.registerMethods({nodeMethod: nodeMethod}, types.Node);
Collection.registerMethods(
{identifierMethod: identifierMethod},
types.Identifier
);

var collection = Collection.fromNodes([b.identifier('foo')]);
const collection = Collection.fromNodes([b.identifier('foo')]);

expect(() => collection.identifierMethod()).not.toThrow();
expect(() => collection.nodeMethod()).not.toThrow();
Expand All @@ -136,7 +136,7 @@ describe('Collection API', function() {
{expressionMethod: function() {}},
types.Expression
);
var collection = Collection.fromNodes([
const collection = Collection.fromNodes([
b.functionExpression(null, [], b.blockStatement([]))
]);
expect(() => collection.expressionMethod()).not.toThrow();
Expand All @@ -153,7 +153,7 @@ describe('Collection API', function() {
types.BinaryExpression
);

var collection = Collection.fromNodes([
const collection = Collection.fromNodes([
b.functionExpression(null, [], b.blockStatement([])),
b.functionExpression(null, [], b.blockStatement([])),
b.binaryExpression('+', b.identifier('a'), b.identifier('b'))
Expand All @@ -177,7 +177,7 @@ describe('Collection API', function() {

describe('hasConflictingRegistration', function () {
function register(methodName, type) {
var methods = {};
const methods = {};
methods[methodName] = function () {};
if (!types[type]) {
throw new Error(type + ' is not a valid type');
Expand Down Expand Up @@ -205,10 +205,10 @@ describe('Collection API', function() {
describe('Processing functions', function() {
describe('filter', function() {
it('lets you filter with custom logic', function() {
var filter = jest.genMockFunction().mockImplementation(function(path) {
const filter = jest.genMockFunction().mockImplementation(function(path) {
return path.value.name === 'foo';
});
var fooVariables = Collection.fromNodes(nodes).filter(filter);
const fooVariables = Collection.fromNodes(nodes).filter(filter);

expect(filter.mock.calls.length).toBe(2);
expect(fooVariables.length).toBe(1);
Expand All @@ -218,26 +218,26 @@ describe('Collection API', function() {

describe('forEach', function() {
it('lets you iterate over each element of an collection', function() {
var each = jest.genMockFunction();
var fVariables = Collection.fromNodes(nodes).forEach(each);
const each = jest.genMockFunction();
const fVariables = Collection.fromNodes(nodes).forEach(each);

expect(each.mock.calls.length).toBe(2);
expect(each.mock.calls[0][0].value).toBe(nodes[0]);
expect(each.mock.calls[1][0].value).toBe(nodes[1]);
});

it('returns the collection itself', function() {
var fVariables = Collection.fromNodes(nodes);
var result = fVariables.forEach(function(){});
const fVariables = Collection.fromNodes(nodes);
const result = fVariables.forEach(function(){});

expect(result).toBe(fVariables);
});
});

describe('map', function() {
it('returns a new collection with mapped values', function() {
var root = Collection.fromNodes(nodes);
var mapped = root.map((_, i) => new NodePath(nodes[+!i]));
const root = Collection.fromNodes(nodes);
const mapped = root.map((_, i) => new NodePath(nodes[+!i]));

expect(root).not.toBe(mapped);
expect(mapped.length).toBe(2);
Expand All @@ -246,9 +246,9 @@ describe('Collection API', function() {
});

it('dedupes elements', function() {
var path = new NodePath(nodes[0]);
var root = Collection.fromNodes(nodes);
var mapped = root.map(_ => path);
const path = new NodePath(nodes[0]);
const root = Collection.fromNodes(nodes);
const mapped = root.map(_ => path);

expect(root).not.toBe(mapped);
expect(mapped.length).toBe(1);
Expand All @@ -258,21 +258,21 @@ describe('Collection API', function() {

describe('at', function() {
it('should work with positive indecies', function() {
var root = Collection.fromNodes(nodes);
const root = Collection.fromNodes(nodes);
expect(root.at(0).nodes()[0]).toEqual(nodes[0]);
expect(root.at(1).nodes()[0]).toEqual(nodes[1]);
});

it('should work with negative indecies', function() {
var root = Collection.fromNodes(nodes);
const root = Collection.fromNodes(nodes);
expect(root.at(-1).nodes()[0]).toEqual(nodes[nodes.length - 1]);
expect(root.at(-2).nodes()[0]).toEqual(nodes[nodes.length - 2]);
});
});

describe('get', function() {
it('should throw descriptive error when no paths are present', function() {
var root = Collection.fromNodes([]);
const root = Collection.fromNodes([]);
expect(() => root.get()).toThrowError(/cannot call "get" on a collection with no paths/);
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/Worker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ describe('Worker API', () => {
});

it('passes j as argument', done => {
var transformPath = createTempFileWith(
const transformPath = createTempFileWith(
`module.exports = function (file, api) {
return api.j(file.source).toSource() + ' changed';
}`
);
var sourcePath = createTempFileWith('const x = 10;');
const sourcePath = createTempFileWith('const x = 10;');

const emitter = worker([transformPath]);
emitter.send({files: [sourcePath]});
Expand Down
Loading

0 comments on commit e4fdb02

Please sign in to comment.