MCPcopy Index your code
hub / github.com/nodejs/node / findExpressionCompleteTarget

Function findExpressionCompleteTarget

lib/internal/repl/completion.js:532–645  ·  view source on GitHub ↗

* 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`

(code)

Source from the content-addressed store, hash-verified

530 * @returns {string|null} a substring of the code representing the complete target is there was one, `null` otherwise
531 */
532function findExpressionCompleteTarget(code) {
533 if (!code) {
534 return null;
535 }
536
537 if (code.at(-1) === '.') {
538 if (code.at(-2) === '?') {
539 // The code ends with the optional chaining operator (`?.`),
540 // such code can't generate a valid AST so we need to strip
541 // the suffix, run this function's logic and add back the
542 // optional chaining operator to the result if present
543 const result = findExpressionCompleteTarget(code.slice(0, -2));
544 return !result ? result : `${result}?.`;
545 }
546
547 // The code ends with a dot, such code can't generate a valid AST
548 // so we need to strip the suffix, run this function's logic and
549 // add back the dot to the result if present
550 const result = findExpressionCompleteTarget(code.slice(0, -1));
551 return !result ? result : `${result}.`;
552 }
553
554 let ast;
555 try {
556 ast = acornParse(code, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' });
557 } catch {
558 const keywords = code.split(' ');
559
560 if (keywords.length > 1) {
561 // Something went wrong with the parsing, however this can be due to incomplete code
562 // (that is for example missing a closing bracket, as for example `{ a: obj.te`), in
563 // this case we take the last code keyword and try again
564 // TODO(dario-piotrowicz): make this more robust, right now we only split by spaces
565 // but that's not always enough, for example it doesn't handle
566 // this code: `{ a: obj['hello world'].te`
567 return findExpressionCompleteTarget(keywords.at(-1));
568 }
569
570 // The ast parsing has legitimately failed so we return null
571 return null;
572 }
573
574 const lastBodyStatement = ast.body[ast.body.length - 1];
575
576 if (!lastBodyStatement) {
577 return null;
578 }
579
580 // If the last statement is a block we know there is not going to be a potential
581 // completion target (e.g. in `{ a: true }` there is no completion to be done)
582 if (lastBodyStatement.type === 'BlockStatement') {
583 return null;
584 }
585
586 // If the last statement is an expression and it has a right side, that's what we
587 // want to potentially complete on, so let's re-run the function's logic on that
588 if (lastBodyStatement.type === 'ExpressionStatement' && lastBodyStatement.expression.right) {
589 const exprRight = lastBodyStatement.expression.right;

Callers 1

completeFunction · 0.85

Calls 3

sliceMethod · 0.65
atMethod · 0.45
splitMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…