import from UXP: Issue #1658 - Part 7: Implement support for optional chaining in console autocomplete (c8d825af)

This commit is contained in:
2022-05-07 01:27:29 +08:00
parent 25603675ea
commit 293a85d901
2 changed files with 48 additions and 0 deletions
@@ -179,6 +179,22 @@ function JSPropertyProvider(dbgObject, anEnvironment, inputValue, cursor) {
}
let completionPart = inputValue.substring(beginning.startPos);
// Strip optional chaining characters from completion part, which we check
// by looking if it has ?. and if there are no digits (marker for ternary).
let optionalChainRegex = /\?\./g;
let optionalElemAccessRegex = /\?\.\[/g;
let digitRegex = /\?\.\d/;
// Handle optional element access
if (optionalElemAccessRegex.test(completionPart)) {
completionPart = completionPart.replace(optionalElemAccessRegex, "[");
}
// Handle optional chaining characters
if (optionalChainRegex.test(completionPart) &&
!digitRegex.test(completionPart)) {
completionPart = completionPart.replace(optionalChainRegex, ".");
}
let lastDot = completionPart.lastIndexOf(".");
// Don't complete on just an empty string.
@@ -145,6 +145,38 @@ function runChecks(dbgObject, dbgEnv) {
do_print("Test that suggestions are not given if there is an hyphen in the chain.");
results = JSPropertyProvider(dbgObject, dbgEnv, "testHyphenated['prop-A'].");
do_check_null(results);
do_print("Test that expression with optional chaining operator are completed");
results = JSPropertyProvider(dbgObject, dbgEnv, "testObject?.prop");
test_has_result(results, "propA");
results = JSPropertyProvider(dbgObject, dbgEnv, "testObject?.propA[0]?.propB?.to");
test_has_result(results, "toString");
results = JSPropertyProvider(dbgObject, dbgEnv, "testObject?.propA?.[0]?.propB?.to");
test_has_result(results, "toString");
results = JSPropertyProvider(dbgObject, dbgEnv, "[1,2,3]?.");
test_has_result(results, "indexOf");
results = JSPropertyProvider(dbgObject, dbgEnv, "'foo'?.");
test_has_result(results, "charAt");
// Check this doesn't throw since `propC` is not defined.
results = JSPropertyProvider(dbgObject, dbgEnv, "testObject?.propC?.this?.does?.not?.exist?.d");
// Test more ternary
results = JSPropertyProvider(dbgObject, dbgEnv, "true ? t");
test_has_result(results, "testObject");
results = JSPropertyProvider(dbgObject, dbgEnv, "true ?? t");
test_has_result(results, "testObject");
results = JSPropertyProvider(dbgObject, dbgEnv, "true ? /* comment */ t");
test_has_result(results, "testObject");
results = JSPropertyProvider(dbgObject, dbgEnv, "true?<t");
test_has_no_results(results);
}
/**