Skip to content

Commit bd3aaed

Browse files
committed
feature: @nirguna/printer-wasm: lint-formatting: add missing-indent rule
1 parent cacaaee commit bd3aaed

3 files changed

Lines changed: 161 additions & 3 deletions

File tree

packages/printer-wasm/test/lint-formatting/lint.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
import * as indent from './rules/indent.js';
22
import * as blankLine from './rules/blank-line.js';
33
import * as trailingSpace from './rules/trailing-space.js';
4+
import * as missingIndent from './rules/missing-indent.js';
45

5-
const rules = [indent, trailingSpace, blankLine];
6+
const rules = [
7+
indent,
8+
trailingSpace,
9+
blankLine,
10+
missingIndent,
11+
];
612

713
export const lint = (text) => {
814
const issues = [];
915
const lines = text.split('\n');
1016

1117
for (const [i, line] of lines.entries()) {
18+
const next = lines[i + 1];
19+
const context = {
20+
isFirstLine: !i,
21+
isLastLine: i === lines.length - 1,
22+
};
23+
1224
for (const rule of rules) {
13-
const next = lines[i + 1];
14-
const position = rule.check(line, next);
25+
const position = rule.check(line, next, context);
1526

1627
if (position)
1728
issues.push({
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const report = () => 'missing indent';
2+
3+
export const check = (line, _, {isFirstLine, isLastLine}) => {
4+
if (isFirstLine || isLastLine)
5+
return null;
6+
7+
if (line.trim() === '')
8+
return null;
9+
10+
if (line.startsWith('s'))
11+
return null;
12+
13+
return {
14+
index: 0,
15+
};
16+
};
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import {test} from 'supertape';
2+
import {montag} from 'montag';
3+
import {check} from './missing-indent.js';
4+
5+
test('missing-indent: flags an unindented line', (t) => {
6+
const position = check('(memory 1)', undefined, {
7+
isFirstLine: false,
8+
isLastLine: false,
9+
});
10+
11+
t.deepEqual(position, {
12+
index: 0,
13+
});
14+
t.end();
15+
});
16+
17+
test('missing-indent: first line is exempt', (t) => {
18+
const position = check('(module', undefined, {
19+
isFirstLine: true,
20+
isLastLine: false,
21+
});
22+
23+
t.notOk(position);
24+
t.end();
25+
});
26+
27+
test('missing-indent: last line is exempt', (t) => {
28+
const position = check(')', undefined, {
29+
isFirstLine: false,
30+
isLastLine: true,
31+
});
32+
33+
t.notOk(position);
34+
t.end();
35+
});
36+
37+
test('missing-indent: blank line is not flagged', (t) => {
38+
const position = check('', undefined, {
39+
isFirstLine: false,
40+
isLastLine: false,
41+
});
42+
43+
t.notOk(position);
44+
t.end();
45+
});
46+
47+
test('missing-indent: correctly indented line passes', (t) => {
48+
const position = check(' (memory 1)', undefined, {
49+
isFirstLine: false,
50+
isLastLine: false,
51+
});
52+
53+
t.notOk(position);
54+
t.end();
55+
});
56+
57+
test('missing-indent: catches the broken-after-table shape from step 0', (t) => {
58+
const source = montag`
59+
(module
60+
(table 1 2 anyfunc)
61+
(memory 1)
62+
(export "x" (func $x))
63+
(func $x)
64+
)
65+
`;
66+
67+
const lines = source.split('\n');
68+
69+
const ctx = (i) => ({
70+
isFirstLine: !i,
71+
isLastLine: i === lines.length - 1,
72+
});
73+
74+
const flagLines = [2, 3, 4];
75+
76+
for (const i of flagLines) {
77+
const position = check(lines[i], lines[i + 1], ctx(i));
78+
t.equal(position?.index, 0, `line ${i + 1} should be flagged`);
79+
}
80+
81+
t.end();
82+
});
83+
84+
test('missing-indent: closing paren at the end is exempt', (t) => {
85+
const source = montag`
86+
(module
87+
(memory 1)
88+
)
89+
`;
90+
91+
const lines = source.split('\n');
92+
93+
const ctx = (i) => ({
94+
isFirstLine: !i,
95+
isLastLine: i === lines.length - 1,
96+
});
97+
98+
for (const [i, line] of lines.entries()) {
99+
const position = check(line, lines[i + 1], ctx(i));
100+
t.notOk(position, `line ${i + 1} should be exempt`);
101+
}
102+
103+
t.end();
104+
});
105+
106+
test('missing-indent: catches the broken-after-elem shape from step 0', (t) => {
107+
const source = montag`
108+
(module
109+
(elem (i32.const 0) $x $y)
110+
(table 1 2 anyfunc)
111+
(func $x)
112+
(func $y)
113+
)
114+
`;
115+
116+
const lines = source.split('\n');
117+
118+
const ctx = (i) => ({
119+
isFirstLine: !i,
120+
isLastLine: i === lines.length - 1,
121+
});
122+
123+
const flagLines = [2];
124+
125+
for (const i of flagLines) {
126+
const position = check(lines[i], lines[i + 1], ctx(i));
127+
t.equal(position?.index, 0, `line ${i + 1} should be flagged`);
128+
}
129+
130+
t.end();
131+
});

0 commit comments

Comments
 (0)