Skip to content

Commit d82e539

Browse files
committed
fix: fix dangerous cases
1 parent 0662610 commit d82e539

2 files changed

Lines changed: 213 additions & 13 deletions

File tree

rules/sort-modules.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,27 +195,16 @@ function analyzeModule({
195195
for (let node of module.body) {
196196
switch (node.type) {
197197
case AST_NODE_TYPES.TSNamespaceExportDeclaration:
198-
case AST_NODE_TYPES.TSImportEqualsDeclaration:
199198
case AST_NODE_TYPES.ExportAllDeclaration:
200-
case AST_NODE_TYPES.TSExportAssignment:
201199
case AST_NODE_TYPES.ImportDeclaration:
202200
case AST_NODE_TYPES.DebuggerStatement:
203201
case AST_NODE_TYPES.ContinueStatement:
204-
case AST_NODE_TYPES.DoWhileStatement:
205-
case AST_NODE_TYPES.LabeledStatement:
202+
/* v8 ignore next 4 -- @preserve These statements cannot appear at module/namespace level in valid code. */
203+
// eslint-disable-next-line no-fallthrough
206204
case AST_NODE_TYPES.ReturnStatement:
207-
case AST_NODE_TYPES.SwitchStatement:
208205
case AST_NODE_TYPES.EmptyStatement:
209206
case AST_NODE_TYPES.BreakStatement:
210-
case AST_NODE_TYPES.WhileStatement:
211-
case AST_NODE_TYPES.ForInStatement:
212-
case AST_NODE_TYPES.ForOfStatement:
213-
case AST_NODE_TYPES.ThrowStatement:
214-
case AST_NODE_TYPES.BlockStatement:
215207
case AST_NODE_TYPES.WithStatement:
216-
case AST_NODE_TYPES.ForStatement:
217-
case AST_NODE_TYPES.TryStatement:
218-
case AST_NODE_TYPES.IfStatement:
219208
continue
220209
case AST_NODE_TYPES.ExportDefaultDeclaration:
221210
case AST_NODE_TYPES.ExportNamedDeclaration:
@@ -224,8 +213,21 @@ function analyzeModule({
224213
case AST_NODE_TYPES.FunctionDeclaration:
225214
case AST_NODE_TYPES.TSModuleDeclaration:
226215
break
216+
case AST_NODE_TYPES.TSImportEqualsDeclaration:
227217
case AST_NODE_TYPES.VariableDeclaration:
228218
case AST_NODE_TYPES.ExpressionStatement:
219+
case AST_NODE_TYPES.TSExportAssignment:
220+
case AST_NODE_TYPES.DoWhileStatement:
221+
case AST_NODE_TYPES.LabeledStatement:
222+
case AST_NODE_TYPES.SwitchStatement:
223+
case AST_NODE_TYPES.WhileStatement:
224+
case AST_NODE_TYPES.ForInStatement:
225+
case AST_NODE_TYPES.ForOfStatement:
226+
case AST_NODE_TYPES.ThrowStatement:
227+
case AST_NODE_TYPES.BlockStatement:
228+
case AST_NODE_TYPES.ForStatement:
229+
case AST_NODE_TYPES.TryStatement:
230+
case AST_NODE_TYPES.IfStatement:
229231
sortingNodeGroupsWithoutOverloadSignature.push([])
230232
continue
231233
case AST_NODE_TYPES.TSDeclareFunction:

test/rules/sort-modules.test.ts

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,204 @@ describe('sort-modules', () => {
285285
})
286286
})
287287

288+
it('creates partitions at if statements', async () => {
289+
await valid({
290+
code: dedent`
291+
enum B { b = 'b' }
292+
if (B.b === 'b') {}
293+
enum A { a = 'a' }
294+
`,
295+
options: [options],
296+
})
297+
})
298+
299+
it('creates partitions at while statements', async () => {
300+
await valid({
301+
code: dedent`
302+
enum B { b = 'b' }
303+
while (B.b === 'b') {}
304+
enum A { a = 'a' }
305+
`,
306+
options: [options],
307+
})
308+
})
309+
310+
it('creates partitions at for-in loops', async () => {
311+
await valid({
312+
code: dedent`
313+
enum B { b = 'b' }
314+
for (const key in B) {}
315+
enum A { a = 'a' }
316+
`,
317+
options: [options],
318+
})
319+
})
320+
321+
it('creates partitions at try/catch statements', async () => {
322+
await valid({
323+
code: dedent`
324+
enum B { b = 'b' }
325+
try { B.b } catch {}
326+
enum A { a = 'a' }
327+
`,
328+
options: [options],
329+
})
330+
})
331+
332+
it('creates partitions at for loops', async () => {
333+
await valid({
334+
code: dedent`
335+
enum B { b = 'b' }
336+
for (let i = 0; i < Object.keys(B).length; i++) {}
337+
enum A { a = 'a' }
338+
`,
339+
options: [options],
340+
})
341+
})
342+
343+
it('creates partitions at for-of loops', async () => {
344+
await valid({
345+
code: dedent`
346+
enum B { b = 'b' }
347+
for (const value of Object.values(B)) {}
348+
enum A { a = 'a' }
349+
`,
350+
options: [options],
351+
})
352+
})
353+
354+
it('creates partitions at do-while loops', async () => {
355+
await valid({
356+
code: dedent`
357+
enum B { b = 'b' }
358+
do { void B.b } while (false)
359+
enum A { a = 'a' }
360+
`,
361+
options: [options],
362+
})
363+
})
364+
365+
it('creates partitions at switch statements', async () => {
366+
await valid({
367+
code: dedent`
368+
enum B { b = 'b' }
369+
switch (B.b) { case 'b': break }
370+
enum A { a = 'a' }
371+
`,
372+
options: [options],
373+
})
374+
})
375+
376+
it('creates partitions at throw statements', async () => {
377+
await valid({
378+
code: dedent`
379+
enum B { b = 'b' }
380+
throw new Error(B.b)
381+
enum A { a = 'a' }
382+
`,
383+
options: [options],
384+
})
385+
})
386+
387+
it('creates partitions at block statements', async () => {
388+
await valid({
389+
code: dedent`
390+
enum B { b = 'b' }
391+
{ const x = B.b }
392+
enum A { a = 'a' }
393+
`,
394+
options: [options],
395+
})
396+
})
397+
398+
it('creates partitions at labeled statements', async () => {
399+
await valid({
400+
code: dedent`
401+
enum B { b = 'b' }
402+
label: { const x = B.b }
403+
enum A { a = 'a' }
404+
`,
405+
options: [options],
406+
})
407+
})
408+
409+
it('creates partitions at TypeScript import-equals declarations', async () => {
410+
await valid({
411+
code: dedent`
412+
enum B { b = 'b' }
413+
import A = B
414+
enum C { c = 'c' }
415+
`,
416+
options: [options],
417+
})
418+
})
419+
420+
it('creates partitions at export assignments', async () => {
421+
await valid({
422+
code: dedent`
423+
enum B { b = 'b' }
424+
export = B
425+
enum A { a = 'a' }
426+
`,
427+
options: [options],
428+
})
429+
})
430+
431+
it('sorts across empty statements', async () => {
432+
await valid({
433+
code: dedent`
434+
enum A { a = 'a' }
435+
;
436+
enum B { b = 'b' }
437+
`,
438+
options: [options],
439+
})
440+
})
441+
442+
it('sorts across debugger statements', async () => {
443+
await valid({
444+
code: dedent`
445+
enum A { a = 'a' }
446+
debugger
447+
enum B { b = 'b' }
448+
`,
449+
options: [options],
450+
})
451+
})
452+
453+
it('sorts across import declarations', async () => {
454+
await valid({
455+
code: dedent`
456+
enum A { a = 'a' }
457+
import 'x'
458+
enum B { b = 'b' }
459+
`,
460+
options: [options],
461+
})
462+
})
463+
464+
it('sorts across export-all declarations', async () => {
465+
await valid({
466+
code: dedent`
467+
enum A { a = 'a' }
468+
export * from 'x'
469+
enum B { b = 'b' }
470+
`,
471+
options: [options],
472+
})
473+
})
474+
475+
it('sorts across namespace export declarations', async () => {
476+
await valid({
477+
code: dedent`
478+
enum A { a = 'a' }
479+
export as namespace Foo
480+
enum B { b = 'b' }
481+
`,
482+
options: [options],
483+
})
484+
})
485+
288486
it('accepts complex predefined group configurations', async () => {
289487
await valid({
290488
options: [

0 commit comments

Comments
 (0)