chaffjs 0.26.0 → 0.27.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/dist/ai-score/human-shares.js +59 -59
- package/dist/ai-score/human-shares.js.map +1 -1
- package/dist/capitals-with-small.d.ts +8 -0
- package/dist/capitals-with-small.d.ts.map +1 -0
- package/dist/capitals-with-small.js +20 -0
- package/dist/capitals-with-small.js.map +1 -0
- package/dist/derived/relative-weekdays.d.ts +16 -0
- package/dist/derived/relative-weekdays.d.ts.map +1 -0
- package/dist/derived/relative-weekdays.js +52 -0
- package/dist/derived/relative-weekdays.js.map +1 -0
- package/dist/detectors/change-rate.d.ts +3 -0
- package/dist/detectors/change-rate.d.ts.map +1 -0
- package/dist/detectors/change-rate.js +102 -0
- package/dist/detectors/change-rate.js.map +1 -0
- package/dist/detectors/derived-numbers.d.ts.map +1 -1
- package/dist/detectors/derived-numbers.js +3 -7
- package/dist/detectors/derived-numbers.js.map +1 -1
- package/dist/detectors/document-date.d.ts +4 -0
- package/dist/detectors/document-date.d.ts.map +1 -0
- package/dist/detectors/document-date.js +12 -0
- package/dist/detectors/document-date.js.map +1 -0
- package/dist/detectors/orthographic-variant.d.ts.map +1 -1
- package/dist/detectors/orthographic-variant.js +16 -11
- package/dist/detectors/orthographic-variant.js.map +1 -1
- package/dist/detectors/registry/change-rate.d.ts +3 -0
- package/dist/detectors/registry/change-rate.d.ts.map +1 -0
- package/dist/detectors/registry/change-rate.js +3 -0
- package/dist/detectors/registry/change-rate.js.map +1 -0
- package/dist/detectors/registry/version-mismatch.d.ts +3 -0
- package/dist/detectors/registry/version-mismatch.d.ts.map +1 -0
- package/dist/detectors/registry/version-mismatch.js +3 -0
- package/dist/detectors/registry/version-mismatch.js.map +1 -0
- package/dist/detectors/relative-dates.d.ts.map +1 -1
- package/dist/detectors/relative-dates.js +3 -1
- package/dist/detectors/relative-dates.js.map +1 -1
- package/dist/detectors/relative-day-words.d.ts +4 -0
- package/dist/detectors/relative-day-words.d.ts.map +1 -0
- package/dist/detectors/relative-day-words.js +105 -0
- package/dist/detectors/relative-day-words.js.map +1 -0
- package/dist/detectors/structure-tree.d.ts +4 -1
- package/dist/detectors/structure-tree.d.ts.map +1 -1
- package/dist/detectors/structure-tree.js +65 -3
- package/dist/detectors/structure-tree.js.map +1 -1
- package/dist/detectors/version-mismatch.d.ts +3 -0
- package/dist/detectors/version-mismatch.d.ts.map +1 -0
- package/dist/detectors/version-mismatch.js +19 -0
- package/dist/detectors/version-mismatch.js.map +1 -0
- package/dist/structure/change-rate.d.ts +33 -0
- package/dist/structure/change-rate.d.ts.map +1 -0
- package/dist/structure/change-rate.js +71 -0
- package/dist/structure/change-rate.js.map +1 -0
- package/dist/structure/list-numbering.d.ts +17 -0
- package/dist/structure/list-numbering.d.ts.map +1 -0
- package/dist/structure/list-numbering.js +27 -0
- package/dist/structure/list-numbering.js.map +1 -0
- package/dist/structure/percent-sum-prose.d.ts +7 -0
- package/dist/structure/percent-sum-prose.d.ts.map +1 -0
- package/dist/structure/percent-sum-prose.js +44 -0
- package/dist/structure/percent-sum-prose.js.map +1 -0
- package/dist/structure/version-mismatch.d.ts +23 -0
- package/dist/structure/version-mismatch.d.ts.map +1 -0
- package/dist/structure/version-mismatch.js +74 -0
- package/dist/structure/version-mismatch.js.map +1 -0
- package/dist/structure/weekday.d.ts +21 -2
- package/dist/structure/weekday.d.ts.map +1 -1
- package/dist/structure/weekday.js +70 -8
- package/dist/structure/weekday.js.map +1 -1
- package/package.json +3 -3
- package/rules/agreement-slip.yaml +2 -2
- package/rules/change-rate-mismatch.yaml +44 -0
- package/rules/date-weekday-mismatch.yaml +4 -4
- package/rules/max-sentence-length.yaml +1 -0
- package/rules/numbering-gap.yaml +4 -4
- package/rules/one-sentence-paragraph-run.yaml +1 -0
- package/rules/orthographic-variant.yaml +3 -3
- package/rules/percent-sum-mismatch.yaml +4 -4
- package/rules/relative-date-mismatch.yaml +4 -4
- package/rules/undefined-acronym.yaml +1 -0
- package/rules/version-mismatch.yaml +77 -0
- package/src/ai-score/human-shares.ts +59 -59
- package/src/capitals-with-small.ts +23 -0
- package/src/derived/relative-weekdays.ts +65 -0
- package/src/detectors/change-rate.ts +122 -0
- package/src/detectors/derived-numbers.ts +3 -11
- package/src/detectors/document-date.ts +18 -0
- package/src/detectors/orthographic-variant.ts +8 -4
- package/src/detectors/registry/change-rate.ts +4 -0
- package/src/detectors/registry/version-mismatch.ts +4 -0
- package/src/detectors/relative-dates.ts +3 -1
- package/src/detectors/relative-day-words.ts +126 -0
- package/src/detectors/structure-tree.ts +76 -4
- package/src/detectors/version-mismatch.ts +23 -0
- package/src/structure/change-rate.ts +103 -0
- package/src/structure/list-numbering.ts +34 -0
- package/src/structure/percent-sum-prose.ts +54 -0
- package/src/structure/version-mismatch.ts +103 -0
- package/src/structure/weekday.ts +83 -6
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// A breakdown written in one sentence (「構成比は、Aが50%、Bが30%、Cが20%」, "By product, A made up 50% of sales, B 30% and
|
|
2
|
+
// C 20%") whose shares do not add up to 100%. The table and list form is structure/percent-sum.ts; the words are the same.
|
|
3
|
+
import type { Span } from "../plugin.ts";
|
|
4
|
+
import type { StructureIssue } from "./issues.ts";
|
|
5
|
+
import type { Amount } from "./total.ts";
|
|
6
|
+
import type { ShareWords } from "./percent-sum.ts";
|
|
7
|
+
|
|
8
|
+
const WHOLE = 100;
|
|
9
|
+
/** A sentence names two shares in passing (「AとBで50%」); a breakdown in prose names at least three parts. */
|
|
10
|
+
const MIN_PARTS = 3;
|
|
11
|
+
/**
|
|
12
|
+
* How far from 100% a sentence may add up and still read as one whole broken down. Far below it, the sentence names only the
|
|
13
|
+
* larger parts (「Aが50%、Bが20%、Cが10%など」); far above it, the percentages are of different wholes ("55% of directors,
|
|
14
|
+
* 48% of managers") or of two periods ("45% (40% last year)"), or the total is among them (「合計100%」).
|
|
15
|
+
*/
|
|
16
|
+
const BAND = 10;
|
|
17
|
+
const HALF = 0.5;
|
|
18
|
+
const DECIMAL_BASE = 10;
|
|
19
|
+
const DECIMALS = /\.(\d+)/u;
|
|
20
|
+
/** A sign before the number (+3%, ▲2%): a change, not a part. */
|
|
21
|
+
const SIGNS = "-+−▲△";
|
|
22
|
+
|
|
23
|
+
const includesAny = (text: string, words: readonly string[]): boolean => {
|
|
24
|
+
const lowered = text.toLowerCase();
|
|
25
|
+
return words.some((word) => lowered.includes(word.toLowerCase()));
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const decimalsOf = (source: string, amount: Amount): number => DECIMALS.exec(source.slice(amount.offset, amount.end))?.[1]?.length ?? 0;
|
|
29
|
+
|
|
30
|
+
const isSigned = (source: string, amount: Amount): boolean => SIGNS.includes(source.slice(0, amount.offset).trimEnd().at(-1) ?? " ");
|
|
31
|
+
|
|
32
|
+
/** The rounding the parts allow: half the last digit each, at least one point. */
|
|
33
|
+
const toleranceOf = (source: string, shares: readonly Amount[]): number =>
|
|
34
|
+
Math.max(1, shares.length * HALF * DECIMAL_BASE ** -Math.max(0, ...shares.map((share) => decimalsOf(source, share))));
|
|
35
|
+
|
|
36
|
+
const isBroken = (sum: number, tolerance: number): boolean => Math.abs(sum - WHOLE) > tolerance && Math.abs(sum - WHOLE) <= BAND;
|
|
37
|
+
|
|
38
|
+
const issueIn = (source: string, sentence: Span, percents: readonly Amount[], words: ShareWords): StructureIssue[] => {
|
|
39
|
+
const text = source.slice(sentence.start, sentence.end);
|
|
40
|
+
if (!includesAny(text, words.labels) || includesAny(text, words.exceptions)) return [];
|
|
41
|
+
const shares = percents.filter((amount) => amount.offset >= sentence.start && amount.end <= sentence.end);
|
|
42
|
+
if (shares.length < MIN_PARTS || shares.some((share) => isSigned(source, share))) return [];
|
|
43
|
+
const sum = shares.reduce((total, share) => total + share.value, 0);
|
|
44
|
+
const decimals = Math.max(0, ...shares.map((share) => decimalsOf(source, share)));
|
|
45
|
+
if (!isBroken(sum, toleranceOf(source, shares))) return [];
|
|
46
|
+
return [{ offset: shares[0]?.offset ?? sentence.start, values: { sum: `${sum.toFixed(decimals)}${shares[0]?.unit ?? ""}` } }];
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** Each sentence that names a share word and three or more unsigned percentages whose sum misses 100% by more than rounding, and by no more than BAND. */
|
|
50
|
+
export const proseShareMismatches = (source: string, sentences: readonly Span[], amounts: readonly Amount[], words: ShareWords): StructureIssue[] => {
|
|
51
|
+
if (words.labels.length === 0) return [];
|
|
52
|
+
const percents = amounts.filter((amount) => words.units.includes(amount.unit));
|
|
53
|
+
return sentences.flatMap((sentence) => issueIn(source, sentence, percents, words));
|
|
54
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// A version written in the lead-in to a code block ("Install version 2.3.0 from npm:") that the block itself does not
|
|
2
|
+
// show (npm install tidyq@2.4.0). Pure: reads the source, the code blocks, and the prose with code covered.
|
|
3
|
+
import type { Span } from "../plugin.ts";
|
|
4
|
+
import type { StructureIssue } from "./issues.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A dotted version of three parts (2.4.0, v1.12.3, 3.0.0-beta.2). Two parts (22.04, 3.1) are too often a decimal, and a
|
|
8
|
+
* first part of four digits is too often a date (2024.01.15).
|
|
9
|
+
*/
|
|
10
|
+
const VERSION = /(?<![\w.])v?(\d{1,3}\.\d{1,4}\.\d{1,4}(?:-[0-9A-Za-z.]+)?)(?![\w.]|\.\d)/gu;
|
|
11
|
+
|
|
12
|
+
/** A word that makes a version a bound or a starting point, not the version installed: 「2.3.0 から」 "from 2.3.0", "20.0.0 or later". */
|
|
13
|
+
export type RangeWord = { readonly word: string; readonly position: "before" | "after" };
|
|
14
|
+
|
|
15
|
+
/** What tells which versions are the block's: the bound words, and the Latin words that lead to the version installed. */
|
|
16
|
+
export type VersionWords = { readonly ranges: readonly RangeWord[]; readonly versionWords: readonly string[] };
|
|
17
|
+
|
|
18
|
+
/** One code block: where it starts and the text between its fences. */
|
|
19
|
+
export type CodeBlock = { readonly start: number; readonly code: string };
|
|
20
|
+
|
|
21
|
+
/** How far a range word may stand from the version (a space, a "v"). */
|
|
22
|
+
const RANGE_GAP = 2;
|
|
23
|
+
|
|
24
|
+
type Found = { readonly start: number; readonly end: number; readonly version: string };
|
|
25
|
+
|
|
26
|
+
const versionsIn = (text: string, offset = 0): Found[] =>
|
|
27
|
+
[...text.matchAll(VERSION)].map((match) => ({ start: offset + match.index, end: offset + match.index + match[0].length, version: match[1] ?? "" }));
|
|
28
|
+
|
|
29
|
+
const isBound = (prose: string, found: Span, words: readonly RangeWord[]): boolean =>
|
|
30
|
+
words.some(({ word, position }) => {
|
|
31
|
+
if (word === "") return false;
|
|
32
|
+
const near =
|
|
33
|
+
position === "after"
|
|
34
|
+
? prose.slice(found.end, found.end + RANGE_GAP + word.length)
|
|
35
|
+
: prose.slice(Math.max(0, found.start - RANGE_GAP - word.length), found.start);
|
|
36
|
+
return near.toLowerCase().includes(word.toLowerCase());
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
/** The names a block pins its version to: tidyq in tidyq@2.4.0, tidyq==2.4.0, tidyq=2.4.0, tidyq:2.4.0, "tidyq": "2.4.0". */
|
|
40
|
+
const PINNED = /^([\w.-]+)(?:@|==|=|:)[v^~]?\d/u;
|
|
41
|
+
const QUOTES = /["']/gu;
|
|
42
|
+
const OPENS_WITH_NUMBER = /^[v^~]?\d/u;
|
|
43
|
+
|
|
44
|
+
/** Each whitespace-separated word of the code, quotes dropped; "tidyq": "2.4.0" is read as tidyq:2.4.0. */
|
|
45
|
+
const codeWords = (code: string): string[] => {
|
|
46
|
+
const words = code.split(/\s+/u).map((word) => word.replaceAll(QUOTES, "").replace(/,$/u, ""));
|
|
47
|
+
return words.map((word, index) => (word.endsWith(":") && OPENS_WITH_NUMBER.test(words[index + 1] ?? "") ? `${word}${words[index + 1] ?? ""}` : word));
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const pinnedNames = (code: string): Set<string> => new Set(codeWords(code).flatMap((word) => PINNED.exec(word)?.[1]?.toLowerCase() ?? []));
|
|
51
|
+
|
|
52
|
+
const LATIN = /^[A-Za-z]/u;
|
|
53
|
+
const WORD_CHAR = /[\w.+-]/u;
|
|
54
|
+
/** How far back the word before a version is looked for. */
|
|
55
|
+
const WORD_REACH = 40;
|
|
56
|
+
|
|
57
|
+
/** The Latin word right before a position, if the word there is Latin: "Node.js" in "Requires Node.js 18.17.0". */
|
|
58
|
+
const latinWordBefore = (text: string): string | undefined => {
|
|
59
|
+
const last = [...(text.trimEnd().split(/\s/u).at(-1) ?? "")];
|
|
60
|
+
const word = last.slice(0, last.findLastIndex((char) => WORD_CHAR.test(char)) + 1).join("");
|
|
61
|
+
return LATIN.test(word) ? word.toLowerCase() : undefined;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Whether a lead-in version is about the block's software. A Latin word right before it must be one of the words that lead
|
|
66
|
+
* to the version installed (version, release, install; lexicon version-lead) or the pinned software: "Requires Node.js
|
|
67
|
+
* 18.17.0" over `npm install pkg@2.4.0` names another program. A version after no Latin word (「npm から 2.3.0」) is the block's.
|
|
68
|
+
*/
|
|
69
|
+
const isAboutBlock = (prose: string, found: Found, names: ReadonlySet<string>, versionWords: readonly string[]): boolean => {
|
|
70
|
+
const word = latinWordBefore(prose.slice(Math.max(0, found.start - WORD_REACH), found.start));
|
|
71
|
+
return word === undefined || names.has(word) || versionWords.some((name) => name.toLowerCase() === word);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const BLANK_LINE = /\r?\n[ \t]*\r?\n/gu;
|
|
75
|
+
const NOT_PROSE = /^\s*(?:#|[-*+]\s|\d{1,3}[.)]\s|```|~~~|\|)/u;
|
|
76
|
+
const LETTER = /\p{L}/u;
|
|
77
|
+
|
|
78
|
+
/** The paragraph right above a code block: the lines up to the blank line before it. A heading, a list item, a table or a bare version above is not a lead-in. */
|
|
79
|
+
const leadInOf = (source: string, blockStart: number): Span | undefined => {
|
|
80
|
+
const lines = source.slice(0, blockStart).trimEnd();
|
|
81
|
+
const last = [...lines.matchAll(BLANK_LINE)].at(-1);
|
|
82
|
+
const start = last === undefined ? 0 : last.index + last[0].length;
|
|
83
|
+
const text = lines.slice(start);
|
|
84
|
+
const words = text.replace(VERSION, "");
|
|
85
|
+
return text === "" || NOT_PROSE.test(text) || !LETTER.test(words) ? undefined : { start, end: lines.length };
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Each version in a code block's lead-in that differs from the one version the block shows. A block with no version, or
|
|
90
|
+
* with two (an upgrade from one to the other), says nothing to compare; a version marked as a bound, or named after another
|
|
91
|
+
* program, is not the one installed.
|
|
92
|
+
*/
|
|
93
|
+
export const versionMismatches = (source: string, prose: string, blocks: readonly CodeBlock[], words: VersionWords): StructureIssue[] =>
|
|
94
|
+
blocks.flatMap((block) => {
|
|
95
|
+
const shown = [...new Set(versionsIn(block.code).map((found) => found.version))];
|
|
96
|
+
const leadIn = leadInOf(source, block.start);
|
|
97
|
+
const [code] = shown;
|
|
98
|
+
if (shown.length !== 1 || code === undefined || leadIn === undefined) return [];
|
|
99
|
+
const names = pinnedNames(block.code);
|
|
100
|
+
return versionsIn(prose.slice(leadIn.start, leadIn.end), leadIn.start)
|
|
101
|
+
.filter((found) => found.version !== code && !isBound(prose, found, words.ranges) && isAboutBlock(prose, found, names, words.versionWords))
|
|
102
|
+
.map((found) => ({ offset: found.start, values: { written: found.version, code } }));
|
|
103
|
+
});
|
package/src/structure/weekday.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { inDocumentOrder } from "./issues.ts";
|
|
|
5
5
|
// 日付と、その横に書いた曜日の食い違い。暦で決まるので、両方読めれば誤りは機械で言える。
|
|
6
6
|
|
|
7
7
|
const FULL_DATE = /^(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})$/u;
|
|
8
|
+
const MONTH_DAY = /^(?<m>\d{2})-(?<d>\d{2})$/u;
|
|
8
9
|
|
|
9
10
|
/** 暦の曜日。日曜日が 0。存在しない日(2月30日)は undefined。 */
|
|
10
11
|
export const weekdayOf = (value: string): number | undefined => {
|
|
@@ -15,11 +16,87 @@ export const weekdayOf = (value: string): number | undefined => {
|
|
|
15
16
|
return date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day ? date.getUTCDay() : undefined;
|
|
16
17
|
};
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
const DAY_MS = 86_400_000;
|
|
20
|
+
/** 年を書いた日付から 5 か月以内なら、前後の年のどれか一つだけがそこに収まる。半年を超えると、どちらの年とも読める。 */
|
|
21
|
+
const YEAR_REACH_DAYS = 153;
|
|
22
|
+
|
|
23
|
+
const timeOf = (value: string): number | undefined => {
|
|
24
|
+
const groups = FULL_DATE.exec(value)?.groups;
|
|
25
|
+
return groups === undefined || weekdayOf(value) === undefined ? undefined : Date.UTC(Number(groups["y"]), Number(groups["m"]) - 1, Number(groups["d"]));
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 年の無い月日(「10-09」)に、年を書いた日付(anchor、「2026-10-06」)の前後の年のうち、anchor から 5 か月以内に収まる年を付ける。
|
|
30
|
+
* メールや議事録は、文書の日付の年を書いて、そのあとの日付の年を省く。12月の文書の「1月5日」は翌年。どの年も収まらなければ undefined。
|
|
31
|
+
*/
|
|
32
|
+
export const withNearestYear = (monthDay: string, anchor: string): string | undefined => {
|
|
33
|
+
const groups = MONTH_DAY.exec(monthDay)?.groups;
|
|
34
|
+
const anchorYear = FULL_DATE.exec(anchor)?.groups?.["y"];
|
|
35
|
+
const anchorTime = timeOf(anchor);
|
|
36
|
+
if (groups === undefined || anchorYear === undefined || anchorTime === undefined) return undefined;
|
|
37
|
+
return [-1, 0, 1]
|
|
38
|
+
.map((offset) => `${String(Number(anchorYear) + offset)}-${groups["m"] ?? ""}-${groups["d"] ?? ""}`)
|
|
39
|
+
.find((candidate) => {
|
|
40
|
+
const time = timeOf(candidate);
|
|
41
|
+
return time !== undefined && Math.abs(time - anchorTime) <= YEAR_REACH_DAYS * DAY_MS;
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 年の無い日付の年を決める手がかり。stamp は文書の日付(「更新日:」の段落など)、sectionStarts は見出しで分けた節の始まり、
|
|
47
|
+
* yearHeadings はそのうち見出しが年を名指す節(「## 2024年度」)の始まり。anchorable は、年を決めるのに使ってよい日付か
|
|
48
|
+
* (引用符で日付だけを挙げた例は使わない)。
|
|
49
|
+
*/
|
|
50
|
+
export type YearContext = {
|
|
51
|
+
readonly stamp?: string | undefined;
|
|
52
|
+
readonly sectionStarts: readonly number[];
|
|
53
|
+
readonly yearHeadings?: readonly number[];
|
|
54
|
+
readonly anchorable?: (node: StructureNode) => boolean;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const sectionOf = (starts: readonly number[], offset: number): number => starts.filter((start) => start <= offset).length;
|
|
58
|
+
|
|
59
|
+
/** 文書の書き出し: 最初の見出しより前と、最初の見出しの節。メールや議事録の頭の日付(日付:、日時:)が書かれる所。 */
|
|
60
|
+
const OPENING_SECTIONS = 1;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 年の無い日付の年を決める、年を書いた日付。同じ節のすぐ前のもの(前に無ければ後ろの最初のもの)。節に無ければ文書の日付、
|
|
64
|
+
* それも無ければ書き出しの最初の日付。年度ごとの見出しの下の「1月24日(火)」はその節の年で読み、別の節の日付の年では読まない。
|
|
65
|
+
*/
|
|
66
|
+
const anchorOf = (dated: readonly StructureNode[], node: StructureNode, context: YearContext): string | undefined => {
|
|
67
|
+
const section = sectionOf(context.sectionStarts, node.span.start);
|
|
68
|
+
const inSection = dated.filter((other) => sectionOf(context.sectionStarts, other.span.start) === section);
|
|
69
|
+
const nearest = inSection.findLast((other) => other.span.start < node.span.start) ?? inSection[0];
|
|
70
|
+
// 見出しが年を名指す節(「## 2024年度」)の日付は、その年の中にある。節の外の日付からは決めない。
|
|
71
|
+
const sectionStart = context.sectionStarts[section - 1];
|
|
72
|
+
if (nearest === undefined && sectionStart !== undefined && (context.yearHeadings ?? []).includes(sectionStart)) return undefined;
|
|
73
|
+
const opening = dated.find((other) => sectionOf(context.sectionStarts, other.span.start) <= OPENING_SECTIONS);
|
|
74
|
+
const stamp = context.stamp !== undefined && FULL_DATE.test(context.stamp) ? context.stamp : undefined;
|
|
75
|
+
const fromTree = (found: StructureNode | undefined): string | undefined => (found === undefined ? undefined : String(found.attrs["value"]));
|
|
76
|
+
return fromTree(nearest) ?? stamp ?? fromTree(opening);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** 日付の年月日。年の無い日付は、近くの年を書いた日付から年を決めて付ける。決まらなければ undefined。 */
|
|
80
|
+
const fullValueOf = (dated: readonly StructureNode[], node: StructureNode, context: YearContext): string | undefined => {
|
|
81
|
+
const value = String(node.attrs["value"]);
|
|
82
|
+
if (!MONTH_DAY.test(value)) return value;
|
|
83
|
+
const anchor = anchorOf(dated, node, context);
|
|
84
|
+
return anchor === undefined ? undefined : withNearestYear(value, anchor);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 書いた曜日が暦と違う日付。年の無い日付は、年を書いた日付(anchorOf)から 5 か月以内に収まる年として比べる。
|
|
89
|
+
* 年を書いた日付が文書に無いか、どの年も収まらなければ、曜日を決められないので見ない。
|
|
90
|
+
*/
|
|
91
|
+
export const weekdayMismatches = (tree: StructureNode, context: YearContext = { sectionStarts: [] }): StructureIssue[] => {
|
|
92
|
+
const dates = inDocumentOrder(tree).filter((node) => node.kind === "date");
|
|
93
|
+
const anchorable = context.anchorable ?? (() => true);
|
|
94
|
+
const dated = dates.filter((node) => timeOf(String(node.attrs["value"])) !== undefined && anchorable(node));
|
|
95
|
+
return dates.flatMap((node) => {
|
|
21
96
|
const written = node.attrs["weekday"];
|
|
22
|
-
if (
|
|
23
|
-
const
|
|
24
|
-
|
|
97
|
+
if (typeof written !== "number") return [];
|
|
98
|
+
const value = fullValueOf(dated, node, context);
|
|
99
|
+
const actual = value === undefined ? undefined : weekdayOf(value);
|
|
100
|
+
return value === undefined || actual === undefined || actual === written ? [] : [{ offset: node.span.start, values: { date: value, written, actual } }];
|
|
25
101
|
});
|
|
102
|
+
};
|