@r_masseater/textlint-rule-preset-ja 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,6 +4,7 @@ A textlint preset for Japanese prose.
4
4
 
5
5
  - `yomiyasu` flags Japanese that is hard to read, the kind AI tends to write.
6
6
  - `no-taigendome` flags a sentence that puts a comma and then ends in a bare noun phrase instead of a predicate.
7
+ - `no-ni-aru-file` flags a sentence that says where a topic is written by ending with a file and the verb phrase "ni aru". It suggests a verb such as "ni kaite aru" or "wo yomu".
7
8
 
8
9
  `no-taigendome` reads morphemes from [@r_masseater/y8](https://www.npmjs.com/package/@r_masseater/y8), which is a peer dependency.
9
10
 
@@ -21,4 +22,4 @@ npm install --save-dev textlint @r_masseater/textlint-rule-preset-ja @r_masseate
21
22
 
22
23
  ## Agent skills
23
24
 
24
- The package ships TanStack Intent skills for coding agents. Load one with `vp exec intent load @r_masseater/textlint-rule-preset-ja#<skill>`.
25
+ The package ships agent skills in `skills/`. Add `permissions: { "@r_masseater/textlint-rule-preset-ja": { skills: true } }` to `pnpm-workspace.yaml` and `vp install` links them into the skill directories of your agents.
package/dist/index.d.mts CHANGED
@@ -74,8 +74,9 @@ type TxtNodeType = keyof typeof ASTNodeTypes;
74
74
  */
75
75
  type AnyTxtNode = TxtNode | TxtTextNode | TxtParentNode;
76
76
  /**
77
- * Position's line start with 1.
78
- * Position's column start with 0.
77
+ * A position in the document text.
78
+ * `line` starts with 1, and `column` starts with 0 and counts UTF-16 code units.
79
+ * The character after a line ending is at column 0 of the next line.
79
80
  * This is for compatibility with JavaScript AST.
80
81
  * https://gist.github.com/azu/8866b2cb9b7a933e01fe
81
82
  */
@@ -84,22 +85,36 @@ type TxtNodePosition = {
84
85
  readonly column: number;
85
86
  };
86
87
  /**
87
- * Location
88
+ * Location.
89
+ * `start` is the position of `range[0]`, and `end` is the position of `range[1]`.
88
90
  */
89
91
  type TxtNodeLocation = {
90
92
  readonly start: TxtNodePosition;
91
93
  readonly end: TxtNodePosition;
92
94
  };
93
95
  /**
94
- * Range starts with 0
96
+ * Range is a pair of offsets in the document text.
97
+ * `startIndex` is inclusive and `endIndex` is exclusive.
98
+ * Offsets start with 0 and count UTF-16 code units.
95
99
  */
96
100
  type TxtNodeRange = readonly [startIndex: number, endIndex: number];
97
101
  /**
98
102
  * TxtNode is abstract interface of AST Node.
99
103
  * Probably, Real TxtNode implementation has more properties.
104
+ *
105
+ * These type definitions are the definition of the generic nodes of TxtAST.
106
+ * A property marked as optional with `?` may be absent, `undefined`, or `null`.
107
+ * Every other property is required in every format.
100
108
  */
101
109
  interface TxtNode {
110
+ /**
111
+ * Name of the node type.
112
+ * A generic node type is PascalCase. A node type that a format adds starts with a lowercase letter.
113
+ */
102
114
  readonly type: TxtNodeType;
115
+ /**
116
+ * Source text of the node. It is the document text from `range[0]` to `range[1]`.
117
+ */
103
118
  readonly raw: string;
104
119
  readonly range: TxtNodeRange;
105
120
  readonly loc: TxtNodeLocation;
@@ -111,6 +126,10 @@ interface TxtNode {
111
126
  * For example, `Str` Node is an TxtTextNode.
112
127
  */
113
128
  interface TxtTextNode extends TxtNode {
129
+ /**
130
+ * Content of the node without the markup, so it may differ from `raw`.
131
+ * Each text node type states what `value` holds.
132
+ */
114
133
  readonly value: string;
115
134
  }
116
135
  /**
@@ -118,9 +137,19 @@ interface TxtTextNode extends TxtNode {
118
137
  * Parent Node has children that are consist of TxtParentNode or TxtTextNode
119
138
  */
120
139
  interface TxtParentNode extends TxtNode {
140
+ /**
141
+ * Child nodes in the source order. It may be empty.
142
+ */
121
143
  readonly children: readonly Content[];
122
144
  }
145
+ /**
146
+ * Alignment of a table column. `null` means that the column has no alignment.
147
+ */
123
148
  type AlignType = "left" | "right" | "center" | null;
149
+ /**
150
+ * How a reference points to its `Definition`.
151
+ * In Markdown, `[text][label]` is "full", `[label][]` is "collapsed", and `[label]` is "shortcut".
152
+ */
124
153
  type ReferenceType = "shortcut" | "collapsed" | "full";
125
154
  type Content = TopLevelContent | ListContent | TableContent | RowContent | PhrasingContent;
126
155
  /**
@@ -131,7 +160,7 @@ type TopLevelContent = BlockContent;
131
160
  * All node types that may be used where markdown block content is accepted.
132
161
  * These types are accepted inside block quotes, list items, and roots.
133
162
  */
134
- type BlockContent = TxtParagraphNode | TxtHeaderNode | TxtHorizontalRuleNode | TxtBlockQuoteNode | TxtListNode | TxtTableNode | TxtHtmlNode | TxtCodeBlockNode;
163
+ type BlockContent = TxtParagraphNode | TxtHeaderNode | TxtHorizontalRuleNode | TxtBlockQuoteNode | TxtListNode | TxtTableNode | TxtHtmlNode | TxtCodeBlockNode | TxtDefinitionNode;
135
164
  /**
136
165
  * All node types that are acceptable inside lists.
137
166
  */
@@ -147,116 +176,246 @@ type RowContent = TxtTableCellNode;
147
176
  /**
148
177
  * All node types that are acceptable in a (interactive) phrasing context (so not in links).
149
178
  */
150
- type PhrasingContent = TxtLinkNode | StaticPhrasingContent;
179
+ type PhrasingContent = TxtLinkNode | TxtLinkReferenceNode | StaticPhrasingContent;
151
180
  /**
152
181
  * All node types that are acceptable in a static phrasing context.
153
182
  */
154
- type StaticPhrasingContent = TxtStrNode | TxtEmphasisNode | TxtStrongNode | TxtDeleteNode | TxtHtmlNode | TxtCodeNode | TxtBreakNode | TxtImageNode | TxtCommentNode;
183
+ type StaticPhrasingContent = TxtStrNode | TxtEmphasisNode | TxtStrongNode | TxtDeleteNode | TxtHtmlNode | TxtCodeNode | TxtBreakNode | TxtImageNode | TxtImageReferenceNode | TxtCommentNode;
184
+ /**
185
+ * The whole document. It is the root of the AST.
186
+ */
155
187
  interface TxtDocumentNode extends TxtParentNode {
156
188
  readonly type: "Document";
157
189
  }
190
+ /**
191
+ * A paragraph.
192
+ */
158
193
  interface TxtParagraphNode extends TxtParentNode {
159
194
  readonly type: "Paragraph";
160
195
  readonly children: readonly PhrasingContent[];
161
196
  }
197
+ /**
198
+ * A heading.
199
+ */
162
200
  interface TxtHeaderNode extends TxtParentNode {
163
201
  readonly type: "Header";
202
+ /**
203
+ * Level of the heading. 1 is the top level.
204
+ */
164
205
  readonly depth: 1 | 2 | 3 | 4 | 5 | 6;
165
206
  readonly children: readonly PhrasingContent[];
166
207
  }
208
+ /**
209
+ * A thematic break.
210
+ */
167
211
  interface TxtHorizontalRuleNode extends TxtNode {
168
212
  readonly type: "HorizontalRule";
169
213
  }
214
+ /**
215
+ * A quotation of blocks.
216
+ */
170
217
  interface TxtBlockQuoteNode extends TxtParentNode {
171
218
  readonly type: "BlockQuote";
172
219
  readonly children: readonly BlockContent[];
173
220
  }
221
+ /**
222
+ * A list. Its children are `ListItem`.
223
+ */
174
224
  interface TxtListNode extends TxtParentNode {
175
225
  readonly type: "List";
226
+ /**
227
+ * Whether the items are ordered.
228
+ */
176
229
  readonly ordered?: boolean | null | undefined;
230
+ /**
231
+ * Number of the first item of an ordered list.
232
+ */
177
233
  readonly start?: number | null | undefined;
234
+ /**
235
+ * Whether any of the items are separated by blank lines.
236
+ */
178
237
  readonly spread?: boolean | null | undefined;
179
238
  readonly children: readonly ListContent[];
180
239
  }
240
+ /**
241
+ * An item of a list.
242
+ */
181
243
  interface TxtListItemNode extends TxtParentNode {
182
244
  readonly type: "ListItem";
245
+ /**
246
+ * `true` or `false` for a task list item. `null` or absent for other items.
247
+ */
183
248
  readonly checked?: boolean | null | undefined;
249
+ /**
250
+ * Whether the item contains blocks separated by blank lines.
251
+ */
184
252
  readonly spread?: boolean | null | undefined;
185
253
  readonly children: readonly BlockContent[];
186
254
  }
255
+ /**
256
+ * A table. Its children are `TableRow`.
257
+ */
187
258
  interface TxtTableNode extends TxtParentNode {
188
259
  readonly type: "Table";
260
+ /**
261
+ * Alignment of each column.
262
+ */
189
263
  readonly align?: readonly AlignType[] | null | undefined;
190
264
  readonly children: readonly TableContent[];
191
265
  }
266
+ /**
267
+ * A row of a table. Its children are `TableCell`.
268
+ */
192
269
  interface TxtTableRowNode extends TxtParentNode {
193
270
  readonly type: "TableRow";
194
271
  readonly children: readonly RowContent[];
195
272
  }
273
+ /**
274
+ * A cell of a table.
275
+ */
196
276
  interface TxtTableCellNode extends TxtParentNode {
197
277
  readonly type: "TableCell";
198
278
  readonly children: readonly PhrasingContent[];
199
279
  }
280
+ /**
281
+ * Raw HTML, as a block or inline. `value` is the HTML.
282
+ */
200
283
  interface TxtHtmlNode extends TxtTextNode {
201
284
  readonly type: "Html";
202
285
  }
286
+ /**
287
+ * A comment of the format. `value` is the content of the comment.
288
+ */
203
289
  interface TxtCommentNode extends TxtTextNode {
204
290
  readonly type: "Comment";
205
291
  }
292
+ /**
293
+ * A block of code. `value` is the code.
294
+ */
206
295
  interface TxtCodeBlockNode extends TxtTextNode {
207
296
  readonly type: "CodeBlock";
297
+ /**
298
+ * Language of the code.
299
+ */
208
300
  readonly lang?: string | null | undefined;
301
+ /**
302
+ * Additional information that follows the language.
303
+ */
209
304
  readonly meta?: string | null | undefined;
210
305
  }
306
+ /**
307
+ * Prose. `value` is the text that rules check.
308
+ * Markup becomes other nodes, and a parser does not split `Str` into sentences or words.
309
+ */
211
310
  interface TxtStrNode extends TxtTextNode {
212
311
  readonly type: "Str";
213
312
  }
313
+ /**
314
+ * Emphasis.
315
+ */
214
316
  interface TxtEmphasisNode extends TxtParentNode {
215
317
  readonly type: "Emphasis";
216
318
  readonly children: readonly PhrasingContent[];
217
319
  }
320
+ /**
321
+ * Strong emphasis.
322
+ */
218
323
  interface TxtStrongNode extends TxtParentNode {
219
324
  readonly type: "Strong";
220
325
  readonly children: readonly PhrasingContent[];
221
326
  }
327
+ /**
328
+ * Deleted text.
329
+ */
222
330
  interface TxtDeleteNode extends TxtParentNode {
223
331
  readonly type: "Delete";
224
332
  readonly children: readonly PhrasingContent[];
225
333
  }
334
+ /**
335
+ * Inline code. `value` is the code.
336
+ */
226
337
  interface TxtCodeNode extends TxtTextNode {
227
338
  readonly type: "Code";
228
339
  }
340
+ /**
341
+ * A hard line break.
342
+ */
229
343
  interface TxtBreakNode extends TxtNode {
230
344
  readonly type: "Break";
231
345
  }
346
+ /**
347
+ * A link.
348
+ */
232
349
  interface TxtLinkNode extends TxtParentNode, TxtResource {
233
350
  readonly type: "Link";
234
351
  readonly children: readonly StaticPhrasingContent[];
235
352
  }
353
+ /**
354
+ * A link that points to a `Definition`.
355
+ */
236
356
  interface TxtLinkReferenceNode extends TxtParentNode, TxtReference {
237
357
  readonly type: "LinkReference";
238
358
  readonly children: readonly StaticPhrasingContent[];
359
+ /**
360
+ * How the reference is written.
361
+ */
239
362
  readonly referenceType: ReferenceType;
240
363
  }
364
+ /**
365
+ * An image.
366
+ */
241
367
  interface TxtImageNode extends TxtNode, TxtResource, TxtAlternative {
242
368
  readonly type: "Image";
243
369
  }
370
+ /**
371
+ * An image that points to a `Definition`.
372
+ */
244
373
  interface TxtImageReferenceNode extends TxtNode, TxtAlternative, TxtReference {
245
374
  readonly type: "ImageReference";
375
+ /**
376
+ * How the reference is written.
377
+ */
246
378
  readonly referenceType: ReferenceType;
247
379
  }
380
+ /**
381
+ * A definition that a reference points to.
382
+ */
248
383
  interface TxtDefinitionNode extends TxtNode, TxtResource, TxtReference {
249
384
  readonly type: "Definition";
250
385
  }
386
+ /**
387
+ * A node that refers to a resource.
388
+ */
251
389
  interface TxtResource {
390
+ /**
391
+ * Location of the resource.
392
+ */
252
393
  readonly url: string;
394
+ /**
395
+ * Title of the resource.
396
+ */
253
397
  readonly title?: string | null | undefined;
254
398
  }
399
+ /**
400
+ * A node that has an alternative text.
401
+ */
255
402
  interface TxtAlternative {
403
+ /**
404
+ * Text that describes the image.
405
+ */
256
406
  readonly alt?: string | null | undefined;
257
407
  }
408
+ /**
409
+ * A node that is matched with another node by an identifier.
410
+ */
258
411
  interface TxtReference {
412
+ /**
413
+ * Normalized form of `label`. A reference and its `Definition` have the same `identifier`.
414
+ */
259
415
  readonly identifier: string;
416
+ /**
417
+ * Label as written in the document.
418
+ */
260
419
  readonly label: string;
261
420
  }
262
421
  /**
@@ -687,10 +846,12 @@ declare const preset: {
687
846
  rules: {
688
847
  yomiyasu: TextlintRuleModule;
689
848
  "no-taigendome": TextlintRuleModule;
849
+ "no-ni-aru-file": TextlintRuleModule;
690
850
  };
691
851
  rulesConfig: {
692
852
  yomiyasu: boolean;
693
853
  "no-taigendome": boolean;
854
+ "no-ni-aru-file": boolean;
694
855
  };
695
856
  };
696
857
  export { preset as default };
package/dist/index.mjs CHANGED
@@ -1,6 +1,39 @@
1
- import { morphemesOf } from "@r_masseater/y8";
2
1
  import { Array, Effect, Option, String as String$1, pipe } from "effect";
3
2
  import { SentenceSplitterSyntax, split } from "sentence-splitter";
3
+ import { morphemesOf } from "@r_masseater/y8";
4
+ // src/shared/lib/sentence-ranges.ts
5
+ const sentenceRanges = (text) => pipe(split(text), Array.filter((child) => child.type === SentenceSplitterSyntax.Sentence), Array.map(({ range }) => range));
6
+ // src/features/check-ni-aru-file/model/no-ni-aru-file.ts
7
+ const EXTENSIONS = "md|mdx|ts|tsx|js|jsx|mjs|cjs|json|jsonc|yaml|yml|toml|rs|css|html|txt|sh|typ|cs|lock|sql|svg|xml|csv|ini|env";
8
+ const FILE_NAME = `[\\w@.~/-]*[\\w-]\\.(?:${EXTENSIONS})(?![\\w-])`;
9
+ const REF = `(?:${`\\[[^\\]]*\\]\\((?![a-z][a-z0-9+.-]*:)[^)\\s#]*\\.(?:${EXTENSIONS})(?:#[^)\\s]*)?(?:\\s[^)]*)?\\)`}|${`\`${FILE_NAME}\``}|${`(?<![\\w/.~-])${FILE_NAME}`})`;
10
+ const LOCATED_AT_REF = new RegExp(`${REF}${/\s*にあ(?:る|ります)$/u.source}`, "iu");
11
+ const REF_CHAIN = new RegExp(`(?:${REF}\\s*[、と]\\s*)+$`, "iu");
12
+ const ANY_REF_TEST = new RegExp(REF, "iu");
13
+ const TRAILING_SYMBOLS$1 = /[\s。..!!??」』))】]+$/u;
14
+ const CODE_SPAN = /`[^`]*`/gu;
15
+ const LINK_DESTINATION$1 = /\]\([^)]*\)/gu;
16
+ const TOPIC = /は/u;
17
+ const CLAUSE_BREAK = /、/u;
18
+ const NO_OFFSET$1 = 0;
19
+ const MESSAGE = "内容の置き場所を「〜は〜にある」と書くのは不自然な日本語です。「〜は [ファイル] に書いてある」「〜は [ファイル] を読む」のように、動詞で書き直してください。";
20
+ const hasTopic = (head) => pipe(head, String$1.replaceAll(LINK_DESTINATION$1, "]"), String$1.replaceAll(CODE_SPAN, ""), (rest) => TOPIC.test(rest));
21
+ const lastClause = (head) => pipe(String$1.split(String$1.replace(REF_CHAIN, "")(head), CLAUSE_BREAK), Array.filter((clause) => String$1.isNonEmpty(String$1.trim(clause))), Array.last, Option.getOrElse(() => head));
22
+ const isLocatedAtFile = (sentence) => {
23
+ const trimmed = String$1.replace(TRAILING_SYMBOLS$1, "")(sentence);
24
+ const found = LOCATED_AT_REF.exec(trimmed);
25
+ if (found === null) return false;
26
+ const clause = lastClause(String$1.slice(NO_OFFSET$1, found.index)(trimmed));
27
+ return hasTopic(clause) && !ANY_REF_TEST.test(clause);
28
+ };
29
+ const violations = (text) => Array.filter(sentenceRanges(text), (range) => isLocatedAtFile(String$1.slice(...range)(text)));
30
+ const noNiAruFile = (context) => {
31
+ const { Syntax, RuleError, report, getSource, locator } = context;
32
+ return { [Syntax.Paragraph]: (node) => {
33
+ const text = getSource(node);
34
+ for (const [start, end] of violations(text)) report(node, new RuleError(MESSAGE, { padding: locator.range([start, end]) }));
35
+ } };
36
+ };
4
37
  // src/features/check-taigendome/model/no-taigendome.ts
5
38
  const COMMA = /[、,]/u;
6
39
  const PARENTHESES = /[((](?<inner>[^()()]*)[))]/gu;
@@ -50,7 +83,6 @@ const CHECKS = [{
50
83
  test: isShortTopicLead,
51
84
  slideOnly: true
52
85
  }];
53
- const sentenceRanges = (text) => pipe(split(text), Array.filter((child) => child.type === SentenceSplitterSyntax.Sentence), Array.map(({ range }) => range));
54
86
  const matchedRanges = (ranges, results) => pipe(Array.zip(ranges, results), Array.filter(([, found]) => found), Array.map(([range]) => range));
55
87
  const findingsOf$1 = (text, ranges) => ({ message, test }) => {
56
88
  const tested = Effect.forEach(ranges, (range) => test(String$1.slice(...range)(text)), { concurrency: "unbounded" });
@@ -2750,11 +2782,13 @@ const yomiyasu = (context) => {
2750
2782
  const preset = {
2751
2783
  rules: {
2752
2784
  yomiyasu,
2753
- "no-taigendome": noTaigendome
2785
+ "no-taigendome": noTaigendome,
2786
+ "no-ni-aru-file": noNiAruFile
2754
2787
  },
2755
2788
  rulesConfig: {
2756
2789
  yomiyasu: true,
2757
- "no-taigendome": true
2790
+ "no-taigendome": true,
2791
+ "no-ni-aru-file": true
2758
2792
  }
2759
2793
  };
2760
2794
  export { preset as default };