Skip to content

Commit

Permalink
repl: include folder extensions in autocomplete
Browse files Browse the repository at this point in the history
When autocompleting `require` calls, the repl strips .js file extensions
from results. However, stripping an extension from a directory results
in an error. Update the autocompletion logic to avoid stripping
extensions from directories.

PR-URL: #14727
Fixes: #14726
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
Reviewed-By: Alexey Orlenko <[email protected]>
  • Loading branch information
not-an-aardvark authored and addaleax committed Aug 13, 2017
1 parent 9237ef8 commit 1c00875
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
35 changes: 19 additions & 16 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ function complete(line, callback) {
completeOn = match[1];
var subdir = match[2] || '';
filter = match[1];
var dir, files, f, name, base, ext, abs, subfiles, s;
var dir, files, f, name, base, ext, abs, subfiles, s, isDirectory;
group = [];
let paths = [];

Expand Down Expand Up @@ -821,23 +821,26 @@ function complete(line, callback) {
// Exclude versioned names that 'npm' installs.
continue;
}
if (exts.indexOf(ext) !== -1) {
if (!subdir || base !== 'index') {
group.push(subdir + base);
}
} else {
abs = path.resolve(dir, name);
abs = path.resolve(dir, name);
try {
isDirectory = fs.statSync(abs).isDirectory();
} catch (e) {
continue;
}
if (isDirectory) {
group.push(subdir + name + '/');
try {
if (fs.statSync(abs).isDirectory()) {
group.push(subdir + name + '/');
subfiles = fs.readdirSync(abs);
for (s = 0; s < subfiles.length; s++) {
if (indexRe.test(subfiles[s])) {
group.push(subdir + name);
}
}
subfiles = fs.readdirSync(abs);
} catch (e) {
continue;
}
for (s = 0; s < subfiles.length; s++) {
if (indexRe.test(subfiles[s])) {
group.push(subdir + name);
}
} catch (e) {}
}
} else if (exts.includes(ext) && (!subdir || base !== 'index')) {
group.push(subdir + base);
}
}
}
Expand Down
Empty file.
10 changes: 10 additions & 0 deletions test/parallel/test-repl-tab-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@ testMe.complete('require(\'n', common.mustCall(function(error, data) {
});
});

{
const path = '../fixtures/repl-folder-extensions/f';
testMe.complete(`require('${path}`, common.mustCall((err, data) => {
assert.ifError(err);
assert.strictEqual(data.length, 2);
assert.strictEqual(data[1], path);
assert.ok(data[0].includes('../fixtures/repl-folder-extensions/foo.js'));
}));
}

process.chdir(cwd);
}

Expand Down

0 comments on commit 1c00875

Please sign in to comment.