@@ -12,6 +12,42 @@ const colonSpacing : Deno.lint.Plugin = {
1212 'before-colon' : {
1313 create ( context ) : Deno . lint . LintVisitor {
1414 return {
15+ FunctionDeclaration ( node ) : void {
16+ if ( node . returnType && node . id ) {
17+ // Section | from "<id>| (<...params>) |:<type>"
18+ const section : Deno . lint . Range = [ node . id . range [ 1 ] , node . returnType . range [ 0 ] ]
19+
20+ if ( node . params . length > 0 ) {
21+ const last = node . params [ node . params . length - 1 ]
22+
23+ if ( last . type === 'Identifier' && last . typeAnnotation ) {
24+ // Section | from "<id>(<...params>)| |:<type>"
25+ section [ 0 ] = last . typeAnnotation . range [ 1 ] + 1 ;
26+ }
27+ } else {
28+ // Text _ from "<id>__(<...params>)____:<type>"
29+ const text = context . sourceCode . getText ( node ) . substring (
30+ section [ 0 ] - node . range [ 0 ] ,
31+ section [ 1 ] - node . range [ 0 ]
32+ )
33+
34+ // Section | from "<id>(<...params>)| |:<type>"
35+ const index = text . search ( / \) / ) + 1 ;
36+
37+ section [ 0 ] += index ;
38+ }
39+
40+ if ( rangeDistance ( section ) !== spaceBeforeColon ) {
41+ context . report ( {
42+ message : `Wrong colon spacing. Expected ${ spaceBeforeColon } space before colon.` ,
43+ range : rangePadding ( section ) ,
44+ fix ( fixer ) : Deno . lint . Fix {
45+ return fixer . replaceTextRange ( section , ' ' ) ;
46+ }
47+ } ) ;
48+ }
49+ }
50+ } ,
1551 TSPropertySignature ( node ) : void {
1652 if ( node . typeAnnotation ) {
1753 // Text _ from "<name>___?__:<type>"
0 commit comments