@nitpicker/analyze-textlint 0.4.4 → 0.5.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.
@@ -0,0 +1,55 @@
1
+ /**
2
+ * A textlint rule configuration map.
3
+ * Keys are rule identifiers; values are `true` (enable with defaults),
4
+ * `false` (disable), or a rule-specific options object.
5
+ */
6
+ type Rule = Record<string, unknown>;
7
+ /**
8
+ * Plugin options for the textlint text-proofreading analysis.
9
+ */
10
+ type Options = {
11
+ /**
12
+ * Custom rule overrides merged on top of the default Japanese-oriented rule set.
13
+ * Set a rule to `false` to disable it; set to `true` or an options object to enable.
14
+ */
15
+ rules?: Rule;
16
+ };
17
+ /**
18
+ * Analyze plugin that runs textlint Japanese text-proofreading rules
19
+ * against each page's HTML.
20
+ *
21
+ * The default rule set is heavily Japanese-oriented (mixed script detection,
22
+ * doubled particles, honorific misuse, etc.) because the primary use case
23
+ * is auditing Japanese corporate websites. Users can override or extend
24
+ * rules via the `rules` option.
25
+ *
26
+ * ## Lazy linter initialization
27
+ *
28
+ * Building the linter is expensive because it dynamically imports 20+
29
+ * rule packages (many of which are CJS and require interop resolution).
30
+ * The linter is therefore created lazily on the first `eachPage` call
31
+ * and the resulting promise is cached for all subsequent pages.
32
+ *
33
+ * This "lazy singleton" pattern (`linterPromise` variable) ensures:
34
+ * 1. Zero startup cost if textlint is configured but no pages match.
35
+ * 2. No duplicate initialization even under concurrent `eachPage` calls,
36
+ * because the same promise is shared (Promise deduplication).
37
+ * @example
38
+ * ```jsonc
39
+ * // nitpicker.config.json
40
+ * {
41
+ * "plugins": {
42
+ * "analyze": {
43
+ * "@nitpicker/analyze-textlint": {
44
+ * "rules": {
45
+ * "max-ten": { "max": 5 },
46
+ * "spellcheck-tech-word": false
47
+ * }
48
+ * }
49
+ * }
50
+ * }
51
+ * }
52
+ * ```
53
+ */
54
+ declare const _default: import("@nitpicker/core").PluginFactory<Options, string>;
55
+ export default _default;
@@ -0,0 +1,256 @@
1
+ import { definePlugin } from '@nitpicker/core';
2
+ import { createLinter } from 'textlint';
3
+ const defaultRules = {
4
+ /**
5
+ * @see https://github.com/textlint-ja/textlint-rule-no-nfd
6
+ */
7
+ 'no-nfd': true,
8
+ /**
9
+ * @see https://github.com/textlint-ja/textlint-rule-max-ten
10
+ */
11
+ 'max-ten': {
12
+ max: 3,
13
+ },
14
+ /**
15
+ * @see https://github.com/azu/textlint-rule-spellcheck-tech-word
16
+ */
17
+ 'spellcheck-tech-word': true,
18
+ /**
19
+ * @see https://github.com/azu/textlint-rule-web-plus-db
20
+ */
21
+ 'web-plus-db': true,
22
+ /**
23
+ * @see https://github.com/textlint-ja/textlint-rule-no-mix-dearu-desumasu
24
+ */
25
+ // cspell:disable-next-line
26
+ 'no-mix-dearu-desumasu': {
27
+ preferInHeader: '',
28
+ preferInBody: '',
29
+ preferInList: '',
30
+ strict: false,
31
+ },
32
+ /**
33
+ * @see https://github.com/textlint-ja/textlint-rule-no-doubled-joshi
34
+ */
35
+ 'no-doubled-joshi': true,
36
+ /**
37
+ * @see https://github.com/textlint-ja/textlint-rule-no-double-negative-ja
38
+ */
39
+ 'no-double-negative-ja': true,
40
+ /**
41
+ * @see https://github.com/textlint-ja/textlint-rule-no-hankaku-kana
42
+ */
43
+ 'no-hankaku-kana': true, // cspell:disable-line
44
+ /**
45
+ * @see https://github.com/textlint-ja/textlint-rule-ja-no-abusage
46
+ */
47
+ 'ja-no-abusage': true,
48
+ 'no-mixed-zenkaku-and-hankaku-alphabet': true, // cspell:disable-line
49
+ 'no-dropping-the-ra': true,
50
+ 'no-doubled-conjunctive-particle-ga': true,
51
+ 'no-doubled-conjunction': true,
52
+ 'ja-no-mixed-period': true,
53
+ /**
54
+ * @see https://github.com/KeitaMoromizato/textlint-rule-max-appearence-count-of-words#readme
55
+ */
56
+ 'max-appearence-count-of-words': true, // cspell:disable-line
57
+ 'ja-hiragana-keishikimeishi': true, // cspell:disable-line
58
+ 'ja-hiragana-fukushi': true, // cspell:disable-line
59
+ 'ja-hiragana-hojodoushi': true, // cspell:disable-line
60
+ 'ja-unnatural-alphabet': true,
61
+ '@textlint-ja/textlint-rule-no-insert-dropping-sa': true,
62
+ 'prefer-tari-tari': true, // cspell:disable-line
63
+ /**
64
+ * @see https://github.com/textlint-ja/textlint-rule-no-synonyms
65
+ */
66
+ '@textlint-ja/no-synonyms': true,
67
+ };
68
+ /**
69
+ * Mapping from short rule identifiers to their npm package names.
70
+ *
71
+ * Most textlint rules follow the convention `textlint-rule-{id}`, but some
72
+ * (especially scoped packages like `@textlint-ja/*`) deviate.
73
+ * This map provides explicit overrides so that `loadModule()` can
74
+ * dynamically import the correct package for each rule.
75
+ */
76
+ const ruleImportMap = {
77
+ 'no-nfd': 'textlint-rule-no-nfd',
78
+ 'max-ten': 'textlint-rule-max-ten',
79
+ 'spellcheck-tech-word': 'textlint-rule-spellcheck-tech-word',
80
+ 'web-plus-db': 'textlint-rule-web-plus-db',
81
+ 'no-mix-dearu-desumasu': 'textlint-rule-no-mix-dearu-desumasu',
82
+ 'no-doubled-joshi': 'textlint-rule-no-doubled-joshi',
83
+ 'no-double-negative-ja': 'textlint-rule-no-double-negative-ja',
84
+ 'no-hankaku-kana': 'textlint-rule-no-hankaku-kana',
85
+ 'ja-no-abusage': 'textlint-rule-ja-no-abusage',
86
+ 'no-mixed-zenkaku-and-hankaku-alphabet': 'textlint-rule-no-mixed-zenkaku-and-hankaku-alphabet',
87
+ 'no-dropping-the-ra': 'textlint-rule-no-dropping-the-ra',
88
+ 'no-doubled-conjunctive-particle-ga': 'textlint-rule-no-doubled-conjunctive-particle-ga',
89
+ 'no-doubled-conjunction': 'textlint-rule-no-doubled-conjunction',
90
+ 'ja-no-mixed-period': 'textlint-rule-ja-no-mixed-period',
91
+ 'max-appearence-count-of-words': 'textlint-rule-max-appearence-count-of-words',
92
+ 'ja-hiragana-keishikimeishi': 'textlint-rule-ja-hiragana-keishikimeishi',
93
+ 'ja-hiragana-fukushi': 'textlint-rule-ja-hiragana-fukushi',
94
+ 'ja-hiragana-hojodoushi': 'textlint-rule-ja-hiragana-hojodoushi',
95
+ 'ja-unnatural-alphabet': 'textlint-rule-ja-unnatural-alphabet',
96
+ '@textlint-ja/textlint-rule-no-insert-dropping-sa': '@textlint-ja/textlint-rule-no-insert-dropping-sa',
97
+ 'prefer-tari-tari': 'textlint-rule-prefer-tari-tari',
98
+ '@textlint-ja/no-synonyms': '@textlint-ja/textlint-rule-no-synonyms',
99
+ };
100
+ /**
101
+ * Dynamically imports a module and resolves CJS/ESM default-export interop.
102
+ *
103
+ * Many textlint rules are published as CommonJS modules. When imported
104
+ * via dynamic `import()` in an ESM context, Node wraps the CJS export
105
+ * in `{ default: ... }`. Some bundlers double-wrap this, producing
106
+ * `{ default: { default: actualExport } }`. The fallback chain
107
+ * `mod.default?.default ?? mod.default ?? mod` handles all three cases:
108
+ *
109
+ * 1. Double-wrapped CJS: `mod.default.default`
110
+ * 2. Single-wrapped CJS: `mod.default`
111
+ * 3. Native ESM: `mod` (no `.default` property)
112
+ * @param moduleName - The npm package name to import.
113
+ * @returns The resolved module export (typically a rule constructor).
114
+ */
115
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
116
+ async function loadModule(moduleName) {
117
+ const mod = await import(moduleName);
118
+ // moduleInterop equivalent: handle default export for CJS/ESM interop
119
+ return mod.default?.default ?? mod.default ?? mod;
120
+ }
121
+ /**
122
+ * Constructs a textlint `Linter` instance with the given rule set.
123
+ *
124
+ * Rules are loaded dynamically via `loadModule()` to support the mix of
125
+ * CJS and ESM packages in the textlint ecosystem. The HTML plugin is
126
+ * always registered so that raw HTML can be linted directly without
127
+ * first converting to Markdown.
128
+ * @param rules - Merged rule configuration (defaults + user overrides).
129
+ * @returns A configured textlint `Linter` ready for `lintText()` calls.
130
+ */
131
+ async function buildLinter(rules) {
132
+ const { TextlintKernelDescriptor } = await import('@textlint/kernel');
133
+ const ruleDescriptors = await Promise.all(Object.entries(rules)
134
+ .filter(([, value]) => value !== false)
135
+ .map(async ([ruleId, options]) => {
136
+ const moduleName = ruleImportMap[ruleId] ?? `textlint-rule-${ruleId}`;
137
+ const rule = await loadModule(moduleName);
138
+ return {
139
+ ruleId,
140
+ rule,
141
+ options: options === true ? {} : options,
142
+ };
143
+ }));
144
+ const htmlPlugin = await loadModule('textlint-plugin-html');
145
+ const descriptor = new TextlintKernelDescriptor({
146
+ rules: ruleDescriptors,
147
+ plugins: [
148
+ {
149
+ pluginId: 'html',
150
+ plugin: htmlPlugin,
151
+ },
152
+ ],
153
+ filterRules: [],
154
+ });
155
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
156
+ return createLinter({ descriptor: descriptor });
157
+ }
158
+ /**
159
+ * Analyze plugin that runs textlint Japanese text-proofreading rules
160
+ * against each page's HTML.
161
+ *
162
+ * The default rule set is heavily Japanese-oriented (mixed script detection,
163
+ * doubled particles, honorific misuse, etc.) because the primary use case
164
+ * is auditing Japanese corporate websites. Users can override or extend
165
+ * rules via the `rules` option.
166
+ *
167
+ * ## Lazy linter initialization
168
+ *
169
+ * Building the linter is expensive because it dynamically imports 20+
170
+ * rule packages (many of which are CJS and require interop resolution).
171
+ * The linter is therefore created lazily on the first `eachPage` call
172
+ * and the resulting promise is cached for all subsequent pages.
173
+ *
174
+ * This "lazy singleton" pattern (`linterPromise` variable) ensures:
175
+ * 1. Zero startup cost if textlint is configured but no pages match.
176
+ * 2. No duplicate initialization even under concurrent `eachPage` calls,
177
+ * because the same promise is shared (Promise deduplication).
178
+ * @example
179
+ * ```jsonc
180
+ * // nitpicker.config.json
181
+ * {
182
+ * "plugins": {
183
+ * "analyze": {
184
+ * "@nitpicker/analyze-textlint": {
185
+ * "rules": {
186
+ * "max-ten": { "max": 5 },
187
+ * "spellcheck-tech-word": false
188
+ * }
189
+ * }
190
+ * }
191
+ * }
192
+ * }
193
+ * ```
194
+ */
195
+ export default definePlugin((options) => {
196
+ const rules = { ...defaultRules, ...options.rules };
197
+ let linterPromise;
198
+ /**
199
+ * Returns a shared linter promise, creating it on first call.
200
+ * Subsequent calls return the same promise (lazy singleton pattern).
201
+ */
202
+ function getLinter() {
203
+ if (!linterPromise) {
204
+ linterPromise = buildLinter(rules);
205
+ }
206
+ return linterPromise;
207
+ }
208
+ return {
209
+ label: 'textlint: テキスト校正',
210
+ async eachPage({ html, url }) {
211
+ const linter = await getLinter();
212
+ const reports = [];
213
+ const result = await linter.lintText(html, url.pathname + '.html');
214
+ reports.push({
215
+ url: url.href,
216
+ results: result.messages,
217
+ });
218
+ const violations = reports.flatMap((report) => {
219
+ return report.results.map((r) => {
220
+ return {
221
+ validator: 'textlint',
222
+ severity: convertSeverity(r.severity),
223
+ rule: r.ruleId,
224
+ code: '-',
225
+ message: `${r.message}`,
226
+ url: `${report.url} (${r.line}:${r.column})`,
227
+ };
228
+ });
229
+ });
230
+ return {
231
+ violations,
232
+ };
233
+ },
234
+ };
235
+ });
236
+ /**
237
+ * Maps textlint's numeric severity levels to Nitpicker's string-based severity.
238
+ *
239
+ * textlint uses `1` for warning and `2` for error, following ESLint convention.
240
+ * Any unexpected value defaults to `"error"` for safety.
241
+ * @param severity - The textlint severity level (1 = warning, 2 = error).
242
+ * @returns The corresponding Nitpicker severity string.
243
+ */
244
+ function convertSeverity(severity) {
245
+ switch (severity) {
246
+ case 1: {
247
+ return 'warning';
248
+ }
249
+ case 2: {
250
+ return 'error';
251
+ }
252
+ default: {
253
+ return 'error';
254
+ }
255
+ }
256
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitpicker/analyze-textlint",
3
- "version": "0.4.4",
3
+ "version": "0.5.0",
4
4
  "description": "Nitpicker plugin for Japanese text proofreading using textlint",
5
5
  "author": "D-ZERO",
6
6
  "license": "Apache-2.0",
@@ -18,8 +18,8 @@
18
18
  "type": "module",
19
19
  "exports": {
20
20
  ".": {
21
- "import": "./lib/index.js",
22
- "types": "./lib/index.d.ts"
21
+ "import": "./lib/textlint-plugin.js",
22
+ "types": "./lib/textlint-plugin.d.ts"
23
23
  }
24
24
  },
25
25
  "scripts": {
@@ -27,8 +27,8 @@
27
27
  "clean": "tsc --build --clean"
28
28
  },
29
29
  "dependencies": {
30
- "@nitpicker/core": "0.4.4",
31
- "@nitpicker/types": "0.4.4",
30
+ "@nitpicker/core": "0.5.0",
31
+ "@nitpicker/types": "0.5.0",
32
32
  "@textlint-ja/textlint-rule-no-insert-dropping-sa": "2.0.1",
33
33
  "@textlint-ja/textlint-rule-no-synonyms": "1.3.0",
34
34
  "@textlint/kernel": "15.5.2",
@@ -64,5 +64,5 @@
64
64
  "@types/turndown": "5.0.6",
65
65
  "glob": "13.0.6"
66
66
  },
67
- "gitHead": "7c9bad351d1a3f4eb6ea7233f7143140d0989ef2"
67
+ "gitHead": "607d06bd596a0270d7088f32373d7367eb47ea94"
68
68
  }