Skip to content

Commit 3b9fea0

Browse files
aqrlnMylesBorins
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. 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 6a27774 commit 3b9fea0

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
@@ -870,7 +870,18 @@ function complete(line, callback) {
870870
filter = match[1];
871871
var dir, files, f, name, base, ext, abs, subfiles, s;
872872
group = [];
873-
var paths = module.paths.concat(Module.globalPaths);
873+
let paths = [];
874+
875+
if (completeOn === '.') {
876+
group = ['./', '../'];
877+
} else if (completeOn === '..') {
878+
group = ['../'];
879+
} else if (/^\.\.?\//.test(completeOn)) {
880+
paths = [process.cwd()];
881+
} else {
882+
paths = module.paths.concat(Module.globalPaths);
883+
}
884+
874885
for (i = 0; i < paths.length; i++) {
875886
dir = path.resolve(paths[i], subdir);
876887
try {

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

+50
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,56 @@ testMe.complete('require(\'n', common.mustCall(function(error, data) {
212212
}));
213213
}
214214

215+
// Test tab completion for require() relative to the current directory
216+
{
217+
putIn.run(['.clear']);
218+
219+
const cwd = process.cwd();
220+
process.chdir(__dirname);
221+
222+
['require(\'.', 'require(".'].forEach((input) => {
223+
testMe.complete(input, common.mustCall((err, data) => {
224+
assert.strictEqual(err, null);
225+
assert.strictEqual(data.length, 2);
226+
assert.strictEqual(data[1], '.');
227+
assert.strictEqual(data[0].length, 2);
228+
assert.ok(data[0].includes('./'));
229+
assert.ok(data[0].includes('../'));
230+
}));
231+
});
232+
233+
['require(\'..', 'require("..'].forEach((input) => {
234+
testMe.complete(input, common.mustCall((err, data) => {
235+
assert.strictEqual(err, null);
236+
assert.deepStrictEqual(data, [['../'], '..']);
237+
}));
238+
});
239+
240+
['./', './test-'].forEach((path) => {
241+
[`require('${path}`, `require("${path}`].forEach((input) => {
242+
testMe.complete(input, common.mustCall((err, data) => {
243+
assert.strictEqual(err, null);
244+
assert.strictEqual(data.length, 2);
245+
assert.strictEqual(data[1], path);
246+
assert.ok(data[0].includes('./test-repl-tab-complete'));
247+
}));
248+
});
249+
});
250+
251+
['../parallel/', '../parallel/test-'].forEach((path) => {
252+
[`require('${path}`, `require("${path}`].forEach((input) => {
253+
testMe.complete(input, common.mustCall((err, data) => {
254+
assert.strictEqual(err, null);
255+
assert.strictEqual(data.length, 2);
256+
assert.strictEqual(data[1], path);
257+
assert.ok(data[0].includes('../parallel/test-repl-tab-complete'));
258+
}));
259+
});
260+
});
261+
262+
process.chdir(cwd);
263+
}
264+
215265
// Make sure tab completion works on context properties
216266
putIn.run(['.clear']);
217267

0 commit comments

Comments
 (0)