Browse Source

repl: include folder extensions in autocomplete

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: https://github.com/nodejs/node/pull/14727
Fixes: https://github.com/nodejs/node/issues/14726
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
v6
Teddy Katz 7 years ago
parent
commit
b646a3df29
No known key found for this signature in database GPG Key ID: B79EC7ABC0AA53A0
  1. 21
      lib/repl.js
  2. 0
      test/fixtures/repl-folder-extensions/foo.js/index.js
  3. 10
      test/parallel/test-repl-tab-complete.js

21
lib/repl.js

@ -822,7 +822,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 = [];
@ -851,23 +851,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);
try {
if (fs.statSync(abs).isDirectory()) {
isDirectory = fs.statSync(abs).isDirectory();
} catch (e) {
continue;
}
if (isDirectory) {
group.push(subdir + name + '/');
try {
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);
}
}
}

0
test/fixtures/repl-folder-extensions/foo.js/index.js

10
test/parallel/test-repl-tab-complete.js

@ -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);
}

Loading…
Cancel
Save