Skip to content

Commit 99db195

Browse files
committed
feat: Import spacing
1 parent 04d0dd0 commit 99db195

File tree

4 files changed

+112
-41
lines changed

4 files changed

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

src/plugins/colon_spacing.ts

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
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
}

src/plugins/imports.ts

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,60 @@
1+
import { rangePadding } from '../range_padding.ts';
2+
13
/**
24
* This plugin ensures consistent formatting for imports.
35
*/
46
const imports : Deno.lint.Plugin = {
57
name: 'imports',
68
rules: {
7-
'spacing': {
9+
'spacing-before': {
810
create(context) : Deno.lint.LintVisitor {
911
return {
1012
ImportDeclaration(node) : void {
11-
const sectionStart = node.range[0];
12-
const sectionEnd = node.range[1];
13-
14-
if (node.specifiers.length > 0) {
15-
context.report({
16-
message: 'Wrong import spacing. Expected 1 space after { and 1 space before }.',
17-
range: [sectionStart - 1, sectionEnd + 1],
18-
// fix(fixer) : Deno.lint.Fix{
19-
// return fixer.replaceTextRange([sectionStart, sectionEnd], ' ');
20-
// }
21-
});
13+
if (node.specifiers.find(specifier => specifier.type === 'ImportSpecifier')) {
14+
const section : Deno.lint.Range = [
15+
node.range[0] + context.sourceCode.getText(node).substring(
16+
0,
17+
node.specifiers[0].range[0] - node.range[0]
18+
).search(/\{(?: +|)/) + 1,
19+
node.specifiers[0].range[0]
20+
]
21+
22+
23+
if (section[1] - section[0] !== 1) {
24+
context.report({
25+
message: 'Wrong import spacing. Expected 1 space after {.',
26+
range: rangePadding(section),
27+
fix(fixer) : Deno.lint.Fix{
28+
return fixer.replaceTextRange(section, ' ');
29+
}
30+
});
31+
}
32+
}
33+
}
34+
};
35+
}
36+
},
37+
'spacing-after': {
38+
create(context) : Deno.lint.LintVisitor {
39+
return {
40+
ImportDeclaration(node) : void {
41+
if (node.specifiers.find(specifier => specifier.type === 'ImportSpecifier')) {
42+
const start = node.specifiers[node.specifiers.length - 1].range[1];
43+
const section : Deno.lint.Range = [
44+
start,
45+
start + context.sourceCode.getText(node).substring(start - node.range[0]).search(/(?<= +|)\}/)
46+
]
47+
48+
49+
if (section[1] - section[0] !== 1) {
50+
context.report({
51+
message: 'Wrong import spacing. Expected 1 space before }.',
52+
range: rangePadding(section),
53+
fix(fixer) : Deno.lint.Fix {
54+
return fixer.replaceTextRange(section, ' ');
55+
}
56+
});
57+
}
2258
}
2359
}
2460
};

src/range_padding.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* This function add padding to the range.
3+
*
4+
* @param range The range to add padding to
5+
* @param padding Amount of padding
6+
*/
7+
export function rangePadding(
8+
range : Deno.lint.Range,
9+
padding : number = 1
10+
) : Deno.lint.Range {
11+
return [
12+
range[0] - padding,
13+
range[1] + padding
14+
]
15+
}

0 commit comments

Comments
 (0)