File tree Expand file tree Collapse file tree 3 files changed +466
-0
lines changed Expand file tree Collapse file tree 3 files changed +466
-0
lines changed Original file line number Diff line number Diff line change @@ -146,6 +146,98 @@ for (let i = 0; i < 1; i++) {}
146
146
147
147
:::
148
148
149
+ This rule additionally supports TypeScript type syntax.
150
+
151
+ Examples of ** incorrect** TypeScript code for this rule:
152
+
153
+ ::: incorrect
154
+
155
+ ``` ts
156
+ /* eslint init-declarations: ["error", "never"] */
157
+
158
+ let arr: string [] = [' arr' , ' ar' ];
159
+
160
+ const class1 = class NAME {
161
+ constructor () {
162
+ var name1: string = ' hello' ;
163
+ }
164
+ };
165
+
166
+ namespace myLib {
167
+ let numberOfGreetings: number = 2 ;
168
+ }
169
+
170
+ ```
171
+
172
+ :::
173
+
174
+ ::: incorrect
175
+
176
+ ``` ts
177
+ /* eslint init-declarations: ["error", "always"] */
178
+
179
+ namespace myLib {
180
+ let numberOfGreetings: number ;
181
+ }
182
+
183
+ namespace myLib1 {
184
+ const foo: number ;
185
+ namespace myLib2 {
186
+ let bar: string ;
187
+ namespace myLib3 {
188
+ let baz: object ;
189
+ }
190
+ }
191
+ }
192
+
193
+ ```
194
+
195
+ :::
196
+
197
+ Examples of ** correct** TypeScript code for this rule:
198
+
199
+ ::: correct
200
+
201
+ ``` ts
202
+ /* eslint init-declarations: ["error", "never"] */
203
+
204
+ declare const foo: number ;
205
+
206
+ declare namespace myLib {
207
+ let numberOfGreetings: number ;
208
+ }
209
+
210
+ interface GreetingSettings {
211
+ greeting: string ;
212
+ duration? : number ;
213
+ color? : string ;
214
+ }
215
+ ```
216
+
217
+ :::
218
+
219
+ ::: correct
220
+
221
+ ``` ts
222
+ /* eslint init-declarations: ["error", "always"] */
223
+
224
+ declare const foo: number ;
225
+
226
+ declare namespace myLib {
227
+ let numberOfGreetings: number ;
228
+ }
229
+
230
+ interface GreetingSettings {
231
+ greeting: string ;
232
+ duration? : number ;
233
+ color? : string ;
234
+ }
235
+
236
+ ```
237
+
238
+ :::
239
+
240
+
149
241
## When Not To Use It
150
242
151
243
When you are indifferent as to how your variables are initialized.
Original file line number Diff line number Diff line change @@ -48,6 +48,8 @@ function isInitialized(node) {
48
48
module . exports = {
49
49
meta : {
50
50
type : "suggestion" ,
51
+ dialects : [ "typescript" , "javascript" ] ,
52
+ language : "javascript" ,
51
53
52
54
docs : {
53
55
description :
@@ -105,15 +107,34 @@ module.exports = {
105
107
const mode = context . options [ 0 ] || MODE_ALWAYS ;
106
108
const params = context . options [ 1 ] || { } ;
107
109
110
+ // Track whether we're inside a declared namespace
111
+ let insideDeclaredNamespace = false ;
112
+
108
113
//--------------------------------------------------------------------------
109
114
// Public API
110
115
//--------------------------------------------------------------------------
111
116
112
117
return {
118
+ TSModuleDeclaration ( node ) {
119
+ if ( node . declare ) {
120
+ insideDeclaredNamespace = true ;
121
+ }
122
+ } ,
123
+
124
+ "TSModuleDeclaration:exit" ( node ) {
125
+ if ( node . declare ) {
126
+ insideDeclaredNamespace = false ;
127
+ }
128
+ } ,
129
+
113
130
"VariableDeclaration:exit" ( node ) {
114
131
const kind = node . kind ,
115
132
declarations = node . declarations ;
116
133
134
+ if ( node . declare || insideDeclaredNamespace ) {
135
+ return ;
136
+ }
137
+
117
138
for ( let i = 0 ; i < declarations . length ; ++ i ) {
118
139
const declaration = declarations [ i ] ,
119
140
id = declaration . id ,
You can’t perform that action at this time.
0 commit comments