mirror of
https://github.com/nodejs/node.git
synced 2026-11-30 11:55:44 -05:00
repl: fix and extend require/import tab complete
PR-URL: https://github.com/nodejs/node/pull/40216 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
da35d4fc4b
commit
370bfda154
3 files changed
+107
-113
No files matched your search
+105
-107
@@ -125,7 +125,7 @@ const { Console } = require('console');
|
||||
const CJSModule = require('internal/modules/cjs/loader').Module;
|
||||
let _builtinLibs = ArrayPrototypeFilter(
|
||||
CJSModule.builtinModules,
|
||||
(e) => !StringPrototypeStartsWith(e, '_') && !StringPrototypeIncludes(e, '/')
|
||||
(e) => !StringPrototypeStartsWith(e, '_'),
|
||||
);
|
||||
const nodeSchemeBuiltinLibs = ArrayPrototypeMap(
|
||||
_builtinLibs, (lib) => `node:${lib}`);
|
||||
@@ -1287,135 +1287,133 @@ function complete(line, callback) {
|
||||
if (completeOn.length) {
|
||||
filter = completeOn;
|
||||
}
|
||||
} else if (RegExpPrototypeTest(requireRE, line) &&
|
||||
this.allowBlockingCompletions) {
|
||||
} else if (RegExpPrototypeTest(requireRE, line)) {
|
||||
// require('...<Tab>')
|
||||
const extensions = ObjectKeys(this.context.require.extensions);
|
||||
const indexes = ArrayPrototypeMap(extensions,
|
||||
(extension) => `index${extension}`);
|
||||
ArrayPrototypePush(indexes, 'package.json', 'index');
|
||||
|
||||
const match = StringPrototypeMatch(line, requireRE);
|
||||
completeOn = match[1];
|
||||
const subdir = match[2] || '';
|
||||
filter = completeOn;
|
||||
group = [];
|
||||
let paths = [];
|
||||
if (this.allowBlockingCompletions) {
|
||||
const subdir = match[2] || '';
|
||||
const extensions = ObjectKeys(this.context.require.extensions);
|
||||
const indexes = ArrayPrototypeMap(extensions,
|
||||
(extension) => `index${extension}`);
|
||||
ArrayPrototypePush(indexes, 'package.json', 'index');
|
||||
|
||||
if (completeOn === '.') {
|
||||
group = ['./', '../'];
|
||||
} else if (completeOn === '..') {
|
||||
group = ['../'];
|
||||
} else if (RegExpPrototypeTest(/^\.\.?\//, completeOn)) {
|
||||
paths = [process.cwd()];
|
||||
} else {
|
||||
paths = ArrayPrototypeConcat(module.paths, CJSModule.globalPaths);
|
||||
}
|
||||
group = [];
|
||||
let paths = [];
|
||||
|
||||
ArrayPrototypeForEach(paths, (dir) => {
|
||||
dir = path.resolve(dir, subdir);
|
||||
const dirents = gracefulReaddir(dir, { withFileTypes: true }) || [];
|
||||
ArrayPrototypeForEach(dirents, (dirent) => {
|
||||
if (RegExpPrototypeTest(versionedFileNamesRe, dirent.name) ||
|
||||
dirent.name === '.npm') {
|
||||
// Exclude versioned names that 'npm' installs.
|
||||
return;
|
||||
}
|
||||
const extension = path.extname(dirent.name);
|
||||
const base = StringPrototypeSlice(dirent.name, 0, -extension.length);
|
||||
if (!dirent.isDirectory()) {
|
||||
if (StringPrototypeIncludes(extensions, extension) &&
|
||||
(!subdir || base !== 'index')) {
|
||||
ArrayPrototypePush(group, `${subdir}${base}`);
|
||||
if (completeOn === '.') {
|
||||
group = ['./', '../'];
|
||||
} else if (completeOn === '..') {
|
||||
group = ['../'];
|
||||
} else if (RegExpPrototypeTest(/^\.\.?\//, completeOn)) {
|
||||
paths = [process.cwd()];
|
||||
} else {
|
||||
paths = ArrayPrototypeConcat(module.paths, CJSModule.globalPaths);
|
||||
}
|
||||
|
||||
ArrayPrototypeForEach(paths, (dir) => {
|
||||
dir = path.resolve(dir, subdir);
|
||||
const dirents = gracefulReaddir(dir, { withFileTypes: true }) || [];
|
||||
ArrayPrototypeForEach(dirents, (dirent) => {
|
||||
if (RegExpPrototypeTest(versionedFileNamesRe, dirent.name) ||
|
||||
dirent.name === '.npm') {
|
||||
// Exclude versioned names that 'npm' installs.
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
ArrayPrototypePush(group, `${subdir}${dirent.name}/`);
|
||||
const absolute = path.resolve(dir, dirent.name);
|
||||
if (ArrayPrototypeSome(
|
||||
gracefulReaddir(absolute) || [],
|
||||
(subfile) => ArrayPrototypeIncludes(indexes, subfile)
|
||||
)) {
|
||||
ArrayPrototypePush(group, `${subdir}${dirent.name}`);
|
||||
}
|
||||
const extension = path.extname(dirent.name);
|
||||
const base = StringPrototypeSlice(dirent.name, 0, -extension.length);
|
||||
if (!dirent.isDirectory()) {
|
||||
if (StringPrototypeIncludes(extensions, extension) &&
|
||||
(!subdir || base !== 'index')) {
|
||||
ArrayPrototypePush(group, `${subdir}${base}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
ArrayPrototypePush(group, `${subdir}${dirent.name}/`);
|
||||
const absolute = path.resolve(dir, dirent.name);
|
||||
if (ArrayPrototypeSome(
|
||||
gracefulReaddir(absolute) || [],
|
||||
(subfile) => ArrayPrototypeIncludes(indexes, subfile)
|
||||
)) {
|
||||
ArrayPrototypePush(group, `${subdir}${dirent.name}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
if (group.length) {
|
||||
ArrayPrototypePush(completionGroups, group);
|
||||
if (group.length) {
|
||||
ArrayPrototypePush(completionGroups, group);
|
||||
}
|
||||
}
|
||||
|
||||
if (!subdir) {
|
||||
ArrayPrototypePush(completionGroups, _builtinLibs, nodeSchemeBuiltinLibs);
|
||||
}
|
||||
} else if (RegExpPrototypeTest(importRE, line) &&
|
||||
this.allowBlockingCompletions) {
|
||||
ArrayPrototypePush(completionGroups, _builtinLibs, nodeSchemeBuiltinLibs);
|
||||
} else if (RegExpPrototypeTest(importRE, line)) {
|
||||
// import('...<Tab>')
|
||||
// File extensions that can be imported:
|
||||
const extensions = ObjectKeys(
|
||||
getOptionValue('--experimental-specifier-resolution') === 'node' ?
|
||||
legacyExtensionFormatMap :
|
||||
extensionFormatMap);
|
||||
|
||||
// Only used when loading bare module specifiers from `node_modules`:
|
||||
const indexes = ArrayPrototypeMap(extensions, (ext) => `index${ext}`);
|
||||
ArrayPrototypePush(indexes, 'package.json');
|
||||
|
||||
const match = StringPrototypeMatch(line, importRE);
|
||||
completeOn = match[1];
|
||||
const subdir = match[2] || '';
|
||||
filter = completeOn;
|
||||
group = [];
|
||||
let paths = [];
|
||||
if (completeOn === '.') {
|
||||
group = ['./', '../'];
|
||||
} else if (completeOn === '..') {
|
||||
group = ['../'];
|
||||
} else if (RegExpPrototypeTest(/^\.\.?\//, completeOn)) {
|
||||
paths = [process.cwd()];
|
||||
} else {
|
||||
paths = ArrayPrototypeSlice(module.paths);
|
||||
}
|
||||
if (this.allowBlockingCompletions) {
|
||||
const subdir = match[2] || '';
|
||||
// File extensions that can be imported:
|
||||
const extensions = ObjectKeys(
|
||||
getOptionValue('--experimental-specifier-resolution') === 'node' ?
|
||||
legacyExtensionFormatMap :
|
||||
extensionFormatMap);
|
||||
|
||||
ArrayPrototypeForEach(paths, (dir) => {
|
||||
dir = path.resolve(dir, subdir);
|
||||
const isInNodeModules = path.basename(dir) === 'node_modules';
|
||||
const dirents = gracefulReaddir(dir, { withFileTypes: true }) || [];
|
||||
ArrayPrototypeForEach(dirents, (dirent) => {
|
||||
const { name } = dirent;
|
||||
if (RegExpPrototypeTest(versionedFileNamesRe, name) ||
|
||||
name === '.npm') {
|
||||
// Exclude versioned names that 'npm' installs.
|
||||
return;
|
||||
}
|
||||
// Only used when loading bare module specifiers from `node_modules`:
|
||||
const indexes = ArrayPrototypeMap(extensions, (ext) => `index${ext}`);
|
||||
ArrayPrototypePush(indexes, 'package.json');
|
||||
|
||||
if (!dirent.isDirectory()) {
|
||||
const extension = path.extname(name);
|
||||
if (StringPrototypeIncludes(extensions, extension)) {
|
||||
ArrayPrototypePush(group, `${subdir}${name}`);
|
||||
group = [];
|
||||
let paths = [];
|
||||
if (completeOn === '.') {
|
||||
group = ['./', '../'];
|
||||
} else if (completeOn === '..') {
|
||||
group = ['../'];
|
||||
} else if (RegExpPrototypeTest(/^\.\.?\//, completeOn)) {
|
||||
paths = [process.cwd()];
|
||||
} else {
|
||||
paths = ArrayPrototypeSlice(module.paths);
|
||||
}
|
||||
|
||||
ArrayPrototypeForEach(paths, (dir) => {
|
||||
dir = path.resolve(dir, subdir);
|
||||
const isInNodeModules = path.basename(dir) === 'node_modules';
|
||||
const dirents = gracefulReaddir(dir, { withFileTypes: true }) || [];
|
||||
ArrayPrototypeForEach(dirents, (dirent) => {
|
||||
const { name } = dirent;
|
||||
if (RegExpPrototypeTest(versionedFileNamesRe, name) ||
|
||||
name === '.npm') {
|
||||
// Exclude versioned names that 'npm' installs.
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ArrayPrototypePush(group, `${subdir}${name}/`);
|
||||
if (!subdir && isInNodeModules) {
|
||||
const absolute = path.resolve(dir, name);
|
||||
const subfiles = gracefulReaddir(absolute) || [];
|
||||
if (ArrayPrototypeSome(subfiles, (subfile) => {
|
||||
return ArrayPrototypeIncludes(indexes, subfile);
|
||||
})) {
|
||||
ArrayPrototypePush(group, `${subdir}${name}`);
|
||||
if (!dirent.isDirectory()) {
|
||||
const extension = path.extname(name);
|
||||
if (StringPrototypeIncludes(extensions, extension)) {
|
||||
ArrayPrototypePush(group, `${subdir}${name}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ArrayPrototypePush(group, `${subdir}${name}/`);
|
||||
if (!subdir && isInNodeModules) {
|
||||
const absolute = path.resolve(dir, name);
|
||||
const subfiles = gracefulReaddir(absolute) || [];
|
||||
if (ArrayPrototypeSome(subfiles, (subfile) => {
|
||||
return ArrayPrototypeIncludes(indexes, subfile);
|
||||
})) {
|
||||
ArrayPrototypePush(group, `${subdir}${name}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
if (group.length) {
|
||||
ArrayPrototypePush(completionGroups, group);
|
||||
if (group.length) {
|
||||
ArrayPrototypePush(completionGroups, group);
|
||||
}
|
||||
}
|
||||
|
||||
if (!subdir) {
|
||||
ArrayPrototypePush(completionGroups, _builtinLibs, nodeSchemeBuiltinLibs);
|
||||
}
|
||||
ArrayPrototypePush(completionGroups, _builtinLibs, nodeSchemeBuiltinLibs);
|
||||
} else if (RegExpPrototypeTest(fsAutoCompleteRE, line) &&
|
||||
this.allowBlockingCompletions) {
|
||||
({ 0: completionGroups, 1: completeOn } = completeFSFunctions(line));
|
||||
|
||||
@@ -5,9 +5,7 @@ const ArrayStream = require('../common/arraystream');
|
||||
const fixtures = require('../common/fixtures');
|
||||
const assert = require('assert');
|
||||
const { builtinModules } = require('module');
|
||||
const publicModules = builtinModules.filter(
|
||||
(lib) => !lib.startsWith('_') && !lib.includes('/'),
|
||||
);
|
||||
const publicModules = builtinModules.filter((lib) => !lib.startsWith('_'));
|
||||
|
||||
if (!common.isMainThread)
|
||||
common.skip('process.chdir is not available in Workers');
|
||||
|
||||
@@ -31,9 +31,7 @@ const assert = require('assert');
|
||||
const path = require('path');
|
||||
const fixtures = require('../common/fixtures');
|
||||
const { builtinModules } = require('module');
|
||||
const publicModules = builtinModules.filter(
|
||||
(lib) => !lib.startsWith('_') && !lib.includes('/'),
|
||||
);
|
||||
const publicModules = builtinModules.filter((lib) => !lib.startsWith('_'));
|
||||
|
||||
const hasInspector = process.features.inspector;
|
||||
|
||||
|
||||
Reference in new issue
Block a user