1+ import { rangePadding } from '../range_padding.ts' ;
2+
13/**
24 * This plugin ensures consistent spacing before and after the colon for type definitions.
35 */
@@ -9,36 +11,47 @@ const colonSpacing : Deno.lint.Plugin = {
911 return {
1012 TSTypeAnnotation ( node ) : void {
1113 if ( [ 'FunctionDeclaration' , 'FunctionExpression' , 'TSPropertySignature' , 'Identifier' ] . includes ( node . parent . type ) ) {
12- const start = node . parent . range [ 0 ] ;
13- const sectionEnd = node . range [ 0 ] ;
14- const index =
15- context . sourceCode . getText ( node . parent ) . substring ( 0 , sectionEnd - start ) . search ( / (?: \) | .) * $ / ) + 1 ;
16- const sectionStart = start + index ;
1714
18- if ( index !== - 1 && sectionEnd - sectionStart !== 1 ) {
15+ const index = context . sourceCode . getText ( node . parent ) . substring (
16+ 0 ,
17+ node . range [ 0 ] - node . parent . range [ 0 ]
18+ ) . search ( / (?: \) | .) * $ / ) + 1 ;
19+
20+ const section : Deno . lint . Range = [
21+ node . parent . range [ 0 ] + index ,
22+ node . range [ 0 ]
23+ ]
24+
25+ if ( index !== - 1 && section [ 1 ] - section [ 0 ] !== 1 ) {
1926 context . report ( {
2027 message : 'Wrong colon spacing. Expected 1 space before colon.' ,
21- range : [ sectionStart - 1 , sectionEnd + 1 ] ,
22- fix ( fixer ) : Deno . lint . Fix {
23- return fixer . replaceTextRange ( [ sectionStart , sectionEnd ] , ' ' ) ;
28+ range : rangePadding ( section ) ,
29+ fix ( fixer ) : Deno . lint . Fix {
30+ return fixer . replaceTextRange ( section , ' ' ) ;
2431 }
2532 } ) ;
2633 }
2734 }
2835 } ,
2936 Property ( node ) : void {
3037 if ( [ 'ObjectExpression' ] . includes ( node . parent . type ) ) {
31- const start = node . range [ 0 ] ;
32- const sectionStart = node . key . range [ 1 ]
33- const index = context . sourceCode . getText ( node ) . substring ( sectionStart - start , node . value . range [ 0 ] - start ) . search ( / \: / )
34- const sectionEnd = sectionStart + index
38+
39+ const index = context . sourceCode . getText ( node ) . substring (
40+ node . key . range [ 1 ] - node . range [ 0 ] ,
41+ node . value . range [ 0 ] - node . range [ 0 ]
42+ ) . search ( / \: / ) ;
43+
44+ const section : Deno . lint . Range = [
45+ node . key . range [ 1 ] ,
46+ node . key . range [ 1 ] + index
47+ ] ;
3548
36- if ( index !== - 1 && sectionEnd - sectionStart !== 0 ) {
49+ if ( index !== - 1 && section [ 1 ] - section [ 0 ] !== 0 ) {
3750 context . report ( {
3851 message : 'Wrong colon spacing. Expected no space before colon.' ,
39- range : [ sectionStart - 1 , sectionEnd + 1 ] ,
40- fix ( fixer ) : Deno . lint . Fix {
41- return fixer . replaceTextRange ( [ sectionStart , sectionEnd ] , '' ) ;
52+ range : rangePadding ( section ) ,
53+ fix ( fixer ) : Deno . lint . Fix {
54+ return fixer . replaceTextRange ( section , '' ) ;
4255 }
4356 } ) ;
4457 }
@@ -52,33 +65,40 @@ const colonSpacing : Deno.lint.Plugin = {
5265 return {
5366 TSTypeAnnotation ( node ) : void {
5467 if ( [ 'FunctionDeclaration' , 'FunctionExpression' , 'TSPropertySignature' , 'Identifier' ] . includes ( node . parent . type ) ) {
55- const sectionStart = node . range [ 0 ] + 1 ;
56- const sectionEnd = node . typeAnnotation . range [ 0 ] ;
68+ const section : Deno . lint . Range = [
69+ node . range [ 0 ] + 1 ,
70+ node . typeAnnotation . range [ 0 ]
71+ ]
5772
58- if ( sectionEnd - sectionStart !== 1 ) {
73+ if ( section [ 1 ] - section [ 0 ] !== 1 ) {
5974 context . report ( {
6075 message : 'Wrong colon spacing. Expected 1 space after colon.' ,
61- range : [ sectionStart - 1 , sectionEnd + 1 ] ,
76+ range : rangePadding ( section ) ,
6277 fix ( fixer ) : Deno . lint . Fix {
63- return fixer . replaceTextRange ( [ sectionStart , sectionEnd ] , ' ' ) ;
78+ return fixer . replaceTextRange ( section , ' ' ) ;
6479 }
6580 } ) ;
6681 }
6782 }
6883 } ,
6984 Property ( node ) : void {
7085 if ( [ 'ObjectExpression' ] . includes ( node . parent . type ) ) {
71- const start = node . range [ 0 ] ;
72- const index = context . sourceCode . getText ( node ) . substring ( node . key . range [ 1 ] - start , node . value . range [ 0 ] - start ) . search ( / \: (?: + | ) $ / )
73- const sectionStart = node . key . range [ 1 ] + index + 1 ;
74- const sectionEnd = node . value . range [ 0 ]
86+ const index = context . sourceCode . getText ( node ) . substring (
87+ node . key . range [ 1 ] - node . range [ 0 ] ,
88+ node . value . range [ 0 ] - node . range [ 0 ]
89+ ) . search ( / \: (?: + | ) $ / ) ;
90+
91+ const section : Deno . lint . Range = [
92+ node . key . range [ 1 ] + index + 1 ,
93+ node . value . range [ 0 ]
94+ ]
7595
7696 if ( index !== - 1 ) {
7797 context . report ( {
7898 message : 'Wrong colon spacing. Expected 1 space after colon.' ,
79- range : [ sectionStart - 1 , sectionEnd + 1 ] ,
99+ range : rangePadding ( section ) ,
80100 fix ( fixer ) : Deno . lint . Fix {
81- return fixer . replaceTextRange ( [ sectionStart , sectionEnd ] , ' ' ) ;
101+ return fixer . replaceTextRange ( section , ' ' ) ;
82102 }
83103 } ) ;
84104 }
0 commit comments