]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-var.js
84e14ae19c905f422e55e5bdc7555b2a82ec4215
2 * @fileoverview Tests for no-var rule.
3 * @author Jamund Ferguson
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require("../../../lib/rules/no-var"),
13 { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester({ parserOptions
: { ecmaVersion
: 6 } });
21 ruleTester
.run("no-var", rule
, {
23 "const JOE = 'schmoe';",
26 code
: "const JOE = 'schmoe';",
27 parserOptions
: { ecmaFeatures
: { globalReturn
: true } }
30 code
: "let moo = 'car';",
31 parserOptions
: { ecmaFeatures
: { globalReturn
: true } }
37 code
: "var foo = bar;",
38 output
: "let foo = bar;",
39 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
42 messageId
: "unexpectedVar",
43 type
: "VariableDeclaration"
48 code
: "var foo = bar, toast = most;",
49 output
: "let foo = bar, toast = most;",
50 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
53 messageId
: "unexpectedVar",
54 type
: "VariableDeclaration"
59 code
: "var foo = bar; let toast = most;",
60 output
: "let foo = bar; let toast = most;",
61 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
64 messageId
: "unexpectedVar",
65 type
: "VariableDeclaration"
70 code
: "for (var a of b) { console.log(a); }",
71 output
: "for (let a of b) { console.log(a); }",
72 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
75 messageId
: "unexpectedVar",
76 type
: "VariableDeclaration"
81 code
: "for (var a in b) { console.log(a); }",
82 output
: "for (let a in b) { console.log(a); }",
83 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
86 messageId
: "unexpectedVar",
87 type
: "VariableDeclaration"
92 code
: "for (let a of b) { var c = 1; console.log(c); }",
93 output
: "for (let a of b) { let c = 1; console.log(c); }",
94 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
97 messageId
: "unexpectedVar",
98 type
: "VariableDeclaration"
103 code
: "for (var i = 0; i < list.length; ++i) { foo(i) }",
104 output
: "for (let i = 0; i < list.length; ++i) { foo(i) }",
105 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
107 { messageId
: "unexpectedVar", type
: "VariableDeclaration" }
111 code
: "for (var i = 0, i = 0; false;);",
113 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
115 { messageId
: "unexpectedVar", type
: "VariableDeclaration" }
119 code
: "var i = 0; for (var i = 1; false;); console.log(i);",
121 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
123 { messageId
: "unexpectedVar", type
: "VariableDeclaration" },
124 { messageId
: "unexpectedVar", type
: "VariableDeclaration" }
128 // Not fix if it's redeclared or it's used from outside of the scope or it's declared on a case chunk.
130 code
: "var a, b, c; var a;",
132 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
134 { messageId
: "unexpectedVar" },
135 { messageId
: "unexpectedVar" }
139 code
: "var a; if (b) { var a; }",
141 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
143 { messageId
: "unexpectedVar" },
144 { messageId
: "unexpectedVar" }
148 code
: "if (foo) { var a, b, c; } a;",
150 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
152 { messageId
: "unexpectedVar" }
156 code
: "for (var i = 0; i < 10; ++i) {} i;",
158 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
160 { messageId
: "unexpectedVar" }
164 code
: "for (var a in obj) {} a;",
166 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
168 { messageId
: "unexpectedVar" }
172 code
: "for (var a of list) {} a;",
174 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
176 { messageId
: "unexpectedVar" }
180 code
: "switch (a) { case 0: var b = 1 }",
182 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
184 { messageId
: "unexpectedVar" }
188 // Don't fix if the variable is in a loop and the behavior might change.
190 code
: "for (var a of b) { arr.push(() => a); }",
192 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
194 { messageId
: "unexpectedVar" }
198 code
: "for (let a of b) { var c; console.log(c); c = 'hello'; }",
200 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
202 { messageId
: "unexpectedVar" }
206 // https://github.com/eslint/eslint/issues/7950
210 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
212 { messageId
: "unexpectedVar" }
216 code
: "var {a = a} = {}",
218 parserOptions
: { ecmaVersion
: 2015, ecmaFeatures
: { globalReturn
: true } },
220 { messageId
: "unexpectedVar" }
224 code
: "var {a = b, b} = {}",
226 parserOptions
: { ecmaVersion
: 2015, ecmaFeatures
: { globalReturn
: true } },
228 { messageId
: "unexpectedVar" }
232 code
: "var {a, b = a} = {}",
233 output
: "let {a, b = a} = {}",
234 parserOptions
: { ecmaVersion
: 2015, ecmaFeatures
: { globalReturn
: true } },
236 { messageId
: "unexpectedVar" }
240 code
: "var a = b, b = 1",
242 parserOptions
: { ecmaVersion
: 2015, ecmaFeatures
: { globalReturn
: true } },
244 { messageId
: "unexpectedVar" }
248 code
: "var a = b; var b = 1",
249 output
: "let a = b; var b = 1",
250 parserOptions
: { ecmaVersion
: 2015, ecmaFeatures
: { globalReturn
: true } },
252 { messageId
: "unexpectedVar" },
253 { messageId
: "unexpectedVar" }
258 * This case is not in TDZ, but it's very hard to distinguish the reference is in TDZ or not.
259 * So this rule does not fix it for safe.
262 code
: "function foo() { a } var a = 1; foo()",
264 parserOptions
: { ecmaVersion
: 2015, ecmaFeatures
: { globalReturn
: true } },
266 { messageId
: "unexpectedVar" }
270 // https://github.com/eslint/eslint/issues/7961
272 code
: "if (foo) var bar = 1;",
274 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
276 { messageId
: "unexpectedVar", type
: "VariableDeclaration" }
280 // https://github.com/eslint/eslint/issues/9520
284 errors
: [{ messageId
: "unexpectedVar" }]
287 code
: "{ var foo = 1 }",
289 errors
: [{ messageId
: "unexpectedVar" }]
292 code
: "if (true) { var foo = 1 }",
294 errors
: [{ messageId
: "unexpectedVar" }]
298 output
: "let foo = 1",
299 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
300 errors
: [{ messageId
: "unexpectedVar" }]
303 // https://github.com/eslint/eslint/issues/11594
305 code
: "declare var foo = 2;",
306 output
: "declare let foo = 2;",
307 parser
: require
.resolve("../../fixtures/parsers/typescript-parsers/declare-var"),
308 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
309 errors
: [{ messageId
: "unexpectedVar" }]
312 // https://github.com/eslint/eslint/issues/11830
314 code
: "function foo() { var let; }",
316 errors
: [{ messageId
: "unexpectedVar" }]
319 code
: "function foo() { var { let } = {}; }",
321 errors
: [{ messageId
: "unexpectedVar" }]