]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for no-return-assign. | |
3 | * @author Ilya Volodin | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/no-return-assign"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } }); | |
20 | ||
21 | ruleTester.run("no-return-assign", rule, { | |
22 | valid: [ | |
23 | { | |
24 | code: "module.exports = {'a': 1};", | |
25 | parserOptions: { | |
26 | sourceType: "module" | |
27 | } | |
28 | }, | |
29 | "var result = a * b;", | |
30 | "function x() { var result = a * b; return result; }", | |
31 | "function x() { return (result = a * b); }", | |
32 | { | |
33 | code: "function x() { var result = a * b; return result; }", | |
34 | options: ["except-parens"] | |
35 | }, | |
36 | { | |
37 | code: "function x() { return (result = a * b); }", | |
38 | options: ["except-parens"] | |
39 | }, | |
40 | { | |
41 | code: "function x() { var result = a * b; return result; }", | |
42 | options: ["always"] | |
43 | }, | |
44 | { | |
45 | code: "function x() { return function y() { result = a * b }; }", | |
46 | options: ["always"] | |
47 | }, | |
48 | { | |
49 | code: "() => { return (result = a * b); }", | |
50 | options: ["except-parens"] | |
51 | }, | |
52 | { | |
53 | code: "() => (result = a * b)", | |
54 | options: ["except-parens"] | |
56c4a2cb DC |
55 | }, |
56 | "const foo = (a,b,c) => ((a = b), c)", | |
57 | `function foo(){ | |
58 | return (a = b) | |
59 | }`, | |
60 | `function bar(){ | |
61 | return function foo(){ | |
62 | return (a = b) && c | |
63 | } | |
64 | }`, | |
65 | { | |
66 | code: "const foo = (a) => (b) => (a = b)", | |
67 | parserOptions: { ecmaVersion: 6 } | |
eb39fafa DC |
68 | } |
69 | ], | |
70 | invalid: [ | |
71 | { | |
72 | code: "function x() { return result = a * b; };", | |
73 | errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] | |
74 | }, | |
75 | { | |
76 | code: "function x() { return (result) = (a * b); };", | |
77 | errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] | |
78 | }, | |
79 | { | |
80 | code: "function x() { return result = a * b; };", | |
81 | options: ["except-parens"], | |
82 | errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] | |
83 | }, | |
84 | { | |
85 | code: "function x() { return (result) = (a * b); };", | |
86 | options: ["except-parens"], | |
87 | errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] | |
88 | }, | |
89 | { | |
90 | code: "() => { return result = a * b; }", | |
91 | errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] | |
92 | }, | |
93 | { | |
94 | code: "() => result = a * b", | |
56c4a2cb DC |
95 | errors: [ |
96 | { | |
97 | messageId: "arrowAssignment", | |
98 | type: "ArrowFunctionExpression" | |
99 | } | |
100 | ] | |
eb39fafa DC |
101 | }, |
102 | { | |
103 | code: "function x() { return result = a * b; };", | |
104 | options: ["always"], | |
105 | errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] | |
106 | }, | |
107 | { | |
108 | code: "function x() { return (result = a * b); };", | |
109 | options: ["always"], | |
110 | errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] | |
111 | }, | |
112 | { | |
113 | code: "function x() { return result || (result = a * b); };", | |
114 | options: ["always"], | |
115 | errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] | |
56c4a2cb DC |
116 | }, |
117 | { | |
118 | code: `function foo(){ | |
119 | return a = b | |
120 | }`, | |
121 | errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] | |
122 | }, | |
123 | { | |
124 | code: `function doSomething() { | |
125 | return foo = bar && foo > 0; | |
126 | }`, | |
127 | errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] | |
128 | }, | |
129 | { | |
130 | code: `function doSomething() { | |
131 | return foo = function(){ | |
132 | return (bar = bar1) | |
133 | } | |
134 | }`, | |
135 | errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] | |
136 | }, | |
137 | { | |
138 | code: `function doSomething() { | |
139 | return foo = () => a | |
140 | }`, | |
141 | parserOptions: { ecmaVersion: 6 }, | |
142 | errors: [ | |
143 | { | |
144 | messageId: "returnAssignment", | |
145 | type: "ReturnStatement" | |
146 | } | |
147 | ] | |
148 | }, | |
149 | { | |
150 | code: `function doSomething() { | |
151 | return () => a = () => b | |
152 | }`, | |
153 | parserOptions: { ecmaVersion: 6 }, | |
154 | errors: [ | |
155 | { | |
156 | messageId: "arrowAssignment", | |
157 | type: "ArrowFunctionExpression" | |
158 | } | |
159 | ] | |
160 | }, | |
161 | { | |
162 | code: `function foo(a){ | |
163 | return function bar(b){ | |
164 | return a = b | |
165 | } | |
166 | }`, | |
167 | errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] | |
168 | }, | |
169 | { | |
170 | code: "const foo = (a) => (b) => a = b", | |
171 | parserOptions: { ecmaVersion: 6 }, | |
172 | errors: [ | |
173 | { | |
174 | messageId: "arrowAssignment", | |
175 | type: "ArrowFunctionExpression" | |
176 | } | |
177 | ] | |
eb39fafa DC |
178 | } |
179 | ] | |
180 | }); |