Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: identifiers #148

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ module.exports = function(options) {
debug('list form of results requested');

tree = Array.from(results);
} else if (config.identifiers) {
debug('identifiers form of results requested');

tree = [{
path: config.filename,
children: results
}];
} else {
debug('object form of results requested');

Expand Down Expand Up @@ -102,8 +109,10 @@ module.exports._getDependencies = function(config) {
for (let i = 0, l = dependencies.length; i < l; i++) {
const dep = dependencies[i];

const depPath = dep.path || dep;

const result = cabinet({
partial: dep,
partial: depPath,
filename: config.filename,
directory: config.directory,
ast: precinct.ast,
Expand All @@ -114,8 +123,13 @@ module.exports._getDependencies = function(config) {
noTypeDefinitions: config.noTypeDefinitions
});

const resultDep = typeof dep === 'string' ? result : {
...dep,
path: result
};

if (!result) {
debug('skipping an empty filepath resolution for partial: ' + dep);
debug('skipping an empty filepath resolution for partial: ' + depPath);
config.nonExistent.push(dep);
continue;
}
Expand All @@ -128,7 +142,7 @@ module.exports._getDependencies = function(config) {
continue;
}

resolvedDependencies.push(result);
resolvedDependencies.push(resultDep);
}

return resolvedDependencies;
Expand All @@ -139,7 +153,7 @@ module.exports._getDependencies = function(config) {
* @return {Object|Set}
*/
function traverse(config) {
let subTree = config.isListForm ? new Set() : {};
let subTree = config.isListForm ? new Set() : config.identifiers ? [] : {};

debug('traversing ' + config.filename);

Expand All @@ -166,15 +180,25 @@ function traverse(config) {

for (let i = 0, l = dependencies.length; i < l; i++) {
const d = dependencies[i];

// If `identifiers: true` was passed, we'll get results objects with `path`, otherwise strings.
const depPath = d.path || d;

const localConfig = config.clone();
localConfig.filename = d;
localConfig.filename = depPath;

if (localConfig.isListForm) {
for (let item of traverse(localConfig)) {
subTree.add(item);
}
} else if (localConfig.identifiers) {
subTree.push({
path: depPath,
identifiers: d.identifiers,
children: traverse(localConfig)
});
} else {
subTree[d] = traverse(localConfig);
subTree[depPath] = traverse(localConfig);
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Config {
this.detectiveConfig = options.detective || options.detectiveConfig || {};
this.tsConfig = options.tsConfig;
this.noTypeDefinitions = options.noTypeDefinitions;
this.identifiers = options.identifiers;

this.filter = options.filter;

Expand Down