1+ import { rangeDistance } from '../range_distance.ts' ;
12import { rangePadding } from '../range_padding.ts' ;
23
34const spaceBeforeColon = 1 ;
@@ -11,8 +12,38 @@ const colonSpacing : Deno.lint.Plugin = {
1112 'before-colon' : {
1213 create ( context ) : Deno . lint . LintVisitor {
1314 return {
15+ TSPropertySignature ( node ) : void {
16+ if ( node . typeAnnotation ) {
17+ // Text _ from "<name>___?__:<type>"
18+ const section : Deno . lint . Range = [ node . key . range [ 1 ] , node . typeAnnotation . range [ 0 ] ]
19+
20+ // Text _ from "<name>___?__:<type>"
21+ const text = context . sourceCode . getText ( node ) . substring (
22+ node . key . range [ 1 ] - node . range [ 0 ] ,
23+ node . typeAnnotation . range [ 0 ] - node . range [ 0 ]
24+ )
25+
26+ if ( node . optional ) {
27+ // Index of ? from "<name>?__:<type>"
28+ const textAfterOptionalIndex = text . search ( / \? / ) + 1
29+
30+ section [ 0 ] += textAfterOptionalIndex
31+ }
32+
33+ if ( rangeDistance ( section ) !== spaceBeforeColon ) {
34+ context . report ( {
35+ message : `Wrong colon spacing. Expected ${ spaceBeforeColon } space before colon.` ,
36+ range : rangePadding ( section ) ,
37+ fix ( fixer ) : Deno . lint . Fix {
38+ return fixer . replaceTextRange ( section , ' ' ) ;
39+ }
40+ } ) ;
41+ }
42+ }
43+ } ,
1444 Identifier ( node ) : void {
1545 if ( node . typeAnnotation ) {
46+ // Text _ from "<name>___?__:<type>"
1647 const section : Deno . lint . Range = [ node . range [ 1 ] , node . typeAnnotation . range [ 0 ] ]
1748
1849 // Text _ from "<name>___?__:<type>"
@@ -28,7 +59,7 @@ const colonSpacing : Deno.lint.Plugin = {
2859 section [ 0 ] += textAfterOptionalIndex
2960 }
3061
31- if ( section [ 1 ] - section [ 0 ] !== spaceBeforeColon ) {
62+ if ( rangeDistance ( section ) !== spaceBeforeColon ) {
3263 context . report ( {
3364 message : `Wrong colon spacing. Expected ${ spaceBeforeColon } space before colon.` ,
3465 range : rangePadding ( section ) ,
0 commit comments