Skip to content

Commit

Permalink
Remove direct dependency on lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
fkling committed Dec 3, 2018
1 parent 143e375 commit 4701096
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 17 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"colors": "^1.1.2",
"flow-parser": "^0.*",
"graceful-fs": "^4.1.11",
"lodash": "^4.13.1",
"micromatch": "^2.3.7",
"neo-async": "^2.5.0",
"node-dir": "^0.1.17",
Expand Down
13 changes: 6 additions & 7 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
'use strict';

const assert = require('assert');
const intersection = require('./utils/intersection');
const recast = require('recast');
const _ = require('lodash');
const union = require('./utils/union');

const astTypes = recast.types;
var types = astTypes.namedTypes;
Expand Down Expand Up @@ -260,8 +261,7 @@ function _inferTypes(paths) {
);
} else {
// try to find a common type
_types = _.intersection.apply(
null,
_types = intersection(
paths.map(path => astTypes.getSupertypeNames(path.node.type))
);
}
Expand All @@ -274,10 +274,9 @@ function _toTypeArray(value) {
value = !Array.isArray(value) ? [value] : value;
value = value.map(v => v.toString());
if (value.length > 1) {
return _.union(value, _.intersection.apply(
null,
value.map(_getSupertypeNames)
));
return union(
[value].concat(intersection(value.map(_getSupertypeNames)))
);
} else {
return value.concat(_getSupertypeNames(value[0]));
}
Expand Down
4 changes: 2 additions & 2 deletions src/collections/JSXElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

'use strict';

const _ = require('lodash');
const Collection = require('../Collection');
const NodeCollection = require('./Node');

const assert = require('assert');
const once = require('../utils/once');
const recast = require('recast');
const requiresModule = require('./VariableDeclarator').filters.requiresModule;

Expand Down Expand Up @@ -199,6 +199,6 @@ function register() {
Collection.registerMethods(traversalMethods, JSXElement);
}

exports.register = _.once(register);
exports.register = once(register);
exports.filters = filterMethods;
exports.mappings = mappingMethods;
4 changes: 2 additions & 2 deletions src/collections/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

'use strict';

const _ = require('lodash');
const Collection = require('../Collection');

const matchNode = require('../matchNode');
const once = require('../utils/once');
const recast = require('recast');

const Node = recast.types.namedTypes.Node;
Expand Down Expand Up @@ -186,4 +186,4 @@ function register() {
Collection.setDefaultCollectionType(Node);
}

exports.register = _.once(register);
exports.register = once(register);
8 changes: 4 additions & 4 deletions src/collections/VariableDeclarator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

'use strict';

const _ = require('lodash');
const Collection = require('../Collection');
const NodeCollection = require('./Node');
const once = require('../utils/once');
const recast = require('recast');

const astNodesAreEquivalent = recast.types.astNodesAreEquivalent;
Expand Down Expand Up @@ -134,8 +134,8 @@ const transformMethods = {
scope = scope.parent;
}
if (scope) { // identifier must refer to declared variable
// It may look like we filtered out properties,

// It may look like we filtered out properties,
// but the filter only ignored property "keys", not "value"s
// In shorthand properties, "key" and "value" both have an
// Identifier with the same structure.
Expand Down Expand Up @@ -163,5 +163,5 @@ function register() {
Collection.registerMethods(transformMethods, VariableDeclarator);
}

exports.register = _.once(register);
exports.register = once(register);
exports.filters = filterMethods;
35 changes: 35 additions & 0 deletions src/utils/__tests__/intersection-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

const intersection = require('../intersection');

function test(testCases) {
for (const testName in testCases) {
const testCase = testCases[testName];
it(testName, function() {
expect(intersection(testCase.input)).toEqual(testCase.output);
});
}
}

describe('intersection', function() {
test({
'intersects string values': {
input: [['foo', 'bar', 'baz'], ['foo', 'bar'], ['bar', 'baz']],
output: ['bar'],
},

'returns empty list if no intersection': {
input: [['foo', 'bar', 'baz'], ['foo'], ['bar']],
output: [],
},
});
});

25 changes: 25 additions & 0 deletions src/utils/__tests__/once-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

const once = require('../once');

describe('once', function() {
it('executes the function only once', function() {
const mock = jest.fn().mockImplementation(foo => foo);
const wrapped = once(mock);

wrapped('foo');
const result = wrapped('bar');

expect(result).toEqual('foo');
expect(mock).toHaveBeenCalledTimes(1);
});
});

35 changes: 35 additions & 0 deletions src/utils/__tests__/union-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

const union = require('../union');

function test(testCases) {
for (const testName in testCases) {
const testCase = testCases[testName];
it(testName, function() {
expect(union(testCase.input)).toEqual(testCase.output);
});
}
}

describe('union', function() {
test({
'unions string values': {
input: [['foo', 'bar', 'baz'], ['foo', 'bar'], ['bar', 'baz']],
output: ['foo', 'bar', 'baz'],
},

'understands empty input arrays': {
input: [[], ['foo'], ['bar']],
output: ['foo', 'bar'],
},
});
});

29 changes: 29 additions & 0 deletions src/utils/intersection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

module.exports = function(arrays) {
const result = new Set(arrays[0]);
let resultSize = result.length;

let i, value, valuesToCheck;
for (i = 1; i < arrays.length; i++) {
valuesToCheck = new Set(arrays[i]);
for (value of result) {
if (!valuesToCheck.has(value)) {
result.delete(value);
resultSize -= 1;
}
if (resultSize === 0) {
return [];
}
}
}

return Array.from(result);
};
23 changes: 23 additions & 0 deletions src/utils/once.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

/**
* This replicates lodash's once functionality for our purposes.
*/
module.exports = function(func) {
let called = false;
let result;
return function(...args) {
if (called) {
return result;
}
called = true;
return result = func.apply(this, args);
};
};
22 changes: 22 additions & 0 deletions src/utils/union.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

module.exports = function(arrays) {
const result = new Set(arrays[0]);

let i,j, array;
for (i = 1; i < arrays.length; i++) {
array = arrays[i];
for (j = 0; j < array.length; j++) {
result.add(array[j]);
}
}

return Array.from(result);
};
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2197,7 +2197,7 @@ lodash.pickby@^4.0.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.pickby/-/lodash.pickby-4.6.0.tgz#7dea21d8c18d7703a27c704c15d3b84a67e33aff"

lodash@^4.0.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.2.0, lodash@^4.3.0:
lodash@^4.0.0, lodash@^4.14.0, lodash@^4.2.0, lodash@^4.3.0:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

Expand Down

0 comments on commit 4701096

Please sign in to comment.