forked from eslint/eslint
/
no-inner-declarations.js
177 lines (169 loc) · 5.65 KB
/
no-inner-declarations.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @fileoverview Tests for no-inner-declarations rule.
* @author Brandon Mills
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-inner-declarations"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("no-inner-declarations", rule, {
// Examples of code that should not trigger the rule
valid: [
"function doSomething() { }",
"function doSomething() { function somethingElse() { } }",
"(function() { function doSomething() { } }());",
"if (test) { var fn = function() { }; }",
"if (test) { var fn = function expr() { }; }",
"function decl() { var fn = function expr() { }; }",
"function decl(arg) { var fn; if (arg) { fn = function() { }; } }",
{ code: "var x = {doSomething() {function doSomethingElse() {}}}", parserOptions: { ecmaVersion: 6 } },
{ code: "function decl(arg) { var fn; if (arg) { fn = function expr() { }; } }", parserOptions: { ecmaVersion: 6 } },
"function decl(arg) { var fn; if (arg) { fn = function expr() { }; } }",
"if (test) { var foo; }",
{ code: "if (test) { let x = 1; }", options: ["both"], parserOptions: { ecmaVersion: 6 } },
{ code: "if (test) { const x = 1; }", options: ["both"], parserOptions: { ecmaVersion: 6 } },
"function doSomething() { while (test) { var foo; } }",
{ code: "var foo;", options: ["both"] },
{ code: "var foo = 42;", options: ["both"] },
{ code: "function doSomething() { var foo; }", options: ["both"] },
{ code: "(function() { var foo; }());", options: ["both"] },
{ code: "foo(() => { function bar() { } });", parserOptions: { ecmaVersion: 6 } },
{ code: "var fn = () => {var foo;}", options: ["both"], parserOptions: { ecmaVersion: 6 } },
{
code: "var x = {doSomething() {var foo;}}",
options: ["both"],
parserOptions: { ecmaVersion: 6 }
}
],
// Examples of code that should trigger the rule
invalid: [{
code: "if (test) { function doSomething() { } }",
options: ["both"],
errors: [{
messageId: "moveDeclToRoot",
data: {
type: "function",
body: "program"
},
type: "FunctionDeclaration"
}]
}, {
code: "if (foo) var a; ",
options: ["both"],
errors: [{
messageId: "moveDeclToRoot",
data: {
type: "variable",
body: "program"
},
type: "VariableDeclaration"
}]
},
{
code: "if (foo) function f(){} ",
options: ["both"],
errors: [{
messageId: "moveDeclToRoot",
data: {
type: "function",
body: "program"
},
type: "FunctionDeclaration"
}]
},
{
code: "function bar() { if (foo) function f(){}; }",
options: ["both"],
errors: [{
messageId: "moveDeclToRoot",
data: {
type: "function",
body: "function body"
},
type: "FunctionDeclaration"
}]
},
{
code: "function bar() { if (foo) var a; }",
options: ["both"],
errors: [{
messageId: "moveDeclToRoot",
data: {
type: "variable",
body: "function body"
},
type: "VariableDeclaration"
}]
},
{
code: "if (foo){ var a; }",
options: ["both"],
errors: [{
messageId: "moveDeclToRoot",
data: {
type: "variable",
body: "program"
},
type: "VariableDeclaration"
}]
}, {
code: "function doSomething() { do { function somethingElse() { } } while (test); }",
errors: [{
messageId: "moveDeclToRoot",
data: {
type: "function",
body: "function body"
},
type: "FunctionDeclaration"
}]
}, {
code: "(function() { if (test) { function doSomething() { } } }());",
errors: [{
messageId: "moveDeclToRoot",
data: {
type: "function",
body: "function body"
},
type: "FunctionDeclaration"
}]
}, {
code: "while (test) { var foo; }",
options: ["both"],
errors: [{
messageId: "moveDeclToRoot",
data: {
type: "variable",
body: "program"
},
type: "VariableDeclaration"
}]
}, {
code: "function doSomething() { if (test) { var foo = 42; } }",
options: ["both"],
errors: [{
messageId: "moveDeclToRoot",
data: {
type: "variable",
body: "function body"
},
type: "VariableDeclaration"
}]
}, {
code: "(function() { if (test) { var foo; } }());",
options: ["both"],
errors: [{
messageId: "moveDeclToRoot",
data: {
type: "variable",
body: "function body"
},
type: "VariableDeclaration"
}]
}]
});