Skip to content

Commit eed0e1d

Browse files
committed
fix: Added spacing for FunctionDeclaration
1 parent 54051fa commit eed0e1d

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
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.6",
3+
"version": "0.3.7",
44
"license": "./LICENSE",
55
"exports": {
66
"./colon_spacing": "./src/plugins/colon_spacing.ts",

src/plugins/colon_spacing.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)