-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
no-self-assign.js
77 lines (71 loc) · 4.66 KB
/
no-self-assign.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
/**
* @fileoverview Tests for no-self-assign rule.
* @author Toru Nagashima
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-self-assign"),
RuleTester = require("../../../lib/testers/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("no-self-assign", rule, {
valid: [
"var a = a",
"a = b",
"a += a",
"a = +a",
"a = [a]",
{ code: "let a = a", parserOptions: { ecmaVersion: 6 } },
{ code: "const a = a", parserOptions: { ecmaVersion: 6 } },
{ code: "[a] = a", parserOptions: { ecmaVersion: 6 } },
{ code: "[a = 1] = [a]", parserOptions: { ecmaVersion: 6 } },
{ code: "[a, b] = [b, a]", parserOptions: { ecmaVersion: 6 } },
{ code: "[a,, b] = [, b, a]", parserOptions: { ecmaVersion: 6 } },
{ code: "[x, a] = [...x, a]", parserOptions: { ecmaVersion: 6 } },
{ code: "[a, b] = {a, b}", parserOptions: { ecmaVersion: 6 } },
{ code: "({a} = a)", parserOptions: { ecmaVersion: 6 } },
{ code: "({a = 1} = {a})", parserOptions: { ecmaVersion: 6 } },
{ code: "({a: b} = {a})", parserOptions: { ecmaVersion: 6 } },
{ code: "({a} = {a: b})", parserOptions: { ecmaVersion: 6 } },
{ code: "({a} = {a() {}})", parserOptions: { ecmaVersion: 6 } },
{ code: "({a} = {[a]: a})", parserOptions: { ecmaVersion: 6 } },
{ code: "({a, ...b} = {a, ...b})", parserOptions: { ecmaVersion: 2018 } },
{ code: "a.b = a.c", options: [{ props: true }] },
{ code: "a.b = c.b", options: [{ props: true }] },
{ code: "a.b = a[b]", options: [{ props: true }] },
{ code: "a[b] = a.b", options: [{ props: true }] },
{ code: "a.b().c = a.b().c", options: [{ props: true }] },
{ code: "b().c = b().c", options: [{ props: true }] },
{ code: "a[b + 1] = a[b + 1]", options: [{ props: true }] }, // it ignores non-simple computed properties.
"a.b = a.b",
"a.b.c = a.b.c",
"a[b] = a[b]",
"a['b'] = a['b']",
"a[\n 'b'\n] = a[\n 'b'\n]"
],
invalid: [
{ code: "a = a", errors: ["'a' is assigned to itself."] },
{ code: "[a] = [a]", parserOptions: { ecmaVersion: 6 }, errors: ["'a' is assigned to itself."] },
{ code: "[a, b] = [a, b]", parserOptions: { ecmaVersion: 6 }, errors: ["'a' is assigned to itself.", "'b' is assigned to itself."] },
{ code: "[a, b] = [a, c]", parserOptions: { ecmaVersion: 6 }, errors: ["'a' is assigned to itself."] },
{ code: "[a, b] = [, b]", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself."] },
{ code: "[a, ...b] = [a, ...b]", parserOptions: { ecmaVersion: 6 }, errors: ["'a' is assigned to itself.", "'b' is assigned to itself."] },
{ code: "[[a], {b}] = [[a], {b}]", parserOptions: { ecmaVersion: 6 }, errors: ["'a' is assigned to itself.", "'b' is assigned to itself."] },
{ code: "({a} = {a})", parserOptions: { ecmaVersion: 6 }, errors: ["'a' is assigned to itself."] },
{ code: "({a: b} = {a: b})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself."] },
{ code: "({a, b} = {a, b})", parserOptions: { ecmaVersion: 6 }, errors: ["'a' is assigned to itself.", "'b' is assigned to itself."] },
{ code: "({a, b} = {b, a})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself.", "'a' is assigned to itself."] },
{ code: "({a, b} = {c, a})", parserOptions: { ecmaVersion: 6 }, errors: ["'a' is assigned to itself."] },
{ code: "({a: {b}, c: [d]} = {a: {b}, c: [d]})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself.", "'d' is assigned to itself."] },
{ code: "({a, b} = {a, ...x, b})", parserOptions: { ecmaVersion: 2018 }, errors: ["'b' is assigned to itself."] },
{ code: "a.b = a.b", options: [{ props: true }], errors: ["'a.b' is assigned to itself."] },
{ code: "a.b.c = a.b.c", options: [{ props: true }], errors: ["'a.b.c' is assigned to itself."] },
{ code: "a[b] = a[b]", options: [{ props: true }], errors: ["'a[b]' is assigned to itself."] },
{ code: "a['b'] = a['b']", options: [{ props: true }], errors: ["'a['b']' is assigned to itself."] },
{ code: "a[\n 'b'\n] = a[\n 'b'\n]", options: [{ props: true }], errors: ["'a['b']' is assigned to itself."] }
]
});