1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const rule = require("../../rules/no-unused-expressions"),
|
13 | RuleTester = require("../RuleTester");
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | const ruleTester = new RuleTester();
|
20 |
|
21 | ruleTester.run("no-unused-expressions", rule, {
|
22 | valid: [
|
23 |
|
24 | "function f(){}",
|
25 | "a = b",
|
26 | "new a",
|
27 | "{}",
|
28 | "f(); g()",
|
29 | "i++",
|
30 | "a()",
|
31 | { code: "a && a()", options: [{ allowShortCircuit: true }] },
|
32 | { code: "a() || (b = c)", options: [{ allowShortCircuit: true }] },
|
33 | { code: "a ? b() : c()", options: [{ allowTernary: true }] },
|
34 | { code: "a ? b() || (c = d) : e()", options: [{ allowShortCircuit: true, allowTernary: true }] },
|
35 | "delete foo.bar",
|
36 | "void new C",
|
37 | "\"use strict\";",
|
38 | "\"directive one\"; \"directive two\"; f();",
|
39 | "function foo() {\"use strict\"; return true; }",
|
40 | { code: "var foo = () => {\"use strict\"; return true; }", parserOptions: { ecmaVersion: 6 } },
|
41 | "function foo() {\"directive one\"; \"directive two\"; f(); }",
|
42 | "function foo() { var foo = \"use strict\"; return true; }",
|
43 | {
|
44 | code: "function* foo(){ yield 0; }",
|
45 | parserOptions: { ecmaVersion: 6 }
|
46 | },
|
47 | {
|
48 | code: "async function foo() { await 5; }",
|
49 | parserOptions: { ecmaVersion: 8 }
|
50 | },
|
51 | {
|
52 | code: "async function foo() { await foo.bar; }",
|
53 | parserOptions: { ecmaVersion: 8 }
|
54 | },
|
55 | {
|
56 | code: "async function foo() { bar && await baz; }",
|
57 | options: [{ allowShortCircuit: true }],
|
58 | parserOptions: { ecmaVersion: 8 }
|
59 | },
|
60 | {
|
61 | code: "async function foo() { foo ? await bar : await baz; }",
|
62 | options: [{ allowTernary: true }],
|
63 | parserOptions: { ecmaVersion: 8 }
|
64 | },
|
65 | {
|
66 | code: "tag`tagged template literal`",
|
67 | options: [{ allowTaggedTemplates: true }],
|
68 | parserOptions: { ecmaVersion: 6 }
|
69 | },
|
70 | {
|
71 | code: "shouldNotBeAffectedByAllowTemplateTagsOption()",
|
72 | options: [{ allowTaggedTemplates: true }],
|
73 | parserOptions: { ecmaVersion: 6 }
|
74 | },
|
75 |
|
76 |
|
77 | "let a = do { if (foo) { foo.bar; } }",
|
78 | "let a = do { foo; }",
|
79 | "let a = do { let b = 2; foo; }",
|
80 | "let a = do { (foo + 1); }",
|
81 | "let a = do { if (foo) { if (foo.bar) { foo.bar; } } }",
|
82 | "let a = do { if (foo) { if (foo.bar) { foo.bar; } else if (foo.baz) { foo.baz; } } }",
|
83 | "foo.bar?.();",
|
84 |
|
85 | ],
|
86 | invalid: [
|
87 | { code: "0", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
88 | { code: "a", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
89 | { code: "f(), 0", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
90 | { code: "{0}", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
91 | { code: "[]", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
92 | { code: "a && b();", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
93 | { code: "a() || false", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
94 | { code: "a || (b = c)", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
95 | { code: "a ? b() || (c = d) : e", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
96 | {
|
97 | code: "`untagged template literal`",
|
98 | parserOptions: { ecmaVersion: 6 },
|
99 | errors: ["Expected an assignment or function call and instead saw an expression."]
|
100 | },
|
101 | {
|
102 | code: "tag`tagged template literal`",
|
103 | parserOptions: { ecmaVersion: 6 },
|
104 | errors: ["Expected an assignment or function call and instead saw an expression."]
|
105 | },
|
106 | { code: "a && b()", options: [{ allowTernary: true }], errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
107 | { code: "a ? b() : c()", options: [{ allowShortCircuit: true }], errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
108 | { code: "a || b", options: [{ allowShortCircuit: true }], errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
109 | { code: "a() && b", options: [{ allowShortCircuit: true }], errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
110 | { code: "a ? b : 0", options: [{ allowTernary: true }], errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
111 | { code: "a ? b : c()", options: [{ allowTernary: true }], errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
112 | { code: "foo.bar;", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
113 | { code: "!a", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
114 | { code: "+a", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
115 | { code: "\"directive one\"; f(); \"directive two\";", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
116 | { code: "function foo() {\"directive one\"; f(); \"directive two\"; }", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
117 | { code: "if (0) { \"not a directive\"; f(); }", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
118 | { code: "function foo() { var foo = true; \"use strict\"; }", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
119 | { code: "var foo = () => { var foo = true; \"use strict\"; }", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
120 | {
|
121 | code: "`untagged template literal`",
|
122 | options: [{ allowTaggedTemplates: true }],
|
123 | parserOptions: { ecmaVersion: 6 },
|
124 | errors: ["Expected an assignment or function call and instead saw an expression."]
|
125 | },
|
126 | {
|
127 | code: "`untagged template literal`",
|
128 | options: [{ allowTaggedTemplates: false }],
|
129 | parserOptions: { ecmaVersion: 6 },
|
130 | errors: ["Expected an assignment or function call and instead saw an expression."]
|
131 | },
|
132 | {
|
133 | code: "tag`tagged template literal`",
|
134 | options: [{ allowTaggedTemplates: false }],
|
135 | parserOptions: { ecmaVersion: 6 },
|
136 | errors: ["Expected an assignment or function call and instead saw an expression."]
|
137 | },
|
138 |
|
139 |
|
140 | { code: "let a = do { foo; let b = 2; }", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
141 | { code: "let a = do { if (foo) { foo.bar } else { a; bar.foo } }", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
|
142 |
|
143 | ]
|
144 | });
|