]> code.delx.au - gnu-emacs-elpa/blob - README.md
Add notation for byte-compilation. refs #13
[gnu-emacs-elpa] / README.md
1 js2-mode
2 ========
3
4 An improved JavaScript mode for GNU Emacs. Forked from <http://code.google.com/p/js2-mode/>.
5
6 Install
7 =======
8
9 $ git clone git://github.com/mooz/js2-mode.git
10 $ cd js2-mode
11 $ emacs --batch -f batch-byte-compile js2-mode.el
12
13 _NOTE: Emacs may fail to byte compile js2-mode.el in interactive mode (e.g., `M-x byte-compile-file`). Following the above instruction is highly recommended. See https://github.com/mooz/js2-mode/issues/13 for details._
14
15 Then, place js2-mode.elc into your site-lisp directory.
16
17 In you emacs config:
18
19 (autoload 'js2-mode "js2-mode" nil t)
20 (add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))
21
22 See <http://code.google.com/p/js2-mode/wiki/InstallationInstructions> for details.
23
24 Differences between original js2-mode.el
25 ========================================
26
27 Popular indentation style
28 -------------------------
29
30 When `js2-consistent-level-indent-inner-bracket-p` is non-nil
31
32 [foo, bar, baz].forEach(function (v) {
33 if (validate(v))
34 process(v);
35 });
36
37 [a, b, c].some(function (v) {
38 return validate(v);
39 });
40
41 When `js2-consistent-level-indent-inner-bracket-p` is nil
42 (Same as original js2-mode's indentation)
43
44 [foo, bar, baz].forEach(function (v) {
45 if (validate(v))
46 process(v);
47 });
48
49 [a, b, c].some(function (v) {
50 return validate(v);
51 });
52
53 Pretty multi-line variable declaration
54 --------------------------------------
55
56 In original js2-mode.el,
57
58 var foo = 10,
59 bar = 20,
60 baz = 30;
61
62 In this js2-mode.el, when the value `js2-pretty-multiline-decl-indentation-p` is non-nil,
63
64 var foo = 10,
65 bar = 20,
66 baz = 30;
67
68 Abbreviated destructuring assignments
69 -------------------------------------
70
71 let {a, b} = {a: 10, b: 20}; // Abbreviated (Not supported in original js2-mode.el)
72 let {a: a, b: b} = {a: 10, b: 20}; // Same as above (Supported in original js2-mode.el)
73
74 (function ({responseText}) { /* */ })(xhr); // As the argument of function
75
76 for (let [k, { name, age }] in Iterator(obj)) // nested
77 print(k, name, age);
78
79 Expression closure in property value
80 ------------------------------------
81
82 let worker = {
83 get age() 20,
84 get sex() "male",
85 fire: function () _fire()
86 };
87
88 Fix for odd indentation of "else if" with no braces
89 ---------------------------------------------------
90
91 In original js2-mode.el,
92
93 if (foo)
94 return foo;
95 else if (bar)
96 return bar; // here
97
98 In this js2-mode.el,
99
100 if (foo)
101 return foo;
102 else if (bar)
103 return bar; // fixed
104
105 Fixes in Imenu support
106 ----------------------
107
108 Supports element-get form:
109
110 foo["bar"] = function() {};
111 foo[647] = function() {};
112
113 Proper position for functions in nested object literals:
114
115 foo = {
116 bar: function() {}, // ok in original
117 baz: {
118 boop: function() {} // fixed here
119 }
120 }
121
122 Imenu support for function nesting
123 ----------------------------------
124
125 Supports one level of nesting:
126
127 function foo() {
128 function bar() { // shown as foo.bar
129 function baz() {} // hidden
130 }
131 }
132
133 Top-level function can be anonymous wrapper:
134
135 (function() {
136 var foo = function() {}; // shown as foo
137 })();
138
139 Examples of output:
140
141 * [Underscore.js](https://github.com/documentcloud/underscore/blob/master/underscore.js)
142 -> <https://gist.github.com/824262>
143 * [Backbone.js](https://github.com/documentcloud/backbone/blob/master/backbone.js)
144 -> <https://gist.github.com/824260>
145
146 No support for library-specific extension methods like _.extend.
147
148 Highlights undeclared/external variables
149 ----------------------------------------
150
151 Original mode highlights them only on the left side of assignments:
152
153 var house;
154 hose = new House(); // highlights "hose"
155
156 Here they are highlighted in all expressions:
157
158 function feed(fishes, food) {
159 for each (var fish in fshes) { // highlights "fshes"
160 food.feed(fsh); // highlights "fsh"
161 }
162 hood.discard(); // highlights "hood"
163 }
164
165 Destructuring assignments and array comprehensions (JS 1.7) are supported:
166
167 let three, [one, two] = [1, 2];
168 thee = one + two; // highlights "thee"
169
170 function revenue(goods) {
171 // highlights "coast"
172 return [price - coast for each ({price, cost} in goods)].reduce(add);
173 }
174
175 Bugs
176 ====
177
178 If you find problems, please report them to <http://github.com/mooz/js2-mode/issues>.