From 535340794b6f02fd90068c00e62183e4f1a6b956 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 19 Dec 2017 14:07:48 -0500 Subject: [PATCH] warn of contract typos --- lib/cmd.js | 4 ++-- lib/contracts/contracts.js | 11 +++++++++++ lib/utils/utils.js | 12 +++++++++++- test_app/config/contracts.json | 3 +++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/cmd.js b/lib/cmd.js index af5d8815..2b31c009 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -1,6 +1,6 @@ const program = require('commander'); const promptly = require('promptly'); -const propose = require('propose'); +const utils = require('./utils/utils.js'); const Embark = require('../lib/index'); let embark = new Embark; @@ -173,7 +173,7 @@ class Cmd { .action(function (cmd) { console.log('unknown command "%s"'.red, cmd); let dictionary = ['new', 'demo', 'build', 'run', 'blockchain', 'simulator', 'test', 'upload', 'version' ]; - let suggestion = propose(cmd, dictionary, {threshold: 0.3}); + let suggestion = utils.proposeAlternative(cmd, dictionary); if (suggestion) { console.log('did you mean "%s"?'.green, suggestion); } diff --git a/lib/contracts/contracts.js b/lib/contracts/contracts.js index 00a26059..f075a485 100644 --- a/lib/contracts/contracts.js +++ b/lib/contracts/contracts.js @@ -2,6 +2,7 @@ let toposort = require('toposort'); let async = require('async'); let Compiler = require('./compiler.js'); +let utils = require('../utils/utils.js'); // TODO: create a contract object @@ -80,6 +81,7 @@ class ContractsManager { /*eslint complexity: ["error", 11]*/ function dealWithSpecialConfigs(callback) { let className, contract, parentContractName, parentContract; + let dictionary = Object.keys(self.contracts); for (className in self.contracts) { contract = self.contracts[className]; @@ -98,6 +100,10 @@ class ContractsManager { if (parentContract === undefined) { self.logger.error(className + ": couldn't find instanceOf contract " + parentContractName); + let suggestion = utils.proposeAlternative(className, dictionary, [className, parentContractName]); + if (suggestion) { + self.logger.warn('did you mean "' + suggestion + '"?'); + } continue; } @@ -124,11 +130,16 @@ class ContractsManager { }, function removeContractsWithNoCode(callback) { let className, contract; + let dictionary = Object.keys(self.contracts); for (className in self.contracts) { contract = self.contracts[className]; if (contract.code === undefined) { self.logger.error(className + " has no code associated"); + let suggestion = utils.proposeAlternative(className, dictionary, [className]); + if (suggestion) { + self.logger.warn('did you mean "' + suggestion + '"?'); + } delete self.contracts[className]; } } diff --git a/lib/utils/utils.js b/lib/utils/utils.js index a609be39..e06d6194 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -5,6 +5,7 @@ let http = require('follow-redirects').http; let https = require('follow-redirects').https; let shelljs = require('shelljs'); var tar = require('tar'); +var propose = require('propose'); //let fs = require('../core/fs.js'); let o_fs = require('fs-extra'); @@ -103,6 +104,14 @@ function extractTar(filename, packageDirectory, cb) { ); } +function proposeAlternative(word, _dictionary, _exceptions) { + let exceptions = _exceptions || []; + let dictionary = _dictionary.filter((entry) => { + return exceptions.indexOf(entry) < 0; + }); + return propose(word, dictionary, {threshold: 0.3}); +} + module.exports = { joinPath: joinPath, filesMatchingPattern: filesMatchingPattern, @@ -116,5 +125,6 @@ module.exports = { sed: sed, exit: exit, downloadFile: downloadFile, - extractTar: extractTar + extractTar: extractTar, + proposeAlternative: proposeAlternative }; diff --git a/test_app/config/contracts.json b/test_app/config/contracts.json index d2a7e2b3..7eabfe1a 100644 --- a/test_app/config/contracts.json +++ b/test_app/config/contracts.json @@ -44,6 +44,9 @@ "AlreadyDeployedToken": { "address": "0xece374063fe5cc7efbaca0a498477cada94e5ad6", "instanceOf": "Token" + }, + "MyToken3": { + "instanceOf": "Tokn" } } },