1 ;;; tests/indent.el --- Some tests for js2-mode.
3 ;; Copyright (C) 2009, 2011-2013 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 (require 'js2-old-indent)
27 (defun js2-test-indent (content keep-indent)
28 (let ((s (replace-regexp-in-string "^ *|" "" content)))
33 (replace-regexp-in-string "^ *" "" s)))
35 (indent-region (point-min) (point-max))
36 (should (string= s (buffer-substring-no-properties
37 (point-min) (point)))))))
39 (cl-defmacro js2-deftest-indent (name content &key bind keep-indent)
40 `(ert-deftest ,(intern (format "js2-%s" name)) ()
41 (let ,(append '(indent-tabs-mode
43 (js2-pretty-multiline-declarations t)
44 (inhibit-point-motion-hooks t))
46 (js2-test-indent ,content ,keep-indent))))
48 (put 'js2-deftest-indent 'lisp-indent-function 'defun)
50 (js2-deftest-indent no-multiline-decl-indent-after-semicolon
54 (js2-deftest-indent multiline-decl-indent-after-comma
58 (js2-deftest-indent no-multiline-decl-when-disabled
61 :bind ((js2-pretty-multiline-declarations nil)))
63 (js2-deftest-indent multiline-decl-with-continued-expr
67 (js2-deftest-indent multiline-decl-with-continued-expr-same-line
71 (js2-deftest-indent no-multiline-decl-with-operator-inside-string
72 "var foo = bar('/protocols/')
75 (js2-deftest-indent no-multiline-decl-implicit-semicolon
79 (js2-deftest-indent multiline-decl-sees-keyword-width
83 (js2-deftest-indent multiline-decl-second-arg-value-parenthesised
91 (js2-deftest-indent multiline-decl-first-arg-function-normal
92 "var foo = function() {
97 (js2-deftest-indent multiline-decl-first-arg-function-indent-all
98 "var foo = function() {
102 :bind ((js2-pretty-multiline-declarations 'all)))
104 (js2-deftest-indent default-keyword-as-property
111 (js2-deftest-indent multiline-string-noop
118 (js2-deftest-indent no-multiline-decl-first-arg-function-dynamic
119 "var foo = function() {
122 :bind ((js2-pretty-multiline-declarations 'dynamic)))
124 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic
125 "var foo = function() {
129 :bind ((js2-pretty-multiline-declarations 'dynamic)))
131 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic-comment
132 "var foo = function() {
134 | }/* MUAHAHAHA, ah ha! */,
136 :bind ((js2-pretty-multiline-declarations 'dynamic)))
138 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic-scan-error
139 "var foo = function() {
143 :bind ((js2-pretty-multiline-declarations 'dynamic)))
145 (js2-deftest-indent indent-generator-method
153 (js2-deftest-indent case-inside-switch
159 (js2-deftest-indent case-inside-switch-with-extra-indent
164 :bind ((js2-indent-switch-body t)))
166 (js2-deftest-indent case-inside-switch-with-extra-indent-curly-after-newline
172 :bind ((js2-indent-switch-body t)))
174 (js2-deftest-indent jsx-one-line
175 "var foo = <div></div>;")
177 (js2-deftest-indent jsx-children-parentheses
189 (js2-deftest-indent jsx-children-unclosed
194 (js2-deftest-indent jsx-argument
207 (js2-deftest-indent jsx-leading-comment
213 (js2-deftest-indent jsx-trailing-comment
219 (js2-deftest-indent jsx-self-closing
220 ;; This ensures we know the bounds of a self-closing element
228 :bind ((sgml-attribute-offset 1))) ; Emacs 24.5 -> 25 compat
230 (js2-deftest-indent jsx-embedded-js-content
233 | {array.map(function () {
241 (js2-deftest-indent jsx-embedded-js-unclosed
244 | {array.map(function () {
248 (js2-deftest-indent jsx-embedded-js-attribute
250 | <div attribute={array.map(function () {