Skip to content
This repository was archived by the owner on Dec 4, 2022. It is now read-only.

Commit 14da1cb

Browse files
author
David First
authored
fix error "Cannot read property push of undefined" when a resolver throws an exception. also, fix the resolver for an unknown extension (#70)
1 parent b73b8b8 commit 14da1cb

File tree

6 files changed

+42
-18
lines changed

6 files changed

+42
-18
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [unreleased]
99

10+
## [1.0.3-dev.10] - 2018-07-09
11+
12+
- fix error "Cannot read property push of undefined" when a resolver throws an exception
13+
- fix the resolver for an unknown extension
14+
1015
## [1.0.3-dev.9] - 2018-07-06
1116

1217
- bug fix - on Linux module path (require('a.js')) is resolved as relative path (require('./a.js'))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bit-javascript",
3-
"version": "1.0.3-dev.9",
3+
"version": "1.0.3-dev.10",
44
"scripts": {
55
"flow": "flow; test $? -eq 0 -o $? -eq 2",
66
"lint": "eslint src && flow check || true",

src/dependency-builder/build-tree.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,8 @@ function mergeMissingToTree(missingGroups, tree: Tree) {
348348
function mergeErrorsToTree(baseDir, errors, tree: Tree) {
349349
if (R.isEmpty(errors)) return;
350350
Object.keys(errors).forEach((file) => {
351-
const relativeFile = processPath(file, {}, baseDir);
352-
if (tree[relativeFile]) tree[relativeFile].error = errors[file];
353-
else tree[relativeFile] = { error: errors[file] };
351+
if (tree[file]) tree[file].error = errors[file];
352+
else tree[file] = { error: errors[file] };
354353
});
355354
}
356355

@@ -361,13 +360,6 @@ function groupBySupportedFiles(filePaths: string[]) {
361360
return R.groupBy(supportCriteria, filePaths);
362361
}
363362

364-
function mergeUnsupportedFilesToTree(baseDir, unsupportedFiles: string[], tree: Tree) {
365-
unsupportedFiles.forEach((file) => {
366-
const relativeFile = processPath(file, {}, baseDir);
367-
tree[relativeFile] = {};
368-
});
369-
}
370-
371363
/**
372364
* Function for fetching dependency tree of file or dir
373365
* @param baseDir working directory
@@ -394,14 +386,13 @@ export async function getDependencyTree({
394386
resolveConfig: resolveConfigAbsolute
395387
};
396388
const { supportedFiles, unsupportedFiles } = groupBySupportedFiles(filePaths);
397-
const { madgeTree, skipped, pathMap, errors } = generateTree(supportedFiles, config);
389+
const { madgeTree, skipped, pathMap, errors } = generateTree(supportedFiles, unsupportedFiles, config);
398390
const tree: Tree = groupDependencyTree(madgeTree, baseDir, bindingPrefix);
399391
const { missingGroups, foundPackages } = groupMissing(skipped, baseDir, consumerPath, bindingPrefix);
400392

401393
if (foundPackages) mergeManuallyFoundPackagesToTree(foundPackages, missingGroups, tree);
402394
if (errors) mergeErrorsToTree(baseDir, errors, tree);
403395
if (missingGroups) mergeMissingToTree(missingGroups, tree);
404-
if (unsupportedFiles) mergeUnsupportedFilesToTree(baseDir, unsupportedFiles, tree);
405396
if (pathMap) updateTreeWithPathMap(tree, pathMap, baseDir);
406397

407398
return { tree };

src/dependency-builder/filing-cabinet/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ module.exports = function cabinet(options: Options) {
7878

7979
const getResolverResults = () => {
8080
// old resolver are not getting an object parameter
81-
if (resolver.name === 'resolveDependencyPath' || ext === '.styl') {
81+
if (resolver.length > 1) {
82+
// check whether the 'resolver' function gets more than one parameter
8283
// $FlowFixMe
8384
return resolver(partial, filename, directory);
8485
}

src/dependency-builder/filing-cabinet/index.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,18 @@ describe('filing-cabinet', () => {
480480
});
481481
});
482482

483+
describe('unrecognized extension', () => {
484+
it('uses a generic resolve for unsupported file extensions', () => {
485+
const result = cabinet({
486+
partial: './bar',
487+
filename: 'barbazim/foo.baz',
488+
directory: 'barbazim/'
489+
});
490+
491+
assert.equal(result, path.normalize(`${mockRootDir}/barbazim/bar.baz`));
492+
});
493+
});
494+
483495
describe('.register', () => {
484496
it('registers a custom resolver for a given extension', () => {
485497
const stub = sinon.stub().returns('foo.foobar');

src/dependency-builder/generate-tree-madge.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function convertTree(depTree, tree, pathCache, baseDir) {
111111
* @param config
112112
* @return {Object}
113113
*/
114-
export default function generateTree(files = [], config) {
114+
export default function generateTree(files = [], unsupportedFiles = [], config) {
115115
const depTree = {};
116116
const visited = {};
117117
const nonExistent = {};
@@ -159,13 +159,28 @@ export default function generateTree(files = [], config) {
159159
});
160160

161161
let tree = convertTree(depTree, {}, pathCache, config.baseDir);
162-
for (const npmKey in npmPaths) {
163-
const id = processPath(npmKey, pathCache, config.baseDir);
164162

163+
unsupportedFiles.forEach((file) => {
164+
const relativeFile = processPath(file, pathCache, config.baseDir);
165+
if (!tree[relativeFile]) tree[relativeFile] = [];
166+
});
167+
168+
// rename errors keys from absolute paths to relative paths
169+
Object.keys(errors).forEach((file) => {
170+
const relativeFile = processPath(file, pathCache, config.baseDir);
171+
if (relativeFile !== file) {
172+
errors[relativeFile] = errors[file];
173+
delete errors[file];
174+
}
175+
});
176+
177+
Object.keys(npmPaths).forEach((npmKey) => {
178+
const id = processPath(npmKey, pathCache, config.baseDir);
179+
if (!tree[id] && errors[id]) return; // if a file has errors, it won't be in the tree object but in the errors object
165180
npmPaths[npmKey].forEach((npmPath) => {
166181
tree[id].push(processPath(npmPath, pathCache, config.baseDir));
167182
});
168-
}
183+
});
169184

170185
if (config.excludeRegExp) {
171186
tree = exclude(tree, config.excludeRegExp);

0 commit comments

Comments
 (0)