@@ -4,13 +4,15 @@ export const RULE_NAME = 'named-tuple-spacing'
4
4
export type MessageIds = 'expectedSpaceAfter' | 'unexpectedSpaceBetween' | 'unexpectedSpaceBefore'
5
5
export type Options = [ ]
6
6
7
+ const RE = / ^ ( [ \w _ $ ] + ) ( \s * ) ( \? \s * ) ? : ( \s * ) ( .* ) $ /
8
+
7
9
export default createEslintRule < Options , MessageIds > ( {
8
10
name : RULE_NAME ,
9
11
meta : {
10
12
type : 'suggestion' ,
11
13
docs : {
12
14
description : 'Expect space before type declaration in named tuple' ,
13
- recommended : 'error ' ,
15
+ recommended : 'stylistic ' ,
14
16
} ,
15
17
fixable : 'code' ,
16
18
schema : [ ] ,
@@ -24,16 +26,18 @@ export default createEslintRule<Options, MessageIds>({
24
26
create : ( context ) => {
25
27
const sourceCode = context . getSourceCode ( )
26
28
return {
27
- TSNamedTupleMember : ( node ) => {
29
+ TSNamedTupleMember : ( node : any ) => {
28
30
const code = sourceCode . text . slice ( node . range [ 0 ] , node . range [ 1 ] )
29
31
30
- const reg = / ( \w + ) ( \s * ) ( \? \s * ) ? : ( \s * ) ( \w + ) /
32
+ const match = code . match ( RE )
33
+ if ( ! match )
34
+ return
31
35
32
36
const labelName = node . label . name
33
- const spaceBeforeColon = code . match ( reg ) ?. [ 2 ]
34
- const optionalMark = code . match ( reg ) ?. [ 3 ]
35
- const spacesAfterColon = code . match ( reg ) ?. [ 4 ]
36
- const elementType = code . match ( reg ) ?. [ 5 ]
37
+ const spaceBeforeColon = match [ 2 ]
38
+ const optionalMark = match [ 3 ]
39
+ const spacesAfterColon = match [ 4 ]
40
+ const elementType = match [ 5 ]
37
41
38
42
function getReplaceValue ( ) {
39
43
let ret = labelName
@@ -49,7 +53,7 @@ export default createEslintRule<Options, MessageIds>({
49
53
node,
50
54
messageId : 'unexpectedSpaceBetween' ,
51
55
* fix ( fixer ) {
52
- yield fixer . replaceTextRange ( node . range , code . replace ( reg , getReplaceValue ( ) ) )
56
+ yield fixer . replaceTextRange ( node . range , code . replace ( RE , getReplaceValue ( ) ) )
53
57
} ,
54
58
} )
55
59
}
@@ -59,17 +63,17 @@ export default createEslintRule<Options, MessageIds>({
59
63
node,
60
64
messageId : 'unexpectedSpaceBefore' ,
61
65
* fix ( fixer ) {
62
- yield fixer . replaceTextRange ( node . range , code . replace ( reg , getReplaceValue ( ) ) )
66
+ yield fixer . replaceTextRange ( node . range , code . replace ( RE , getReplaceValue ( ) ) )
63
67
} ,
64
68
} )
65
69
}
66
70
67
- if ( spacesAfterColon . length !== 1 ) {
71
+ if ( spacesAfterColon != null && spacesAfterColon . length !== 1 ) {
68
72
context . report ( {
69
73
node,
70
74
messageId : 'expectedSpaceAfter' ,
71
75
* fix ( fixer ) {
72
- yield fixer . replaceTextRange ( node . range , code . replace ( reg , getReplaceValue ( ) ) )
76
+ yield fixer . replaceTextRange ( node . range , code . replace ( RE , getReplaceValue ( ) ) )
73
77
} ,
74
78
} )
75
79
}
0 commit comments