Whitespace and Formatting Rules
CoffeeScript is whitespace-sensitive, so consistent spacing is about more than looks: it determines how code is parsed. These rules enforce indentation, line length, line endings, and spacing around operators, arrows, braces, colons, and commas.
Rules on this page:
- arrow_spacing
- braces_spacing
- colon_assignment_spacing
- eol_last
- indentation
- line_endings
- max_line_length
- newlines_after_classes
- no_tabs
- no_trailing_semicolons
- no_trailing_whitespace
- space_operators
- spacing_after_comma
arrow_spacing
| Property | Value |
|---|---|
| Default level | ignore |
| Message | Function arrows (-> and =>) must be spaced properly |
| Options | none |
Requires whitespace before and after the function arrows -> and =>.
# Good
add = (a, b) -> a + b
# Bad: no space between the parameter list and the arrow
add = (a, b)-> a + b
Some positions are always accepted:
- An arrow at the start of an indented line or right after an opening parenthesis of a call with no parameters, so both
x(-> 3)andx( -> 3)pass. - Empty functions.
# Neither of these triggers an error
x(-> 3)
x( -> 3)
# This does
x((a,b)-> 3)
braces_spacing
| Property | Value |
|---|---|
| Default level | ignore |
| Message | Curly braces must have the proper spacing |
| Options | spaces (default 0), empty_object_spaces (default 0) |
Enforces the number of spaces just inside curly braces. Only explicit braces whose opening and closing brace sit on the same line are checked; implicit objects (written without braces) are ignored.
# spaces: 0
{a: b} # Good
{ a: b } # Bad
# spaces: 1
{ a: b } # Good
{a: b} # Bad
{ a: b } # Bad
# empty_object_spaces: 0
{} # Good
{ } # Bad
# empty_object_spaces: 1
{ } # Good
{} # Bad
The context names the expected spacing, for example There should be 1 space inside "{".
Note: Because your settings are merged with the defaults,
empty_object_spacesis always present (default0). Settingspaces: 1therefore still requires empty objects to be written as{}. If you want{ }, setempty_object_spaces: 1explicitly.
{
"braces_spacing": {
"level": "error",
"spaces": 1,
"empty_object_spaces": 0
}
}
colon_assignment_spacing
| Property | Value |
|---|---|
| Default level | ignore |
| Message | Colon assignment without proper spacing |
| Options | spacing.left (default 0), spacing.right (default 0) |
Checks the number of spaces before (left) and after (right) the colon in object properties and class members. The usual style is left: 0, right: 1:
{
"colon_assignment_spacing": {
"level": "error",
"spacing": {
"left": 0,
"right": 1
}
}
}
# With left: 0, right: 1
user = {name: 'Ada', age: 36} # Good
user = {name : 'Ada'} # Bad: space before the colon
user = {name:'Ada'} # Bad: no space after the colon
class Dog
canBark: true # Good
When the colon is the last thing on a line (its value starts on the next line), the right side is accepted regardless of the setting.
Warning:
spacingis replaced as a whole when merged with the defaults. Always set bothleftandright; if one is missing, every same-line colon assignment is reported.
eol_last
| Property | Value |
|---|---|
| Default level | ignore |
| Message | File does not end with a single newline |
| Options | none |
Requires the file to end with exactly one newline character: a file whose last line has no newline is reported, and so is a file ending with blank lines. Many editors and POSIX tools expect this convention, and it keeps diffs clean.
indentation
| Property | Value |
|---|---|
| Default level | error |
| Message | Line contains inconsistent indentation |
| Options | value: number of spaces per indentation level (default 2) |
Enforces a fixed number of spaces per indentation level. Since whitespace is significant in CoffeeScript, mixing widths leads to code that is hard to read even though it compiles:
# Valid CoffeeScript, but flagged by this rule
twoSpaces = () ->
fourSpaces = () ->
eightSpaces = () ->
'this is valid CoffeeScript'
The context reports what was found, for example Expected 2 got 4.
The rule understands common multi-line constructs and does not flag them:
- Continuation lines after
=or,. - Array elements aligned freely inside brackets.
- Chained method calls that start with a dot. The dot must be indented by a multiple of
value. - Assigned
if/elseexpressions, which may be indented relative to either the line or theifkeyword.
items = ["foo",
"bar"]
$('#menu')
.addClass('open')
.show()
result = if ready
start()
else
wait()
To use four spaces:
{
"indentation": {
"level": "error",
"value": 4
}
}
Indentation using tabs is handled by no_tabs.
line_endings
| Property | Value |
|---|---|
| Default level | ignore |
| Message | Line contains incorrect line endings |
| Options | value: unix (default) or windows |
Ensures every line uses the chosen line ending: \n for unix, \r\n for windows. Enabling this rule in a cross-platform team catches files that were saved with the wrong setting before they produce noisy diffs.
Any other value makes CoffeeLint throw unknown line ending type.
max_line_length
| Property | Value |
|---|---|
| Default level | error |
| Message | Line exceeds maximum allowed length |
| Options | value (default 80), limitComments (default true) |
Limits line length. The context shows the measurement, for example Length is 93, max is 80.
Details:
- Trailing whitespace is not counted.
- A comment line containing nothing but a URL (such as
# https://example.com/a/very/long/link) is always allowed, since URLs cannot be wrapped. - With
limitComments: false, lines that contain no code tokens, such as comment-only lines, are not checked. - In Literate CoffeeScript, the two-character
#prefix CoffeeLint adds to Markdown lines internally is not counted.
{
"max_line_length": {
"level": "error",
"value": 100,
"limitComments": false
}
}
For a single unavoidable long line, use an inline directive instead of raising the limit for everyone.
newlines_after_classes
| Property | Value |
|---|---|
| Default level | ignore |
| Message | Wrong count of newlines between a class and other code |
| Options | value: required number of blank lines after a class (default 3) |
Checks the number of blank lines between the end of a class body and the code that follows. Comment lines between them are not counted as blank lines. A class at the end of the file is not checked.
class Foo
bar: -> 1
foo = new Foo() # Good with value: 3
The context reports the difference, for example Expected 3 got 1.
no_tabs
| Property | Value |
|---|---|
| Default level | error |
| Message | Line contains tab indentation |
| Options | none |
Forbids tab characters in indentation. Only lines that contain code are checked, so tabs inside multi-line strings and block comments are not reported. Tabs elsewhere on a line (not in the indentation) are also allowed.
no_trailing_semicolons
| Property | Value |
|---|---|
| Default level | error |
| Message | Line contains a trailing semicolon |
| Options | none |
Semicolons are only meaningful in CoffeeScript when separating statements on one line. A trailing semicolon is redundant:
# This semicolon is meaningful.
x = '1234'; console.log(x)
# This semicolon is redundant.
alert('end of line');
A semicolon followed by a comment is still reported (alert('x'); # note). Semicolons inside strings, multi-line strings, and regular expressions are ignored.
no_trailing_whitespace
| Property | Value |
|---|---|
| Default level | error |
| Message | Line ends with trailing whitespace |
| Options | allowed_in_comments (default false), allowed_in_empty_lines (default true) |
Forbids spaces and tabs at the end of lines.
allowed_in_empty_lines: true(the default) tolerates lines that consist only of whitespace, which many editors leave behind in indented blocks. Set it tofalseto flag those too.allowed_in_comments: truetolerates trailing whitespace on lines that contain a comment.
{
"no_trailing_whitespace": {
"level": "error",
"allowed_in_comments": false,
"allowed_in_empty_lines": false
}
}
space_operators
| Property | Value |
|---|---|
| Default level | ignore |
| Message | Operators must be spaced properly |
| Options | none |
Requires spaces around binary operators: assignment (=), compound assignment (+=, -=, ...), arithmetic (+, -, *, /, %, **), comparison, and logical and bitwise operators (&&, ||, &, |, ^). Unary + and - must not be followed by a space.
# Good
total = price * quantity + tax
delta = -offset
count += 1
# Bad
total=price*quantity
delta = - offset
The context shows the offending operator. + and - are not checked inside string interpolations or extended regular expressions.
spacing_after_comma
| Property | Value |
|---|---|
| Default level | ignore |
| Message | a space is required after commas |
| Options | none |
Requires whitespace after every comma. A comma at the end of a line is fine.
# Good
move(x, y)
# Bad
move(x,y)
See Also
- Rules Overview for the complete list.
- Configuration for setting levels and options.