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. 35
      lib/repl.js
  2. 0
      test/fixtures/repl-folder-extensions/foo.js/index.js
  3. 10
      test/parallel/test-repl-tab-complete.js

35
lib/repl.js

@ -822,7 +822,7 @@ function complete(line, callback) {
completeOn = match[1]; completeOn = match[1];
var subdir = match[2] || ''; var subdir = match[2] || '';
filter = match[1]; 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 = []; group = [];
let paths = []; let paths = [];
@ -851,23 +851,26 @@ function complete(line, callback) {
// Exclude versioned names that 'npm' installs. // Exclude versioned names that 'npm' installs.
continue; continue;
} }
if (exts.indexOf(ext) !== -1) { abs = path.resolve(dir, name);
if (!subdir || base !== 'index') { try {
group.push(subdir + base); isDirectory = fs.statSync(abs).isDirectory();
} } catch (e) {
} else { continue;
abs = path.resolve(dir, name); }
if (isDirectory) {
group.push(subdir + name + '/');
try { try {
if (fs.statSync(abs).isDirectory()) { subfiles = fs.readdirSync(abs);
group.push(subdir + name + '/'); } catch (e) {
subfiles = fs.readdirSync(abs); continue;
for (s = 0; s < subfiles.length; s++) { }
if (indexRe.test(subfiles[s])) { for (s = 0; s < subfiles.length; s++) {
group.push(subdir + name); 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); process.chdir(cwd);
} }

Loading…
Cancel
Save