Skip to content

Commit

Permalink
Bubble errors up to editor UI
Browse files Browse the repository at this point in the history
  • Loading branch information
remy committed Jul 15, 2014
1 parent 994ef63 commit 5963e43
Show file tree
Hide file tree
Showing 7 changed files with 13,443 additions and 1,369 deletions.
1 change: 0 additions & 1 deletion lib/routes.js
Expand Up @@ -11,7 +11,6 @@ var express = require('express'),
metrics = require('./metrics'),
scripts = require('../scripts.json'),
Promise = require('rsvp').Promise, // jshint ignore:line
processors = require('./processors'),
undefsafe = require('undefsafe'),
config = require('./config'),
reBin = null; // created when the module is imported
Expand Down
14 changes: 10 additions & 4 deletions public/js/editors/panel.js
Expand Up @@ -7,12 +7,13 @@ var editorModes = {
html: 'htmlmixed',
javascript: 'javascript',
css: 'css',
scss: 'text/x-scss',
typescript: 'javascript',
markdown: 'markdown',
coffeescript: 'coffeescript',
jsx: 'jsx',
less: 'less',
jsx: 'javascript',
less: 'text/x-less',
sass: 'text/x-sass',
scss: 'text/x-scss',
processing: 'text/x-csrc'
};

Expand All @@ -39,7 +40,12 @@ var simpleJsHint = function(cm) {
CodeMirror.commands.autocomplete = simpleJsHint;

CodeMirror.commands.snippets = function (cm) {
return CodeMirror.snippets(cm);
'use strict';
if (['htmlmixed', 'javascript', 'css'].indexOf(cm.options.mode) !== 0) {
return CodeMirror.simpleHint(cm, CodeMirror.hint.anyword);
} else {
return CodeMirror.snippets(cm);
}
};

var Panel = function (name, settings) {
Expand Down
2 changes: 1 addition & 1 deletion public/js/editors/snippets.cm.js
Expand Up @@ -2,7 +2,7 @@
(function() {
'use strict';
var defaults = {
cl: 'console.log("$0");',
cl: 'console.log(\'$0\');',
fn: 'function $0() {\n\t\n}'
};

Expand Down
171 changes: 112 additions & 59 deletions public/js/processors/processor.js
@@ -1,4 +1,4 @@
/*globals jsbin, _, $, RSVP*/
/*globals jsbin, _, $, RSVP, renderLivePreview, editors, throttle, debounceAsync, hintingDone, CodeMirror, Panel, editorModes */
var processors = jsbin.processors = (function () {
'use strict';
/*
Expand Down Expand Up @@ -50,7 +50,9 @@ var processors = jsbin.processors = (function () {
processorData = _.pick(opts, 'id', 'target', 'extensions');

opts.extensions = opts.extensions || [];
if (!opts.extensions.length) opts.extensions = [opts.id];
if (!opts.extensions.length) {
opts.extensions = [opts.id];
}

opts.extensions.forEach(function (ext) {
processorBy.extension[ext] = opts.id;
Expand All @@ -63,7 +65,9 @@ var processors = jsbin.processors = (function () {

// Overwritten when the script loads
var callback = function () {
window.console && window.console.warn('Processor is not ready yet - trying again');
if (window.console) {
window.console.warn('Processor is not ready yet - trying again');
}
failed = true;
return '';
};
Expand Down Expand Up @@ -143,18 +147,24 @@ var processors = jsbin.processors = (function () {
target: 'javascript',
extensions: ['coffee'],
url: jsbin.static + '/js/vendor/coffee-script.js',
init: function (ready) {
init: function coffeescript(ready) {
$.getScript(jsbin.static + '/js/vendor/codemirror4/mode/coffeescript/coffeescript.js', ready);
},
handler: function (source, resolve, reject) {
var renderedCode = '';
try {
renderedCode = CoffeeScript.compile(source, {
renderedCode = window.CoffeeScript.compile(source, {
bare: true
});
resolve(renderedCode);
} catch (e) {
reject(e);
var errors = {
line: parseInt(e.location.first_line, 10) || 0, // jshint ignore:line
ch: parseInt(e.location.first_column, 10) || 0, // jshint ignore:line
msg: e.message
};

reject([errors]);
}
}
}),
Expand All @@ -164,7 +174,7 @@ var processors = jsbin.processors = (function () {
target: 'javascript',
extensions: ['jsx'],
url: jsbin.static + '/js/vendor/JSXTransformer.js',
init: function (ready) {
init: function jsx(ready) {
// Don't add React if the code already contains a script whose name
// starts with 'react', to avoid duplicate copies.
var code = editors.html.getCode();
Expand All @@ -176,7 +186,7 @@ var processors = jsbin.processors = (function () {
handler: function (source, resolve, reject) {
var renderedCode = '';
try {
renderedCode = JSXTransformer.transform(source).code;
renderedCode = window.JSXTransformer.transform(source).code;
resolve(renderedCode);
} catch (e) {
reject(e);
Expand All @@ -190,7 +200,7 @@ var processors = jsbin.processors = (function () {
extensions: ['ts'],
url: jsbin.static + '/js/vendor/typescript.min.js',
init: passthrough,
handler: function (source, resolve, reject) {
handler: function typescript(source, resolve, reject) {
var noop = function () {};
var outfile = {
source: '',
Expand All @@ -211,7 +221,7 @@ var processors = jsbin.processors = (function () {

var parseErrors = [];

var compiler = new TypeScript.TypeScriptCompiler(outfile, outerr);
var compiler = new window.TypeScript.TypeScriptCompiler(outfile, outerr);

compiler.setErrorCallback(function (start, len, message) {
parseErrors.push({ start: start, len: len, message: message });
Expand Down Expand Up @@ -242,12 +252,12 @@ var processors = jsbin.processors = (function () {
target: 'html',
extensions: ['md', 'markdown', 'mdown'],
url: jsbin.static + '/js/vendor/markdown.js',
init: function (ready) {
init: function markdown(ready) {
$.getScript(jsbin.static + '/js/vendor/codemirror4/mode/markdown/markdown.js', ready);
},
handler: function (source, resolve, reject) {
try {
resolve(markdown.toHTML(source));
resolve(window.markdown.toHTML(source));
} catch (e) {
reject(e);
}
Expand All @@ -264,9 +274,9 @@ var processors = jsbin.processors = (function () {
// init and expose jade
$.getScript(jsbin.static + '/js/vendor/codemirror4/mode/clike/clike.js', ready);
},
handler: function (source, resolve, reject) {
handler: function processing(source, resolve, reject) {
try {
var sketch = Processing.compile(source).sourceCode;
var sketch = window.Processing.compile(source).sourceCode;
resolve([
'(function(){',
' var canvas = document.querySelector("canvas");',
Expand All @@ -291,16 +301,26 @@ var processors = jsbin.processors = (function () {
target: 'html',
extensions: ['jade'],
url: jsbin.static + '/js/vendor/jade.js',
init: function (ready) {
// init and expose jade
window.jade = require('jade');
ready();
},
handler: function (source, resolve, reject) {
init: passthrough,
handler: function jade(source, resolve, reject) {
try {
resolve(jade.compile(source, { pretty: true })());
resolve(window.jade.compile(source, { pretty: true })());
} catch (e) {
reject(e);
console.log('Errors', e);
// index starts at 1
var lineMatch = e.message.match(/Jade:(\d+)/) || [,];
var line = parseInt(lineMatch[1], 10) || 0;
if (line > 0) {
line = line - 1;
}
var msg = e.message.match(/\n\n(.+)$/) || [,];
var errors = {
line: line,
ch: null,
msg: msg[1]
};

reject([errors]);
}
}
}),
Expand All @@ -310,14 +330,26 @@ var processors = jsbin.processors = (function () {
target: 'css',
extensions: ['less'],
url: jsbin.static + '/js/vendor/less-1.7.3.min.js',
init: function (ready) {
// In CodeMirror 4, less is now included in the css mode, so no files to load
ready();
},
handler: function (source, resolve, reject) {
less.Parser().parse(source, function (err, result) {
if (err) {
return reject(err);
init: passthrough,
handler: function less(source, resolve, reject) {
window.less.Parser().parse(source, function (error, result) {
if (error) {
// index starts at 1
var line = parseInt(error.line, 10) || 0;
var ch = parseInt(error.column, 10) || 0;
if (line > 0) {
line = line - 1;
}
if (ch > 0) {
ch = ch - 1;
}
var errors = {
line: line,
ch: ch,
msg: error.message
};

return reject([errors]);
}
resolve(result.toCSS().trim());
});
Expand All @@ -329,12 +361,10 @@ var processors = jsbin.processors = (function () {
target: 'scss',
extensions: ['scss'],
// url: jsbin.static + '/js/vendor/sass/dist/sass.worker.js',
init: function (ready) {
init: passthrough,
/* keeping old code for local version of scss if we ever want it again */
// $.getScript(jsbin.static + '/js/vendor/codemirror3/mode/sass/sass.js', function () {
// Sass.initialize(jsbin.static + '/js/vendor/sass/dist/worker.min.js');
ready();
// });
},
// Sass.initialize(jsbin.static + '/js/vendor/sass/dist/worker.min.js');
handler: throttle(debounceAsync(function (source, resolve, reject, done) {
$.ajax({
type: 'post',
Expand Down Expand Up @@ -379,7 +409,7 @@ var processors = jsbin.processors = (function () {
target: 'sass',
extensions: ['sass'],
init: function (ready) {
ready();
$.getScript(jsbin.static + '/js/vendor/codemirror4/mode/sass/sass.js', ready);
},
handler: throttle(debounceAsync(function (source, resolve, reject, done) {
$.ajax({
Expand Down Expand Up @@ -422,9 +452,24 @@ var processors = jsbin.processors = (function () {
},
handler: function (source, resolve, reject) {
try {
resolve(myth(source));
resolve(window.myth(source));
} catch (e) {
reject(e);
// index starts at 1
var line = parseInt(e.line, 10) || 0;
var ch = parseInt(e.column, 10) || 0;
if (line > 0) {
line = line - 1;
}
if (ch > 0) {
ch = ch - 1;
}
var errors = {
line: line,
ch: ch,
msg: e.message
};

reject([errors]);
}
}
}),
Expand All @@ -435,10 +480,23 @@ var processors = jsbin.processors = (function () {
extensions: ['styl'],
url: jsbin.static + '/js/vendor/stylus.js',
init: passthrough,
handler: function (source, resolve, reject) {
stylus(source).render(function (err, result) {
if (err) {
return reject(err);
handler: function stylus(source, resolve, reject) {
window.stylus(source).render(function (error, result) {
if (error) {
// index starts at 1
var lineMatch = error.message.match(/stylus:(\d+)/) || [,];
var line = parseInt(lineMatch[1], 10) || 0;
var msg = error.message.match(/\n\n(.+)\n$/) || [,];
if (line > 0) {
line = line - 1;
}
var errors = {
line: line,
ch: null,
msg: msg[1]
};

return reject([errors]);
}

resolve(result.trim());
Expand All @@ -460,10 +518,10 @@ var processors = jsbin.processors = (function () {
init: function (ready) {
// Only create these once, when the processor is loaded
$('#library').val( $('#library').find(':contains("Traceur")').val() ).trigger('change');
SourceMapConsumer = traceur.outputgeneration.SourceMapConsumer;
SourceMapGenerator = traceur.outputgeneration.SourceMapGenerator;
ProjectWriter = traceur.outputgeneration.ProjectWriter;
ErrorReporter = traceur.util.ErrorReporter;
SourceMapConsumer = window.traceur.outputgeneration.SourceMapConsumer;
SourceMapGenerator = window.traceur.outputgeneration.SourceMapGenerator;
ProjectWriter = window.traceur.outputgeneration.ProjectWriter;
ErrorReporter = window.traceur.util.ErrorReporter;
ready();
},
handler: function (source, resolve, reject) {
Expand All @@ -475,12 +533,12 @@ var processors = jsbin.processors = (function () {
};

var url = location.href;
var project = new traceur.semantics.symbols.Project(url);
var project = new window.traceur.semantics.symbols.Project(url);
var name = 'jsbin';

var sourceFile = new traceur.syntax.SourceFile(name, source);
var sourceFile = new window.traceur.syntax.SourceFile(name, source);
project.addFile(sourceFile);
var res = traceur.codegeneration.Compiler.compile(reporter, project, false);
var res = window.traceur.codegeneration.Compiler.compile(reporter, project, false);

var msg = '/*\nIf you\'ve just translated to JS, make sure traceur is in the HTML panel.\nThis is terrible, sorry, but the only way we could get around race conditions.\n\nHugs & kisses,\nDave xox\n*/\ntry{window.traceur = top.traceur;}catch(e){}\n';
resolve(msg + ProjectWriter.write(res));
Expand Down Expand Up @@ -527,8 +585,7 @@ var processors = jsbin.processors = (function () {

e.preventDefault();
var target = this.hash.substring(1),
label = $(this).text(),
code;
label = $(this).text();
if (target !== 'convert') {
$panelButton.html(label);
$label.text(label);
Expand Down Expand Up @@ -575,13 +632,7 @@ var processors = jsbin.processors = (function () {

// For JSX, use the plain JavaScript mode but disable smart indentation
// because it doesn't work properly
var smartIndent = cmMode !== 'jsx';
cmMode = cmMode === 'jsx' ? 'javascript' : cmMode;

// For less, the mode definition is changed in CodeMirror 4
if (cmMode === 'less') {
cmMode = 'text/x-less';
}
var smartIndent = processorName !== 'jsx';

if (!panel) { return; }

Expand Down Expand Up @@ -635,7 +686,9 @@ var processors = jsbin.processors = (function () {
*/
processors.findByExtension = function (ext) {
var id = processorBy.extension[ext];
if (!id) return defaultProcessor;
if (!id) {
return defaultProcessor;
}
return jsbin.processors[id];
};

Expand Down

0 comments on commit 5963e43

Please sign in to comment.