Skip to content

Commit 8b01a52

Browse files
committed
fix: Spacing after colon
1 parent c4107ea commit 8b01a52

File tree

2 files changed

+80
-21
lines changed

2 files changed

+80
-21
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@loat-dev/lint-plugins",
3-
"version": "0.3.8",
3+
"version": "0.3.9",
44
"license": "./LICENSE",
55
"exports": {
66
"./colon_spacing": "./src/plugins/colon_spacing.ts",

src/plugins/colon_spacing.ts

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { rangeDistance } from '../range_distance.ts';
22
import { rangePadding } from '../range_padding.ts';
33

44
const spaceBeforeColon = 1;
5+
const spaceAfterColon = 1;
56

67
/**
78
* This plugin ensures consistent spacing before and after the colon for type definitions.
@@ -12,6 +13,51 @@ const colonSpacing : Deno.lint.Plugin = {
1213
'before-colon': {
1314
create(context) : Deno.lint.LintVisitor {
1415
return {
16+
FunctionExpression(node) : void {
17+
if (node.returnType) {
18+
// Section | from "<id> |(<...params>) |:<type>"
19+
const section : Deno.lint.Range = [
20+
node.parent.type === 'Property' ? node.parent.key.range[1] :
21+
node.parent.type === 'CallExpression' ? node.parent.callee.range[1] + 1 : node.parent.range[1],
22+
node.returnType.range[0]
23+
]
24+
25+
if (node.params.length > 0) {
26+
const last = node.params[node.params.length - 1]
27+
28+
if (last.type === 'Identifier') {
29+
// Section | from "<id>(<...params>|) |:<type>"
30+
section[0] = last.range[1] + 1;
31+
32+
if (last.typeAnnotation) {
33+
// Section | from "<id>(<...params>:<type>|) |:<type>"
34+
section[0] = last.typeAnnotation.range[1] + 1;
35+
}
36+
}
37+
}
38+
39+
// Text _ from "<id>__(<...params>:<type>_)____:<type>"
40+
const text = context.sourceCode.getText(node).substring(
41+
section[0] - node.range[0],
42+
section[1] - node.range[0]
43+
)
44+
45+
// Section | from "<id>(<...params>)| |:<type>"
46+
const index = text.search(/\)/) + 1;
47+
48+
section[0] += index;
49+
50+
if (rangeDistance(section) !== spaceBeforeColon) {
51+
context.report({
52+
message: `Wrong colon spacing. Expected ${spaceBeforeColon} space before colon.`,
53+
range: rangePadding(section),
54+
fix(fixer) : Deno.lint.Fix {
55+
return fixer.replaceTextRange(section, ' ');
56+
}
57+
});
58+
}
59+
}
60+
},
1561
FunctionDeclaration(node) : void {
1662
if (node.returnType && node.id) {
1763
// Section | from "<id>| (<...params>) |:<type>"
@@ -24,18 +70,18 @@ const colonSpacing : Deno.lint.Plugin = {
2470
// Section | from "<id>(<...params>)| |:<type>"
2571
section[0] = last.typeAnnotation.range[1] + 1;
2672
}
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-
)
73+
}
74+
75+
// Text _ from "<id>__(<...params>_)____:<type>"
76+
const text = context.sourceCode.getText(node).substring(
77+
section[0] - node.range[0],
78+
section[1] - node.range[0]
79+
)
3380

34-
// Section | from "<id>(<...params>)| |:<type>"
35-
const index = text.search(/\)/) + 1;
81+
// Section | from "<id>(<...params>)| |:<type>"
82+
const index = text.search(/\)/) + 1;
3683

37-
section[0] += index;
38-
}
84+
section[0] += index;
3985

4086
if (rangeDistance(section) !== spaceBeforeColon) {
4187
context.report({
@@ -113,21 +159,34 @@ const colonSpacing : Deno.lint.Plugin = {
113159
create(context) : Deno.lint.LintVisitor {
114160
return {
115161
TSTypeAnnotation(node) : void {
116-
if (['FunctionDeclaration', 'FunctionExpression', 'TSPropertySignature', 'Identifier', 'PropertyDefinition'].includes(node.parent.type)) {
117-
const section : Deno.lint.Range = [
118-
node.range[0] + 1,
119-
node.typeAnnotation.range[0]
120-
]
162+
if (node.parent.type !== 'TSFunctionType') {
121163

122-
if (section[1] - section[0] !== 1) {
164+
// Text _ from "<name>___?__:<type>"
165+
const section : Deno.lint.Range = [node.range[0] + 1, node.typeAnnotation.range[0]]
166+
167+
// Text _ from "<id>__(<...params>:<type>):___(__<type>)"
168+
const text = context.sourceCode.getText(node).substring(
169+
section[0] - node.range[0],
170+
section[1] - node.range[0]
171+
)
172+
173+
// Section | from "<id>(<...params>):| |( <type>"
174+
const index = text.search(/\(/);
175+
176+
if (index !== -1) {
177+
section[1] = section[0] + index;
178+
}
179+
180+
if (rangeDistance(section) !== spaceAfterColon) {
123181
context.report({
124-
message: 'Wrong colon spacing. Expected 1 space after colon.',
182+
message: `Wrong colon spacing. Expected ${spaceAfterColon} space after colon.`,
125183
range: rangePadding(section),
126184
fix(fixer) : Deno.lint.Fix{
127-
return fixer.replaceTextRange(section, ' ');
128-
}
185+
return fixer.replaceTextRange(section, ' ');
186+
}
129187
});
130188
}
189+
131190
}
132191
},
133192
Property(node) : void {
@@ -144,7 +203,7 @@ const colonSpacing : Deno.lint.Plugin = {
144203

145204
if (index !== -1) {
146205
context.report({
147-
message: 'Wrong colon spacing. Expected 1 space after colon.',
206+
message: `Wrong colon spacing. Expected ${spaceAfterColon} space after colon.`,
148207
range: rangePadding(section),
149208
fix(fixer) : Deno.lint.Fix{
150209
return fixer.replaceTextRange(section, ' ');

0 commit comments

Comments
 (0)