/
getter-return.js
96 lines (77 loc) · 5.6 KB
/
getter-return.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* @fileoverview Enforces that a return statement is present in property getters.
* @author Aladdin-ADD(hh_2013@foxmail.com)
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/getter-return");
const RuleTester = require("../../../lib/testers/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
// data is not working, so specify a name: "getter 'bar'"
const name = "getter 'bar'";
const noReturnMessage = `Expected to return a value in ${name}.`;
const noLastReturnMessage = `Expected to return a value at the end of ${name}.`;
const parserOptions = { ecmaVersion: 6 };
const options = [{ noImplicit: true }];
ruleTester.run("getter-return", rule, {
valid: [
// test obj: get, option: {noImplicit: false}
{ code: "var foo = { get bar(){return true;} };" },
{ code: "var foo = { get bar(){return;} };" },
{ code: "var foo = { bar: function(){return true;} };" },
{ code: "var foo = { bar: function(){return;} };" },
{ code: "var foo = { bar(){return true;} };", parserOptions },
{ code: "var foo = { bar(){return;} };", parserOptions },
// test class: get, option: {noImplicit: false}
{ code: "class foo { get bar(){return true;} }", parserOptions },
{ code: "class foo { get bar(){if(baz){return true;} else {return false;} } }", parserOptions },
{ code: "class foo { get bar(){if(baz){return;} else {return false;} } }", parserOptions },
{ code: "class foo { get(){return true;} }", parserOptions },
{ code: "var foo = { get bar(){if(bar) {return;} return true;} };", parserOptions, errors: [] },
// test object.defineProperty(s), option: {noImplicit: false}
{ code: "Object.defineProperty(foo, \"bar\", { get: function () {return true;}});" },
{ code: "Object.defineProperty(foo, \"bar\", { get: function () {return;}});" },
{ code: "Object.defineProperies(foo, { bar: { get: function () {return true;}} });" },
{ code: "Object.defineProperies(foo, { bar: { get: function () {return;}} });" },
// test option: {noImplicit: true}
{ code: "var foo = { get bar(){return true;} };", options },
{ code: "class foo { get bar(){return true;} }", options, parserOptions },
{ code: "Object.defineProperty(foo, \"bar\", { get: function () {return true;}});", options },
{ code: "Object.defineProperies(foo, { bar: { get: function () {return true;}} });", options },
// not getter.
{ code: "var get = function(){};" },
{ code: "var get = function(){ return true; };" },
{ code: "var foo = { bar: function(){} };" },
{ code: "var foo = { bar: function(){ return true; } };" },
{ code: "var foo = { bar(){} };", parserOptions },
{ code: "var foo = { bar(){ return true; } };", parserOptions }
],
invalid: [
// test obj: get
{ code: "var foo = { get bar() {} };", parserOptions, errors: [{ message: noReturnMessage }] },
{ code: "var foo = { get bar() {return;} };", options, parserOptions, errors: [{ message: noReturnMessage }] },
{ code: "var foo = { get bar() {return; ;} };", options, parserOptions, errors: [{ message: noReturnMessage }] },
{ code: "var foo = { get bar() {return; return 1;} };", options, parserOptions, errors: [{ message: noReturnMessage }] },
{ code: "var foo = { get bar(){if(bar) {return true;}} };", parserOptions, errors: [{ message: noLastReturnMessage }] },
{ code: "var foo = { get bar(){if(bar) {return true;} return;} };", options, parserOptions, errors: [{ message: noReturnMessage }] },
{ code: "var foo = { get bar(){if(bar) {return true;} ;} };", parserOptions, errors: [{ message: noLastReturnMessage }] },
{ code: "var foo = { get bar(){if(bar) {return;} return true;} };", options, parserOptions, errors: [{ message: noReturnMessage }] },
// test class: get
{ code: "class foo { get bar(){} }", parserOptions, errors: [{ message: noReturnMessage }] },
{ code: "class foo { get bar(){} }", options, parserOptions, errors: [{ message: noReturnMessage }] },
{ code: "class foo { get bar(){return;} }", options, parserOptions, errors: [{ message: noReturnMessage }] },
{ code: "class foo { get bar(){if(bar) {return true;}} }", options, parserOptions, errors: [{ message: noLastReturnMessage }] },
{ code: "class foo { get bar(){if(bar) {return;} return true;} }", options, parserOptions, errors: [{ message: noReturnMessage }] },
// test object.defineProperty
{ code: "Object.defineProperty(foo, \"bar\", { get: function (){return;}});", options, errors: [{ message: "Expected to return a value in method 'get'." }] },
{ code: "Object.defineProperty(foo, \"bar\", { get: function (){if(bar) {return true;}}});", errors: [{ message: "Expected to return a value at the end of method 'get'." }] },
{ code: "Object.defineProperty(foo, \"bar\", { get: function (){if(bar) {return;} return true;}});", options: [{ noImplicit: true }], errors: [{ message: "Expected to return a value in method 'get'." }] },
// test option: {noImplicit: true}
{ code: "Object.defineProperty(foo, \"bar\", { get: function (){return;}});", options, errors: [{ message: "Expected to return a value in method 'get'." }] }
]
});