jsParse: Unnest functions
Nesting functions can be helpful for private helper functions, but here they are accessing some variables from the outer scope and shadowing others. Split them out to avoid any ambiguity. https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/805
This commit is contained in:
parent
fea5ecc9e8
commit
bef5043135
@ -84,29 +84,28 @@ function findMatchingBrace(expr, offset) {
|
|||||||
let closeBrace = expr.charAt(offset);
|
let closeBrace = expr.charAt(offset);
|
||||||
let openBrace = ({ ')': '(', ']': '[' })[closeBrace];
|
let openBrace = ({ ')': '(', ']': '[' })[closeBrace];
|
||||||
|
|
||||||
function findTheBrace(expr, offset) {
|
return findTheBrace(expr, offset - 1, openBrace, closeBrace);
|
||||||
if (offset < 0) {
|
}
|
||||||
|
|
||||||
|
function findTheBrace(expr, offset, ...braces) {
|
||||||
|
let [openBrace, closeBrace] = braces;
|
||||||
|
|
||||||
|
if (offset < 0)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
if (expr.charAt(offset) == openBrace) {
|
if (expr.charAt(offset) == openBrace)
|
||||||
return offset;
|
return offset;
|
||||||
}
|
|
||||||
if (expr.charAt(offset).match(/['"]/)) {
|
|
||||||
return findTheBrace(expr, findMatchingQuote(expr, offset) - 1);
|
|
||||||
}
|
|
||||||
if (expr.charAt(offset) == '/') {
|
|
||||||
return findTheBrace(expr, findMatchingSlash(expr, offset) - 1);
|
|
||||||
}
|
|
||||||
if (expr.charAt(offset) == closeBrace) {
|
|
||||||
return findTheBrace(expr, findTheBrace(expr, offset - 1) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return findTheBrace(expr, offset - 1);
|
if (expr.charAt(offset).match(/['"]/))
|
||||||
|
return findTheBrace(expr, findMatchingQuote(expr, offset) - 1, ...braces);
|
||||||
|
|
||||||
}
|
if (expr.charAt(offset) == '/')
|
||||||
|
return findTheBrace(expr, findMatchingSlash(expr, offset) - 1, ...braces);
|
||||||
|
|
||||||
return findTheBrace(expr, offset - 1);
|
if (expr.charAt(offset) == closeBrace)
|
||||||
|
return findTheBrace(expr, findTheBrace(expr, offset - 1, ...braces) - 1, ...braces);
|
||||||
|
|
||||||
|
return findTheBrace(expr, offset - 1, ...braces);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Walk expr backwards from offset looking for the beginning of an
|
// Walk expr backwards from offset looking for the beginning of an
|
||||||
@ -189,24 +188,26 @@ function getCommonPrefix(words) {
|
|||||||
return word;
|
return word;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if there is reason to think that eval(str)
|
// Remove any blocks that are quoted or are in a regex
|
||||||
// will modify the global scope
|
function removeLiterals(str) {
|
||||||
function isUnsafeExpression(str) {
|
if (str.length == 0)
|
||||||
// Remove any blocks that are quoted or are in a regex
|
|
||||||
function removeLiterals(str) {
|
|
||||||
if (str.length == 0) {
|
|
||||||
return '';
|
return '';
|
||||||
}
|
|
||||||
|
|
||||||
let currChar = str.charAt(str.length - 1);
|
let currChar = str.charAt(str.length - 1);
|
||||||
if (currChar == '"' || currChar == '\'') {
|
if (currChar == '"' || currChar == '\'') {
|
||||||
return removeLiterals(str.slice(0, findMatchingQuote(str, str.length - 1)));
|
return removeLiterals(
|
||||||
|
str.slice(0, findMatchingQuote(str, str.length - 1)));
|
||||||
} else if (currChar == '/') {
|
} else if (currChar == '/') {
|
||||||
return removeLiterals(str.slice(0, findMatchingSlash(str, str.length - 1)));
|
return removeLiterals(
|
||||||
|
str.slice(0, findMatchingSlash(str, str.length - 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return removeLiterals(str.slice(0, str.length - 1)) + currChar;
|
return removeLiterals(str.slice(0, str.length - 1)) + currChar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns true if there is reason to think that eval(str)
|
||||||
|
// will modify the global scope
|
||||||
|
function isUnsafeExpression(str) {
|
||||||
|
|
||||||
// Check for any sort of assignment
|
// Check for any sort of assignment
|
||||||
// The strategy used is dumb: remove any quotes
|
// The strategy used is dumb: remove any quotes
|
||||||
|
Loading…
Reference in New Issue
Block a user