feat(@embark/console): better determine suggestions for any js object not just with the dot.'

This commit is contained in:
Iuri Matias 2018-11-15 13:50:36 -05:00
parent 23f623d750
commit d427e626c2
4 changed files with 24 additions and 16 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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));

View File

@ -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": []
}