From a5b586f22dace6436d133ba48a602203f485b9d6 Mon Sep 17 00:00:00 2001 From: Jackson Ray Hamilton Date: Sun, 7 Jun 2015 15:56:02 -0700 Subject: [PATCH] Test const scoping. --- tests/parser.el | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/tests/parser.el b/tests/parser.el index 1a93f362e..8be0833f6 100644 --- a/tests/parser.el +++ b/tests/parser.el @@ -721,19 +721,40 @@ the test." (should (= (js2-symbol-decl-type var-entry) js2-VAR)) (should (js2-name-node-p (js2-symbol-ast-node var-entry))))) -(js2-deftest for-node-is-declaration-scope "for (let i = 0; i; ++i) {};" - (js2-mode) - (search-forward "i") +(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 (js2-for-node-p (js2-get-defining-scope scope "i"))))) + (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) - (search-forward "x") - (forward-char -1) - (let ((scope (js2-node-get-enclosing-scope (js2-node-at-point)))) - (should (js2-comp-loop-node-p (js2-get-defining-scope scope "x"))))) + (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];" -- 2.39.2