/
one-var-declaration-per-line.js
91 lines (76 loc) · 5.26 KB
/
one-var-declaration-per-line.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
88
89
90
91
/**
* @fileoverview Tests for one-var-declaration-per-line rule.
* @author Alberto Rodríguez
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/one-var-declaration-per-line"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Fixtures
//------------------------------------------------------------------------------
/**
* Returns an error object at the specified line and column
* @private
* @param {int} line line number
* @param {int} column column number
* @returns {Object} Error object
*/
function errorAt(line, column) {
return {
messageId: "expectVarOnNewline",
type: "VariableDeclaration",
line,
column
};
}
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("one-var-declaration-per-line", rule, {
valid: [
{ code: "var a, b, c,\nd = 0;", options: ["initializations"] },
{ code: "var a, b, c,\n\nd = 0;", options: ["initializations"] },
{ code: "var a, b,\nc=0\nd = 0;", options: ["initializations"] },
{ code: "let a, b;", options: ["initializations"], parserOptions: { ecmaVersion: 6 } },
{ code: "var a = 0; var b = 0;", options: ["initializations"] },
"var a, b,\nc=0\nd = 0;",
{ code: "var a,\nb,\nc,\nd = 0;", options: ["always"] },
{ code: "var a = 0,\nb;", options: ["always"] },
{ code: "var a = 0,\n\nb;", options: ["always"] },
{ code: "var a; var b;", options: ["always"] },
{ code: "for(var a = 0, b = 0;;){}", options: ["always"] },
{ code: "for(let a = 0, b = 0;;){}", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "for(const a = 0, b = 0;;){}", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "for(var a in obj){}", options: ["always"] },
{ code: "for(let a in obj){}", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "for(const a in obj){}", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "for(var a of arr){}", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "for(let a of arr){}", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "for(const a of arr){}", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "export let a, b;", options: ["initializations"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "export let a,\n b = 0;", options: ["initializations"], parserOptions: { ecmaVersion: 6, sourceType: "module" } }
],
invalid: [
{ code: "var foo, bar;", output: "var foo, \nbar;", options: ["always"], errors: [{ line: 1, column: 10, endLine: 1, endColumn: 13 }] },
{ code: "var a, b;", output: "var a, \nb;", options: ["always"], errors: [errorAt(1, 8)] },
{ code: "let a, b;", output: "let a, \nb;", options: ["always"], parserOptions: { ecmaVersion: 6 }, errors: [errorAt(1, 8)] },
{ code: "var a, b = 0;", output: "var a, \nb = 0;", options: ["always"], errors: [errorAt(1, 8)] },
{ code: "var a = {\n foo: bar\n}, b;", output: "var a = {\n foo: bar\n}, \nb;", options: ["always"], errors: [errorAt(3, 4)] },
{ code: "var a\n=0, b;", output: "var a\n=0, \nb;", options: ["always"], errors: [errorAt(2, 5)] },
{ code: "let a, b = 0;", output: "let a, \nb = 0;", options: ["always"], parserOptions: { ecmaVersion: 6 }, errors: [errorAt(1, 8)] },
{ code: "const a = 0, b = 0;", output: "const a = 0, \nb = 0;", options: ["always"], parserOptions: { ecmaVersion: 6 }, errors: [errorAt(1, 14)] },
{ code: "var foo, bar, baz = 0;", output: "var foo, bar, \nbaz = 0;", options: ["initializations"], errors: [{ line: 1, column: 15, endLine: 1, endColumn: 22 }] },
{ code: "var a, b, c = 0;", output: "var a, b, \nc = 0;", options: ["initializations"], errors: [errorAt(1, 11)] },
{ code: "var a, b,\nc = 0, d;", output: "var a, b,\nc = 0, \nd;", options: ["initializations"], errors: [errorAt(2, 8)] },
{ code: "var a, b,\nc = 0, d = 0;", output: "var a, b,\nc = 0, \nd = 0;", options: ["initializations"], errors: [errorAt(2, 8)] },
{ code: "var a\n=0, b = 0;", output: "var a\n=0, \nb = 0;", options: ["initializations"], errors: [errorAt(2, 5)] },
{ code: "var a = {\n foo: bar\n}, b;", output: "var a = {\n foo: bar\n}, \nb;", options: ["initializations"], errors: [errorAt(3, 4)] },
{ code: "for(var a = 0, b = 0;;){\nvar c,d;}", output: "for(var a = 0, b = 0;;){\nvar c,\nd;}", options: ["always"], errors: [errorAt(2, 7)] },
{ code: "export let a, b;", output: "export let a, \nb;", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [errorAt(1, 15)] },
{ code: "export let a, b = 0;", output: "export let a, \nb = 0;", options: ["initializations"], parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [errorAt(1, 15)] }
]
});