Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use verbose error message when redeclaring an imported function (#54) #55

Merged
merged 1 commit into from
Apr 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/js/input-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
19 changes: 19 additions & 0 deletions test/input-panel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '♈';
Expand Down
2 changes: 1 addition & 1 deletion test/pretty.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down