Skip to content

Commit 54051fa

Browse files
committed
fix: Added TSPropertySignature for correct spacing in interfaces
1 parent 5ba6cab commit 54051fa

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
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.5",
3+
"version": "0.3.6",
44
"license": "./LICENSE",
55
"exports": {
66
"./colon_spacing": "./src/plugins/colon_spacing.ts",

src/plugins/colon_spacing.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { rangeDistance } from '../range_distance.ts';
12
import { rangePadding } from '../range_padding.ts';
23

34
const 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),

src/range_distance.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* This function calculates the distance in a range.
3+
*
4+
* @param range The range to calculate the distance for
5+
*/
6+
export function rangeDistance(range : Deno.lint.Range) : number {
7+
return range[1] - range[0];
8+
}

0 commit comments

Comments
 (0)