-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcoffeescript.language.txt
145 lines (125 loc) · 8.46 KB
/
coffeescript.language.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
┏━━━━━━━━━━━━━━━━━━┓
┃ COFFEESCRIPT ┃
┗━━━━━━━━━━━━━━━━━━┛
coffee #Compile CoffeeScript to JavaScript, which adds syntactic sugar to JavaScript.
#Version 1.9.0
#Can be fired:
# - coffee: like nodejs interactive mode, but as CoffeeScript.
# Can use CTRL-V at beginning|end of statement for multiline, but need to do for each
# statement, and no history.
# - coffee [FILE.coffee]: compiles then executes (def: stdin|stdout)
# - coffee -c FILE.coffee [-o DIR]: compiles FILE.coffee to DIR/FILE.js (def: $PWD)
# Use -s for stdin + stdout, and -p for stdout only
# - coffee -e STR: same as coffee <<<STR
#There are also options to compile runtime, but it is better to do compile time.
-w #Watch mode
-l #Literate CoffeeScript:
# - using extension .litcoffee make -l useless (automatically recognizes)
# - Markdown document: only keep code blocks, removes rest
-m #Adds FILE.js.map, and link to it from FILE.js
-j FILE2.coffee #Concanates into a single FILE.js output
--nodejs --ARG #Pass options to Node.js
┌────────────┐
│ SYNTAX │
└────────────┘
TOP-LEVEL ==> #(function(){ }).call( this ), unless coffee -b
#Goal: not polluting global namespace.
INDENTATION ==> #{ } (everywhere, including in plain OBJ)
#Can also use then for while|if|switch, and ( ) for ambiguous one-liners
EXPR #EXPR;
VAR.PROP #VAR["PROP"] if reserved word.
VAR #var VAR;
#Put before when first used, unless VAR exist in outer function
FUNC ARG #FUNC(ARG)
[do] [(...)] -> [...] #(function(...){ ... })[()]
=> #Same as -> but binds to current this (like FUNC.call(this))
{ [...] VAL } #return VAL (last expression of any block, including functions)
yield VAL #Transform function() to function*() (ES6 generator, i.e. return but keep state)
(ARG = VAL) -> ... #function(ARG) { if (ARG == null) { ARG = VAL; } ... }
(ARR...) -> ... #function() { ARR = arguments.length < 1 ? [] : [].slice.call(ARR,0) } (uses actual "...")
FUNC ARR... #FUNC.apply(null, ARR); (uses actual "...")
@VAR #this.VAR
TEST #( TEST )
... if|while|for ...2 #Same as if|while|for ...2 ...
VAR? #typeof VAR !== "undefined" && VAR !== null
VAR ?= VAL #if ( typeof VAR !== "undefined" && VAR !== null ) VAR = VAL
VAR ? VAL #Same as if VAR? then VAR else VAL
VAR?.VAR2 #Same as if VAR? then VAR.VAR2
FUNC? ARG #if ( typeof VAR === "function" ) FUNC(ARG)
if TEST then VAL else VAL2 #( TEST ) ? VAL : VAL2 (if short enough)
VAL <|...|> VAL2 <|...|> VAL3 #VAL <|...|> VAL2 && VAL2 <|...|> VAL3
unless TEST #if ( ! TEST )
until TEST #while ( ! TEST )
loop #while ( true )
VAR in ARR #ARR.indexOf(VAR) >= 0
KEY of OBJ #KEY in OBJ
(VAR for VAR in|of ARR|OBJ) #Returns ARR|OBJ with only VAR|KEY member
for [own] VAR[, VAR2] #for ( VAR2 = 0; VAR2 < ARR.length ; VAR2++ ) { VAR = ARR[VAR2]; if ( TEST ) { ... } }
in|of ARR|OBJ [when TEST] ... #Using of OBJ iterates VAR, VAR2 over key, value instead. If "own", check hasOwnProperty().
(for|while TEST ...) #Make while|for a function returning all return values as VAR_ARR2
switch TEST #switch ( TEST ) {
when VAL[, VAL2...] # case VAL:
... # case VAL2:
when... # ...; break;
else # case...
... # default:
# ...;
#}
VAR = switch ... #Turn cases into functions returning values.
# COMMENT #// COMMENT
"...#{EXPR}..." #"..." + ( EXPR ) + "..."
"...
..." #"... ..." (concatenates whitespaces into one space in-between)
"""
... #Like "...": keeps all whitespaces except initial tabs (based on the line with the smallest
""" #indentation)
###
...
### #Same for /* ... */
///
...
/// #Same for /REGEXP/. Ignores whitespaces in ... (must be escaped to include in REGEXP)
[ #[
VAR... # VAR,...
VAR2 # VAR2
] #]
[NUM..[.]NUM2] [by NUM3] #Returns an ARR. If three dots, excludes NUM2
ARR[[NUM]..[.][NUM2]] [= ARR2] #ARR.slice(NUM, NUM2[-1]). Can assign ARR2 (use splice() then)
#NUM by def is 0. NUM2 by def is ARR size.
ARR_LIT = ARR2 #ARR[0] = ARR2[0], etc., or OBJ.VAR = OBJ2.VAR, etc.
OBJ_LIT = OBJ2 #Can be mixed, with several levels of depth.
#ARR_LIT can contain [ARR3]... (uses actual "...") in the middle to catch all remaining and
#assign (or not) to ARR3.
== #===
!= #!==
is #===
isnt #!==
not #!
and #&&
or #||
on|yes #true
off|no #false
NUM ** NUM2 #Math.pow(NUM, NUM2)
NUM // NUM2 #Math.floor(NUM / NUM2)
NUM %% NUM2 #( NUM % NUM2 + NUM2 ) % NUM2 (different from NUM % NUM2 when NUM2 negative)
class TYPE [extends TYPE2] #Do the whole inheritance thing.
[constructor: FUNC] #Functions can use super() to fire TYPE2()
VAR: VAL #If constructor FUNC(@VAR3), will assign this.VAR3 = ARG
@VAR2: VAL #@VAR2: VAL assigns to this.VAR2, VAR: VAL to this.prototype.VAR
VAR::VAR2 #VAR.prototype.VAR2
`...` #Literal JavaScript insertion
┌───────────┐
│ TOOLS │
└───────────┘
cake [TASK] #Look for ./Cakefile (CoffeeScript file), and:
# - without TASK, prints options, registered with option("-o", "--o [DIR]", DESC_STR)
# - with TASK, fire FUNC registered with task("TASK[:SUBTASK]", DESC_STR, FUNC(OBJ)),
# where OBJ is populated by options, and fire invoke "TASK"
js2coffee [FILE] #Inverse. Useful for migrations.
require("coffee-script/register");
require("./gulpfile.coffee"); #gulpfile.js definition in order to use gulpfile.coffee instead
GULP-COFFEE([OBJ]) #Transform from CoffeeScript to JavaScript. OBJ are same as coffee-script, e.g. OBJ.bare for
#-b|--bare
#Can emit error(ERROR)
#To use with sourcemaps, do same as for Uglify.
#Version 2.3.1