From d427e626c2a8aa6d619e977ae1c9f3954ad09aa5 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 15 Nov 2018 13:50:36 -0500 Subject: [PATCH] feat(@embark/console): better determine suggestions for any js object not just with the dot.' --- src/lib/core/modules/coderunner/codeRunner.js | 12 ++++++------ src/lib/core/modules/coderunner/runCode.js | 6 ++++-- src/lib/modules/console/suggestions.ts | 19 ++++++++++++------- tslint.json | 3 ++- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/lib/core/modules/coderunner/codeRunner.js b/src/lib/core/modules/coderunner/codeRunner.js index 97026acc8..8b0341757 100644 --- a/src/lib/core/modules/coderunner/codeRunner.js +++ b/src/lib/core/modules/coderunner/codeRunner.js @@ -62,7 +62,7 @@ class CodeRunner { this.runCode.registerVar(varName, code); } - async evalCode(code, cb, forConsoleOnly = false) { + async evalCode(code, cb, forConsoleOnly = false, tolerateError = false) { cb = cb || function() {}; const awaitIdx = code.indexOf('await'); let awaiting = false; @@ -73,14 +73,14 @@ class CodeRunner { const last = instructions.pop(); if (!last.trim().startsWith('return')) { - instructions.push(`return ${last}`); + instructions.push(`return ${last}`); } else { - instructions.push(last); + instructions.push(last); } - + code = `(async function() {${instructions.join(';')}})();`; } - let result = this.runCode.doEval(code); + let result = this.runCode.doEval(code, tolerateError); if (forConsoleOnly && this.ipc.isServer()) { this.commands.push({code}); @@ -100,7 +100,7 @@ class CodeRunner { error.message += '. Are you connected to an Ethereum node?'; } - cb(error); + cb(error); } } diff --git a/src/lib/core/modules/coderunner/runCode.js b/src/lib/core/modules/coderunner/runCode.js index 2ec3cdaa2..c8dbe9cee 100644 --- a/src/lib/core/modules/coderunner/runCode.js +++ b/src/lib/core/modules/coderunner/runCode.js @@ -9,11 +9,13 @@ class RunCode { }); } - doEval(code) { + doEval(code, tolerateError = false) { try { return vm.runInNewContext(code, this.context); } catch(e) { - this.logger.error(e.message); + if (!tolerateError) { + this.logger.error(e.message); + } return e.message; } } diff --git a/src/lib/modules/console/suggestions.ts b/src/lib/modules/console/suggestions.ts index b32a9e848..42ba03077 100644 --- a/src/lib/modules/console/suggestions.ts +++ b/src/lib/modules/console/suggestions.ts @@ -83,16 +83,21 @@ export default class Suggestions { suggestions.push({value: "web3", command_type: "javascript object", description: "instantiated web3.js object configured to the current environment"}); suggestions.push({value: "EmbarkJS", command_type: "javascript object", description: "EmbarkJS static functions for Storage, Messages, Names, etc."}); - if (cmd[cmd.length - 1] === ".") { - return this.events.request("runcode:eval", "Object.keys(" + cmd.slice(0, cmd.length - 1) + ")", (err: any, result: any) => { - if (Array.isArray(result)) { - result.forEach((match: string) => { - suggestions.push({value: cmd + match, command_type: "javascript object", description: ""}); - }); + if (cmd.indexOf(".") > 0) { + const toRemove: string = "." + cmd.split(".").reverse()[0]; + const cmdToSearch: string = cmd.replace((new RegExp(toRemove + "$")), ""); + return this.events.request("runcode:eval", "Object.keys(" + cmdToSearch + ")", (err: any, result: any) => { + try { + if (Array.isArray(result)) { + result.forEach((match: string) => { + suggestions.push({value: cmdToSearch + "." + match, command_type: "javascript object", description: ""}); + }); + } + } catch (e) { } return cb(fuzzySearch(cmd, suggestions, (suggestion: Suggestion) => suggestion.value + " " + suggestion.description).map((x: any) => x.original)); - }); + }, false, true); } return cb(fuzzySearch(cmd, suggestions, (suggestion: Suggestion) => suggestion.value + " " + suggestion.description).map((x: any) => x.original)); diff --git a/tslint.json b/tslint.json index 7c0a381aa..0f77ce2c5 100644 --- a/tslint.json +++ b/tslint.json @@ -8,7 +8,8 @@ "max-line-length": [true, 200], "interface-name": [true, "never-prefix"], "member-ordering": [false], - "no-var-requires": false + "no-var-requires": false, + "no-empty": [true, "allow-empty-catch"] }, "rulesDirectory": [] }