@@ -19,29 +19,36 @@ export default function ftl(
19
19
let code = strings . reduce ( ( acc , cur ) => acc + values . shift ( ) + cur ) ;
20
20
let lines = code . split ( "\n" ) ;
21
21
22
- const first = lines . shift ( ) ;
22
+ let first = lines . shift ( ) ;
23
23
if ( first === undefined || ! RE_BLANK . test ( first ) ) {
24
24
throw new RangeError ( "Content must start on a new line." ) ;
25
25
}
26
26
27
- const commonIndent = lines . pop ( ) ;
27
+ let commonIndent = lines . pop ( ) ;
28
28
if ( commonIndent === undefined || ! RE_BLANK . test ( commonIndent ) ) {
29
29
throw new RangeError ( "Closing delimiter must appear on a new line." ) ;
30
30
}
31
31
32
- return lines . map ( ( line : string , idx : number ) : string => {
32
+ let dedented = [ ] ;
33
+ for ( let i = 0 ; i < lines . length ; i ++ ) {
34
+ let line = lines [ i ] ;
33
35
let lineIndent = line . slice ( 0 , commonIndent . length ) ;
34
36
if ( lineIndent . length === 0 ) {
35
37
// Empty blank lines are preserved even if technically they are not
36
38
// indented at all. This also short-circuits the dedentation logic when
37
39
// commonIndent.length is 0, i.e. when all indents should be kept.
38
- return line ;
40
+ dedented . push ( line ) ;
41
+ continue ;
39
42
}
43
+
40
44
if ( lineIndent !== commonIndent ) {
41
45
// The indentation of the line must match commonIndent exacty.
42
- throw new RangeError ( `Insufficient indentation in line ${ idx + 1 } .` ) ;
46
+ throw new RangeError ( `Insufficient indentation in line ${ i + 1 } .` ) ;
43
47
}
48
+
44
49
// Strip commonIndent.
45
- return line . slice ( commonIndent . length ) ;
46
- } ) . join ( "\n" ) ;
50
+ dedented . push ( line . slice ( commonIndent . length ) ) ;
51
+ }
52
+
53
+ return dedented . join ( "\n" ) ;
47
54
}
0 commit comments