From 9b1c93130ec2c468aee48b6bbbed3187c2f9b78e Mon Sep 17 00:00:00 2001 From: Sean Poulter Date: Fri, 30 Mar 2018 13:15:47 -0400 Subject: [PATCH] Use verbose error message when redeclaring an imported function (#54) --- lib/js/input-panel.js | 7 ++++++- test/input-panel.test.js | 19 +++++++++++++++++++ test/pretty.test.js | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/js/input-panel.js b/lib/js/input-panel.js index 1691566..7d7275c 100644 --- a/lib/js/input-panel.js +++ b/lib/js/input-panel.js @@ -28,13 +28,18 @@ const transformConfig = { const ramdaStr = `const {${R.keys(R).join(',')}} = R;`; const evalSource = R.compose(R.toString, eval); // eslint-disable-line no-eval +const errRedeclaringRamdaFn = new RegExp(`^ramda: Duplicate declaration "(${R.keys(R).join('|')})"`); const formatCode = code => evalSource(code) .replace('"use strict"', '') const formatError = err => err.message .replace(ramdaStr, '') - .replace(/(?=\d).*(?=\|)/g, a => Number(a.trim()) - 1); + .replace(/(?=\d).*(?=\|)/g, a => Number(a.trim()) - 1) + .replace( + errRedeclaringRamdaFn, + 'ramda: Cannot redeclare "$1" that has already been imported from Ramda' + ); function compile(input, options) { diff --git a/test/input-panel.test.js b/test/input-panel.test.js index d539c99..430bb86 100644 --- a/test/input-panel.test.js +++ b/test/input-panel.test.js @@ -100,6 +100,25 @@ describe('Input panel', function() { }); + it('should set verbose error message if a compile fails when trying to redeclaring an imported function', function() { + + const input = bindInputPanel({ + input: inputEl, + evalError: errMsgEl, + output, + delay: 0 + }); + + const fn = 'test'; + input.setValue(`var ${fn};`); + + clock.tick(10); // debounce + + sinon.assert.notCalled(output.setValue); + assert.equal(`ramda: Cannot redeclare "${fn}" that has already been imported from Ramda`, errMsgEl.textContent); + + }); + it('clears error messages before a compile', function() { errMsgEl.textContent = '♈'; diff --git a/test/pretty.test.js b/test/pretty.test.js index 426e06d..398c049 100644 --- a/test/pretty.test.js +++ b/test/pretty.test.js @@ -19,7 +19,7 @@ describe('Clicking the "pretty" button', function() { it('should instruct the "output" CodeMirror to reformat the text', function() { const unformattedCode = "['a', 'b', 'c,']"; - const formattedCode = `[\n "a",\n "b",\n "c,"\n]`; + const formattedCode = '[\n "a",\n "b",\n "c,"\n]'; output = { setValue : sinon.spy(),