-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathclauseNums.ts
More file actions
155 lines (144 loc) · 4.89 KB
/
Copy pathclauseNums.ts
File metadata and controls
155 lines (144 loc) · 4.89 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
import type Clause from './Clause';
import type { Spec } from './ecmarkup';
export interface ClauseNumberIterator {
next(clauseStack: Clause[], node: HTMLElement): string;
}
export default function iterator(spec: Spec): ClauseNumberIterator {
const ids: (string | number[])[] = [];
let inAnnex = false;
let currentLevel = 0;
let hasWarnedForExcessNesting = false;
const MAX_LEVELS = spec.opts.maxClauseDepth ?? Infinity;
return {
next(clauseStack: Clause[], node: HTMLElement) {
const annex = node.nodeName === 'EMU-ANNEX';
const level = clauseStack.length;
if (inAnnex && !annex) {
spec.warn({
type: 'node',
node,
ruleId: 'clause-after-annex',
message: 'clauses cannot follow annexes',
});
}
if (level - currentLevel > 1 && (level < MAX_LEVELS || currentLevel < MAX_LEVELS - 1)) {
spec.warn({
type: 'node',
node,
ruleId: 'skipped-clause',
message: 'clause is being numbered without numbering its parent clause',
});
}
if (!hasWarnedForExcessNesting && level + 1 > (spec.opts.maxClauseDepth ?? Infinity)) {
spec.warn({
type: 'node',
node,
ruleId: 'max-clause-depth',
message: `clause exceeds maximum nesting depth of ${spec.opts.maxClauseDepth}`,
});
hasWarnedForExcessNesting = true;
}
const nextNum = annex ? nextAnnexNum : nextClauseNum;
if (level >= MAX_LEVELS) {
if (ids.length === MAX_LEVELS) {
const lastLevelIndex = MAX_LEVELS - 1;
const lastLevel = ids[lastLevelIndex] as number[];
lastLevel[lastLevel.length - 1]++;
} else {
while (ids.length < MAX_LEVELS) {
ids.push([1]);
}
}
} else {
if (level === currentLevel) {
ids[currentLevel] = nextNum(clauseStack, node);
} else if (level > currentLevel) {
ids.push(nextNum(clauseStack, node));
} else {
ids.length = level + 1;
ids[level] = nextNum(clauseStack, node);
}
}
currentLevel = Math.min(level, MAX_LEVELS - 1);
return ids.flat().join('.');
},
};
function nextAnnexNum(clauseStack: Clause[], node: HTMLElement): string | number[] {
const level = clauseStack.length;
if (level === 0 && node.hasAttribute('number')) {
spec.warn({
type: 'attr',
node,
attr: 'number',
ruleId: 'annex-clause-number',
message:
'top-level annexes do not support explicit numbers; if you need this, open a bug on ecmarkup',
});
}
if (!inAnnex) {
if (level > 0) {
spec.warn({
type: 'node',
node,
ruleId: 'annex-depth',
message: 'first annex must be at depth 0',
});
}
inAnnex = true;
return 'A';
}
if (level === 0) {
return String.fromCharCode((ids[0] as string).charCodeAt(0) + 1);
}
return nextClauseNum(clauseStack, node);
}
function nextClauseNum({ length: level }: { length: number }, node: HTMLElement) {
if (node.hasAttribute('number')) {
const nums = node
.getAttribute('number')!
.split('.')
.map(n => Number(n));
if (nums.length === 0 || nums.some(num => !Number.isSafeInteger(num) || num <= 0)) {
spec.warn({
type: 'attr-value',
node,
attr: 'number',
ruleId: 'invalid-clause-number',
message: 'clause numbers must be positive integers or dotted lists of positive integers',
});
}
if (ids[level] !== undefined) {
if (nums.length !== ids[level].length) {
spec.warn({
type: 'attr-value',
node,
attr: 'number',
ruleId: 'invalid-clause-number',
message:
'multi-step explicit clause numbers should not be mixed with single-step clause numbers in the same parent clause',
});
} else {
// Make sure that `nums` is strictly greater than `ids[level]` (i.e.,
// that their items are not identical and that the item in `nums` is
// strictly greater than the value in `ids[level]` at the first
// index where they differ).
const i = nums.findIndex((num, i) => num !== ids[level][i]);
if (i < 0 || !(nums[i] > (ids[level] as number[])[i])) {
spec.warn({
type: 'attr-value',
node,
attr: 'number',
ruleId: 'invalid-clause-number',
message: 'clause numbers should be strictly increasing',
});
}
}
}
return nums;
}
if (ids[level] === undefined) return [1];
const head = (ids[level] as number[]).slice(0, -1);
const tail = (ids[level] as number[])[ids[level].length - 1];
return [...head, tail + 1];
}
}