forked from textlint/textlint-plugin-typst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinting.test.ts
More file actions
379 lines (349 loc) · 10.6 KB
/
linting.test.ts
File metadata and controls
379 lines (349 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import fs from "node:fs";
import path from "node:path";
import {
TextlintKernel,
type TextlintKernelRule,
type TextlintResult,
} from "@textlint/kernel";
// @ts-expect-error
import textlintRulePeriodInListItem from "textlint-rule-period-in-list-item";
import {
rules as textlintRulePresetJaTechnicalWritingRules,
rulesConfig as textlintRulePresetJaTechnicalWritingRulesConfig,
// @ts-expect-error
} from "textlint-rule-preset-ja-technical-writing";
// @ts-expect-error
import textlintRuleSentenceLength from "textlint-rule-sentence-length";
import { beforeAll, describe, expect, it } from "vitest";
import typstPlugin from "../../src";
describe("linting", () => {
describe("smoke tests", () => {
describe("textlint-rule-preset-ja-technical-writing", () => {
let textlintResult: TextlintResult;
beforeAll(async () => {
const kernel = new TextlintKernel();
textlintResult = await kernel.lintText(
fs.readFileSync(
path.join(
__dirname,
"./fixtures/smoke/textlint-rule-preset-ja-technical-writing/main.typ",
),
"utf-8",
),
{
ext: ".typ",
plugins: [
{
pluginId: "typst",
plugin: typstPlugin,
},
],
rules: [
// Set each rule in the preset individually
...Object.entries(textlintRulePresetJaTechnicalWritingRules).map(
([id, rule]) =>
({
ruleId: `ja-technical-writing/${id}`,
rule: rule,
options:
textlintRulePresetJaTechnicalWritingRulesConfig[id] ||
true,
}) as TextlintKernelRule,
),
],
},
);
});
// Rule test configurations
const ruleTests = [
{
ruleId: "sentence-length",
expectedMessage: "exceeds the maximum sentence length",
},
{
ruleId: "max-ten",
expectedMessage: '一つの文で"、"を4つ以上使用',
},
{
ruleId: "max-kanji-continuous-len",
expectedMessage: "漢字が7つ以上連続",
},
{
ruleId: "no-mix-dearu-desumasu",
expectedMessage: '"ですます"調 でなければなりません',
},
{
ruleId: "ja-no-mixed-period",
expectedMessage: '文末が"。"で終わっていません',
},
{
ruleId: "no-doubled-joshi",
expectedMessage: "一文に二回以上利用されている助詞",
},
{
ruleId: "no-dropping-the-ra",
expectedMessage: "ら抜き言葉を使用",
},
{
ruleId: "no-doubled-conjunctive-particle-ga",
expectedMessage: '逆接の接続助詞 "が" が二回以上使われています',
},
{
ruleId: "no-doubled-conjunction",
expectedMessage: "同じ接続詞(しかし)が連続",
},
{
ruleId: "no-exclamation-question-mark",
expectedMessage: 'Disallow to use "!"',
},
{
ruleId: "no-hankaku-kana",
expectedMessage: "Disallow to use 半角カタカナ",
},
{
ruleId: "ja-no-weak-phrase",
expectedMessage: '弱い表現: "かも" が使われています',
},
{
ruleId: "ja-no-successive-word",
expectedMessage: "が連続して2回使われています",
},
{
ruleId: "ja-no-redundant-expression",
expectedMessage: 'することが可能です"は冗長な表現',
},
{
ruleId: "ja-unnatural-alphabet",
expectedMessage: "不自然なアルファベット",
},
{
ruleId: "no-unmatched-pair",
expectedMessage: "Cannot find a pairing character for (",
},
];
// Special cases with multiple expected messages
const multiMessageTests = [
{
ruleId: "arabic-kanji-numbers",
expectedMessages: ["十番目 => 10番目", "1時的 => 一時的"],
},
{
ruleId: "no-double-negative-ja",
expectedMessages: [
"二重否定: 〜なくもない",
"二重否定: 〜ないことはない",
],
},
{
ruleId: "no-nfd",
expectedMessages: ['ホ゜" => "ポ"', 'シ゛" => "ジ"'],
},
{
ruleId: "ja-no-abusage",
expectedMessages: ["可変する", "適用"],
},
];
const getViolations = (ruleId: string) => {
return textlintResult.messages.filter(
(message) => message.ruleId === `ja-technical-writing/${ruleId}`,
);
};
// Single message tests
for (const { ruleId, expectedMessage } of ruleTests) {
it(`should detect ${ruleId} violations`, () => {
const violations = getViolations(ruleId);
expect(violations.length).toBeGreaterThan(0);
expect(violations[0].message).toContain(expectedMessage);
});
}
// Multi-message tests
for (const { ruleId, expectedMessages } of multiMessageTests) {
it(`should detect ${ruleId} violations`, () => {
const violations = getViolations(ruleId);
expect(violations.length).toBeGreaterThan(0);
for (const expectedMessage of expectedMessages) {
expect(
violations.some((v) => v.message.includes(expectedMessage)),
).toBe(true);
}
});
}
});
describe("textlint-rule-period-in-list-item", () => {
let textlintResult: TextlintResult;
beforeAll(async () => {
const kernel = new TextlintKernel();
textlintResult = await kernel.lintText(
fs.readFileSync(
path.join(
__dirname,
"./fixtures/smoke/textlint-rule-period-in-list-item/main.typ",
),
"utf-8",
),
{
ext: ".typ",
plugins: [
{
pluginId: "typst",
plugin: typstPlugin,
},
],
rules: [
{
ruleId: "period-in-list-item",
rule: textlintRulePeriodInListItem,
},
],
},
);
});
const getViolations = () => {
return textlintResult.messages.filter(
(message) => message.ruleId === "period-in-list-item",
);
};
it("should detect period in single line bullet list items", () => {
const violations = getViolations();
const singleLineBulletViolation = violations.find(
(v) => v.loc.start.line === 6,
);
expect(singleLineBulletViolation).toBeDefined();
expect(singleLineBulletViolation?.message).toBe(
'Should remove period mark(".") at end of list item.',
);
});
it("should detect period in multiple lines bullet list items", () => {
const violations = getViolations();
const multipleLinesBulletViolation = violations.find(
(v) => v.loc.start.line === 12,
);
expect(multipleLinesBulletViolation).toBeDefined();
expect(multipleLinesBulletViolation?.message).toBe(
'Should remove period mark(".") at end of list item.',
);
});
it("should detect period in single line numbered list items", () => {
const violations = getViolations();
const singleLineNumberedViolation = violations.find(
(v) => v.loc.start.line === 27,
);
expect(singleLineNumberedViolation).toBeDefined();
expect(singleLineNumberedViolation?.message).toBe(
'Should remove period mark(".") at end of list item.',
);
});
it("should detect period in multiple lines numbered list items", () => {
const violations = getViolations();
const multipleLineNumberedViolation = violations.find(
(v) => v.loc.start.line === 41,
);
expect(multipleLineNumberedViolation).toBeDefined();
expect(multipleLineNumberedViolation?.message).toBe(
'Should remove period mark(".") at end of list item.',
);
});
});
describe("textlint-rule-sentence-length", () => {
const lintFile = async (fileName: string) => {
const kernel = new TextlintKernel();
return await kernel.lintText(
fs.readFileSync(
path.join(
__dirname,
`./fixtures/smoke/textlint-rule-sentence-length/${fileName}`,
),
"utf-8",
),
{
ext: ".typ",
plugins: [
{
pluginId: "typst",
plugin: typstPlugin,
},
],
rules: [
{
ruleId: "sentence-length",
rule: textlintRuleSentenceLength,
options: {
max: 50,
},
},
],
},
);
};
const getViolations = (result: TextlintResult) => {
return result.messages.filter(
(message) => message.ruleId === "sentence-length",
);
};
it("should ignore single line comments", async () => {
const result = await lintFile("single_line_comment_valid.typ");
const violations = getViolations(result);
expect(violations).toEqual([]);
});
it("should ignore multiline comments", async () => {
const result = await lintFile("multiline_comment_valid.typ");
const violations = getViolations(result);
expect(violations).toEqual([]);
});
// see: https://github.com/textlint/textlint-plugin-typst/pull/31
it.fails(
"should handle elements immediately below comments",
async () => {
const result = await lintFile(
"element_immediately_below_comment_valid.typ",
);
const violations = getViolations(result);
expect(violations).toEqual([]);
},
);
it("should handle term lists", async () => {
const result = await lintFile("term_list_valid.typ");
const violations = getViolations(result);
expect(violations).toEqual([]);
});
// see: https://github.com/textlint/textlint-plugin-typst/pull/31
it.fails("should handle nested lists", async () => {
const result = await lintFile("nested_list_valid.typ");
const violations = getViolations(result);
expect(violations).toEqual([]);
});
it("should ignore math blocks", async () => {
const result = await lintFile("math_valid.typ");
const violations = getViolations(result);
expect(violations).toEqual([]);
});
it("should handle figures", async () => {
const result = await lintFile("figure_valid.typ");
const violations = getViolations(result);
expect(violations).toEqual([]);
});
it("should handle nested content blocks", async () => {
const result = await lintFile("nested_content_blocks_valid.typ");
const violations = getViolations(result);
expect(violations).toEqual([]);
});
it("should detect violations in lists", async () => {
const result = await lintFile("list_invalid.typ");
const violations = getViolations(result);
expect(
violations.length,
`Expected 1 violation but got ${violations.length}. Violations: ${JSON.stringify(violations, null, 2)}`,
).toBe(1);
});
// see: https://github.com/textlint/textlint-plugin-typst/pull/31
it.fails("should detect violations in term lists", async () => {
const result = await lintFile("term_list_invalid.typ");
const violations = getViolations(result);
expect(
violations.length,
`Expected 1 violation but got ${violations.length}. Violations: ${JSON.stringify(violations, null, 2)}`,
).toBe(1);
});
});
});
});