Skip to content

Commit 667b3da

Browse files
committed
feat: lint plugin tests
1 parent a150c02 commit 667b3da

File tree

5 files changed

+168
-3
lines changed

5 files changed

+168
-3
lines changed

deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"@esbuild": "npm:esbuild@^0.25.11",
115115
"@solid-js": "npm:solid-js@1.9.10",
116116
"@solid-js/router": "npm:@solidjs/router@^0.15.3",
117+
"@std/assert": "jsr:@std/assert@^1.0.15",
117118
"@std/cli": "jsr:@std/cli@^1.0.6",
118119
"@std/http": "jsr:@std/http@^1.0.6",
119120
"@std/fmt": "jsr:@std/fmt@^1.0.2",

deno.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lint/plugins/colon_spacing.test.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import colonSpacing from './colon_spacing.ts';
2+
import { assertEquals } from '@std/assert';
3+
4+
Deno.test('colon-spacing plugin', async (test) => {
5+
await test.step('after-function rule, no space', () => {
6+
const diagnostics = Deno.lint.runPlugin(
7+
colonSpacing,
8+
'main.ts',
9+
`
10+
export function Foo(arg1: string, arg2: number): string {
11+
return 'Hello World!';
12+
}
13+
`
14+
)
15+
16+
assertEquals(diagnostics.length, 1);
17+
18+
const diagnostic = diagnostics[0];
19+
20+
assertEquals(diagnostic.id, 'colon-spacing/after-function');
21+
assertEquals(diagnostic.message, 'Wrong colon spacing. Expected 1 space after function.');
22+
assertEquals(diagnostic.fix, [{range: [54, 54], text: ' '}]);
23+
});
24+
25+
await test.step('after-function rule, too many spaces', () => {
26+
const diagnostics = Deno.lint.runPlugin(
27+
colonSpacing,
28+
'main.ts',
29+
`
30+
export function Foo(arg1: string, arg2: number) : string {
31+
return 'Hello World!';
32+
}
33+
`
34+
)
35+
36+
assertEquals(diagnostics.length, 1);
37+
38+
const diagnostic = diagnostics[0];
39+
40+
assertEquals(diagnostic.id, 'colon-spacing/after-function');
41+
assertEquals(diagnostic.message, 'Wrong colon spacing. Expected 1 space after function.');
42+
assertEquals(diagnostic.fix, [{range: [54, 56], text: ' '}]);
43+
});
44+
45+
await test.step('before-type rule, no space', () => {
46+
const diagnostics = Deno.lint.runPlugin(
47+
colonSpacing,
48+
'main.ts',
49+
`
50+
export function Foo(prop1: string, prop2: number) :string {
51+
return 'Hello World!';
52+
}
53+
`
54+
)
55+
56+
assertEquals(diagnostics.length, 1);
57+
58+
const diagnostic = diagnostics[0];
59+
60+
assertEquals(diagnostic.id, 'colon-spacing/before-type');
61+
assertEquals(diagnostic.message, 'Wrong colon spacing. Expected 1 space before type.');
62+
assertEquals(diagnostic.fix, [{range: [58, 58], text: ' '}]);
63+
});
64+
65+
await test.step('before-type rule, too many spaces', () => {
66+
const diagnostics = Deno.lint.runPlugin(
67+
colonSpacing,
68+
'main.ts',
69+
`
70+
export function Foo(prop1: string, prop2: number) : string {
71+
return 'Hello World!';
72+
}
73+
`
74+
)
75+
76+
assertEquals(diagnostics.length, 1);
77+
78+
const diagnostic = diagnostics[0];
79+
80+
assertEquals(diagnostic.id, 'colon-spacing/before-type');
81+
assertEquals(diagnostic.message, 'Wrong colon spacing. Expected 1 space before type.');
82+
assertEquals(diagnostic.fix, [{range: [58, 60], text: ' '}]);
83+
});
84+
85+
await test.step('before-type rule, no space, both', () => {
86+
const diagnostics = Deno.lint.runPlugin(
87+
colonSpacing,
88+
'main.ts',
89+
`
90+
export function Foo(prop1: string, prop2: number):string {
91+
return 'Hello World!';
92+
}
93+
`
94+
)
95+
96+
assertEquals(diagnostics.length, 2);
97+
98+
const afterFunctionDiagnostic = diagnostics[0];
99+
const beforeTypeDiagnostic = diagnostics[1];
100+
101+
assertEquals(afterFunctionDiagnostic.id, 'colon-spacing/after-function');
102+
assertEquals(afterFunctionDiagnostic.message, 'Wrong colon spacing. Expected 1 space after function.');
103+
assertEquals(afterFunctionDiagnostic.fix, [{range: [56, 56], text: ' '}]);
104+
105+
assertEquals(beforeTypeDiagnostic.id, 'colon-spacing/before-type');
106+
assertEquals(beforeTypeDiagnostic.message, 'Wrong colon spacing. Expected 1 space before type.');
107+
assertEquals(beforeTypeDiagnostic.fix, [{range: [57, 57], text: ' '}]);
108+
});
109+
110+
await test.step('before-type rule, too many spaces, both', () => {
111+
const diagnostics = Deno.lint.runPlugin(
112+
colonSpacing,
113+
'main.ts',
114+
`
115+
export function Foo(prop1: string, prop2: number) : string {
116+
return 'Hello World!';
117+
}
118+
`
119+
)
120+
121+
assertEquals(diagnostics.length, 2);
122+
123+
const afterFunctionDiagnostic = diagnostics[0];
124+
const beforeTypeDiagnostic = diagnostics[1];
125+
126+
assertEquals(afterFunctionDiagnostic.id, 'colon-spacing/after-function');
127+
assertEquals(afterFunctionDiagnostic.message, 'Wrong colon spacing. Expected 1 space after function.');
128+
assertEquals(afterFunctionDiagnostic.fix, [{range: [56, 58], text: ' '}]);
129+
130+
assertEquals(beforeTypeDiagnostic.id, 'colon-spacing/before-type');
131+
assertEquals(beforeTypeDiagnostic.message, 'Wrong colon spacing. Expected 1 space before type.');
132+
assertEquals(beforeTypeDiagnostic.fix, [{range: [59, 61], text: ' '}]);
133+
});
134+
});

lint/plugins/colon_spacing.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,31 @@ export default {
1717

1818
if (index !== -1 && sectionEnd - sectionStart !== 1) {
1919
context.report({
20-
message: `Wrong colon spacing.`,
20+
message: `Wrong colon spacing. Expected 1 space after function.`,
2121
range: [sectionStart - 1, sectionEnd + 1],
22-
fix(fixer) {
22+
fix(fixer) : Deno.lint.Fix{
23+
return fixer.replaceTextRange([sectionStart, sectionEnd], ' ');
24+
}
25+
});
26+
}
27+
}
28+
}
29+
};
30+
}
31+
},
32+
'before-type': {
33+
create(context): Deno.lint.LintVisitor {
34+
return {
35+
TSTypeAnnotation(node): void {
36+
if (node.parent.type === 'FunctionDeclaration') {
37+
const sectionStart = node.range[0] + 1;
38+
const sectionEnd = node.typeAnnotation.range[0];
39+
40+
if (sectionEnd - sectionStart !== 1) {
41+
context.report({
42+
message: `Wrong colon spacing. Expected 1 space before type.`,
43+
range: [sectionStart - 1, sectionEnd + 1],
44+
fix(fixer) : Deno.lint.Fix{
2345
return fixer.replaceTextRange([sectionStart, sectionEnd], ' ');
2446
}
2547
});

src/components/toggle_switch/toggle_switch.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createSignal, type JSXElement } from '@solid-js';
22

33
import './index.css';
44

5-
export function ToggleSwitch(_foo: string, _bar: string) : JSXElement {
5+
export function ToggleSwitch() : JSXElement {
66
const [state, setState] = createSignal(false);
77

88
const toggle = () => {

0 commit comments

Comments
 (0)