[gnome-shell/wip/fmuellner/es6-class-preparations: 16/18] lookingGlass: Stop using eval()



commit 467b7c1bcacebdcb90ba916651b5bcc897865e20
Author: Florian Müllner <fmuellner gnome org>
Date:   Fri Nov 30 16:57:47 2018 +0100

    lookingGlass: Stop using eval()
    
    Its use is not only strongly discouraged[0], we also rely on it injecting
    variables into the global scope to get command-line-like behavior. That
    behavior is incompatible with strict mode[1], and in extension with ES6
    classes (which imply strict mode).
    
    So to prepare for porting to ES6 classes, replace it with a somewhat less
    evil construct that is compatible with strict mode.
    
    [0] 
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#Do_not_ever_use_eval!
    [1]
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#Simplifying_variable_uses
    
    https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/350

 js/ui/lookingGlass.js | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
---
diff --git a/js/ui/lookingGlass.js b/js/ui/lookingGlass.js
index 34c60cb65..186b53ce3 100644
--- a/js/ui/lookingGlass.js
+++ b/js/ui/lookingGlass.js
@@ -1003,11 +1003,14 @@ var LookingGlass = new Lang.Class({
     _evaluate(command) {
         this._history.addItem(command);
 
-        let fullCmd = commandHeader + command;
+        let lines = command.split(';');
+        lines.push(`return ${lines.pop()}`);
+
+        let fullCmd = commandHeader + lines.join(';');
 
         let resultObj;
         try {
-            resultObj = eval(fullCmd);
+            resultObj = Function(fullCmd)();
         } catch (e) {
             resultObj = '<exception ' + e + '>';
         }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]