Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: improve require() autocompletion #14409

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,18 @@ function complete(line, callback) {
filter = match[1];
var dir, files, f, name, base, ext, abs, subfiles, s;
group = [];
var paths = module.paths.concat(Module.globalPaths);
let paths = [];

if (completeOn === '.') {
group = ['./', '../'];
} else if (completeOn === '..') {
group = ['../'];
} else if (/^\.\.?\//.test(completeOn)) {
Copy link
Contributor

@refack refack Jul 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: isn't there a difference between what is available for ./* and ../*?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this context no, there isn't, both ./ and ../ just mean that we should start collecting completions starting from process.cwd(). The whole magic with path.resolve() is happening in the loop below.

paths = [process.cwd()];
} else {
paths = module.paths.concat(Module.globalPaths);
}

for (i = 0; i < paths.length; i++) {
dir = path.resolve(paths[i], subdir);
try {
Expand Down
50 changes: 50 additions & 0 deletions test/parallel/test-repl-tab-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,56 @@ testMe.complete('require(\'n', common.mustCall(function(error, data) {
}));
}

// Test tab completion for require() relative to the current directory
{
putIn.run(['.clear']);

const cwd = process.cwd();
process.chdir(__dirname);

['require(\'.', 'require(".'].forEach((input) => {
testMe.complete(input, common.mustCall((err, data) => {
assert.strictEqual(err, null);
assert.strictEqual(data.length, 2);
assert.strictEqual(data[1], '.');
assert.strictEqual(data[0].length, 2);
assert.ok(data[0].includes('./'));
assert.ok(data[0].includes('../'));
}));
});

['require(\'..', 'require("..'].forEach((input) => {
testMe.complete(input, common.mustCall((err, data) => {
assert.strictEqual(err, null);
assert.deepStrictEqual(data, [['../'], '..']);
}));
});

['./', './test-'].forEach((path) => {
[`require('${path}`, `require("${path}`].forEach((input) => {
testMe.complete(input, common.mustCall((err, data) => {
assert.strictEqual(err, null);
assert.strictEqual(data.length, 2);
assert.strictEqual(data[1], path);
assert.ok(data[0].includes('./test-repl-tab-complete'));
}));
});
});

['../parallel/', '../parallel/test-'].forEach((path) => {
[`require('${path}`, `require("${path}`].forEach((input) => {
testMe.complete(input, common.mustCall((err, data) => {
assert.strictEqual(err, null);
assert.strictEqual(data.length, 2);
assert.strictEqual(data[1], path);
assert.ok(data[0].includes('../parallel/test-repl-tab-complete'));
}));
});
});

process.chdir(cwd);
}

// Make sure tab completion works on context properties
putIn.run(['.clear']);

Expand Down