3
3
export function validateCommitOrder ( positions : string [ ] ) : boolean {
4
4
// loop over positions
5
5
const errors : number [ ] = [ ] ;
6
- let previous = { level : 0 , step : 0 } ;
7
- let current = { level : 0 , step : 0 } ;
6
+ let previous = { level : 0 , step : 0 , type : "" } ;
7
+ let current = { level : 0 , step : 0 , type : "" } ;
8
8
positions . forEach ( ( position : string , index : number ) => {
9
9
if ( position === "INIT" ) {
10
10
if ( previous . level !== 0 && previous . step !== 0 ) {
11
11
errors . push ( index ) ;
12
12
}
13
- current = { level : 0 , step : 0 } ;
13
+ current = { level : 0 , step : 0 , type : "" } ;
14
14
return ;
15
15
} else {
16
16
// @deprecate - remove L|Q
17
17
const levelMatch = position . match ( / ^ (?< level > [ 0 - 9 ] + ) $ / ) ;
18
18
// @deprecate - remove S|Q|A
19
19
const stepMatch = position . match (
20
- / ^ (?< level > [ 0 - 9 ] + ) \. (?< step > [ 0 - 9 ] + ) : [ T | S ] $ /
20
+ / ^ (?< level > [ 0 - 9 ] + ) \. (?< step > [ 0 - 9 ] + ) : (?< stepType > [ T | S ] ) $ /
21
21
) ;
22
22
if ( levelMatch ) {
23
23
// allows next level or step
@@ -27,7 +27,7 @@ export function validateCommitOrder(positions: string[]): boolean {
27
27
return ;
28
28
}
29
29
const level = Number ( levelString ) ;
30
- current = { level, step : 0 } ;
30
+ current = { level, step : 0 , type : "" } ;
31
31
} else if ( stepMatch ) {
32
32
// allows next level or step
33
33
if ( ! stepMatch ?. groups ?. level || ! stepMatch ?. groups . step ) {
@@ -38,13 +38,26 @@ export function validateCommitOrder(positions: string[]): boolean {
38
38
39
39
const level = Number ( levelString ) ;
40
40
const step = Number ( stepString ) ;
41
- current = { level, step } ;
41
+ const type = stepMatch ?. groups . stepType ;
42
+
43
+ const sameStep = previous . level === level && previous . step === step ;
44
+
45
+ if (
46
+ // tests should come before the solution
47
+ ( sameStep && type === "T" && previous . type === "S" ) ||
48
+ // step should have tests
49
+ ( ! sameStep && type === "S" )
50
+ ) {
51
+ errors . push ( index ) ;
52
+ }
53
+ current = { level, step, type } ;
42
54
} else {
43
55
// error
44
56
console . warn ( `Invalid commit position: ${ position } ` ) ;
45
57
return ;
46
58
}
47
59
if (
60
+ // levels or steps are out of order
48
61
current . level < previous . level ||
49
62
( current . level === previous . level && current . step < previous . step )
50
63
) {
0 commit comments