Is there a way to catch typos when writing CoffeeS

2019-03-31 05:54发布

问题:

This small CoffeeScript contains a typo

drinks = "Coffee"
drinks = drinks + ", " + "Tea"
drinsk = drinks + ", " + "Lemonade"
alert drinks

The intention was to alert "Coffee, Tea, Lemonade" but the result is instead "Coffee, Tea". The generated JavaScript is still valid and passes JSLint; it declares the variables before usage which is good, but its the wrong variables.

var drinks, drinsk;
drinks = "Coffee";
drinks = drinks + ", " + "Tea";
drinsk = drinks + ", " + "Lemonade";
alert(drinks);

If the same example was written in plain JavaScript then JSLint would catch the error:

var drinks;
drinks = "Coffee";
drinks = drinks + ", " + "Tea";
drinsk = drinks + ", " + "Lemonade";
alert(drinks);

------------------
Problem at line 4 character 1: 'drinsk' was used before it was defined.
drinsk = drinks + ", " + "Lemonade";

To the question: Is there a way to keep the errors I make so that I can find them? I would love to see tools like JSLint still work.

Also tried http://www.coffeelint.org/ and it tells me "Your code is lint free!"

回答1:

You can use IDE which supports identifier spellchecking, for example, IntelliJ IDEA which BTW has a plugin for CoffeScript editing.



回答2:

I'd solve this by writing specs for your JavaScript. Lint type tools are great, but there are still plenty of other mistakes you can make.

Personally I use jasmine via jasmine-headless-webkit for this