-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathutils.ts
274 lines (237 loc) · 7.4 KB
/
utils.ts
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
import type { ElementLocation } from 'parse5';
import type Spec from './Spec';
import * as jsdom from 'jsdom';
import * as chalk from 'chalk';
import * as emd from 'ecmarkdown';
import * as fs from 'fs';
import * as path from 'path';
import { collectNonterminalsFromEmd } from './lint/utils';
export function warnEmdFailure(
report: Spec['warn'],
node: Element | Text,
e: SyntaxError & { line?: number; column?: number }
) {
if (typeof e.line === 'number' && typeof e.column === 'number') {
report({
type: 'contents',
ruleId: 'invalid-emd',
message: `ecmarkdown failed to parse: ${e.message}`,
node,
nodeRelativeLine: e.line,
nodeRelativeColumn: e.column,
});
} else {
report({
type: 'node',
ruleId: 'invalid-emd',
message: `ecmarkdown failed to parse: ${e.message}`,
node,
});
}
}
export function wrapEmdFailure(src: string) {
return `#### ECMARKDOWN PARSE FAILED ####<pre>${src}</pre>`;
}
/*@internal*/
export function emdTextNode(spec: Spec, node: Text, namespace: string) {
const loc = spec.locate(node);
let c;
if (loc?.endTag == null) {
c = node.textContent!.replace(/</g, '<');
} else {
const start = loc.startTag.endOffset;
const end = loc.endTag.startOffset;
c = loc.source.slice(start, end);
}
let processed;
try {
const parts = emd.parseFragment(c);
if (spec.opts.lintSpec && loc != null) {
const nonterminals = collectNonterminalsFromEmd(parts).map(({ name, loc }) => ({
name,
loc,
node,
namespace,
}));
spec._ntStringRefs = spec._ntStringRefs.concat(nonterminals);
}
processed = emd.emit(parts);
} catch (e: any) {
warnEmdFailure(spec.warn, node, e);
processed = wrapEmdFailure(c);
}
const template = spec.doc.createElement('template');
template.innerHTML = processed;
replaceTextNode(node, template.content);
}
/*@internal*/
export function htmlToDom(html: string) {
return new jsdom.JSDOM(html, { includeNodeLocations: true });
}
/*@internal*/
export function domWalkBackward(root: Node, cb: (node: Element) => boolean | undefined) {
const childNodes = root.childNodes;
const childLen = childNodes.length;
for (let i = childLen - 1; i >= 0; i--) {
const node = childNodes[i];
if (node.nodeType !== 1) continue;
const cont = cb(node as Element);
if (cont === false) continue;
domWalkBackward(node, cb);
}
}
/*@internal*/
export function replaceTextNode(node: Node, frag: DocumentFragment) {
// Append all the nodes
const parent = node.parentNode;
if (!parent) return [];
const newXrefNodes = Array.from(frag.querySelectorAll('EMU-XREF'));
const first = frag.childNodes[0];
if (first.nodeType === 3) {
node.textContent = first.textContent;
frag.removeChild(first);
} else {
// set it to empty because we don't want to break iteration
// (I think it should work to delete it... investigate possible jsdom bug)
node.textContent = '';
}
parent.insertBefore(frag, node.nextSibling);
return newXrefNodes;
}
/*@internal*/
export function logVerbose(str: string) {
const dateString = new Date().toISOString();
console.error(chalk.gray('[' + dateString + '] ') + str);
}
/*@internal*/
export function logWarning(str: string) {
const dateString = new Date().toISOString();
console.error(chalk.gray('[' + dateString + '] ') + chalk.red(str));
}
/*@internal*/
export function shouldInline(node: Node) {
let parent = node.parentNode;
if (!parent) return false;
while (
parent &&
parent.parentNode &&
(parent.nodeName === 'EMU-GRAMMAR' ||
parent.nodeName === 'EMU-IMPORT' ||
parent.nodeName === 'INS' ||
parent.nodeName === 'DEL')
) {
parent = parent.parentNode;
}
return (
['EMU-ANNEX', 'EMU-CLAUSE', 'EMU-INTRO', 'EMU-NOTE', 'BODY'].indexOf(parent.nodeName) === -1
);
}
/*@internal*/
export function readFile(file: string) {
return new Promise<string>((resolve, reject) => {
fs.readFile(file, 'utf8', (err, data) => (err ? reject(err) : resolve(data)));
});
}
/*@internal*/
export function writeFile(file: string, content: string) {
return new Promise<void>((resolve, reject) => {
// we could do this async, but it's not worth worrying about
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFile(file, content, { encoding: 'utf8' }, err => (err ? reject(err) : resolve()));
});
}
/*@internal*/
export async function copyFile(src: string, dest: string) {
const content = await readFile(src);
await writeFile(dest, content);
}
export function offsetToLineAndColumn(string: string, offset: number) {
const lines = string.split('\n');
let line = 0;
let seen = 0;
while (true) {
if (line >= lines.length) {
throw new Error(`offset ${offset} exceeded string ${JSON.stringify(string)}`);
}
if (seen + lines[line].length >= offset) {
break;
}
seen += lines[line].length + 1; // +1 for the '\n'
++line;
}
const column = offset - seen;
return { line: line + 1, column: column + 1 };
}
export function attrLocation(source: string | undefined, loc: ElementLocation, attr: string) {
const attrLoc = loc.startTag.attrs?.[attr];
if (attrLoc == null) {
return { line: loc.startTag.startLine, column: loc.startTag.startCol };
} else {
return { line: attrLoc.startLine, column: attrLoc.startCol };
}
}
export function attrValueLocation(source: string | undefined, loc: ElementLocation, attr: string) {
const attrLoc = loc.startTag.attrs?.[attr];
if (attrLoc == null || source == null) {
return { line: loc.startTag.startLine, column: loc.startTag.startCol };
} else {
const tagText = source.slice(attrLoc.startOffset, attrLoc.endOffset);
// RegExp.escape when
const matcher = new RegExp(attr.replace(/[/\\^$*+?.()|[\]{}]/g, '\\$&') + '="?', 'i');
return {
line: attrLoc.startLine,
column: attrLoc.startCol + (tagText.match(matcher)?.[0].length ?? 0),
};
}
}
const KNOWN_EFFECTS = ['user-code'];
export function validateEffects(spec: Spec, effectsRaw: string[], node: Element) {
const effects = [];
const unknownEffects = [];
for (const e of effectsRaw) {
if (KNOWN_EFFECTS.indexOf(e) !== -1) {
effects.push(e);
} else {
unknownEffects.push(e);
}
}
if (unknownEffects.length !== 0) {
spec.warn({
type: 'node',
ruleId: 'unknown-effects',
message: `unknown effects: ${unknownEffects}`,
node,
});
}
return effects;
}
export function doesEffectPropagateToParent(node: Element, effect: string) {
// Effects should not propagate past explicit fences in parent steps.
//
// Abstract Closures are considered automatic fences for the user-code
// effect, since those are effectively nested functions.
//
// Calls to Abstract Closures that can call user code must be explicitly
// marked as such with <emu-meta effects="user-code">...</emu-meta>.
for (; node.parentElement; node = node.parentElement) {
const parent = node.parentElement;
// This is super hacky. It's checking the output of ecmarkdown.
if (parent.tagName !== 'LI') continue;
if (
effect === 'user-code' &&
/be a new (\w+ )*Abstract Closure/.test(parent.textContent ?? '')
) {
return false;
}
if (
parent
.getAttribute('fence-effects')
?.split(',')
.map(s => s.trim())
.includes(effect)
) {
return false;
}
}
return true;
}