X-Git-Url: https://code.delx.au/gnu-emacs-elpa/blobdiff_plain/730d2fe311363fadfe3af1b3f1cfbbbec5cf5790..988e91c4ed2962c03ae79e1d6cc3b435d67c5f6a:/packages/js2-mode/tests/parser.el diff --git a/packages/js2-mode/tests/parser.el b/packages/js2-mode/tests/parser.el index 60634bbc8..dc8c001ca 100644 --- a/packages/js2-mode/tests/parser.el +++ b/packages/js2-mode/tests/parser.el @@ -22,8 +22,10 @@ (require 'ert) (require 'ert-x) (require 'js2-mode) +(require 'cl-lib) (defmacro js2-deftest (name buffer-contents &rest body) + (declare (indent defun)) `(ert-deftest ,(intern (format "js2-%s" name)) () (with-temp-buffer (save-excursion @@ -33,40 +35,39 @@ ,@body) (fundamental-mode))))) -(put 'js2-deftest 'lisp-indent-function 'defun) - (defun js2-test-string-to-ast (s) + (insert s) + (js2-mode) + (should (null js2-mode-buffer-dirty-p)) + js2-mode-ast) + +(cl-defun js2-test-parse-string (code-string &key syntax-error errors-count + reference) (ert-with-test-buffer (:name 'origin) - (insert s) - (js2-mode) - (should (null js2-mode-buffer-dirty-p)) - js2-mode-ast)) - -(defun* js2-test-parse-string (code-string &key syntax-error errors-count - reference) - (let ((ast (js2-test-string-to-ast code-string))) - (if syntax-error - (let ((errors (js2-ast-root-errors ast))) - (should (= (or errors-count 1) (length errors))) - (destructuring-bind (_ pos len) (first errors) - (should (string= syntax-error (substring code-string - (1- pos) (+ pos len -1)))))) - (should (= 0 (length (js2-ast-root-errors ast)))) - (ert-with-test-buffer (:name 'copy) - (js2-print-tree ast) - (skip-chars-backward " \t\n") - (should (string= (or reference code-string) - (buffer-substring-no-properties - (point-min) (point)))))))) - -(defmacro* js2-deftest-parse (name code-string &key bind syntax-error errors-count - reference) + (let ((ast (js2-test-string-to-ast code-string))) + (if syntax-error + (let ((errors (js2-ast-root-errors ast))) + (should (= (or errors-count 1) (length errors))) + (cl-destructuring-bind (_ pos len) (car (last errors)) + (should (string= syntax-error (substring code-string + (1- pos) (+ pos len -1)))))) + (should (= 0 (length (js2-ast-root-errors ast)))) + (ert-with-test-buffer (:name 'copy) + (js2-print-tree ast) + (skip-chars-backward " \t\n") + (should (string= (or reference code-string) + (buffer-substring-no-properties + (point-min) (point))))))))) + +(cl-defmacro js2-deftest-parse (name code-string &key bind syntax-error errors-count + reference) "Parse CODE-STRING. If SYNTAX-ERROR is nil, print syntax tree with `js2-print-tree' and assert the result to be equal to REFERENCE, if present, or the original string. If SYNTAX-ERROR is passed, expect syntax error highlighting substring equal to SYNTAX-ERROR value. BIND defines bindings to apply them around the test." + (declare (indent defun)) `(ert-deftest ,(intern (format "js2-%s" name)) () (let ,(append bind '((js2-basic-offset 2))) (js2-test-parse-string ,code-string @@ -74,8 +75,6 @@ the test." :errors-count ,errors-count :reference ,reference)))) -(put 'js2-deftest-parse 'lisp-indent-function 'defun) - ;;; Basics (js2-deftest-parse variable-assignment @@ -87,6 +86,9 @@ the test." (js2-deftest-parse empty-array-literal "c = [];") +(js2-deftest-parse array-with-missing-elements + "var a = [1, 2, ,];") + (js2-deftest-parse comma-after-regexp "d = /eee/, 42;") @@ -96,8 +98,11 @@ the test." (js2-deftest-parse function-statement "function foo() {\n}") +(js2-deftest-parse function-statement-inside-block + "if (true) {\n function foo() {\n }\n}") + (js2-deftest-parse function-expression-statements-are-verboten - "function() {}" :syntax-error "function") + "function() {}" :syntax-error "(") (js2-deftest-parse member-expr-as-function-name "function a.b.c[2](x, y) {\n}" @@ -106,6 +111,21 @@ the test." (js2-deftest-parse named-function-expression "a = function b() {};") +(js2-deftest-parse parenthesized-expression + "(1 + 2);") + +(js2-deftest-parse for-with-in-operator-in-parens + "for (var y = (0 in []) in {}) {\n}") + +(js2-deftest-parse for-with-in-operator-in-cond + "for (var y = 1 ? 0 in [] : false in {}) {\n}") + +(js2-deftest-parse let-expression + "(let (x = 42) x);") + +(js2-deftest-parse let-expression-statement + "let (x = 42) x;") + ;;; Callers of `js2-valid-prop-name-token' (js2-deftest-parse parse-property-access-when-not-keyword @@ -129,9 +149,12 @@ the test." ;;; 'of' contextual keyword -(js2-deftest-parse parse-array-comp-loop-with-of +(js2-deftest-parse parse-legacy-array-comp-loop-with-of "[a for (a of [])];") +(js2-deftest-parse parse-array-comp-loop + "[for (a of []) a];") + (js2-deftest-parse parse-for-of "for (var a of []) {\n}") @@ -155,18 +178,58 @@ the test." (js2-deftest-parse destruct-in-catch-clause "try {\n} catch ({a, b}) {\n a + b;\n}") +;;; Object literals + +(js2-deftest-parse object-literal-shorthand + "var x = {a: 1, b, c: 1, d};") + +(js2-deftest-parse object-literal-shorthard-with-number + "var a = {1};" :syntax-error "}" :errors-count 2) + +(js2-deftest-parse object-literal-method + "var x = {f(y) { return y;\n}};") + +(js2-deftest-parse object-literal-getter-method + "var x = {get f() { return 42;\n}};") + +(js2-deftest-parse object-literal-setter-method + "var x = {set f(y) { x = y;\n}};") + +(js2-deftest-parse object-literal-computed-keys + "var x = {[Symbol.iterator]: function() {}};") + +(js2-deftest-parse object-literal-computed-function-keys + "var x = {[foo + bar](y) { return y;\n}};") + +(js2-deftest-parse object-literal-computed-getter-key + "var x = {get [foo + bar]() { return 42;\n}};") + +(js2-deftest-parse object-literal-generator + "var x = {*foo() { yield 42;\n}};") + +(js2-deftest-parse object-literal-computed-generator-key + "var x = {*[foo + bar]() { yield 42;\n}};") + +;;; Function definition + +(js2-deftest function-redeclaring-var "var gen = 3; function gen() {};" + (js2-mode) + (should (= (length (js2-ast-root-warnings js2-mode-ast)) 1))) + +(js2-deftest function-expression-var-same-name "var gen = function gen() {};" + (js2-mode) + (should (null (js2-ast-root-warnings js2-mode-ast)))) + ;;; Function parameters (js2-deftest-parse function-with-default-parameters "function foo(a = 1, b = a + 1) {\n}") (js2-deftest-parse function-with-no-default-after-default - "function foo(a = 1, b) {\n}" - :syntax-error "b") + "function foo(a = 1, b) {\n}") (js2-deftest-parse function-with-destruct-after-default - "function foo(a = 1, {b, c}) {\n}" - :syntax-error "{") + "function foo(a = 1, {b, c}) {\n}") (js2-deftest-parse function-with-rest-parameter "function foo(a, b, ...rest) {\n}") @@ -182,6 +245,59 @@ the test." (js2-deftest-parse function-with-rest-after-default-parameter "function foo(a = 1, ...rest) {\n}") +;;; Strict mode errors + +(js2-deftest-parse function-bad-strict-parameters + "'use strict';\nfunction foo(eval, {arguments}, bar) {\n}" + :syntax-error "eval" :errors-count 2) + +(js2-deftest-parse function-retroactive-bad-strict-parameters + "function foo(arguments) {'use strict';}" + :syntax-error "arguments" :errors-count 1) + +(js2-deftest-parse function-duplicate-strict-parameters + "'use strict';\nfunction foo(a, a) {\n}" + :syntax-error "a" :errors-count 1) + +(js2-deftest-parse function-bad-strict-function-name + "'use strict';\nfunction eval() {\n}" + :syntax-error "eval" :errors-count 1) + +(js2-deftest-parse function-bad-retroactive-strict-function-name + "function arguments() {'use strict';}" + :syntax-error "arguments" :errors-count 1) + +(js2-deftest-parse function-bad-strict-catch-name + "'use strict';\ntry {} catch (eval) {}" + :syntax-error "eval" :errors-count 1) + +(js2-deftest-parse function-bad-strict-variable-name + "'use strict';\nvar eval = 'kekeke';" + :syntax-error "eval" :errors-count 1) + +(js2-deftest-parse function-bad-strict-assignment + "'use strict';\narguments = 'fufufu';" + :syntax-error "arguments" :errors-count 1) + +(js2-deftest-parse function-property-strict-assignment + "'use strict';\narguments.okay = 'alright';") + +(js2-deftest-parse function-strict-with + "'use strict';\nwith ({}) {}" + :syntax-error "with" :errors-count 1) + +(js2-deftest-parse function-strict-octal + "'use strict';\nvar number = 0644;" + :syntax-error "0644" :errors-count 1) + +(js2-deftest-parse function-strict-duplicate-keys + "'use strict';\nvar object = {a: 1, a: 2, 'a': 3, ['a']: 4, 1: 5, '1': 6, [1 + 1]: 7};" + :syntax-error "a" :errors-count 4) ; "a" has 3 dupes, "1" has 1 dupe. + +;; errors... or lackthereof. +(js2-deftest-parse function-strict-const-scope + "'use strict';\nconst a;\nif (1) {\n const a;\n}") + ;;; Spread operator (js2-deftest-parse spread-in-array-literal @@ -198,11 +314,17 @@ the test." (js2-deftest-parse arrow-function-with-args-and-curlies "(a, b = 1, ...c) => { c;\n};") +(js2-deftest-parse arrow-function-with-destructuring + "([{a}, b]) => { a + b;\n};") + (js2-deftest-parse parenless-arrow-function-prohibits-rest - "...b => {b + 1;};" :syntax-error "b" :errors-count 2) + "...b => {b + 1;};" :syntax-error "=>") (js2-deftest-parse parenless-arrow-function-prohibits-destructuring - "[a, b] => {a + b;};" :syntax-error "]" :errors-count 5) + "[a, b] => {a + b;};" :syntax-error "]" :errors-count 4) + +(js2-deftest-parse arrow-function-recovers-from-error + "[(,foo) => 1];" :syntax-error "," :errors-count 6) ;;; Automatic semicolon insertion @@ -242,6 +364,391 @@ the test." (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}") +(js2-deftest-parse harmony-generator-yield-star "(function*(a) { yield* a;\n});") + +;;; Comprehensions + +(js2-deftest-parse parse-legacy-array-comp-loop-with-filter + "[a for (a in b) if (a == 2)];") + +(js2-deftest-parse parse-array-comp-loop-with-filters + "[for (a in b) if (a == 2) if (b != 10) a];") + +(js2-deftest-parse parse-generator-comp-loop-with-filters + "(for (x of y) if (x != 4) x);") + +(js2-deftest-parse parse-array-comp-with-yield-is-ok + "(function() { return [for (x of []) yield x];\n});") + +(js2-deftest-parse parse-generator-comp-with-yield-is-not-ok + "(function() { return (for (x of []) yield x);\n});" + :syntax-error "yield") + +(js2-deftest-parse parse-generator-comp-with-yield-inside-function-is-ok + "(for (x of []) function*() { yield x;\n});") + +;;; Numbers + +(js2-deftest-parse decimal-starting-with-zero "081;" :reference "81;") + +(js2-deftest-parse huge-hex "0x0123456789abcdefABCDEF;" :reference "-1;") + +(js2-deftest-parse octal-without-o "071;" :reference "57;") + +(js2-deftest-parse hex-number-okay "0x123;" :reference "291;") + +(js2-deftest-parse hex-number-broken "0xz23;" + :syntax-error "0xz" :errors-count 2) + +(js2-deftest-parse binary-number-okay "0b101;" :reference "5;") + +(js2-deftest-parse binary-number-broken "0b210;" + :syntax-error "0b2" :errors-count 2) + +(js2-deftest-parse octal-number-okay "0o765;" :reference "501;") + +(js2-deftest-parse octal-number-broken "0o812;" + :syntax-error "0o8" :errors-count 2) + +;;; Modules + +(js2-deftest parse-export-bindings "{one, two as dos}" + (js2-init-scanner) + (should (js2-match-token js2-LC)) + (let ((imports (js2-parse-export-bindings))) + (should (not (equal nil imports))) + (should (= 2 (length imports))) + (let ((first (nth 0 imports)) + (second (nth 1 imports))) + (should (equal "one" (js2-name-node-name (js2-export-binding-node-extern-name first)))) + (should (equal "two" (js2-name-node-name (js2-export-binding-node-extern-name second)))) + (let ((first-name (js2-export-binding-node-local-name first)) + (second-name (js2-export-binding-node-local-name second))) + (should (equal first (js2-node-parent first-name))) + (should (equal 3 (js2-node-len first-name))) + (should (equal "one" (js2-name-node-name first-name))) + (should (equal second (js2-node-parent second-name))) + (should (equal 3 (js2-node-len second-name))) + (should (equal "dos" (js2-name-node-name second-name))))))) + +(js2-deftest parse-export-binding-as-default "one as default" + (js2-init-scanner) + (let ((binding (js2-maybe-parse-export-binding))) + (should binding) + (should (js2-export-binding-node-p binding)) + (let ((name (js2-export-binding-node-local-name binding))) + (should name) + (should (equal "default" (js2-name-node-name name)))))) + +(js2-deftest parse-namepsace-import "* as lib;" + (js2-init-scanner) + (should (js2-match-token js2-MUL)) + (let ((namespace-import (js2-parse-namespace-import))) + (should (not (equal nil namespace-import))) + (should (js2-namespace-import-node-p namespace-import)) + (should (= 1 (js2-node-pos namespace-import))) + (should (equal 8 (js2-node-len namespace-import))) + (let ((name-node (js2-namespace-import-node-name namespace-import))) + (should (equal "lib" (js2-name-node-name name-node))) + (should (= 5 (js2-node-pos name-node)))))) + +(js2-deftest parse-from-clause "from 'foo/bar';" + (js2-init-scanner) + (let ((from (js2-parse-from-clause))) + (should (not (equal nil from))) + (should (= 1 (js2-node-pos from))) + (should (= 14 (js2-node-len from))) + (should (equal "foo/bar" (js2-from-clause-node-module-id from))))) + +(js2-deftest parse-import-module-id-only "import 'src/lib'" + (js2-init-scanner) + (should (js2-match-token js2-IMPORT)) + (let ((import (js2-parse-import))) + (should (not (equal nil import))) + (should (= 1 (js2-node-pos import))) + (should (= 16 (js2-node-len import))) + (should (equal nil (js2-import-node-import import))) + (should (equal nil (js2-import-node-from import))))) + +(js2-deftest parse-imported-default-binding "import theDefault from 'src/lib'" + (js2-push-scope (make-js2-scope :pos 0)) + (js2-init-scanner) + (should (js2-match-token js2-IMPORT)) + (let ((import-node (js2-parse-import))) + (should (not (equal nil import-node))) + (should (equal "src/lib" (js2-import-node-module-id import-node))) + (let ((import (js2-import-node-import import-node))) + (should (not (equal nil import))) + (should (equal nil (js2-import-clause-node-namespace-import import))) + (should (equal nil (js2-import-clause-node-named-imports import))) + (let ((default (js2-import-clause-node-default-binding import))) + (should (not (equal nil default))) + (should (js2-export-binding-node-p default)) + (should (equal "theDefault" (js2-name-node-name (js2-export-binding-node-extern-name default))))))) + (should (js2-scope-get-symbol js2-current-scope "theDefault"))) + +(js2-deftest parse-import-namespace-binding "import * as lib from 'src/lib'" + (js2-push-scope (make-js2-scope :pos 0)) + (js2-init-scanner) + (should (js2-match-token js2-IMPORT)) + (let ((import-node (js2-parse-import))) + (should (not (equal nil import-node))) + (should (equal "src/lib" (js2-import-node-module-id import-node))) + (let ((import (js2-import-node-import import-node))) + (should (not (equal nil import))) + (should (equal nil (js2-import-clause-node-default-binding import))) + (should (equal nil (js2-import-clause-node-named-imports import))) + (let ((ns-import (js2-import-clause-node-namespace-import import))) + (should (not (equal nil ns-import))) + (should (js2-namespace-import-node-p ns-import)) + (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import))))))) + (should (js2-scope-get-symbol js2-current-scope "lib"))) + +(js2-deftest parse-import-named-imports "import {foo as bar, baz} from 'src/lib'" + (js2-push-scope (make-js2-scope :pos 0)) + (js2-init-scanner) + (should (js2-match-token js2-IMPORT)) + (let ((import-node (js2-parse-import))) + (should (not (equal nil import-node))) + (should (equal "src/lib" (js2-import-node-module-id import-node))) + (let ((import (js2-import-node-import import-node))) + (should (not (equal nil import))) + (should (equal nil (js2-import-clause-node-default-binding import))) + (should (equal nil (js2-import-clause-node-namespace-import import))) + (let ((named-imports (js2-import-clause-node-named-imports import))) + (should (not (equal nil named-imports))) + (should (listp named-imports)) + (should (= 2 (length named-imports))) + (let ((first (nth 0 named-imports)) + (second (nth 1 named-imports))) + (should (equal "bar" (js2-name-node-name (js2-export-binding-node-local-name first)))) + (should (equal "baz" (js2-name-node-name (js2-export-binding-node-local-name second)))))))) + (should (js2-scope-get-symbol js2-current-scope "bar")) + (should (js2-scope-get-symbol js2-current-scope "baz"))) + +(js2-deftest parse-import-default-and-namespace "import stuff, * as lib from 'src/lib'" + (js2-push-scope (make-js2-scope :pos 0)) + (js2-init-scanner) + (should (js2-match-token js2-IMPORT)) + (let ((import-node (js2-parse-import))) + (should (not (equal nil import-node))) + (should (equal "src/lib" (js2-import-node-module-id import-node))) + (let ((import (js2-import-node-import import-node))) + (should (not (equal nil import))) + (should (equal nil (js2-import-clause-node-named-imports import))) + (let ((default (js2-import-clause-node-default-binding import)) + (ns-import (js2-import-clause-node-namespace-import import))) + (should (not (equal nil default))) + (should (equal "stuff" (js2-name-node-name (js2-export-binding-node-local-name default)))) + (should (not (equal nil ns-import))) + (should (js2-namespace-import-node-p ns-import)) + (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import))))))) + (should (js2-scope-get-symbol js2-current-scope "stuff")) + (should (js2-scope-get-symbol js2-current-scope "lib"))) + +(js2-deftest parse-import-default-and-named-imports + "import robert as bob, {cookies, pi as PIE} from 'src/lib'" + (js2-push-scope (make-js2-scope :pos 0)) + (js2-init-scanner) + (should (js2-match-token js2-IMPORT)) + (let ((import-node (js2-parse-import))) + (should (not (equal nil import-node))) + (should (equal "src/lib" (js2-import-node-module-id import-node))) + (let ((import (js2-import-node-import import-node))) + (should (not (equal nil import))) + (should (not (equal nil (js2-import-clause-node-named-imports import)))) + (let ((default (js2-import-clause-node-default-binding import)) + (named-imports (js2-import-clause-node-named-imports import))) + (should (not (equal nil default))) + (should (equal "bob" (js2-name-node-name (js2-export-binding-node-local-name default)))) + (should (not (equal nil named-imports))) + (should (= 2 (length named-imports)))))) + (should (js2-scope-get-symbol js2-current-scope "bob")) + (should (js2-scope-get-symbol js2-current-scope "cookies")) + (should (js2-scope-get-symbol js2-current-scope "PIE"))) + +(js2-deftest parse-this-module-in-from-clause "import {url} from this module;" + (js2-push-scope (make-js2-scope :pos 0)) + (js2-init-scanner) + (should (js2-match-token js2-IMPORT)) + (let ((import-node (js2-parse-import))) + (should import-node) + (let ((from-clause (js2-import-node-from import-node))) + (should from-clause) + (should (equal "this" (js2-from-clause-node-module-id from-clause))) + (should (js2-from-clause-node-metadata-p from-clause))))) + +(js2-deftest-parse import-only-for-side-effects "import 'src/lib';") +(js2-deftest-parse import-default-only "import theDefault from 'src/lib';") +(js2-deftest-parse import-named-only "import {one, two} from 'src/lib';") +(js2-deftest-parse import-default-and-named "import theDefault, {one, two} from 'src/lib';") +(js2-deftest-parse import-renaming-default "import * as lib from 'src/mylib';") +(js2-deftest-parse import-renaming-named "import {one as uno, two as dos} from 'src/lib';") +(js2-deftest-parse import-default-and-namespace "import robert as bob, * as lib from 'src/lib';") +(js2-deftest-parse import-from-this-module "import {url} from this module;") + +;; Module Exports + +(js2-deftest export-rexport "export * from 'other/lib'" + (js2-init-scanner) + (should (js2-match-token js2-EXPORT)) + (let ((export-node (js2-parse-export))) + (should export-node) + (should (js2-export-node-from-clause export-node)))) + +(js2-deftest export-export-named-list "export {foo, bar as bang};" + (js2-init-scanner) + (should (js2-match-token js2-EXPORT)) + (let ((export-node (js2-parse-export))) + (should export-node) + (let ((exports (js2-export-node-exports-list export-node))) + (should exports) + (should (= 2 (length exports)))))) + +(js2-deftest re-export-named-list "export {foo, bar as bang} from 'other/lib'" + (js2-init-scanner) + (should (js2-match-token js2-EXPORT)) + (let ((export-node (js2-parse-export))) + (should export-node) + (should (js2-export-node-from-clause export-node)) + (let ((exports (js2-export-node-exports-list export-node))) + (should exports) + (should (= 2 (length exports)))))) + +(js2-deftest export-variable-statement "export var foo = 'bar', baz = 'bang';" + (js2-init-scanner) + (js2-push-scope (make-js2-scope :pos 0)) + (should (js2-match-token js2-EXPORT)) + (let ((export-node (js2-parse-export))) + (should export-node) + (should (js2-export-node-declaration export-node)))) + +(js2-deftest export-const-declaration "export const PI = Math.PI;" + (js2-init-scanner) + (js2-push-scope (make-js2-scope :pos 0)) + (should (js2-match-token js2-EXPORT)) + (let ((export-node (js2-parse-export))) + (should export-node) + (should (js2-export-node-declaration export-node)))) + +(js2-deftest export-let-declaration "export let foo = [1];" + (js2-init-scanner) + (js2-push-scope (make-js2-scope :pos 0)) + (should (js2-match-token js2-EXPORT)) + (let ((export-node (js2-parse-export))) + (should export-node) + (should (js2-var-decl-node-p (js2-export-node-declaration export-node))))) + +(js2-deftest export-class-declaration "export class Foo {};" + (js2-init-scanner) + (js2-push-scope (make-js2-scope :pos 0)) + (should (js2-match-token js2-EXPORT)) + (let ((export-node (js2-parse-export))) + (should export-node) + (should (js2-class-node-p (js2-export-node-declaration export-node))))) + +(js2-deftest export-function-declaration "export default function doStuff() {};" + (js2-init-scanner) + (js2-push-scope (make-js2-scope :pos 0)) + (should (js2-match-token js2-EXPORT)) + (let ((export-node (js2-parse-export))) + (should export-node) + (should (js2-export-node-default export-node)))) + +(js2-deftest export-generator-declaration "export default function* one() {};" + (js2-init-scanner) + (js2-push-scope (make-js2-scope :pos 0)) + (should (js2-match-token js2-EXPORT)) + (let ((export-node (js2-parse-export))) + (should export-node) + (should (js2-export-node-default export-node)))) + +(js2-deftest export-assignment-expression "export default a = b;" + (js2-init-scanner) + (js2-push-scope (make-js2-scope :pos 0)) + (should (js2-match-token js2-EXPORT)) + (let ((export-node (js2-parse-export))) + (should export-node) + (should (js2-export-node-default export-node)))) + +(js2-deftest export-function-no-semicolon "export default function foo() {}" + (js2-mode) + (should (equal nil js2-parsed-warnings))) +(js2-deftest export-default-function-no-semicolon "export function foo() {}" + (js2-mode) + (should (equal nil js2-parsed-warnings))) +(js2-deftest export-anything-else-does-require-a-semicolon "export var obj = {}" + (js2-mode) + (should (not (equal nil js2-parsed-warnings)))) + +(js2-deftest-parse parse-export-rexport "export * from 'other/lib';") +(js2-deftest-parse parse-export-export-named-list "export {foo, bar as bang};") +(js2-deftest-parse parse-re-export-named-list "export {foo, bar as bang} from 'other/lib';") +(js2-deftest-parse parse-export-const-declaration "export const PI = Math.PI;") +(js2-deftest-parse parse-export-let-declaration "export let foo = [1];") +(js2-deftest-parse parse-export-function-declaration "export default function doStuff() {};") +(js2-deftest-parse parse-export-generator-declaration "export default function* one() {};") +(js2-deftest-parse parse-export-assignment-expression "export default a = b;") + +;;; Strings + +(js2-deftest-parse string-literal + "var x = 'y';") + +(js2-deftest-parse object-get-string-literal + "var x = {y: 5};\nvar z = x[\"y\"];") + +(js2-deftest-parse template-no-substritutions + "var x = `abc + def`, y = `\\u0000`;") + +(js2-deftest-parse template-with-substitutions + "var y = `${a + b} ${d + e + f}`;") + +(js2-deftest-parse tagged-template + "foo.args`${++x, \"o\"}k`;") + +;;; Classes + +(js2-deftest-parse parse-harmony-class-statement + "class Foo {\n get bar() { return 42;\n}\n set bar(x) { y = x;\n}\n}") + +(js2-deftest-parse parse-harmony-class-statement-without-name-is-not-ok + "class {\n get bar() { return 42;\n}\n}" + :syntax-error "{") + +(js2-deftest-parse parse-harmony-class-expression + "var Foo1 = class Foo {\n bar() { return 42;\n}\n};") + +(js2-deftest-parse parse-harmony-anonymous-class-expression + "var Foo = class {\n set bar(x) { bar = x;\n}\n};") + +(js2-deftest-parse parse-harmony-class-with-extends + "class Foo extends Bar {\n}") + +(js2-deftest-parse parse-harmony-anonymous-class-with-extends + "foo.Foo = class extends Bar {\n set bar(x) { bar = x;\n}\n};") + +(js2-deftest-parse parse-harmony-class-with-complex-extends + "class Foo extends foo[BAR][2].Baz {\n}") + +(js2-deftest-parse parse-harmony-class-missing-extended-class-is-not-ok + "class Foo extends {\n}" + :syntax-error "extends") + +(js2-deftest-parse parse-harmony-class-static-method + "class Foo extends Bar {\n static bar() { return 42;\n}\n}") + +(js2-deftest-parse parse-unterminated-class-is-not-okay + "class Foo {\n get bar() { return 42;\n}" + :syntax-error "}") + +(js2-deftest-parse parse-super-keyword + "class Foo {\n constructor() { super(42);\n}\n foo() { super.foo();\n}\n}") + +(js2-deftest-parse parse-class-keywordlike-method + "class C {\n delete() {}\n if() {}\n}") + ;;; Scopes (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}" @@ -265,6 +772,50 @@ the test." (should (= (js2-symbol-decl-type var-entry) js2-VAR)) (should (js2-name-node-p (js2-symbol-ast-node var-entry))))) +(defun js2-test-scope-of-nth-variable-satisifies-predicate (variable nth predicate) + (goto-char (point-min)) + (dotimes (n (1+ nth)) (search-forward variable)) + (forward-char -1) + (let ((scope (js2-node-get-enclosing-scope (js2-node-at-point)))) + (should (funcall predicate (js2-get-defining-scope scope variable))))) + +(js2-deftest for-node-is-declaration-scope "for (let i = 0; i; ++i) {};" + (js2-mode) + (js2-test-scope-of-nth-variable-satisifies-predicate "i" 0 #'js2-for-node-p)) + +(js2-deftest const-scope-sloppy-script "{const a;} a;" + (js2-mode) + (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-script-node-p) + (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'js2-script-node-p)) + +(js2-deftest const-scope-strict-script "'use strict'; { const a; } a;" + (js2-mode) + (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-block-node-p) + (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'null)) + +(js2-deftest const-scope-sloppy-function "function f() { { const a; } a; }" + (js2-mode) + (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-function-node-p) + (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'js2-function-node-p)) + +(js2-deftest const-scope-strict-function "function f() { 'use strict'; { const a; } a; }" + (js2-mode) + (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-block-node-p) + (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'null)) + +(js2-deftest array-comp-is-result-scope "[x * 2 for (x in y)];" + (js2-mode) + (js2-test-scope-of-nth-variable-satisifies-predicate "x" 0 #'js2-comp-loop-node-p)) + +(js2-deftest array-comp-has-parent-scope + "var a,b=[for (i of [[1,2]]) for (j of i) j * a];" + (js2-mode) + (search-forward "for") + (forward-char -3) + (let ((node (js2-node-at-point))) + (should (js2-scope-parent-scope node)) + (should (js2-get-defining-scope node "j")))) + ;;; Tokenizer (js2-deftest get-token "(1+1)" @@ -312,6 +863,19 @@ the test." (should (eq 1 (js2-token-number (js2-current-token)))))) +(js2-deftest get-token-template-literal "`abc ${i} z ${j} def`" + (js2-init-scanner) + (should (eq js2-TEMPLATE_HEAD (js2-next-token))) + (should (equal "abc " (js2-current-token-string))) + (should (eq js2-NAME (js2-next-token))) + (should (eq js2-RC (js2-next-token))) + (should (eq js2-TEMPLATE_HEAD (js2-next-token 'TEMPLATE_TAIL))) + (should (equal " z " (js2-current-token-string))) + (should (eq js2-NAME (js2-next-token))) + (should (eq js2-RC (js2-next-token))) + (should (eq js2-NO_SUBS_TEMPLATE (js2-next-token 'TEMPLATE_TAIL))) + (should (equal " def" (js2-current-token-string)))) + ;;; Error handling (js2-deftest for-node-with-error-len "for " @@ -322,3 +886,151 @@ the test." (js2-deftest function-without-parens-error "function b {}" ;; Should finish the parse. (js2-mode)) + +;;; Comments + +(js2-deftest comment-node-length "//" + (js2-mode) + (let ((node (js2-node-at-point (point-min)))) + (should (= (js2-node-len node) 2)))) + +(js2-deftest comment-node-length-newline "//\n" + (js2-mode) + (let ((node (js2-node-at-point (point-min)))) + (should (= (js2-node-len node) 3)))) + +;;; Variables classification + +(defun js2--variables-summary (vars) + (let (r) + (setq vars (let (aslist) + (maphash (lambda (k v) (push (cons k v) aslist)) vars) + aslist)) + (dolist (v (sort vars (lambda (a b) (< (js2-node-abs-pos (js2-symbol-ast-node (car a))) + (js2-node-abs-pos (js2-symbol-ast-node (car b))))))) + (let* ((symbol (car v)) + (inition (cadr v)) + (uses (cddr v)) + (symn (js2-symbol-ast-node symbol)) + (namen (js2--get-name-node symn))) + (push (format "%s@%s:%s" + (js2-symbol-name symbol) + (js2-node-abs-pos namen) + (if (eq inition ?P) + "P" + (if uses + (if inition "I" "N") + "U"))) r) + (dolist (u (sort (cddr v) (lambda (a b) (< (js2-node-abs-pos a) + (js2-node-abs-pos b))))) + (push (js2-node-abs-pos u) r)))) + (reverse r))) + +(defmacro js2-deftest-classify-variables (name buffer-contents summary) + (declare (indent defun)) + `(ert-deftest ,(intern (format "js2-classify-variables-%s" name)) () + (with-temp-buffer + (save-excursion + (insert ,buffer-contents)) + (unwind-protect + (progn + (js2-mode) + (should (equal ,summary (js2--variables-summary + (js2--classify-variables))))) + (fundamental-mode))))) + +(js2-deftest-classify-variables incomplete-var-statement + "var" + '()) + +(js2-deftest-classify-variables unused-variable + "function foo () { var x; return 42; }" + '("foo@10:U" "x@23:U")) + +(js2-deftest-classify-variables unused-variable-declared-twice + "function foo (a) { var x; function bar () { var x; x=42; }; return a;}" + '("foo@10:U" "a@15:P" 68 "x@24:U" "bar@36:U" "x@49:U")) + +(js2-deftest-classify-variables assigned-variable + "function foo () { var x; x=42; return x; }" + '("foo@10:U" "x@23:I" 39)) + +(js2-deftest-classify-variables assignment-in-nested-function + "function foo () { var x; function bar () { x=42; }; }" + '("foo@10:U" "x@23:U" "bar@35:U")) + +(js2-deftest-classify-variables unused-nested-function + "function foo() { var i, j=1; function bar() { var x, y=42, z=i; return y; } return i; }" + '("foo@10:U" "i@22:N" 62 84 "j@25:U" "bar@39:U" "x@51:U" "y@54:I" 72 "z@60:U")) + +(js2-deftest-classify-variables prop-get-initialized + "function foo () { var x, y={}; y.a=x; }" + '("foo@10:U" "x@23:N" 36 "y@26:I" 32)) + +(js2-deftest-classify-variables prop-get-uninitialized + "function foo () { var x; if(x.foo) alert('boom'); }" + '("foo@10:U" "x@23:N" 29)) + +(js2-deftest-classify-variables prop-get-function-assignment + "(function(w) { w.f = function() { var a=42, m; return a; }; })(window);" + '("w@11:P" 11 16 "a@39:I" 55 "m@45:U")) + +(js2-deftest-classify-variables let-declaration + "function foo () { let x,y=1; return x; }" + '("foo@10:U" "x@23:N" 37 "y@25:U")) + +(js2-deftest-classify-variables external-function-call + "function foo (m) { console.log(m, arguments); }" + '("foo@10:U" "m@15:P" 32)) + +(js2-deftest-classify-variables global-function-call + "function bar () { return 42; } function foo (a) { return bar(); }" + '("bar@10:I" 58 "foo@41:U" "a@46:P")) + +(js2-deftest-classify-variables let-declaration-for-scope + "function foo () { for(let x=1,y; x