Skip to content

Commit 5a8862b

Browse files
aqrlnaddaleax
authored andcommitted
repl: improve require() autocompletion
Currently REPL supports autocompletion for core modules and those found in node_modules. This commit adds tab completion for modules relative to the current directory. Backport-PR-URL: #14525 Backport-Reviewed-By: Anna Henningsen <[email protected]> PR-URL: #14409 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 23cd934 commit 5a8862b

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

lib/repl.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,18 @@ function complete(line, callback) {
793793
filter = match[1];
794794
var dir, files, f, name, base, ext, abs, subfiles, s;
795795
group = [];
796-
var paths = module.paths.concat(require('module').globalPaths);
796+
let paths = [];
797+
798+
if (completeOn === '.') {
799+
group = ['./', '../'];
800+
} else if (completeOn === '..') {
801+
group = ['../'];
802+
} else if (/^\.\.?\//.test(completeOn)) {
803+
paths = [process.cwd()];
804+
} else {
805+
paths = module.paths.concat(Module.globalPaths);
806+
}
807+
797808
for (i = 0; i < paths.length; i++) {
798809
dir = path.resolve(paths[i], subdir);
799810
try {

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

+50
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,56 @@ testMe.complete('require(\'n', common.mustCall(function(error, data) {
232232
}));
233233
}
234234

235+
// Test tab completion for require() relative to the current directory
236+
{
237+
putIn.run(['.clear']);
238+
239+
const cwd = process.cwd();
240+
process.chdir(__dirname);
241+
242+
['require(\'.', 'require(".'].forEach((input) => {
243+
testMe.complete(input, common.mustCall((err, data) => {
244+
assert.strictEqual(err, null);
245+
assert.strictEqual(data.length, 2);
246+
assert.strictEqual(data[1], '.');
247+
assert.strictEqual(data[0].length, 2);
248+
assert.ok(data[0].includes('./'));
249+
assert.ok(data[0].includes('../'));
250+
}));
251+
});
252+
253+
['require(\'..', 'require("..'].forEach((input) => {
254+
testMe.complete(input, common.mustCall((err, data) => {
255+
assert.strictEqual(err, null);
256+
assert.deepStrictEqual(data, [['../'], '..']);
257+
}));
258+
});
259+
260+
['./', './test-'].forEach((path) => {
261+
[`require('${path}`, `require("${path}`].forEach((input) => {
262+
testMe.complete(input, common.mustCall((err, data) => {
263+
assert.strictEqual(err, null);
264+
assert.strictEqual(data.length, 2);
265+
assert.strictEqual(data[1], path);
266+
assert.ok(data[0].includes('./test-repl-tab-complete'));
267+
}));
268+
});
269+
});
270+
271+
['../parallel/', '../parallel/test-'].forEach((path) => {
272+
[`require('${path}`, `require("${path}`].forEach((input) => {
273+
testMe.complete(input, common.mustCall((err, data) => {
274+
assert.strictEqual(err, null);
275+
assert.strictEqual(data.length, 2);
276+
assert.strictEqual(data[1], path);
277+
assert.ok(data[0].includes('../parallel/test-repl-tab-complete'));
278+
}));
279+
});
280+
});
281+
282+
process.chdir(cwd);
283+
}
284+
235285
// Make sure tab completion works on context properties
236286
putIn.run(['.clear']);
237287

0 commit comments

Comments
 (0)