]> code.delx.au - gnu-emacs-elpa/blob - README.md
Handle unlimited function nesting in imenu index
[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 function nesting and anonymous wrappers:
126
127 (function() {
128 var foo = function() {
129 function bar() { // shown as foo.bar.<definition-1>
130 function baz() {} // foo.bar.baz
131 var qux = function() {}; // foo.bar.quux
132 }
133 };
134 });
135
136 Examples of output:
137
138 * [jQuery 1.5](https://gist.github.com/845449)
139 * [Underscore.js](https://gist.github.com/824262)
140 * [Backbone.js](https://gist.github.com/824260)
141
142 No support for library-specific extension methods like $.extend.
143
144 Highlights undeclared/external variables
145 ----------------------------------------
146
147 Original mode highlights them only on the left side of assignments:
148
149 var house;
150 hose = new House(); // highlights "hose"
151
152 Here they are highlighted in all expressions:
153
154 function feed(fishes, food) {
155 for each (var fish in fshes) { // highlights "fshes"
156 food.feed(fsh); // highlights "fsh"
157 }
158 hood.discard(); // highlights "hood"
159 }
160
161 Destructuring assignments and array comprehensions (JS 1.7) are supported:
162
163 let three, [one, two] = [1, 2];
164 thee = one + two; // highlights "thee"
165
166 function revenue(goods) {
167 // highlights "coast"
168 return [price - coast for each ({price, cost} in goods)].reduce(add);
169 }
170
171 Bugs
172 ====
173
174 If you find problems, please report them to <http://github.com/mooz/js2-mode/issues>.