-
-
Notifications
You must be signed in to change notification settings - Fork 35.6k
Expand file tree
/
Copy pathcompletion.js
More file actions
802 lines (715 loc) Β· 28 KB
/
completion.js
File metadata and controls
802 lines (715 loc) Β· 28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
'use strict';
const {
ArrayPrototypeFilter,
ArrayPrototypeForEach,
ArrayPrototypeIncludes,
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypePop,
ArrayPrototypePush,
ArrayPrototypePushApply,
ArrayPrototypeShift,
ArrayPrototypeSlice,
ArrayPrototypeSome,
ArrayPrototypeSort,
ArrayPrototypeUnshift,
ObjectGetOwnPropertyDescriptor,
ObjectGetPrototypeOf,
ObjectKeys,
ReflectApply,
RegExpPrototypeExec,
SafeSet,
StringPrototypeCodePointAt,
StringPrototypeEndsWith,
StringPrototypeIncludes,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
StringPrototypeToLocaleLowerCase,
StringPrototypeTrimStart,
} = primordials;
const {
kContextId,
getREPLResourceName,
globalBuiltins,
getReplBuiltinLibs,
fixReplRequire,
} = require('internal/repl/utils');
const { sendInspectorCommand } = require('internal/util/inspector');
const {
isProxy,
} = require('internal/util/types');
const CJSModule = require('internal/modules/cjs/loader').Module;
const {
extensionFormatMap,
} = require('internal/modules/esm/formats');
const path = require('path');
const fs = require('fs');
const {
constants: {
ALL_PROPERTIES,
SKIP_SYMBOLS,
},
getOwnNonIndexProperties,
} = internalBinding('util');
const {
isIdentifierStart,
isIdentifierChar,
parse: acornParse,
} = require('internal/deps/acorn/acorn/dist/acorn');
const acornWalk = require('internal/deps/acorn/acorn-walk/dist/walk');
const importRE = /\bimport\s*\(\s*['"`](([\w@./:-]+\/)?(?:[\w@./:-]*))(?![^'"`])$/;
const requireRE = /\brequire\s*\(\s*['"`](([\w@./:-]+\/)?(?:[\w@./:-]*))(?![^'"`])$/;
const fsAutoCompleteRE = /fs(?:\.promises)?\.\s*[a-z][a-zA-Z]+\(\s*["'](.*)/;
const versionedFileNamesRe = /-\d+\.\d+/;
fixReplRequire(module);
const { BuiltinModule } = require('internal/bootstrap/realm');
const nodeSchemeBuiltinLibs = ArrayPrototypeMap(getReplBuiltinLibs(), (lib) => `node:${lib}`);
ArrayPrototypeForEach(
BuiltinModule.getSchemeOnlyModuleNames(),
(lib) => ArrayPrototypePush(nodeSchemeBuiltinLibs, `node:${lib}`),
);
function isIdentifier(str) {
if (str === '') {
return false;
}
const first = StringPrototypeCodePointAt(str, 0);
if (!isIdentifierStart(first)) {
return false;
}
const firstLen = first > 0xffff ? 2 : 1;
for (let i = firstLen; i < str.length; i += 1) {
const cp = StringPrototypeCodePointAt(str, i);
if (!isIdentifierChar(cp)) {
return false;
}
if (cp > 0xffff) {
i += 1;
}
}
return true;
}
function isNotLegacyObjectPrototypeMethod(str) {
return isIdentifier(str) &&
str !== '__defineGetter__' &&
str !== '__defineSetter__' &&
str !== '__lookupGetter__' &&
str !== '__lookupSetter__';
}
function getGlobalLexicalScopeNames(contextId) {
return sendInspectorCommand((session) => {
let names = [];
session.post('Runtime.globalLexicalScopeNames', {
executionContextId: contextId,
}, (error, result) => {
if (!error) names = result.names;
});
return names;
}, () => []);
}
function filteredOwnPropertyNames(obj) {
if (!obj) return [];
// `Object.prototype` is the only non-contrived object that fulfills
// `Object.getPrototypeOf(X) === null &&
// Object.getPrototypeOf(Object.getPrototypeOf(X.constructor)) === X`.
let isObjectPrototype = false;
if (ObjectGetPrototypeOf(obj) === null) {
const ctorDescriptor = ObjectGetOwnPropertyDescriptor(obj, 'constructor');
if (ctorDescriptor?.value) {
const ctorProto = ObjectGetPrototypeOf(ctorDescriptor.value);
isObjectPrototype = ctorProto && ObjectGetPrototypeOf(ctorProto) === obj;
}
}
const filter = ALL_PROPERTIES | SKIP_SYMBOLS;
return ArrayPrototypeFilter(
getOwnNonIndexProperties(obj, filter),
isObjectPrototype ? isNotLegacyObjectPrototypeMethod : isIdentifier);
}
function addCommonWords(completionGroups) {
// Only words which do not yet exist as global property should be added to
// this list.
ArrayPrototypePush(completionGroups, [
'async', 'await', 'break', 'case', 'catch', 'const', 'continue',
'debugger', 'default', 'delete', 'do', 'else', 'export', 'false',
'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'let',
'new', 'null', 'return', 'switch', 'this', 'throw', 'true', 'try',
'typeof', 'var', 'void', 'while', 'with', 'yield',
]);
}
function gracefulReaddir(...args) {
try {
return ReflectApply(fs.readdirSync, null, args);
} catch {
// Continue regardless of error.
}
}
function completeFSFunctions(match) {
let baseName = '';
let filePath = match[1];
let fileList = gracefulReaddir(filePath, { withFileTypes: true });
if (!fileList) {
baseName = path.basename(filePath);
filePath = path.dirname(filePath);
fileList = gracefulReaddir(filePath, { withFileTypes: true }) || [];
}
const completions = ArrayPrototypeMap(
ArrayPrototypeFilter(
fileList,
(dirent) => StringPrototypeStartsWith(dirent.name, baseName),
),
(d) => d.name,
);
return [[completions], baseName];
}
// Provide a list of completions for the given leading text. This is
// given to the readline interface for handling tab completion.
//
// Example:
// complete('let foo = util.')
// -> [['util.print', 'util.debug', 'util.log', 'util.inspect'],
// 'util.' ]
//
// Warning: This evals code like "foo.bar.baz", so it could run property
// getter code. To avoid potential triggering side-effects with getters the completion
// logic is skipped when getters or proxies are involved in the expression.
// (see: https://github.com/nodejs/node/issues/57829).
function complete(line, callback) {
// List of completion lists, one for each inheritance "level"
let completionGroups = [];
let completeOn, group;
// Ignore right whitespace. It could change the outcome.
line = StringPrototypeTrimStart(line);
let filter = '';
let match;
// REPL commands (e.g. ".break").
if ((match = RegExpPrototypeExec(/^\s*\.(\w*)$/, line)) !== null) {
ArrayPrototypePush(completionGroups, ObjectKeys(this.commands));
completeOn = match[1];
if (completeOn.length) {
filter = completeOn;
}
} else if ((match = RegExpPrototypeExec(requireRE, line)) !== null) {
// require('...<Tab>')
completeOn = match[1];
filter = completeOn;
if (this.allowBlockingCompletions) {
const subdir = match[2] || '';
const extensions = ObjectKeys(CJSModule._extensions);
const indexes = ArrayPrototypeMap(extensions,
(extension) => `index${extension}`);
ArrayPrototypePush(indexes, 'package.json', 'index');
group = [];
let paths = [];
if (completeOn === '.') {
group = ['./', '../'];
} else if (completeOn === '..') {
group = ['../'];
} else if (RegExpPrototypeExec(/^\.\.?\//, completeOn) !== null) {
paths = [process.cwd()];
} else {
paths = [];
ArrayPrototypePushApply(paths, module.paths);
ArrayPrototypePushApply(paths, CJSModule.globalPaths);
}
ArrayPrototypeForEach(paths, (dir) => {
dir = path.resolve(dir, subdir);
const dirents = gracefulReaddir(dir, { withFileTypes: true }) || [];
ArrayPrototypeForEach(dirents, (dirent) => {
if (RegExpPrototypeExec(versionedFileNamesRe, dirent.name) !== null ||
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}`);
}
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);
}
}
ArrayPrototypePush(completionGroups, getReplBuiltinLibs(), nodeSchemeBuiltinLibs);
} else if ((match = RegExpPrototypeExec(importRE, line)) !== null) {
// import('...<Tab>')
completeOn = match[1];
filter = completeOn;
if (this.allowBlockingCompletions) {
const subdir = match[2] || '';
// File extensions that can be imported:
const extensions = ObjectKeys(extensionFormatMap);
// Only used when loading bare module specifiers from `node_modules`:
const indexes = ArrayPrototypeMap(extensions, (ext) => `index${ext}`);
ArrayPrototypePush(indexes, 'package.json');
group = [];
let paths = [];
if (completeOn === '.') {
group = ['./', '../'];
} else if (completeOn === '..') {
group = ['../'];
} else if (RegExpPrototypeExec(/^\.\.?\//, completeOn) !== null) {
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 (RegExpPrototypeExec(versionedFileNamesRe, name) !== null ||
name === '.npm') {
// Exclude versioned names that 'npm' installs.
return;
}
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);
}
}
ArrayPrototypePush(completionGroups, getReplBuiltinLibs(), nodeSchemeBuiltinLibs);
} else if ((match = RegExpPrototypeExec(fsAutoCompleteRE, line)) !== null &&
this.allowBlockingCompletions) {
({ 0: completionGroups, 1: completeOn } = completeFSFunctions(match));
} else if (line.length === 0 ||
RegExpPrototypeExec(/\w|\.|\$/, line[line.length - 1]) !== null) {
const completeTarget = line.length === 0 ? line : findExpressionCompleteTarget(line);
if (line.length !== 0 && !completeTarget) {
completionGroupsLoaded();
return;
}
let expr = '';
completeOn = completeTarget;
if (StringPrototypeEndsWith(line, '.')) {
expr = StringPrototypeSlice(completeTarget, 0, -1);
} else if (line.length !== 0) {
const bits = StringPrototypeSplit(completeTarget, '.');
filter = ArrayPrototypePop(bits);
expr = ArrayPrototypeJoin(bits, '.');
}
// Resolve expr and get its completions.
if (!expr) {
// Get global vars synchronously
ArrayPrototypePush(completionGroups,
getGlobalLexicalScopeNames(this[kContextId]));
let contextProto = this.context;
while ((contextProto = ObjectGetPrototypeOf(contextProto)) !== null) {
ArrayPrototypePush(completionGroups,
filteredOwnPropertyNames(contextProto));
}
const contextOwnNames = filteredOwnPropertyNames(this.context);
if (!this.useGlobal) {
// When the context is not `global`, builtins are not own
// properties of it.
// `globalBuiltins` is a `SafeSet`, not an Array-like.
ArrayPrototypePush(contextOwnNames, ...globalBuiltins);
}
ArrayPrototypePush(completionGroups, contextOwnNames);
if (filter !== '') addCommonWords(completionGroups);
completionGroupsLoaded();
return;
}
// If the target ends with a dot (e.g. `obj.foo.`) such code won't be valid for AST parsing
// so in order to make it correct we add an identifier to its end (e.g. `obj.foo.x`)
const parsableCompleteTarget = completeTarget.endsWith('.') ? `${completeTarget}x` : completeTarget;
let completeTargetAst;
try {
completeTargetAst = acornParse(
parsableCompleteTarget, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' },
);
} catch { /* No need to specifically handle parse errors */ }
if (!completeTargetAst) {
return completionGroupsLoaded();
}
return includesProxiesOrGetters(
completeTargetAst.body[0].expression,
parsableCompleteTarget,
this.eval,
this.context,
(includes) => {
if (includes) {
// The expression involves proxies or getters, meaning that it
// can trigger side-effectful behaviors, so bail out
return completionGroupsLoaded();
}
let chaining = '.';
if (StringPrototypeEndsWith(expr, '?')) {
expr = StringPrototypeSlice(expr, 0, -1);
chaining = '?.';
}
const memberGroups = [];
const evalExpr = `try { ${expr} } catch {}`;
this.eval(evalExpr, this.context, getREPLResourceName(), (e, obj) => {
try {
let p;
if ((typeof obj === 'object' && obj !== null) ||
typeof obj === 'function') {
ArrayPrototypePush(memberGroups, filteredOwnPropertyNames(obj));
p = ObjectGetPrototypeOf(obj);
} else {
p = obj.constructor ? obj.constructor.prototype : null;
}
// Circular refs possible? Let's guard against that.
let sentinel = 5;
while (p !== null && sentinel-- !== 0) {
ArrayPrototypePush(memberGroups, filteredOwnPropertyNames(p));
p = ObjectGetPrototypeOf(p);
}
} catch {
// Maybe a Proxy object without `getOwnPropertyNames` trap.
// We simply ignore it here, as we don't want to break the
// autocompletion. Fixes the bug
// https://github.com/nodejs/node/issues/2119
}
if (memberGroups.length) {
expr += chaining;
ArrayPrototypeForEach(memberGroups, (group) => {
ArrayPrototypePush(completionGroups,
ArrayPrototypeMap(group,
(member) => `${expr}${member}`));
});
filter &&= `${expr}${filter}`;
}
completionGroupsLoaded();
});
});
}
return completionGroupsLoaded();
// Will be called when all completionGroups are in place
// Useful for async autocompletion
function completionGroupsLoaded() {
// Filter, sort (within each group), uniq and merge the completion groups.
if (completionGroups.length && filter) {
const newCompletionGroups = [];
const lowerCaseFilter = StringPrototypeToLocaleLowerCase(filter);
ArrayPrototypeForEach(completionGroups, (group) => {
const filteredGroup = ArrayPrototypeFilter(group, (str) => {
// Filter is always case-insensitive following chromium autocomplete
// behavior.
return StringPrototypeStartsWith(
StringPrototypeToLocaleLowerCase(str),
lowerCaseFilter,
);
});
if (filteredGroup.length) {
ArrayPrototypePush(newCompletionGroups, filteredGroup);
}
});
completionGroups = newCompletionGroups;
}
const completions = [];
// Unique completions across all groups.
const uniqueSet = new SafeSet();
uniqueSet.add('');
// Completion group 0 is the "closest" (least far up the inheritance
// chain) so we put its completions last: to be closest in the REPL.
ArrayPrototypeForEach(completionGroups, (group) => {
ArrayPrototypeSort(group, (a, b) => (b > a ? 1 : -1));
const setSize = uniqueSet.size;
ArrayPrototypeForEach(group, (entry) => {
if (!uniqueSet.has(entry)) {
ArrayPrototypeUnshift(completions, entry);
uniqueSet.add(entry);
}
});
// Add a separator between groups.
if (uniqueSet.size !== setSize) {
ArrayPrototypeUnshift(completions, '');
}
});
// Remove obsolete group entry, if present.
if (completions[0] === '') {
ArrayPrototypeShift(completions);
}
callback(null, [completions, completeOn]);
}
}
/**
* This function tries to extract a target for tab completion from code representing an expression.
*
* Such target is basically the last piece of the expression that can be evaluated for the potential
* tab completion.
*
* Some examples:
* - The complete target for `const a = obj.b` is `obj.b`
* (because tab completion will evaluate and check the `obj.b` object)
* - The complete target for `tru` is `tru`
* (since we'd ideally want to complete that to `true`)
* - The complete target for `{ a: tru` is `tru`
* (like the last example, we'd ideally want that to complete to true)
* - There is no complete target for `{ a: true }`
* (there is nothing to complete)
* @param {string} code the code representing the expression to analyze
* @returns {string|null} a substring of the code representing the complete target is there was one, `null` otherwise
*/
function findExpressionCompleteTarget(code) {
if (!code) {
return null;
}
if (code.at(-1) === '.') {
if (code.at(-2) === '?') {
// The code ends with the optional chaining operator (`?.`),
// such code can't generate a valid AST so we need to strip
// the suffix, run this function's logic and add back the
// optional chaining operator to the result if present
const result = findExpressionCompleteTarget(StringPrototypeSlice(code, 0, -2));
return !result ? result : `${result}?.`;
}
// The code ends with a dot, such code can't generate a valid AST
// so we need to strip the suffix, run this function's logic and
// add back the dot to the result if present
const result = findExpressionCompleteTarget(StringPrototypeSlice(code, 0, -1));
return !result ? result : `${result}.`;
}
let ast;
try {
ast = acornParse(code, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' });
} catch {
const keywords = code.split(' ');
if (keywords.length > 1) {
// Something went wrong with the parsing, however this can be due to incomplete code
// (that is for example missing a closing bracket, as for example `{ a: obj.te`), in
// this case we take the last code keyword and try again
// TODO(dario-piotrowicz): make this more robust, right now we only split by spaces
// but that's not always enough, for example it doesn't handle
// this code: `{ a: obj['hello world'].te`
return findExpressionCompleteTarget(keywords.at(-1));
}
// The ast parsing has legitimately failed so we return null
return null;
}
const lastBodyStatement = ast.body[ast.body.length - 1];
if (!lastBodyStatement) {
return null;
}
// If the last statement is a block we know there is not going to be a potential
// completion target (e.g. in `{ a: true }` there is no completion to be done)
if (lastBodyStatement.type === 'BlockStatement') {
return null;
}
// If the last statement is an expression and it has a right side, that's what we
// want to potentially complete on, so let's re-run the function's logic on that
if (lastBodyStatement.type === 'ExpressionStatement' && lastBodyStatement.expression.right) {
const exprRight = lastBodyStatement.expression.right;
const exprRightCode = StringPrototypeSlice(code, exprRight.start, exprRight.end);
return findExpressionCompleteTarget(exprRightCode);
}
// If the last statement is a variable declaration statement the last declaration is
// what we can potentially complete on, so let's re-run the function's logic on that
if (lastBodyStatement.type === 'VariableDeclaration') {
const lastDeclarationInit = lastBodyStatement.declarations.at(-1).init;
if (!lastDeclarationInit) {
// If there is no initialization we can simply return
return null;
}
const lastDeclarationInitCode = StringPrototypeSlice(code, lastDeclarationInit.start, lastDeclarationInit.end);
return findExpressionCompleteTarget(lastDeclarationInitCode);
}
// If the last statement is an expression statement with a unary operator (delete, typeof, etc.)
// we want to extract the argument for completion (e.g. for `delete obj.prop` we want `obj.prop`)
if (lastBodyStatement.type === 'ExpressionStatement' &&
lastBodyStatement.expression.type === 'UnaryExpression' &&
lastBodyStatement.expression.argument) {
const argument = lastBodyStatement.expression.argument;
const argumentCode = StringPrototypeSlice(code, argument.start, argument.end);
return findExpressionCompleteTarget(argumentCode);
}
// If the last statement is an expression statement with "new" syntax
// we want to extract the callee for completion (e.g. for `new Sample` we want `Sample`)
if (lastBodyStatement.type === 'ExpressionStatement' &&
lastBodyStatement.expression.type === 'NewExpression' &&
lastBodyStatement.expression.callee) {
const callee = lastBodyStatement.expression.callee;
const calleeCode = StringPrototypeSlice(code, callee.start, callee.end);
return findExpressionCompleteTarget(calleeCode);
}
// Walk the AST for the current block of code, and check whether it contains any
// statement or expression type that would potentially have side effects if evaluated.
let isAllowed = true;
const disallow = () => isAllowed = false;
acornWalk.simple(lastBodyStatement, {
ForInStatement: disallow,
ForOfStatement: disallow,
CallExpression: disallow,
AssignmentExpression: disallow,
UpdateExpression: disallow,
});
if (!isAllowed) {
return null;
}
// If any of the above early returns haven't activated then it means that
// the potential complete target is the full code (e.g. the code represents
// a simple partial identifier, a member expression, etc...)
return StringPrototypeSlice(code, lastBodyStatement.start, lastBodyStatement.end);
}
/**
* Utility used to determine if an expression includes object getters or proxies.
*
* Example: given `obj.foo`, the function lets you know if `foo` has a getter function
* associated to it, or if `obj` is a proxy
* @param {any} expr The expression, in AST format to analyze
* @param {string} exprStr The string representation of the expression
* @param {(str: string, ctx: any, resourceName: string, cb: (error, evaled) => void) => void} evalFn
* Eval function to use
* @param {any} ctx The context to use for any code evaluation
* @param {(includes: boolean) => void} callback Callback that will be called with the result of the operation
* @returns {void}
*/
function includesProxiesOrGetters(expr, exprStr, evalFn, ctx, callback) {
if (expr?.type !== 'MemberExpression') {
// If the expression is not a member one for obvious reasons no getters are involved
return callback(false);
}
if (expr.object.type === 'MemberExpression') {
// The object itself is a member expression, so we need to recurse (e.g. the expression is `obj.foo.bar`)
return includesProxiesOrGetters(
expr.object,
StringPrototypeSlice(exprStr, 0, expr.object.end),
evalFn,
ctx,
(includes, lastEvaledObj) => {
if (includes) {
// If the recurred call found a getter we can also terminate
return callback(includes);
}
if (isProxy(lastEvaledObj)) {
return callback(true);
}
// If a getter/proxy hasn't been found by the recursion call we need to check if maybe a getter/proxy
// is present here (e.g. in `obj.foo.bar` we found that `obj.foo` doesn't involve any getters so we now
// need to check if `bar` on `obj.foo` (i.e. `lastEvaledObj`) has a getter or if `obj.foo.bar` is a proxy)
return hasGetterOrIsProxy(lastEvaledObj, expr.property, (doesHaveGetterOrIsProxy) => {
return callback(doesHaveGetterOrIsProxy);
});
},
);
}
// This is the base of the recursion we have an identifier for the object and an identifier or literal
// for the property (e.g. we have `obj.foo` or `obj['foo']`, `obj` is the object identifier and `foo`
// is the property identifier/literal)
if (expr.object.type === 'Identifier') {
return evalFn(`try { ${expr.object.name} } catch {}`, ctx, getREPLResourceName(), (err, obj) => {
if (err) {
return callback(false);
}
if (isProxy(obj)) {
return callback(true);
}
return hasGetterOrIsProxy(obj, expr.property, (doesHaveGetterOrIsProxy) => {
if (doesHaveGetterOrIsProxy) {
return callback(true);
}
return evalFn(
`try { ${exprStr} } catch {} `, ctx, getREPLResourceName(), (err, obj) => {
if (err) {
return callback(false);
}
return callback(false, obj);
});
});
});
}
/**
* Utility to see if a property has a getter associated to it or if
* the property itself is a proxy object.
* @returns {void}
*/
function hasGetterOrIsProxy(obj, astProp, cb) {
if (!obj || !astProp) {
return cb(false);
}
if (astProp.type === 'Literal') {
// We have something like `obj['foo'].x` where `x` is the literal
return propHasGetterOrIsProxy(obj, astProp.value, cb);
}
if (
astProp.type === 'Identifier' &&
exprStr.at(astProp.start - 1) === '.'
) {
// We have something like `obj.foo.x` where `foo` is the identifier
return propHasGetterOrIsProxy(obj, astProp.name, cb);
}
return evalFn(
// Note: this eval runs the property expression, which might be side-effectful, for example
// the user could be running `obj[getKey()].` where `getKey()` has some side effects.
// Arguably this behavior should not be too surprising, but if it turns out that it is,
// then we can revisit this behavior and add logic to analyze the property expression
// and eval it only if we can confidently say that it can't have any side effects
`try { ${StringPrototypeSlice(exprStr, astProp.start, astProp.end)} } catch {} `,
ctx,
getREPLResourceName(),
(err, evaledProp) => {
if (err) {
return cb(false);
}
if (typeof evaledProp === 'string') {
return propHasGetterOrIsProxy(obj, evaledProp, cb);
}
return cb(false);
},
);
}
return callback(false);
}
/**
* Given an object and a property name, checks whether the property has a getter, if not checks whether its
* value is a proxy.
*
* Note: the order is relevant here, we want to check whether the property has a getter _before_ we check
* whether its value is a proxy, to ensure that is the property does have a getter we don't end up
* triggering it when checking its value
* @param {any} obj The target object
* @param {string | number | bigint | boolean | RegExp} prop The target property
* @param {(includes: boolean) => void} cb Callback that will be called with the result of the operation
* @returns {void}
*/
function propHasGetterOrIsProxy(obj, prop, cb) {
const propDescriptor = ObjectGetOwnPropertyDescriptor(
obj,
prop,
);
const propHasGetter = typeof propDescriptor?.get === 'function';
if (propHasGetter) {
return cb(true);
}
if (isProxy(obj[prop])) {
return cb(true);
}
return cb(false);
}
module.exports = {
complete,
};