@@ -14,6 +14,16 @@ const baseSchema = Array.isArray(baseRule.meta.schema)
14
14
? baseRule . meta . schema [ 0 ]
15
15
: baseRule . meta . schema ;
16
16
17
+ /**
18
+ * TODO: replace with native .at() once Node 14 stops being supported
19
+ */
20
+ function at < T > ( arr : T [ ] , position : number ) : T | undefined {
21
+ if ( position < 0 ) {
22
+ return arr [ arr . length + position ] ;
23
+ }
24
+ return arr [ position ] ;
25
+ }
26
+
17
27
export default util . createRule < Options , MessageIds > ( {
18
28
name : 'key-spacing' ,
19
29
meta : {
@@ -41,7 +51,7 @@ export default util.createRule<Options, MessageIds>({
41
51
function adjustedColumn ( position : TSESTree . Position ) : number {
42
52
const line = position . line - 1 ; // position.line is 1-indexed
43
53
return util . getStringLength (
44
- sourceCode . lines [ line ] . slice ( 0 , position . column ) ,
54
+ at ( sourceCode . lines , line ) ! . slice ( 0 , position . column ) ,
45
55
) ;
46
56
}
47
57
@@ -87,7 +97,7 @@ export default util.createRule<Options, MessageIds>({
87
97
return code . slice (
88
98
0 ,
89
99
sourceCode . getTokenAfter (
90
- node . parameters . at ( - 1 ) ! ,
100
+ at ( node . parameters , - 1 ) ! ,
91
101
util . isClosingBracketToken ,
92
102
) ! . range [ 1 ] - node . range [ 0 ] ,
93
103
) ;
@@ -102,7 +112,7 @@ export default util.createRule<Options, MessageIds>({
102
112
return getLastTokenBeforeColon (
103
113
node . type !== AST_NODE_TYPES . TSIndexSignature
104
114
? node . key
105
- : node . parameters . at ( - 1 ) ! ,
115
+ : at ( node . parameters , - 1 ) ! ,
106
116
) . loc . end ;
107
117
}
108
118
@@ -202,7 +212,7 @@ export default util.createRule<Options, MessageIds>({
202
212
if (
203
213
leadingComments . length &&
204
214
leadingComments [ 0 ] . loc . start . line - groupEndLine <= 1 &&
205
- candidateValueStartLine - leadingComments . at ( - 1 ) ! . loc . end . line <= 1
215
+ candidateValueStartLine - at ( leadingComments , - 1 ) ! . loc . end . line <= 1
206
216
) {
207
217
for ( let i = 1 ; i < leadingComments . length ; i ++ ) {
208
218
if (
@@ -373,7 +383,7 @@ export default util.createRule<Options, MessageIds>({
373
383
let prevNode : TSESTree . Node | undefined = undefined ;
374
384
375
385
for ( const node of members ) {
376
- let prevAlignedNode = currentAlignGroup . at ( - 1 ) ;
386
+ let prevAlignedNode = at ( currentAlignGroup , - 1 ) ;
377
387
if ( prevAlignedNode !== prevNode ) {
378
388
prevAlignedNode = undefined ;
379
389
}
0 commit comments