1 ;;; tests/indent.el --- Some tests for js2-mode.
3 ;; Copyright (C) 2009, 2011-2016 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 (js2-reparse) ; solely for js2-jsx-self-closing, for some reason
36 (indent-region (point-min) (point-max))
37 (should (string= s (buffer-substring-no-properties
38 (point-min) (point)))))))
40 (cl-defmacro js2-deftest-indent (name content &key bind keep-indent)
41 `(ert-deftest ,(intern (format "js2-%s" name)) ()
42 (let ,(append '(indent-tabs-mode
44 (js2-pretty-multiline-declarations t)
45 (inhibit-point-motion-hooks t))
47 (js2-test-indent ,content ,keep-indent))))
49 (put 'js2-deftest-indent 'lisp-indent-function 'defun)
51 (js2-deftest-indent no-multiline-decl-indent-after-semicolon
55 (js2-deftest-indent multiline-decl-indent-after-comma
59 (js2-deftest-indent no-multiline-decl-when-disabled
62 :bind ((js2-pretty-multiline-declarations nil)))
64 (js2-deftest-indent multiline-decl-with-continued-expr
68 (js2-deftest-indent multiline-decl-with-continued-expr-same-line
72 (js2-deftest-indent no-multiline-decl-with-operator-inside-string
73 "var foo = bar('/protocols/')
76 (js2-deftest-indent no-multiline-decl-implicit-semicolon
80 (js2-deftest-indent multiline-decl-sees-keyword-width
84 (js2-deftest-indent multiline-decl-second-arg-value-parenthesised
92 (js2-deftest-indent multiline-decl-first-arg-function-normal
93 "var foo = function() {
98 (js2-deftest-indent multiline-decl-first-arg-function-indent-all
99 "var foo = function() {
103 :bind ((js2-pretty-multiline-declarations 'all)))
105 (js2-deftest-indent default-keyword-as-property
112 (js2-deftest-indent multiline-string-noop
119 (js2-deftest-indent no-multiline-decl-first-arg-function-dynamic
120 "var foo = function() {
123 :bind ((js2-pretty-multiline-declarations 'dynamic)))
125 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic
126 "var foo = function() {
130 :bind ((js2-pretty-multiline-declarations 'dynamic)))
132 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic-comment
133 "var foo = function() {
135 | }/* MUAHAHAHA, ah ha! */,
137 :bind ((js2-pretty-multiline-declarations 'dynamic)))
139 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic-scan-error
140 "var foo = function() {
144 :bind ((js2-pretty-multiline-declarations 'dynamic)))
146 (js2-deftest-indent indent-generator-method
154 (js2-deftest-indent indent-generator-computed-method
156 | *[Symbol.iterator]() {
162 (js2-deftest-indent case-inside-switch
168 (js2-deftest-indent case-inside-switch-with-extra-indent
173 :bind ((js2-indent-switch-body t)))
175 (js2-deftest-indent case-inside-switch-with-extra-indent-curly-after-newline
181 :bind ((js2-indent-switch-body t)))
183 (js2-deftest-indent continued-expression-vs-unary-minus
190 (js2-deftest-indent spread-inside-array
197 (js2-deftest-indent jsx-one-line
198 "var foo = <div></div>;")
200 (js2-deftest-indent jsx-children-parentheses
212 (js2-deftest-indent jsx-children-unclosed
217 (js2-deftest-indent jsx-argument
230 (js2-deftest-indent jsx-leading-comment
236 (js2-deftest-indent jsx-trailing-comment
242 (js2-deftest-indent jsx-self-closing
243 ;; This ensures we know the bounds of a self-closing element
251 :bind ((sgml-attribute-offset 1))) ; Emacs 24.5 -> 25 compat
253 (js2-deftest-indent jsx-embedded-js-content
256 | {array.map(function () {
264 (js2-deftest-indent jsx-embedded-js-unclosed
267 | {array.map(function () {
271 (js2-deftest-indent jsx-embedded-js-attribute
273 | <div attribute={array.map(function () {