Skip to content

Commit 2e9d116

Browse files
shixianqin9romise
andauthored
fix(space-before-blocks): check space before TSModuleBlock nodes (#798)
Co-authored-by: Vida Xie <[email protected]>
1 parent 3d1b817 commit 2e9d116

4 files changed

Lines changed: 240 additions & 20 deletions

File tree

packages/eslint-plugin/rules/space-before-blocks/README.md

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ Having an inconsistent style distracts the reader from seeing the important part
2121
This rule will enforce consistency of spacing before blocks. It is only applied on blocks that don’t begin on a new line.
2222

2323
- This rule ignores spacing which is between `=>` and a block. The spacing is handled by the `arrow-spacing` rule.
24-
- This rule ignores spacing which is between a keyword and a block. The spacing is handled by the `keyword-spacing` rule.
24+
- This rule ignores spacing which is between a keyword (e.g. `catch`) and a block. The spacing is handled by the `keyword-spacing` rule.
2525
- This rule ignores spacing which is between `:` of a switch case and a block. The spacing is handled by the `switch-colon-spacing` rule.
2626

2727
## Options
2828

2929
This rule takes one argument. If it is `"always"` then blocks must always have at least one preceding space. If `"never"`
3030
then all blocks should never have any preceding space. If different spacing is desired for function
31-
blocks, keyword blocks and classes, an optional configuration object can be passed as the rule argument to
31+
blocks, keyword blocks, classes, interfaces, enums and ts-modules, an optional configuration object can be passed as the rule argument to
3232
configure the cases separately. If any value in the configuration object is `"off"`, then neither style will be enforced for blocks of that kind.
3333

34-
( e.g. `{ "functions": "never", "keywords": "always", "classes": "always" }` )
34+
( e.g. `{ "functions": "never", "keywords": "always", "classes": "always", "modules": "always" }` )
3535

3636
The default is `"always"`.
3737

@@ -59,6 +59,10 @@ try {} catch(a){}
5959
class Foo{
6060
constructor(){}
6161
}
62+
63+
namespace Foo{
64+
bar: string
65+
}
6266
```
6367

6468
:::
@@ -91,6 +95,10 @@ for (;;) {
9195
}
9296

9397
try {} catch(a) {}
98+
99+
namespace Foo {
100+
bar: string
101+
}
94102
```
95103

96104
:::
@@ -115,6 +123,10 @@ for (;;) {
115123
}
116124

117125
try {} catch(a) {}
126+
127+
namespace Foo {
128+
bar: string
129+
}
118130
```
119131

120132
:::
@@ -141,34 +153,42 @@ try{} catch(a){}
141153
class Foo{
142154
constructor(){}
143155
}
156+
157+
namespace Foo{
158+
bar: string
159+
}
144160
```
145161

146162
:::
147163

148-
Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never" }`:
164+
Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never", "modules": "always" }`:
149165

150166
::: incorrect
151167

152168
```js
153-
/* eslint @stylistic/space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }] */
169+
/* eslint @stylistic/space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never", "modules": "never" }] */
154170

155171
function a() {}
156172

157173
try {} catch(a){}
158174

159-
class Foo{
175+
class Foo {
160176
constructor() {}
161177
}
178+
179+
namespace Foo {
180+
bar: string
181+
}
162182
```
163183

164184
:::
165185

166-
Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never" }`:
186+
Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never", "modules": "never" }`:
167187

168188
::: correct
169189

170190
```js
171-
/* eslint @stylistic/space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }] */
191+
/* eslint @stylistic/space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never", "modules": "never" }] */
172192

173193
for (;;) {
174194
// ...
@@ -181,16 +201,20 @@ describe(function(){
181201
class Foo{
182202
constructor(){}
183203
}
204+
205+
namespace Foo{
206+
bar: string
207+
}
184208
```
185209

186210
:::
187211

188-
Examples of **incorrect** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never" }`:
212+
Examples of **incorrect** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never", "modules": "never" }`:
189213

190214
::: incorrect
191215

192216
```js
193-
/* eslint @stylistic/space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }] */
217+
/* eslint @stylistic/space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never", "modules": "never" }] */
194218

195219
function a(){}
196220

@@ -199,16 +223,20 @@ try {} catch(a) {}
199223
class Foo {
200224
constructor(){}
201225
}
226+
227+
namespace Foo {
228+
bar: string
229+
}
202230
```
203231

204232
:::
205233

206-
Examples of **correct** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never" }`:
234+
Examples of **correct** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never", "modules": "never" }`:
207235

208236
::: correct
209237

210238
```js
211-
/* eslint @stylistic/space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }] */
239+
/* eslint @stylistic/space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never", "modules": "never" }] */
212240

213241
if (a){
214242
b();
@@ -219,34 +247,46 @@ var a = function() {}
219247
class Foo{
220248
constructor() {}
221249
}
250+
251+
namespace Foo{
252+
bar: string
253+
}
222254
```
223255

224256
:::
225257

226-
Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always" }`:
258+
Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always", "modules": "always" }`:
227259

228260
::: incorrect
229261

230262
```js
231-
/* eslint @stylistic/space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }] */
263+
/* eslint @stylistic/space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always", "modules": "always" }] */
232264

233265
class Foo{
234266
constructor(){}
235267
}
268+
269+
namespace Foo{
270+
bar: string
271+
}
236272
```
237273

238274
:::
239275

240-
Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always" }`:
276+
Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always", "modules": "always" }`:
241277

242278
::: correct
243279

244280
```js
245-
/* eslint @stylistic/space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }] */
281+
/* eslint @stylistic/space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always", "modules": "always" }] */
246282

247283
class Foo {
248284
constructor(){}
249285
}
286+
287+
namespace Foo {
288+
bar: string
289+
}
250290
```
251291

252292
:::

packages/eslint-plugin/rules/space-before-blocks/space-before-blocks._ts_.test.ts

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,61 @@ run<RuleOptions, MessageIds>({
8181
`,
8282
options: [{ classes: 'off' }],
8383
},
84+
{
85+
code: $`
86+
namespace Test{
87+
type foo = number;
88+
}
89+
declare module 'foo'{
90+
type foo = number;
91+
}
92+
`,
93+
options: ['never'],
94+
},
95+
{
96+
code: $`
97+
namespace Test {
98+
type foo = number;
99+
}
100+
declare module 'foo' {
101+
type foo = number;
102+
}
103+
`,
104+
options: ['always'],
105+
},
106+
{
107+
code: $`
108+
namespace Test{
109+
type foo = number;
110+
}
111+
declare module 'foo'{
112+
type foo = number;
113+
}
114+
`,
115+
options: [{ modules: 'never' }],
116+
},
117+
{
118+
code: $`
119+
namespace Test {
120+
type foo = number;
121+
}
122+
declare module 'foo' {
123+
type foo = number;
124+
}
125+
`,
126+
options: [{ modules: 'always' }],
127+
},
128+
{
129+
code: $`
130+
namespace Test{
131+
type foo = number;
132+
}
133+
declare module 'foo'{
134+
type foo = number;
135+
}
136+
`,
137+
options: [{ modules: 'off' }],
138+
},
84139
],
85140
invalid: [
86141
{
@@ -235,5 +290,113 @@ run<RuleOptions, MessageIds>({
235290
],
236291
options: [{ classes: 'never' }],
237292
},
293+
{
294+
code: $`
295+
namespace Test {
296+
type foo = number;
297+
}
298+
declare module 'foo' {
299+
type foo = number;
300+
}
301+
`,
302+
output: $`
303+
namespace Test{
304+
type foo = number;
305+
}
306+
declare module 'foo'{
307+
type foo = number;
308+
}
309+
`,
310+
errors: [
311+
{
312+
messageId: 'unexpectedSpace',
313+
},
314+
{
315+
messageId: 'unexpectedSpace',
316+
},
317+
],
318+
options: ['never'],
319+
},
320+
{
321+
code: $`
322+
namespace Test{
323+
type foo = number;
324+
}
325+
declare module 'foo'{
326+
type foo = number;
327+
}
328+
`,
329+
output: $`
330+
namespace Test {
331+
type foo = number;
332+
}
333+
declare module 'foo' {
334+
type foo = number;
335+
}
336+
`,
337+
errors: [
338+
{
339+
messageId: 'missingSpace',
340+
},
341+
{
342+
messageId: 'missingSpace',
343+
},
344+
],
345+
options: ['always'],
346+
},
347+
{
348+
code: $`
349+
namespace Test {
350+
type foo = number;
351+
}
352+
declare module 'foo' {
353+
type foo = number;
354+
}
355+
`,
356+
output: $`
357+
namespace Test{
358+
type foo = number;
359+
}
360+
declare module 'foo'{
361+
type foo = number;
362+
}
363+
`,
364+
errors: [
365+
{
366+
messageId: 'unexpectedSpace',
367+
},
368+
{
369+
messageId: 'unexpectedSpace',
370+
},
371+
],
372+
options: [{ modules: 'never' }],
373+
},
374+
{
375+
code: $`
376+
namespace Test{
377+
type foo = number;
378+
}
379+
declare module 'foo'{
380+
type foo = number;
381+
}
382+
`,
383+
output: $`
384+
namespace Test {
385+
type foo = number;
386+
}
387+
declare module 'foo' {
388+
type foo = number;
389+
}
390+
`,
391+
errors: [
392+
{
393+
messageId: 'missingSpace',
394+
},
395+
{
396+
messageId: 'missingSpace',
397+
},
398+
],
399+
options: [{ modules: 'always' }],
400+
},
238401
],
239402
})

0 commit comments

Comments
 (0)