/
arrow-body-style.js
87 lines (82 loc) · 3.88 KB
/
arrow-body-style.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
/**
* @fileoverview Tests for arrow-body-style
* @author Alberto Rodríguez
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
var rule = require("../../../lib/rules/arrow-body-style"),
RuleTester = require("../../../lib/testers/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
var ruleTester = new RuleTester();
ruleTester.run("arrow-body-style", rule, {
valid: [
{ code: "var foo = () => {};", parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = () => 0;", parserOptions: { ecmaVersion: 6 } },
{ code: "var addToB = (a) => { b = b + a };", parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = () => { /* do nothing */ };", parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = () => {\n /* do nothing */ \n};", parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = (retv, name) => {\nretv[name] = true;\nreturn retv;\n};", parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = () => ({});", parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = () => { bar(); };", parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = () => { b = a };", parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = () => { bar: 1 };", parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = () => { return 0; };", parserOptions: { ecmaVersion: 6 }, options: ["always"] },
{ code: "var foo = () => { return bar(); };", parserOptions: { ecmaVersion: 6 }, options: ["always"] },
{ code: "var foo = () => 0;", parserOptions: { ecmaVersion: 6 }, options: ["never"] },
{ code: "var foo = () => ({ foo: 0 });", parserOptions: { ecmaVersion: 6 }, options: ["never"] }
],
invalid: [
{
code: "var foo = () => 0;",
parserOptions: { ecmaVersion: 6 },
options: ["always"],
errors: [
{ line: 1, column: 17, type: "ArrowFunctionExpression", message: "Expected block statement surrounding arrow body." }
]
},
{
code: "var foo = () => ({});",
parserOptions: { ecmaVersion: 6 },
options: ["always"],
errors: [
{ line: 1, column: 18, type: "ArrowFunctionExpression", message: "Expected block statement surrounding arrow body." }
]
},
{
code: "var foo = () => { return 0; };",
parserOptions: { ecmaVersion: 6 },
options: ["as-needed"],
errors: [
{ line: 1, column: 17, type: "ArrowFunctionExpression", message: "Unexpected block statement surrounding arrow body." }
]
},
{
code: "var foo = () => { return bar(); };",
parserOptions: { ecmaVersion: 6 },
options: ["as-needed"],
errors: [
{ line: 1, column: 17, type: "ArrowFunctionExpression", message: "Unexpected block statement surrounding arrow body." }
]
},
{
code: "var foo = () => {\nreturn 0;\n};",
parserOptions: { ecmaVersion: 6 },
options: ["never"],
errors: [
{ line: 1, column: 17, type: "ArrowFunctionExpression", message: "Unexpected block statement surrounding arrow body." }
]
},
{
code: "var foo = (retv, name) => {\nretv[name] = true;\nreturn retv;\n};",
parserOptions: { ecmaVersion: 6 },
options: ["never"],
errors: [
{ line: 1, column: 27, type: "ArrowFunctionExpression", message: "Unexpected block statement surrounding arrow body." }
]
}
]
});