Skip to content

Commit

Permalink
Added new test cases to recognize const variables
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=158079845
  • Loading branch information
bellashim authored and brad4d committed Jun 6, 2017
1 parent c89b771 commit f9c5c67
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions test/com/google/javascript/jscomp/InferConstsTest.java
Expand Up @@ -97,33 +97,55 @@ public void testForLet() {
} }


public void testForConst() { public void testForConst() {
// Using 'const' here is not allowed, and ConstCheck should warn for this // Using 'const' here is not allowed, and ConstCheck will warn for this
testConsts("for (const x = 0; x < 2; x++) {}", "x"); testConsts("for (const x = 0; x < 2; x++) {}", "x");
}

public void testForConst1() {
testConsts("for (const x in [1, 2, 3]) {}", "x"); testConsts("for (const x in [1, 2, 3]) {}", "x");
testConsts("for (const x of {a, b, c}) {}", "x"); testConsts("for (const x of {a, b, c}) {}", "x");
} }


public void testForConstJSDoc() {
testConsts("for (/** @const */ let x = 0; x < 2; x++) {}", "x");
testConsts("for (/** @const */ let x in [1, 2, 3]) {}", "x");
testConsts("for (/** @const */ let x of {a, b, c}) {}", "x");
}

public void testFunctionParam() { public void testFunctionParam() {
testConsts("var x = function(){};", "x"); testConsts("var x = function(){};", "x");
testConsts("var x = ()=>{};", "x"); testConsts("var x = ()=>{};", "x");
testConsts("const x = ()=>{};", "x");
testConsts("/** @const */ let x = ()=>{};", "x");
testConsts("function fn(a){var b = a + 1}; ", "a", "b"); testConsts("function fn(a){var b = a + 1}; ", "a", "b");
testConsts("function fn(a = 1){var b = a + 1}; ", "a", "b"); testConsts("function fn(a = 1){var b = a + 1}; ", "a", "b");
testConsts("function fn(a, {b, c}){var d = a + 1}; ", "a", "b", "c", "d"); testConsts("function fn(a, {b, c}){var d = a + 1}; ", "a", "b", "c", "d");
} }


public void testClass() { public void testClass() {
testConsts("var Foo = class {}", "Foo"); testConsts("var Foo = class {}", "Foo");
testConsts("const Foo = class {}", "Foo");
testConsts("class Foo {}", "Foo"); testConsts("class Foo {}", "Foo");
testConsts("var Foo = function() {};", "Foo"); testConsts("var Foo = function() {};", "Foo");
testConsts("const Foo = function() {}", "Foo");
testConsts("function Foo() {}", "Foo"); testConsts("function Foo() {}", "Foo");
} }


public void testArguments() { public void testVarArguments() {
testConsts("var arguments = 3;", "arguments"); testConsts("var arguments = 3;", "arguments");
} }


public void testConstArguments() {
testConsts("const arguments = 4;", "arguments");
}

public void testArgumentsJSDoc() {
testConsts("/** @const */let arguments = 5;", "arguments");
}

public void testDestructuring() { public void testDestructuring() {
testConsts("var [a, b, c] = [1, 2, 3];", "a", "b", "c"); testConsts("var [a, b, c] = [1, 2, 3];", "a", "b", "c");
testConsts("const [a, b, c] = [1, 2, 3];", "a", "b", "c");
testNotConsts("var [a, b, c] = obj;", "obj"); testNotConsts("var [a, b, c] = obj;", "obj");
testNotConsts("" testNotConsts(""
+ "var [a, b, c] = [1, 2, 3];" + "var [a, b, c] = [1, 2, 3];"
Expand All @@ -132,6 +154,9 @@ public void testDestructuring() {
+ "var [a, b, c] = [1, 2, 3];" + "var [a, b, c] = [1, 2, 3];"
+ "[a, b]= [1, 2];", "c"); + "[a, b]= [1, 2];", "c");


testNotConsts("var [a, b, c] = [1, 2, 3]; [a, b] = [1, 2];", "a", "b");
testConsts("var [a, b, c] = [1, 2, 3]; [a, b] = [1, 2];", "c");

testConsts("var {a: b} = {a: 1}", "b"); testConsts("var {a: b} = {a: 1}", "b");
testNotConsts("var {a: b} = {a: 1}", "a"); testNotConsts("var {a: b} = {a: 1}", "a");
// Note that this "a" looks for the destructured "a" // Note that this "a" looks for the destructured "a"
Expand All @@ -143,6 +168,9 @@ public void testDestructuring() {
testNotConsts("" testNotConsts(""
+ "let fg = '', bg = '';" + "let fg = '', bg = '';"
+ "({fg, bg} = pal[val - 1]);", "fg", "bg"); + "({fg, bg} = pal[val - 1]);", "fg", "bg");

testConsts("var [a, , b] = [1, 2, 3];", "a", "b");
testConsts("const [a, , b] = [1, 2, 3];", "a", "b");
} }


public void testDefaultValue() { public void testDefaultValue() {
Expand All @@ -157,6 +185,27 @@ public void testVarInBlock() {
testConsts("function f() { if (true) { var x = function() {}; x(); } }", "x"); testConsts("function f() { if (true) { var x = function() {}; x(); } }", "x");
} }


public void testGeneratorFunctionVar() {
testNotConsts(
LINE_JOINER.join(
"function *gen() {",
" var x = 0; ",
" while (x < 3)",
" yield x++;",
"}"),
"x");
}

public void testGeneratorFunctionConst() {
testConsts(
LINE_JOINER.join(
"function *gen() {",
" var x = 0;",
" yield x;",
"}"),
"x");
}

private void testConsts(String js, String... constants) { private void testConsts(String js, String... constants) {
testInferConstsHelper(true, js, constants); testInferConstsHelper(true, js, constants);
} }
Expand Down

0 comments on commit f9c5c67

Please sign in to comment.