2011-10-29 04:20:49 -04:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
2023-07-12 19:52:06 -04:00
|
|
|
const AsyncFunction = async function () {}.constructor;
|
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* Returns a list of potential completions for text. Completions either
|
|
|
|
* follow a dot (e.g. foo.ba -> bar) or they are picked from globalCompletionList (e.g. fo -> foo)
|
|
|
|
* commandHeader is prefixed on any expression before it is eval'ed. It will most likely
|
|
|
|
* consist of global constants that might not carry over from the calling environment.
|
|
|
|
*
|
|
|
|
* This function is likely the one you want to call from external modules
|
|
|
|
*
|
|
|
|
* @param {string} text
|
|
|
|
* @param {string} commandHeader
|
|
|
|
* @param {readonly string[]} [globalCompletionList]
|
|
|
|
*/
|
2023-07-10 05:53:00 -04:00
|
|
|
export async function getCompletions(text, commandHeader, globalCompletionList) {
|
2011-10-29 04:20:49 -04:00
|
|
|
let methods = [];
|
2019-01-31 09:08:00 -05:00
|
|
|
let expr_, base;
|
2011-10-29 04:20:49 -04:00
|
|
|
let attrHead = '';
|
2019-08-19 20:51:42 -04:00
|
|
|
if (globalCompletionList == null)
|
2011-10-29 04:20:49 -04:00
|
|
|
globalCompletionList = [];
|
|
|
|
|
|
|
|
let offset = getExpressionOffset(text, text.length - 1);
|
|
|
|
if (offset >= 0) {
|
|
|
|
text = text.slice(offset);
|
|
|
|
|
|
|
|
// Look for expressions like "Main.panel.foo" and match Main.panel and foo
|
|
|
|
let matches = text.match(/(.*)\.(.*)/);
|
|
|
|
if (matches) {
|
2019-01-31 09:08:00 -05:00
|
|
|
[expr_, base, attrHead] = matches;
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2023-07-12 19:52:06 -04:00
|
|
|
methods = (await getPropertyNamesFromExpression(base, commandHeader)).filter(
|
2020-04-03 19:52:29 -04:00
|
|
|
attr => attr.slice(0, attrHead.length) === attrHead);
|
2011-10-29 04:20:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the empty expression or partially entered words
|
|
|
|
// not proceeded by a dot and match them against global constants
|
|
|
|
matches = text.match(/^(\w*)$/);
|
2023-08-06 20:51:19 -04:00
|
|
|
if (text === '' || matches) {
|
2019-01-31 09:08:00 -05:00
|
|
|
[expr_, attrHead] = matches;
|
2017-10-30 20:38:18 -04:00
|
|
|
methods = globalCompletionList.filter(
|
2020-04-03 19:52:29 -04:00
|
|
|
attr => attr.slice(0, attrHead.length) === attrHead);
|
2011-10-29 04:20:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [methods, attrHead];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* A few functions for parsing strings of javascript code.
|
|
|
|
*/
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* Identify characters that delimit an expression. That is,
|
|
|
|
* if we encounter anything that isn't a letter, '.', ')', or ']',
|
|
|
|
* we should stop parsing.
|
|
|
|
*
|
|
|
|
* @param {string} c
|
|
|
|
*/
|
2011-10-29 04:20:49 -04:00
|
|
|
function isStopChar(c) {
|
2019-01-29 17:44:38 -05:00
|
|
|
return !c.match(/[\w.)\]]/);
|
2011-10-29 04:20:49 -04:00
|
|
|
}
|
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* Given the ending position of a quoted string, find where it starts
|
|
|
|
*
|
|
|
|
* @param {string} expr
|
|
|
|
* @param {number} offset
|
|
|
|
*/
|
2023-07-10 05:53:00 -04:00
|
|
|
export function findMatchingQuote(expr, offset) {
|
2011-10-29 04:20:49 -04:00
|
|
|
let quoteChar = expr.charAt(offset);
|
|
|
|
for (let i = offset - 1; i >= 0; --i) {
|
2023-08-06 20:51:19 -04:00
|
|
|
if (expr.charAt(i) === quoteChar && expr.charAt(i - 1) !== '\\')
|
2011-10-29 04:20:49 -04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* Given the ending position of a regex, find where it starts
|
|
|
|
*
|
|
|
|
* @param {string} expr
|
|
|
|
* @param {number} offset
|
|
|
|
*/
|
2023-07-10 05:53:00 -04:00
|
|
|
export function findMatchingSlash(expr, offset) {
|
2011-10-29 04:20:49 -04:00
|
|
|
for (let i = offset - 1; i >= 0; --i) {
|
2023-08-06 20:51:19 -04:00
|
|
|
if (expr.charAt(i) === '/' && expr.charAt(i - 1) !== '\\')
|
2011-10-29 04:20:49 -04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* If expr.charAt(offset) is ')' or ']',
|
|
|
|
* return the position of the corresponding '(' or '[' bracket.
|
|
|
|
* This function does not check for syntactic correctness. e.g.,
|
|
|
|
* findMatchingBrace("[(])", 3) returns 1.
|
|
|
|
*
|
|
|
|
* @param {string} expr
|
|
|
|
* @param {number} offset
|
|
|
|
*/
|
2023-07-10 05:53:00 -04:00
|
|
|
export function findMatchingBrace(expr, offset) {
|
2011-10-29 04:20:49 -04:00
|
|
|
let closeBrace = expr.charAt(offset);
|
2023-08-06 18:40:20 -04:00
|
|
|
let openBrace = {')': '(', ']': '['}[closeBrace];
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2019-08-19 20:24:28 -04:00
|
|
|
return findTheBrace(expr, offset - 1, openBrace, closeBrace);
|
|
|
|
}
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* @param {*} expr
|
|
|
|
* @param {*} offset
|
|
|
|
* @param {...any} braces
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
2023-07-10 05:53:00 -04:00
|
|
|
export function findTheBrace(expr, offset, ...braces) {
|
2019-08-19 20:24:28 -04:00
|
|
|
let [openBrace, closeBrace] = braces;
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2019-08-19 20:24:28 -04:00
|
|
|
if (offset < 0)
|
|
|
|
return -1;
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2023-08-06 20:51:19 -04:00
|
|
|
if (expr.charAt(offset) === openBrace)
|
2019-08-19 20:24:28 -04:00
|
|
|
return offset;
|
|
|
|
|
|
|
|
if (expr.charAt(offset).match(/['"]/))
|
|
|
|
return findTheBrace(expr, findMatchingQuote(expr, offset) - 1, ...braces);
|
|
|
|
|
2023-08-06 20:51:19 -04:00
|
|
|
if (expr.charAt(offset) === '/')
|
2019-08-19 20:24:28 -04:00
|
|
|
return findTheBrace(expr, findMatchingSlash(expr, offset) - 1, ...braces);
|
|
|
|
|
2023-08-06 20:51:19 -04:00
|
|
|
if (expr.charAt(offset) === closeBrace)
|
2019-08-19 20:24:28 -04:00
|
|
|
return findTheBrace(expr, findTheBrace(expr, offset - 1, ...braces) - 1, ...braces);
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2019-08-19 20:24:28 -04:00
|
|
|
return findTheBrace(expr, offset - 1, ...braces);
|
2011-10-29 04:20:49 -04:00
|
|
|
}
|
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* Walk expr backwards from offset looking for the beginning of an
|
|
|
|
* expression suitable for passing to eval.
|
|
|
|
* There is no guarantee of correct javascript syntax between the return
|
|
|
|
* value and offset. This function is meant to take a string like
|
|
|
|
* "foo(Obj.We.Are.Completing" and allow you to extract "Obj.We.Are.Completing"
|
|
|
|
*
|
|
|
|
* @param {string} expr
|
|
|
|
* @param {number} offset
|
|
|
|
*/
|
2023-07-10 05:53:00 -04:00
|
|
|
export function getExpressionOffset(expr, offset) {
|
2011-10-29 04:20:49 -04:00
|
|
|
while (offset >= 0) {
|
|
|
|
let currChar = expr.charAt(offset);
|
|
|
|
|
2019-08-19 20:51:42 -04:00
|
|
|
if (isStopChar(currChar))
|
2011-10-29 04:20:49 -04:00
|
|
|
return offset + 1;
|
|
|
|
|
2019-08-19 20:51:42 -04:00
|
|
|
if (currChar.match(/[)\]]/))
|
2011-10-29 04:20:49 -04:00
|
|
|
offset = findMatchingBrace(expr, offset);
|
|
|
|
|
|
|
|
--offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset + 1;
|
|
|
|
}
|
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* Things with non-word characters or that start with a number
|
|
|
|
* are not accessible via .foo notation and so aren't returned
|
|
|
|
*
|
|
|
|
* @param {string} w
|
|
|
|
*/
|
2011-10-29 04:20:49 -04:00
|
|
|
function isValidPropertyName(w) {
|
|
|
|
return !(w.match(/\W/) || w.match(/^\d/));
|
|
|
|
}
|
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* To get all properties (enumerable and not), we need to walk
|
|
|
|
* the prototype chain ourselves
|
|
|
|
*
|
|
|
|
* @param {object} obj
|
|
|
|
*/
|
2023-07-10 05:53:00 -04:00
|
|
|
export function getAllProps(obj) {
|
2019-08-19 20:51:42 -04:00
|
|
|
if (obj === null || obj === undefined)
|
2011-10-29 04:20:49 -04:00
|
|
|
return [];
|
2019-08-19 20:51:42 -04:00
|
|
|
|
2019-08-19 13:55:49 -04:00
|
|
|
return Object.getOwnPropertyNames(obj).concat(getAllProps(Object.getPrototypeOf(obj)));
|
2011-10-29 04:20:49 -04:00
|
|
|
}
|
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* Given a string _expr_, returns all methods
|
|
|
|
* that can be accessed via '.' notation.
|
|
|
|
* e.g., expr="({ foo: null, bar: null, 4: null })" will
|
|
|
|
* return ["foo", "bar", ...] but the list will not include "4",
|
|
|
|
* since methods accessed with '.' notation must star with a letter or _.
|
|
|
|
*
|
|
|
|
* @param {string} expr
|
|
|
|
* @param {string=} commandHeader
|
|
|
|
*/
|
2023-07-10 05:53:00 -04:00
|
|
|
export async function getPropertyNamesFromExpression(expr, commandHeader = '') {
|
2011-10-29 04:20:49 -04:00
|
|
|
let obj = {};
|
|
|
|
if (!isUnsafeExpression(expr)) {
|
|
|
|
try {
|
2023-07-12 19:52:06 -04:00
|
|
|
const lines = expr.split('\n');
|
2023-07-12 19:52:06 -04:00
|
|
|
lines.push(`return ${lines.pop()}`);
|
2023-07-12 19:52:06 -04:00
|
|
|
obj = await AsyncFunction(commandHeader + lines.join(';'))();
|
2011-10-29 04:20:49 -04:00
|
|
|
} catch (e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
let propsUnique = {};
|
2019-01-28 20:27:05 -05:00
|
|
|
if (typeof obj === 'object') {
|
2011-10-29 04:20:49 -04:00
|
|
|
let allProps = getAllProps(obj);
|
|
|
|
// Get only things we are allowed to complete following a '.'
|
2019-08-19 13:55:49 -04:00
|
|
|
allProps = allProps.filter(isValidPropertyName);
|
2011-10-29 04:20:49 -04:00
|
|
|
|
|
|
|
// Make sure propsUnique contains one key for every
|
|
|
|
// property so we end up with a unique list of properties
|
2019-08-19 16:20:35 -04:00
|
|
|
allProps.map(p => (propsUnique[p] = null));
|
2011-10-29 04:20:49 -04:00
|
|
|
}
|
|
|
|
return Object.keys(propsUnique).sort();
|
|
|
|
}
|
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* Given a list of words, returns the longest prefix they all have in common
|
|
|
|
*
|
|
|
|
* @param {readonly string[]} words
|
|
|
|
*/
|
2023-07-10 05:53:00 -04:00
|
|
|
export function getCommonPrefix(words) {
|
2011-10-29 04:20:49 -04:00
|
|
|
let word = words[0];
|
|
|
|
for (let i = 0; i < word.length; i++) {
|
|
|
|
for (let w = 1; w < words.length; w++) {
|
2023-08-06 20:51:19 -04:00
|
|
|
if (words[w].charAt(i) !== word.charAt(i))
|
2011-10-29 04:20:49 -04:00
|
|
|
return word.slice(0, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return word;
|
|
|
|
}
|
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* Remove any blocks that are quoted or are in a regex
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
*/
|
2023-07-10 05:53:00 -04:00
|
|
|
export function removeLiterals(str) {
|
2023-08-06 20:51:19 -04:00
|
|
|
if (str.length === 0)
|
2019-08-19 20:24:28 -04:00
|
|
|
return '';
|
|
|
|
|
|
|
|
let currChar = str.charAt(str.length - 1);
|
2023-08-06 20:51:19 -04:00
|
|
|
if (currChar === '"' || currChar === '\'') {
|
2019-08-19 20:24:28 -04:00
|
|
|
return removeLiterals(
|
|
|
|
str.slice(0, findMatchingQuote(str, str.length - 1)));
|
2023-08-06 20:51:19 -04:00
|
|
|
} else if (currChar === '/') {
|
2019-08-19 20:24:28 -04:00
|
|
|
return removeLiterals(
|
|
|
|
str.slice(0, findMatchingSlash(str, str.length - 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return removeLiterals(str.slice(0, str.length - 1)) + currChar;
|
|
|
|
}
|
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* Returns true if there is reason to think that eval(str)
|
|
|
|
* will modify the global scope
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
*/
|
2023-07-10 05:53:00 -04:00
|
|
|
export function isUnsafeExpression(str) {
|
2011-10-29 04:20:49 -04:00
|
|
|
// Check for any sort of assignment
|
|
|
|
// The strategy used is dumb: remove any quotes
|
|
|
|
// or regexs and comparison operators and see if there is an '=' character.
|
|
|
|
// If there is, it might be an unsafe assignment.
|
|
|
|
|
|
|
|
let prunedStr = removeLiterals(str);
|
2019-08-19 13:55:49 -04:00
|
|
|
prunedStr = prunedStr.replace(/[=!]==/g, ''); // replace === and !== with nothing
|
|
|
|
prunedStr = prunedStr.replace(/[=<>!]=/g, ''); // replace ==, <=, >=, != with nothing
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2019-08-20 17:20:31 -04:00
|
|
|
if (prunedStr.match(/[=]/)) {
|
2011-10-29 04:20:49 -04:00
|
|
|
return true;
|
|
|
|
} else if (prunedStr.match(/;/)) {
|
|
|
|
// If we contain a semicolon not inside of a quote/regex, assume we're unsafe as well
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-10 05:25:01 -04:00
|
|
|
/**
|
|
|
|
* Returns a list of global keywords derived from str
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
*/
|
2023-07-10 05:53:00 -04:00
|
|
|
export function getDeclaredConstants(str) {
|
2011-10-29 04:20:49 -04:00
|
|
|
let ret = [];
|
2017-10-30 20:38:18 -04:00
|
|
|
str.split(';').forEach(s => {
|
2019-01-31 09:08:00 -05:00
|
|
|
let base_, keyword;
|
2011-10-29 04:20:49 -04:00
|
|
|
let match = s.match(/const\s+(\w+)\s*=/);
|
|
|
|
if (match) {
|
2019-01-31 09:08:00 -05:00
|
|
|
[base_, keyword] = match;
|
2011-10-29 04:20:49 -04:00
|
|
|
ret.push(keyword);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|