@searchspring/snap-toolbox
Advanced tools
Comparing version 0.63.0 to 0.63.1
@@ -15,2 +15,47 @@ "use strict"; | ||
exports.getContext = void 0; | ||
var JAVASCRIPT_KEYWORDS = new Set([ | ||
'break', | ||
'case', | ||
'catch', | ||
'class', | ||
'const', | ||
'continue', | ||
'debugger', | ||
'default', | ||
'delete', | ||
'do', | ||
'else', | ||
'export', | ||
'extends', | ||
'finally', | ||
'for', | ||
'function', | ||
'if', | ||
'import', | ||
'in', | ||
'instanceof', | ||
'new', | ||
'return', | ||
'super', | ||
'switch', | ||
'this', | ||
'throw', | ||
'try', | ||
'typeof', | ||
'var', | ||
'void', | ||
'while', | ||
'with', | ||
'yield', | ||
'let', | ||
'static', | ||
'enum', | ||
'await', | ||
'implements', | ||
'package', | ||
'protected', | ||
'interface', | ||
'private', | ||
'public', | ||
]); | ||
function getContext(evaluate, script) { | ||
@@ -53,12 +98,32 @@ var _a, _b, _c, _d, _e; | ||
// attempt to grab inner HTML variables | ||
var scriptInnerVars = (_d = scriptInnerHTML.match(/([a-zA-Z_$][a-zA-Z_$0-9]*)\s?=/g)) === null || _d === void 0 ? void 0 : _d.map(function (match) { return match.replace(/[\s=]/g, ''); }); | ||
var scriptInnerVars = (_d = scriptInnerHTML | ||
// first remove all string literals (including template literals) to avoid false matches | ||
.replace(/`(?:\\[\s\S]|[^`\\])*`|'(?:\\[\s\S]|[^'\\])*'|"(?:\\[\s\S]|[^"\\])*"/g, '') | ||
// then find variable assignments | ||
.match(/([a-zA-Z_$][a-zA-Z_$0-9]*)\s*=/g)) === null || _d === void 0 ? void 0 : _d.map(function (match) { return match.replace(/[\s=]/g, ''); }); | ||
var combinedVars = evaluate.concat(scriptInnerVars || []); | ||
// de-dupe vars | ||
var evaluateVars = combinedVars.filter(function (item, index) { | ||
return combinedVars.indexOf(item) === index; | ||
var isKeyword = JAVASCRIPT_KEYWORDS.has(item); | ||
// console error if keyword | ||
if (isKeyword) { | ||
console.error("getContext: JavaScript keyword found: '".concat(item, "'! Please use a different variable name.")); | ||
} | ||
return combinedVars.indexOf(item) === index && !isKeyword; | ||
}); | ||
// evaluate text and put into variables | ||
evaluate === null || evaluate === void 0 ? void 0 : evaluate.forEach(function (name) { | ||
var fn = new Function("\n\t\t\tvar ".concat(evaluateVars.join(', '), ";\n\t\t\t").concat(scriptInnerHTML, "\n\t\t\treturn ").concat(name, ";\n\t\t")); | ||
scriptVariables[name] = fn(); | ||
try { | ||
var fn = new Function("\n\t\t\t\tvar ".concat(evaluateVars.join(', '), ";\n\t\t\t\t").concat(scriptInnerHTML, "\n\t\t\t\treturn ").concat(name, ";\n\t\t\t")); | ||
scriptVariables[name] = fn(); | ||
} | ||
catch (err) { | ||
// if evaluation fails, set to undefined | ||
var isKeyword = JAVASCRIPT_KEYWORDS.has(name); | ||
if (!isKeyword) { | ||
console.error("getContext: error evaluating '".concat(name, "'")); | ||
console.error(err); | ||
} | ||
scriptVariables[name] = undefined; | ||
} | ||
}); | ||
@@ -65,0 +130,0 @@ var variables = __assign(__assign({}, removeUndefined(attributeVariables)), removeUndefined(scriptVariables)); |
@@ -0,1 +1,46 @@ | ||
const JAVASCRIPT_KEYWORDS = new Set([ | ||
'break', | ||
'case', | ||
'catch', | ||
'class', | ||
'const', | ||
'continue', | ||
'debugger', | ||
'default', | ||
'delete', | ||
'do', | ||
'else', | ||
'export', | ||
'extends', | ||
'finally', | ||
'for', | ||
'function', | ||
'if', | ||
'import', | ||
'in', | ||
'instanceof', | ||
'new', | ||
'return', | ||
'super', | ||
'switch', | ||
'this', | ||
'throw', | ||
'try', | ||
'typeof', | ||
'var', | ||
'void', | ||
'while', | ||
'with', | ||
'yield', | ||
'let', | ||
'static', | ||
'enum', | ||
'await', | ||
'implements', | ||
'package', | ||
'protected', | ||
'interface', | ||
'private', | ||
'public', | ||
]); | ||
export function getContext(evaluate = [], script) { | ||
@@ -36,16 +81,37 @@ if (!script || typeof script === 'string') { | ||
// attempt to grab inner HTML variables | ||
const scriptInnerVars = scriptInnerHTML.match(/([a-zA-Z_$][a-zA-Z_$0-9]*)\s?=/g)?.map((match) => match.replace(/[\s=]/g, '')); | ||
const scriptInnerVars = scriptInnerHTML | ||
// first remove all string literals (including template literals) to avoid false matches | ||
.replace(/`(?:\\[\s\S]|[^`\\])*`|'(?:\\[\s\S]|[^'\\])*'|"(?:\\[\s\S]|[^"\\])*"/g, '') | ||
// then find variable assignments | ||
.match(/([a-zA-Z_$][a-zA-Z_$0-9]*)\s*=/g) | ||
?.map((match) => match.replace(/[\s=]/g, '')); | ||
const combinedVars = evaluate.concat(scriptInnerVars || []); | ||
// de-dupe vars | ||
const evaluateVars = combinedVars.filter((item, index) => { | ||
return combinedVars.indexOf(item) === index; | ||
const isKeyword = JAVASCRIPT_KEYWORDS.has(item); | ||
// console error if keyword | ||
if (isKeyword) { | ||
console.error(`getContext: JavaScript keyword found: '${item}'! Please use a different variable name.`); | ||
} | ||
return combinedVars.indexOf(item) === index && !isKeyword; | ||
}); | ||
// evaluate text and put into variables | ||
evaluate?.forEach((name) => { | ||
const fn = new Function(` | ||
var ${evaluateVars.join(', ')}; | ||
${scriptInnerHTML} | ||
return ${name}; | ||
`); | ||
scriptVariables[name] = fn(); | ||
try { | ||
const fn = new Function(` | ||
var ${evaluateVars.join(', ')}; | ||
${scriptInnerHTML} | ||
return ${name}; | ||
`); | ||
scriptVariables[name] = fn(); | ||
} | ||
catch (err) { | ||
// if evaluation fails, set to undefined | ||
const isKeyword = JAVASCRIPT_KEYWORDS.has(name); | ||
if (!isKeyword) { | ||
console.error(`getContext: error evaluating '${name}'`); | ||
console.error(err); | ||
} | ||
scriptVariables[name] = undefined; | ||
} | ||
}); | ||
@@ -52,0 +118,0 @@ const variables = { |
{ | ||
"name": "@searchspring/snap-toolbox", | ||
"version": "0.63.0", | ||
"version": "0.63.1", | ||
"description": "Snap Toolbox", | ||
@@ -26,3 +26,3 @@ "main": "dist/cjs/index.js", | ||
], | ||
"gitHead": "46278e19380f22e90fdff4afc01359be27feb5a8" | ||
"gitHead": "2feab8c22416a48cb0c9b76b9413c6a0e3ba774c" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
79446
1627