Skip to content

Commit 72650ac

Browse files
authored
feat: support TS syntax in init-declarations (#19540)
* feat: support TS syntax in `init-declarations` * chore: fix lint * chore: fix formatting * chore: refactor code * chore: refactor code * chore: fix lint * test: add more cases
1 parent 03fb0bc commit 72650ac

File tree

3 files changed

+466
-0
lines changed

3 files changed

+466
-0
lines changed

docs/src/rules/init-declarations.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,98 @@ for (let i = 0; i < 1; i++) {}
146146

147147
:::
148148

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+
149241
## When Not To Use It
150242

151243
When you are indifferent as to how your variables are initialized.

lib/rules/init-declarations.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ function isInitialized(node) {
4848
module.exports = {
4949
meta: {
5050
type: "suggestion",
51+
dialects: ["typescript", "javascript"],
52+
language: "javascript",
5153

5254
docs: {
5355
description:
@@ -105,15 +107,34 @@ module.exports = {
105107
const mode = context.options[0] || MODE_ALWAYS;
106108
const params = context.options[1] || {};
107109

110+
// Track whether we're inside a declared namespace
111+
let insideDeclaredNamespace = false;
112+
108113
//--------------------------------------------------------------------------
109114
// Public API
110115
//--------------------------------------------------------------------------
111116

112117
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+
113130
"VariableDeclaration:exit"(node) {
114131
const kind = node.kind,
115132
declarations = node.declarations;
116133

134+
if (node.declare || insideDeclaredNamespace) {
135+
return;
136+
}
137+
117138
for (let i = 0; i < declarations.length; ++i) {
118139
const declaration = declarations[i],
119140
id = declaration.id,

0 commit comments

Comments
 (0)