Skip to content

Commit

Permalink
Adding ES6 module checks for each warning type in CheckJSDoc pass. Pa…
Browse files Browse the repository at this point in the history
…rt of verification/adding of ES6 module compatibility to compiler.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=159291258
  • Loading branch information
hjkaria authored and blickly committed Jun 19, 2017
1 parent a0fc1de commit 327c18e
Showing 1 changed file with 193 additions and 1 deletion.
194 changes: 193 additions & 1 deletion test/com/google/javascript/jscomp/CheckJsDocTest.java
Expand Up @@ -63,6 +63,14 @@ public void testInlineJsDoc_ES6() {
testWarning("function f([/** number */ x]) {}", MISPLACED_ANNOTATION); testWarning("function f([/** number */ x]) {}", MISPLACED_ANNOTATION);
} }


public void testValidInlineJsDoc_ES6_withES6Modules() {
testSame("export function f(/** string */ x) {};");
}

public void testInvalidInlineJsDoc_ES6_withES6Modules() {
testWarning("export function f([/** number */ x]) {};", MISPLACED_ANNOTATION);
}

// TODO(tbreisacher): These should be a MISPLACED_ANNOTATION warning instead of silently failing. // TODO(tbreisacher): These should be a MISPLACED_ANNOTATION warning instead of silently failing.
public void testInlineJsDocInsideObjectParams() { public void testInlineJsDocInsideObjectParams() {
testSame("function f({ prop: {/** string */ x} }) {}"); testSame("function f({ prop: {/** string */ x} }) {}");
Expand All @@ -71,6 +79,10 @@ public void testInlineJsDocInsideObjectParams() {
testSame("function f({ prop: /** number */ x }) {}"); testSame("function f({ prop: /** number */ x }) {}");
} }


public void testInlineJsDocInsideObjectParams_withES6Modules() {
testSame("export function f({ prop: {/** string */ x} }) {};");
}

public void testInvalidClassJsdoc() { public void testInvalidClassJsdoc() {
testSame("class Foo { /** @param {number} x */ constructor(x) {}}"); testSame("class Foo { /** @param {number} x */ constructor(x) {}}");


Expand All @@ -91,6 +103,15 @@ public void testInvalidClassJsdoc() {
DISALLOWED_MEMBER_JSDOC); DISALLOWED_MEMBER_JSDOC);
} }


public void testValidClassJsdoc_withES6Modules() {
testSame("export class Foo { /** @param {number} x */ constructor(x) {}; };");
}

public void testInvalidClassJsdoc_withES6Modules() {
testWarning(
"export class Foo { /** @constructor */ constructor() {}; };", DISALLOWED_MEMBER_JSDOC);
}

public void testMisplacedParamAnnotation() { public void testMisplacedParamAnnotation() {
testWarning(LINE_JOINER.join( testWarning(LINE_JOINER.join(
"/** @param {string} x */ var Foo = goog.defineClass(null, {", "/** @param {string} x */ var Foo = goog.defineClass(null, {",
Expand All @@ -103,6 +124,15 @@ public void testMisplacedParamAnnotation() {
"};"), MISPLACED_ANNOTATION); "};"), MISPLACED_ANNOTATION);
} }


public void testMisplacedParamAnnotation_withES6Modules() {
testWarning(
LINE_JOINER.join(
"export /** @param {string} x */ var Foo = goog.defineClass(null, {",
" constructor(x) {}",
"});"),
MISPLACED_ANNOTATION);
}

public void testAbstract_method() { public void testAbstract_method() {
testSame("class Foo { /** @abstract */ doSomething() {}}"); testSame("class Foo { /** @abstract */ doSomething() {}}");
testSame(LINE_JOINER.join( testSame(LINE_JOINER.join(
Expand All @@ -122,13 +152,26 @@ public void testAbstract_method() {
"Foo.prototype.something = function() {}")); "Foo.prototype.something = function() {}"));
} }


public void testAbstract_method_withES6Modules() {
testSame("export class Foo { /** @abstract */ doSomething() {}; };");
}

public void testAbstract_getter_setter() { public void testAbstract_getter_setter() {
testSame("class Foo { /** @abstract */ get foo() {}}"); testSame("class Foo { /** @abstract */ get foo() {}}");
testSame("class Foo { /** @abstract */ set foo(val) {}}"); testSame("class Foo { /** @abstract */ set foo(val) {}}");
testWarning("class Foo { /** @abstract */ static get foo() {}}", MISPLACED_ANNOTATION); testWarning("class Foo { /** @abstract */ static get foo() {}}", MISPLACED_ANNOTATION);
testWarning("class Foo { /** @abstract */ static set foo(val) {}}", MISPLACED_ANNOTATION); testWarning("class Foo { /** @abstract */ static set foo(val) {}}", MISPLACED_ANNOTATION);
} }


public void testValidAbstract_getter_setter_withES6Modules() {
testSame("export class Foo { /** @abstract */ get foo() {}; };");
}

public void testInvalidAbstract_getter_setter_withES6Modules() {
testWarning(
"export class Foo { /** @abstract */ static get foo() {}; };", MISPLACED_ANNOTATION);
}

public void testAbstract_nonEmptyMethod() { public void testAbstract_nonEmptyMethod() {
testWarning( testWarning(
"class Foo { /** @abstract */ doSomething() { return 0; }}", "class Foo { /** @abstract */ doSomething() { return 0; }}",
Expand All @@ -142,6 +185,12 @@ public void testAbstract_nonEmptyMethod() {
MISPLACED_ANNOTATION); MISPLACED_ANNOTATION);
} }


public void testAbstract_nonEmptyMethod_withES6Modules() {
testWarning(
"export class Foo { /** @abstract */ doSomething() { return 0; }; };",
MISPLACED_ANNOTATION);
}

public void testAbstract_staticMethod() { public void testAbstract_staticMethod() {
testWarning( testWarning(
"class Foo { /** @abstract */ static doSomething() {}}", "class Foo { /** @abstract */ static doSomething() {}}",
Expand All @@ -155,6 +204,11 @@ public void testAbstract_staticMethod() {
MISPLACED_ANNOTATION); MISPLACED_ANNOTATION);
} }


public void testAbstract_staticMethod_withES6Modules() {
testWarning(
"export class Foo { /** @abstract */ static doSomething() {}; };", MISPLACED_ANNOTATION);
}

public void testAbstract_class() { public void testAbstract_class() {
testSame("/** @abstract */ class Foo { constructor() {}}"); testSame("/** @abstract */ class Foo { constructor() {}}");
testSame("/** @abstract */ exports.Foo = class {}"); testSame("/** @abstract */ exports.Foo = class {}");
Expand All @@ -164,6 +218,10 @@ public void testAbstract_class() {
testSame("/** @abstract @constructor */ var Foo = function() { var x = 1; };"); testSame("/** @abstract @constructor */ var Foo = function() { var x = 1; };");
} }


public void testAbstract_class_withES6Modules() {
testSame("export /** @abstract */ class Foo { constructor() {}; };");
}

public void testAbstract_defineClass() { public void testAbstract_defineClass() {
testSame("/** @abstract */ goog.defineClass(null, { constructor: function() {} });"); testSame("/** @abstract */ goog.defineClass(null, { constructor: function() {} });");
testSame("/** @abstract */ var Foo = goog.defineClass(null, { constructor: function() {} });"); testSame("/** @abstract */ var Foo = goog.defineClass(null, { constructor: function() {} });");
Expand All @@ -187,6 +245,17 @@ public void testAbstract_defineClass() {
"});"), MISPLACED_ANNOTATION); "});"), MISPLACED_ANNOTATION);
} }


public void testValidAbstract_defineClass_withES6Modules() {
testSame(
LINE_JOINER.join(
"export /** @abstract */ var Foo = goog.defineClass(null, {",
"constructor: function() {} });"));
}

public void testInvalidAbstract_defineClass_withES6Modules() {
testWarning("export /** @abstract */ var Foo;", MISPLACED_ANNOTATION);
}

public void testAbstract_constructor() { public void testAbstract_constructor() {
testWarning( testWarning(
"class Foo { /** @abstract */ constructor() {}}", "class Foo { /** @abstract */ constructor() {}}",
Expand All @@ -198,6 +267,17 @@ public void testAbstract_constructor() {
"/** @constructor */ var C = foo(); /** @abstract */ C.prototype.method = function() {};"); "/** @constructor */ var C = foo(); /** @abstract */ C.prototype.method = function() {};");
} }


public void testInvalidAbstract_constructor_withES6Modules() {
testWarning("export class Foo { /** @abstract */ constructor() {}; };", MISPLACED_ANNOTATION);
}

public void testValidAbstract_constructor_withES6Modules() {
testSame(
LINE_JOINER.join(
"export /** @constructor */ var C = foo();",
"/** @abstract */ C.prototype.method = function() {};"));
}

public void testAbstract_field() { public void testAbstract_field() {
testWarning( testWarning(
"class Foo { constructor() { /** @abstract */ this.x = 1;}}", "class Foo { constructor() { /** @abstract */ this.x = 1;}}",
Expand All @@ -212,6 +292,12 @@ public void testAbstract_field() {
MISPLACED_ANNOTATION); MISPLACED_ANNOTATION);
} }


public void testAbstract_field_withES6Modules() {
testWarning(
"export class Foo { constructor() { /** @abstract */ this.x = 1; }; };",
MISPLACED_ANNOTATION);
}

public void testAbstract_var() { public void testAbstract_var() {
testWarning( testWarning(
"class Foo { constructor() {/** @abstract */ var x = 1;}}", "class Foo { constructor() {/** @abstract */ var x = 1;}}",
Expand All @@ -226,6 +312,12 @@ public void testAbstract_var() {
MISPLACED_ANNOTATION); MISPLACED_ANNOTATION);
} }


public void testAbstract_var_withES6Modules() {
testWarning(
"export class Foo { constructor() { /** @abstract */ var x = 1; }; };",
MISPLACED_ANNOTATION);
}

public void testAbstract_function() { public void testAbstract_function() {
testWarning( testWarning(
"class Foo { constructor() {/** @abstract */ var x = function() {};}}", "class Foo { constructor() {/** @abstract */ var x = function() {};}}",
Expand All @@ -240,6 +332,12 @@ public void testAbstract_function() {
MISPLACED_ANNOTATION); MISPLACED_ANNOTATION);
} }


public void testAbstract_function_withES6Modules() {
testWarning(
"export class Foo { constructor() { /** @abstract */ var x = function() {}; }}",
MISPLACED_ANNOTATION);
}

public void testInlineJSDoc() { public void testInlineJSDoc() {
testSame("function f(/** string */ x) {}"); testSame("function f(/** string */ x) {}");
testSame("function f(/** @type {string} */ x) {}"); testSame("function f(/** @type {string} */ x) {}");
Expand All @@ -250,6 +348,10 @@ public void testInlineJSDoc() {
testSame("var /** @type {string} */ x, /** @type {number} */ y;"); testSame("var /** @type {string} */ x, /** @type {number} */ y;");
} }


public void testInlineJSDoc_withES6Modules() {
testSame("export function f(/** string */ x) {}");
}

public void testFunctionJSDocOnMethods() { public void testFunctionJSDocOnMethods() {
testSame("class Foo { /** @return {?} */ bar() {} }"); testSame("class Foo { /** @return {?} */ bar() {} }");
testSame("class Foo { /** @return {?} */ static bar() {} }"); testSame("class Foo { /** @return {?} */ static bar() {} }");
Expand All @@ -262,27 +364,55 @@ public void testFunctionJSDocOnMethods() {
testSame("class Foo { /** @return {?} x */ set [bar](x) {} }"); testSame("class Foo { /** @return {?} x */ set [bar](x) {} }");
} }


public void testValidFunctionJSDocOnMethods_withES6Modules() {
testSame("export class Foo { /** @return {?} */ bar() {} }");
}

public void testObjectLiterals() { public void testObjectLiterals() {
testSame("var o = { /** @type {?} */ x: y };"); testSame("var o = { /** @type {?} */ x: y };");
testWarning("var o = { x: /** @type {?} */ y };", MISPLACED_ANNOTATION); testWarning("var o = { x: /** @type {?} */ y };", MISPLACED_ANNOTATION);
} }


public void testValidObjectLiterals_withES6Modules() {
testSame("export var o = { /** @type {?} */ x: y };");
}

public void testInvalidObjectLiterals_withES6Modules() {
testWarning("export var o = { x: /** @type {?} */ y };", MISPLACED_ANNOTATION);
}

public void testMethodsOnObjectLiterals() { public void testMethodsOnObjectLiterals() {
testSame("var x = { /** @return {?} */ foo() {} };"); testSame("var x = { /** @return {?} */ foo() {} };");
testSame("var x = { /** @return {?} */ [foo]() {} };"); testSame("var x = { /** @return {?} */ [foo]() {} };");
testSame("var x = { /** @return {?} */ foo: someFn };"); testSame("var x = { /** @return {?} */ foo: someFn };");
testSame("var x = { /** @return {?} */ [foo]: someFn };"); testSame("var x = { /** @return {?} */ [foo]: someFn };");
} }


public void testMethodsOnObjectLiterals_withES6Modules() {
testSame("export var x = { /** @return {?} */ foo() {} };");
}

public void testExposeDeprecated() { public void testExposeDeprecated() {
testWarning("/** @expose */ var x = 0;", ANNOTATION_DEPRECATED); testWarning("/** @expose */ var x = 0;", ANNOTATION_DEPRECATED);
} }


public void testExposeDeprecated_withES6Modules() {
testWarning("export /** @expose */ var x = 0;", ANNOTATION_DEPRECATED);
}

public void testJSDocFunctionNodeAttachment() { public void testJSDocFunctionNodeAttachment() {
testWarning("var a = /** @param {number} index */5;" testWarning("var a = /** @param {number} index */5;"
+ "/** @return boolean */function f(index){}", MISPLACED_ANNOTATION); + "/** @return boolean */function f(index){}", MISPLACED_ANNOTATION);
} }


public void testJSDocFunctionNodeAttachment_withES6Modules() {
testWarning(
LINE_JOINER.join(
"export var a = /** @param {number} index */ 5;",
"export /** @return boolean */ function f(index){}"),
MISPLACED_ANNOTATION);
}

public void testJSDocDescAttachment() { public void testJSDocDescAttachment() {
testWarning( testWarning(
"function f() { return /** @type {string} */ (g(1 /** @desc x */)); };", "function f() { return /** @type {string} */ (g(1 /** @desc x */)); };",
Expand Down Expand Up @@ -311,6 +441,11 @@ public void testJSDocTypeAttachment() {
"function f() { /** @type {string} */ return; };", MISPLACED_ANNOTATION); "function f() { /** @type {string} */ return; };", MISPLACED_ANNOTATION);
} }


public void testJSDocTypeAttachment_withES6Modules() {
testWarning(
"export function f() { /** @type {string} */ if (true) return; };", MISPLACED_ANNOTATION);
}

public void testJSDocOnExports() { public void testJSDocOnExports() {
testSame(LINE_JOINER.join( testSame(LINE_JOINER.join(
"goog.module('foo');", "goog.module('foo');",
Expand Down Expand Up @@ -364,6 +499,11 @@ public void testMisplacedTypeAnnotation7() {
MISPLACED_ANNOTATION); MISPLACED_ANNOTATION);
} }


public void testMisplacedTypeAnnotation_withES6Modules() {
testWarning(
"export var o = {}; /** @type {string} */ o.prop1 = 1, o.prop2 = 2;", MISPLACED_ANNOTATION);
}

public void testAllowedNocollapseAnnotation1() { public void testAllowedNocollapseAnnotation1() {
testSame("var foo = {}; /** @nocollapse */ foo.bar = true;"); testSame("var foo = {}; /** @nocollapse */ foo.bar = true;");
} }
Expand All @@ -375,13 +515,25 @@ public void testAllowedNocollapseAnnotation2() {
+ "/** @nocollapse */ ns.bar = Foo.prototype.blah;"); + "/** @nocollapse */ ns.bar = Foo.prototype.blah;");
} }


public void testMisplacedNocollapseAnnotation1() { public void testAllowedNocollapseAnnotation_withES6Modules() {
testSame("export var foo = {}; /** @nocollapse */ foo.bar = true;");
}

public void testMisplacedNocollapseAnnotation() {
testWarning( testWarning(
"/** @constructor */ function foo() {};" "/** @constructor */ function foo() {};"
+ "/** @nocollapse */ foo.prototype.bar = function() {};", + "/** @nocollapse */ foo.prototype.bar = function() {};",
MISPLACED_ANNOTATION); MISPLACED_ANNOTATION);
} }


public void testMisplacedNocollapseAnnotation_withES6Modules() {
testWarning(
LINE_JOINER.join(
"export /** @constructor */ function foo() {};",
"/** @nocollapse */ foo.prototype.bar = function() {};"),
MISPLACED_ANNOTATION);
}

public void testNocollapseInExterns() { public void testNocollapseInExterns() {
testSame("var foo = {}; /** @nocollapse */ foo.bar = true;", testSame("var foo = {}; /** @nocollapse */ foo.bar = true;",
"foo.bar;", MISPLACED_ANNOTATION); "foo.bar;", MISPLACED_ANNOTATION);
Expand All @@ -400,11 +552,24 @@ public void testArrowFuncAsConstructor() {
ARROW_FUNCTION_AS_CONSTRUCTOR); ARROW_FUNCTION_AS_CONSTRUCTOR);
} }


public void testArrowFuncAsConstructor_withES6Modules() {
testWarning(
"export /** @constructor */ var a = ()=>{}; var b = a();", ARROW_FUNCTION_AS_CONSTRUCTOR);
}

public void testDefaultParam() { public void testDefaultParam() {
testWarning("function f(/** number */ x=0) {}", DEFAULT_PARAM_MUST_BE_MARKED_OPTIONAL); testWarning("function f(/** number */ x=0) {}", DEFAULT_PARAM_MUST_BE_MARKED_OPTIONAL);
testSame("function f(/** number= */ x=0) {}"); testSame("function f(/** number= */ x=0) {}");
} }


public void testInvalidDefaultParam_withES6Modules() {
testWarning("export function f(/** number */ x=0) {}", DEFAULT_PARAM_MUST_BE_MARKED_OPTIONAL);
}

public void testValidDefaultParam_withES6Modules() {
testSame("export function f(/** number= */ x=0) {}");
}

private void testBadTemplate(String code) { private void testBadTemplate(String code) {
testWarning(code, MISPLACED_ANNOTATION); testWarning(code, MISPLACED_ANNOTATION);
} }
Expand All @@ -431,6 +596,13 @@ public void testGoodTemplate4() {
testSame("x.y.z = goog.defineClass(null, {/** @return T @template T */ m: function() {}});"); testSame("x.y.z = goog.defineClass(null, {/** @return T @template T */ m: function() {}});");
} }


public void testGoodTemplate_withES6Modules() {
testSame(
LINE_JOINER.join(
"export class C { /** @template T \n @param {T} a\n @param {T} b \n */ ",
"constructor(a,b){} }"));
}

public void testBadTemplate1() { public void testBadTemplate1() {
testBadTemplate("/** @template T */ foo();"); testBadTemplate("/** @template T */ foo();");
} }
Expand All @@ -448,6 +620,10 @@ public void testBadTemplate3() {
testBadTemplate("/** @template T */ Foo.prototype.f = function() {};"); testBadTemplate("/** @template T */ Foo.prototype.f = function() {};");
} }


public void testBadTemplate_withES6Modules() {
testBadTemplate("export /** @template T */ function f() {}");
}

public void testBadTypedef() { public void testBadTypedef() {
testWarning( testWarning(
"/** @typedef {{foo: string}} */ class C { constructor() { this.foo = ''; }}", "/** @typedef {{foo: string}} */ class C { constructor() { this.foo = ''; }}",
Expand All @@ -462,6 +638,12 @@ public void testBadTypedef() {
MISPLACED_ANNOTATION); MISPLACED_ANNOTATION);
} }


public void testBadTypedef_withES6Modules() {
testWarning(
"export /** @typedef {{foo: string}} */ class C { constructor() { this.foo = ''; }}",
MISPLACED_ANNOTATION);
}

public void testInvalidAnnotation1() throws Exception { public void testInvalidAnnotation1() throws Exception {
testWarning("/** @nosideeffects */ function foo() {}", INVALID_NO_SIDE_EFFECT_ANNOTATION); testWarning("/** @nosideeffects */ function foo() {}", INVALID_NO_SIDE_EFFECT_ANNOTATION);
} }
Expand All @@ -486,7 +668,17 @@ public void testInvalidAnnotation5() throws Exception {
INVALID_NO_SIDE_EFFECT_ANNOTATION); INVALID_NO_SIDE_EFFECT_ANNOTATION);
} }


public void testInvalidAnnotation_withES6Modules() {
testWarning(
"export /** @nosideeffects */ function foo() {}", INVALID_NO_SIDE_EFFECT_ANNOTATION);
}

public void testInvalidModifiesAnnotation() throws Exception { public void testInvalidModifiesAnnotation() throws Exception {
testWarning("/** @modifies {this} */ var f = function() {};", INVALID_MODIFIES_ANNOTATION); testWarning("/** @modifies {this} */ var f = function() {};", INVALID_MODIFIES_ANNOTATION);
} }

public void testInvalidModifiesAnnotation_withES6Modules() {
testWarning(
"export /** @modifies {this} */ var f = function() {};", INVALID_MODIFIES_ANNOTATION);
}
} }

0 comments on commit 327c18e

Please sign in to comment.