Ostatnio pokazywałem coffeescript kumplowi, specjaliście od js. I już po kilka minutach wytknął kilka głupich błędów w kompilacji. Musiał bym poszukać o co chodziło, ale na pewno nie było to coś co wykładało kod, a bardziej stwarzało lukę (niezgodność z parserem js). Tak więc polecam wrzucić kilka ciekawszych kawałków kodu na ich stronie i zobaczyć czy to co wypluje jest w 100% poprawne.
Chwalą się, że:
You can use any existing JavaScript library seamlessly (and vice-versa). The compiled output is readable and pretty-printed, passes through JavaScript Lint without warnings, will work in every JavaScript implementation, and tends to run as fast or faster than the equivalent handwritten JavaScript.
Polecam wkleić to co wypluwa coffee script w parser Lint i zobaczyć czy nie będzie warningów. U mnie były.
PS Warto zwrócić uwagę, że parser Lint do jakiego oni linkują na stronie jest dość pobłążliwy. Polecam wkleić choć by pierwszy z przykładów ze strony coffe script do JsLint (the Crockford one) http://www.jslint.com/ błędów będzie na pewno cała masa.
Ich kod wypluty z przykładu nr 1
var cubes, list, math, num, number, opposite, race, square;
var __slice = Array.prototype.slice;
number = 42;
opposite = true;
if (opposite) number = -42;
square = function(x) {
return x * x;
};
list = [1, 2, 3, 4, 5];
math = {
root: Math.sqrt,
square: square,
cube: function(x) {
return x * square(x);
}
};
race = function() {
var runners, winner;
winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
return print(winner, runners);
};
if (typeof elvis !== "undefined" && elvis !== null) alert("I knew it!");
cubes = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = list.length; _i < _len; _i++) {
num = list[_i];
_results.push(math.cube(num));
}
return _results;
})();
Odpowiedź parsera:
[code]Error:
Problem at line 2 character 5: Unexpected dangling ‘_’ in ‘__slice’.
var __slice = Array.prototype.slice;
Problem at line 5 character 15: Expected ‘{’ and instead saw ‘number’.
if (opposite) number = -42;
Problem at line 6 character 18: Expected exactly one space between ‘function’ and ‘(’.
square = function(x) {
Problem at line 7 character 3: Missing ‘use strict’ statement.
return x * x;
Problem at line 7 character 3: Expected ‘return’ at column 5, not column 3.
return x * x;
Problem at line 11 character 3: Expected ‘root’ at column 5, not column 3.
root: Math.sqrt,
Problem at line 12 character 3: Expected ‘square’ at column 5, not column 3.
square: square,
Problem at line 13 character 3: Expected ‘cube’ at column 5, not column 3.
cube: function(x) {
Problem at line 13 character 17: Expected exactly one space between ‘function’ and ‘(’.
cube: function(x) {
Problem at line 14 character 5: Missing ‘use strict’ statement.
return x * square(x);
Problem at line 14 character 5: Expected ‘return’ at column 9, not column 5.
return x * square(x);
Problem at line 15 character 3: Expected ‘}’ at column 5, not column 3.
}
Problem at line 17 character 16: Expected exactly one space between ‘function’ and ‘(’.
race = function() {
Problem at line 18 character 3: Missing ‘use strict’ statement.
var runners, winner;
Problem at line 18 character 3: Expected ‘var’ at column 5, not column 3.
var runners, winner;
Problem at line 19 character 3: Expected ‘winner’ at column 5, not column 3.
winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(argum…
Problem at line 19 character 12: Use a named parameter.
winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(argum…
Problem at line 19 character 24: Expected ‘;’ and instead saw ‘,’.
winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(argum…
Problem at line 19 character 24: Expected an identifier and instead saw ‘,’.
winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(argum…
Problem at line 19 character 24: Stopping. (61% scanned).
Unused variable: runners 17 ‘race’[/code]
Nie jestem specjalistą od JS, ani tym bardziej coffe script, ale może ktoś mądrzejszy się wypowie?