@khanacademy/perseus-linter 0.1.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/CHANGELOG.md +12 -0
- package/dist/es/index.js +3152 -0
- package/dist/es/index.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3129 -0
- package/dist/index.js.flow +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +31 -0
- package/src/README.md +41 -0
- package/src/__tests__/matcher_test.js +498 -0
- package/src/__tests__/rule_test.js +102 -0
- package/src/__tests__/rules_test.js +488 -0
- package/src/__tests__/selector-parser_test.js +52 -0
- package/src/__tests__/tree-transformer_test.js +446 -0
- package/src/index.js +281 -0
- package/src/proptypes.js +29 -0
- package/src/rule.js +412 -0
- package/src/rules/absolute-url.js +24 -0
- package/src/rules/all-rules.js +72 -0
- package/src/rules/blockquoted-math.js +10 -0
- package/src/rules/blockquoted-widget.js +10 -0
- package/src/rules/double-spacing-after-terminal.js +12 -0
- package/src/rules/extra-content-spacing.js +12 -0
- package/src/rules/heading-level-1.js +14 -0
- package/src/rules/heading-level-skip.js +20 -0
- package/src/rules/heading-sentence-case.js +11 -0
- package/src/rules/heading-title-case.js +63 -0
- package/src/rules/image-alt-text.js +21 -0
- package/src/rules/image-in-table.js +10 -0
- package/src/rules/image-spaces-around-urls.js +35 -0
- package/src/rules/image-widget.js +50 -0
- package/src/rules/link-click-here.js +11 -0
- package/src/rules/lint-utils.js +48 -0
- package/src/rules/long-paragraph.js +14 -0
- package/src/rules/math-adjacent.js +10 -0
- package/src/rules/math-align-extra-break.js +11 -0
- package/src/rules/math-align-linebreaks.js +43 -0
- package/src/rules/math-empty.js +10 -0
- package/src/rules/math-font-size.js +12 -0
- package/src/rules/math-frac.js +10 -0
- package/src/rules/math-nested.js +11 -0
- package/src/rules/math-starts-with-space.js +12 -0
- package/src/rules/math-text-empty.js +10 -0
- package/src/rules/math-without-dollars.js +14 -0
- package/src/rules/nested-lists.js +11 -0
- package/src/rules/profanity.js +10 -0
- package/src/rules/table-missing-cells.js +20 -0
- package/src/rules/unbalanced-code-delimiters.js +14 -0
- package/src/rules/unescaped-dollar.js +10 -0
- package/src/rules/widget-in-table.js +10 -0
- package/src/selector.js +505 -0
- package/src/tree-transformer.js +587 -0
- package/src/types.js +10 -0
package/src/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# perseus-linter
|
|
2
|
+
|
|
3
|
+
The linter is implemented by the following files:
|
|
4
|
+
|
|
5
|
+
tree-transformer.js:
|
|
6
|
+
|
|
7
|
+
This file defines a TreeTransformer class for traversing (and
|
|
8
|
+
optionally transforming by inserting, reparenting and removing
|
|
9
|
+
nodes) markdown parse trees. When traversing a tree, a
|
|
10
|
+
TreeTransformer calls the function you specify on each node. This is
|
|
11
|
+
a post-order traversal: the function is called on the way back up,
|
|
12
|
+
not on the way down. When the function is invoked, it is passed the
|
|
13
|
+
current node, a state object, and the concatenated content of the
|
|
14
|
+
node and its descendants. The state object is the interesting one:
|
|
15
|
+
it is an instance of TraversalState, which is a class defined in
|
|
16
|
+
(but not exported by) this same file. TraversalState has an API for
|
|
17
|
+
querying the ancestors and siblings of the current node, and also an
|
|
18
|
+
API for replacing the current node with a new one.
|
|
19
|
+
|
|
20
|
+
selector.js:
|
|
21
|
+
|
|
22
|
+
This file defines the Selector class which works like a CSS selector
|
|
23
|
+
for markdown parse trees. Selector.parse() converts strings like
|
|
24
|
+
"heading + paragraph > text" to Selector objects. A Selector object
|
|
25
|
+
has a match() method that tests whether a given node in a parse tree
|
|
26
|
+
matches. The match() method takes a TraversalState object as its
|
|
27
|
+
argument, so selectors can only be used during a TreeTransformer
|
|
28
|
+
traversal.
|
|
29
|
+
|
|
30
|
+
rule.js:
|
|
31
|
+
|
|
32
|
+
This file defines the Rule class which represents a single lint
|
|
33
|
+
rule. A Rule object has a check() method that takes the same (node,
|
|
34
|
+
state, content) arguments that a TreeTransformer passes to the
|
|
35
|
+
traversal callback function. Rules can have a selector, a regular
|
|
36
|
+
expression, and a function. If a node matches the selector and its
|
|
37
|
+
content matches the regular expression, then the function is called
|
|
38
|
+
to check the node and return a warning message if the node does, in
|
|
39
|
+
fact, have lint.
|
|
40
|
+
|
|
41
|
+
See the individual file for additional documentation.
|
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
/* These tests exercise the Selector.match() method and also test that we
|
|
3
|
+
* can integrate ../../perseus-markdown.js with ../tree-transform.js and
|
|
4
|
+
* ../selector.js
|
|
5
|
+
*/
|
|
6
|
+
import * as PureMarkdown from "@khanacademy/pure-markdown";
|
|
7
|
+
|
|
8
|
+
import Selector from "../selector.js";
|
|
9
|
+
import TreeTransformer from "../tree-transformer.js";
|
|
10
|
+
|
|
11
|
+
describe("Gorgon selector matching:", () => {
|
|
12
|
+
const markdown = `
|
|
13
|
+
### A
|
|
14
|
+
|
|
15
|
+
B
|
|
16
|
+
|
|
17
|
+
C
|
|
18
|
+
|
|
19
|
+
- D
|
|
20
|
+
- E
|
|
21
|
+
- F
|
|
22
|
+
|
|
23
|
+
*G*H
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
function parseTree() {
|
|
27
|
+
return PureMarkdown.parse(markdown);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
it("wildcards match every node", () => {
|
|
31
|
+
const tree = parseTree();
|
|
32
|
+
const selector = Selector.parse("*");
|
|
33
|
+
const tt = new TreeTransformer(tree);
|
|
34
|
+
tt.traverse((n, state, content) => {
|
|
35
|
+
// The wildcard selector should match at every node
|
|
36
|
+
// $FlowFixMe[incompatible-use]
|
|
37
|
+
expect(selector.match(state)[0]).toEqual(n);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("type-based matching works", () => {
|
|
42
|
+
const tree = parseTree();
|
|
43
|
+
const selectors = {};
|
|
44
|
+
const tt = new TreeTransformer(tree);
|
|
45
|
+
|
|
46
|
+
// Traverse the tree once and create a selector for every type
|
|
47
|
+
// of node we find
|
|
48
|
+
tt.traverse((n, state, content) => {
|
|
49
|
+
if (!selectors[n.type]) {
|
|
50
|
+
selectors[n.type] = Selector.parse(n.type);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const types = Object.keys(selectors);
|
|
55
|
+
|
|
56
|
+
// Now traverse the tree again. At each node run all of the
|
|
57
|
+
// selectors we've created. Only those with matching types
|
|
58
|
+
// should match.
|
|
59
|
+
tt.traverse((n, state, content) => {
|
|
60
|
+
types.forEach((type) => {
|
|
61
|
+
const selector = selectors[type];
|
|
62
|
+
const match = selector.match(state);
|
|
63
|
+
if (n.type === type) {
|
|
64
|
+
expect(match[0]).toEqual(n);
|
|
65
|
+
} else {
|
|
66
|
+
expect(match).toEqual(null);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if (!selectors[n.type]) {
|
|
71
|
+
selectors[n.type] = Selector.parse(n.type);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("parent combinator", () => {
|
|
77
|
+
const selector = Selector.parse("paragraph > text");
|
|
78
|
+
const tree = parseTree();
|
|
79
|
+
const tt = new TreeTransformer(tree);
|
|
80
|
+
let numMatches = 0;
|
|
81
|
+
let matchedText = "";
|
|
82
|
+
|
|
83
|
+
tt.traverse((n, state, content) => {
|
|
84
|
+
const match = selector.match(state);
|
|
85
|
+
const parent = state.parent();
|
|
86
|
+
// $FlowFixMe[incompatible-use]
|
|
87
|
+
// $FlowFixMe[incompatible-type]
|
|
88
|
+
if (n.type === "text" && parent.type === "paragraph") {
|
|
89
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
90
|
+
expect(match).toHaveLength(2);
|
|
91
|
+
// $FlowFixMe[incompatible-use]
|
|
92
|
+
expect(match[0]).toEqual(parent);
|
|
93
|
+
// $FlowFixMe[incompatible-use]
|
|
94
|
+
expect(match[1]).toEqual(n);
|
|
95
|
+
matchedText += content;
|
|
96
|
+
numMatches++;
|
|
97
|
+
} else {
|
|
98
|
+
expect(match).toEqual(null);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
expect(numMatches).toEqual(3);
|
|
103
|
+
expect(matchedText).toEqual("BCH");
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("double parent combinator", () => {
|
|
107
|
+
const selector = Selector.parse("paragraph > em > text");
|
|
108
|
+
const tree = parseTree();
|
|
109
|
+
const tt = new TreeTransformer(tree);
|
|
110
|
+
let numMatches = 0;
|
|
111
|
+
let matchedText = "";
|
|
112
|
+
|
|
113
|
+
tt.traverse((n, state, content) => {
|
|
114
|
+
const match = selector.match(state);
|
|
115
|
+
// Make a mutable copy before popping.
|
|
116
|
+
const ancestors = [...state.ancestors()];
|
|
117
|
+
const parent = ancestors.pop();
|
|
118
|
+
const grandparent = ancestors.pop();
|
|
119
|
+
if (
|
|
120
|
+
n.type === "text" &&
|
|
121
|
+
parent.type === "em" &&
|
|
122
|
+
grandparent.type === "paragraph"
|
|
123
|
+
) {
|
|
124
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
125
|
+
expect(match).toHaveLength(3);
|
|
126
|
+
// $FlowFixMe[incompatible-use]
|
|
127
|
+
expect(match[0]).toEqual(grandparent);
|
|
128
|
+
// $FlowFixMe[incompatible-use]
|
|
129
|
+
expect(match[1]).toEqual(parent);
|
|
130
|
+
// $FlowFixMe[incompatible-use]
|
|
131
|
+
expect(match[2]).toEqual(n);
|
|
132
|
+
matchedText += content;
|
|
133
|
+
numMatches++;
|
|
134
|
+
} else {
|
|
135
|
+
expect(match).toEqual(null);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
expect(numMatches).toEqual(1);
|
|
140
|
+
expect(matchedText).toEqual("G");
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("ancestor combinator", () => {
|
|
144
|
+
const selector = Selector.parse("paragraph text");
|
|
145
|
+
const tree = parseTree();
|
|
146
|
+
const tt = new TreeTransformer(tree);
|
|
147
|
+
let numMatches = 0;
|
|
148
|
+
let matchedText = "";
|
|
149
|
+
|
|
150
|
+
tt.traverse((n, state, content) => {
|
|
151
|
+
const match = selector.match(state);
|
|
152
|
+
if (match !== null) {
|
|
153
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
154
|
+
expect(match).toHaveLength(2);
|
|
155
|
+
// $FlowFixMe[incompatible-use]
|
|
156
|
+
expect(match[0].type).toEqual("paragraph");
|
|
157
|
+
// $FlowFixMe[incompatible-use]
|
|
158
|
+
expect(match[1].type).toEqual("text");
|
|
159
|
+
// $FlowFixMe[incompatible-use]
|
|
160
|
+
expect(match[1]).toEqual(n);
|
|
161
|
+
matchedText += content;
|
|
162
|
+
numMatches++;
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
expect(numMatches).toEqual(4);
|
|
167
|
+
expect(matchedText).toEqual("BCGH");
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("double ancestor combinator", () => {
|
|
171
|
+
const selector = Selector.parse("paragraph em text");
|
|
172
|
+
const tree = parseTree();
|
|
173
|
+
const tt = new TreeTransformer(tree);
|
|
174
|
+
let numMatches = 0;
|
|
175
|
+
let matchedText = "";
|
|
176
|
+
|
|
177
|
+
tt.traverse((n, state, content) => {
|
|
178
|
+
const match = selector.match(state);
|
|
179
|
+
if (match !== null) {
|
|
180
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
181
|
+
expect(match).toHaveLength(3);
|
|
182
|
+
// $FlowFixMe[incompatible-use]
|
|
183
|
+
expect(match[0].type).toEqual("paragraph");
|
|
184
|
+
// $FlowFixMe[incompatible-use]
|
|
185
|
+
expect(match[1].type).toEqual("em");
|
|
186
|
+
// $FlowFixMe[incompatible-use]
|
|
187
|
+
expect(match[2].type).toEqual("text");
|
|
188
|
+
// $FlowFixMe[incompatible-use]
|
|
189
|
+
expect(match[2]).toEqual(n);
|
|
190
|
+
matchedText += content;
|
|
191
|
+
numMatches++;
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
expect(numMatches).toEqual(1);
|
|
196
|
+
expect(matchedText).toEqual("G");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("previous combinator", () => {
|
|
200
|
+
const selector = Selector.parse("heading + paragraph");
|
|
201
|
+
const tree = parseTree();
|
|
202
|
+
const tt = new TreeTransformer(tree);
|
|
203
|
+
let numMatches = 0;
|
|
204
|
+
let matchedText = "";
|
|
205
|
+
|
|
206
|
+
tt.traverse((n, state, content) => {
|
|
207
|
+
const match = selector.match(state);
|
|
208
|
+
if (match !== null) {
|
|
209
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
210
|
+
expect(match).toHaveLength(2);
|
|
211
|
+
// $FlowFixMe[incompatible-use]
|
|
212
|
+
expect(match[0].type).toEqual("heading");
|
|
213
|
+
// $FlowFixMe[incompatible-use]
|
|
214
|
+
expect(match[0]).toEqual(state.previousSibling());
|
|
215
|
+
// $FlowFixMe[incompatible-use]
|
|
216
|
+
expect(match[1].type).toEqual("paragraph");
|
|
217
|
+
// $FlowFixMe[incompatible-use]
|
|
218
|
+
expect(match[1]).toEqual(n);
|
|
219
|
+
matchedText += content;
|
|
220
|
+
numMatches++;
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
expect(numMatches).toEqual(1);
|
|
225
|
+
expect(matchedText).toEqual("B");
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("double previous combinator", () => {
|
|
229
|
+
const selector = Selector.parse("heading + paragraph + paragraph");
|
|
230
|
+
const tree = parseTree();
|
|
231
|
+
const tt = new TreeTransformer(tree);
|
|
232
|
+
let numMatches = 0;
|
|
233
|
+
let matchedText = "";
|
|
234
|
+
|
|
235
|
+
tt.traverse((n, state, content) => {
|
|
236
|
+
const match = selector.match(state);
|
|
237
|
+
if (match !== null) {
|
|
238
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
239
|
+
expect(match).toHaveLength(3);
|
|
240
|
+
// $FlowFixMe[incompatible-use]
|
|
241
|
+
expect(match[0].type).toEqual("heading");
|
|
242
|
+
// $FlowFixMe[incompatible-use]
|
|
243
|
+
expect(match[1].type).toEqual("paragraph");
|
|
244
|
+
// $FlowFixMe[incompatible-use]
|
|
245
|
+
expect(match[1]).toEqual(state.previousSibling());
|
|
246
|
+
// $FlowFixMe[incompatible-use]
|
|
247
|
+
expect(match[2].type).toEqual("paragraph");
|
|
248
|
+
// $FlowFixMe[incompatible-use]
|
|
249
|
+
expect(match[2]).toEqual(n);
|
|
250
|
+
matchedText += content;
|
|
251
|
+
numMatches++;
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
expect(numMatches).toEqual(1);
|
|
256
|
+
expect(matchedText).toEqual("C");
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("sibling combinator", () => {
|
|
260
|
+
const selector = Selector.parse("heading ~ paragraph");
|
|
261
|
+
const tree = parseTree();
|
|
262
|
+
const tt = new TreeTransformer(tree);
|
|
263
|
+
let numMatches = 0;
|
|
264
|
+
let matchedText = "";
|
|
265
|
+
|
|
266
|
+
tt.traverse((n, state, content) => {
|
|
267
|
+
const match = selector.match(state);
|
|
268
|
+
if (match !== null) {
|
|
269
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
270
|
+
expect(match).toHaveLength(2);
|
|
271
|
+
// $FlowFixMe[incompatible-use]
|
|
272
|
+
expect(match[0].type).toEqual("heading");
|
|
273
|
+
// $FlowFixMe[incompatible-use]
|
|
274
|
+
expect(match[1].type).toEqual("paragraph");
|
|
275
|
+
// $FlowFixMe[incompatible-use]
|
|
276
|
+
expect(match[1]).toEqual(n);
|
|
277
|
+
matchedText += content;
|
|
278
|
+
numMatches++;
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
expect(numMatches).toEqual(3);
|
|
283
|
+
expect(matchedText).toEqual("BCGH");
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("double sibling combinator", () => {
|
|
287
|
+
const selector = Selector.parse("heading ~ paragraph ~ paragraph");
|
|
288
|
+
const tree = parseTree();
|
|
289
|
+
const tt = new TreeTransformer(tree);
|
|
290
|
+
let numMatches = 0;
|
|
291
|
+
let matchedText = "";
|
|
292
|
+
|
|
293
|
+
tt.traverse((n, state, content) => {
|
|
294
|
+
const match = selector.match(state);
|
|
295
|
+
if (match !== null) {
|
|
296
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
297
|
+
expect(match).toHaveLength(3);
|
|
298
|
+
// $FlowFixMe[incompatible-use]
|
|
299
|
+
expect(match[0].type).toEqual("heading");
|
|
300
|
+
// $FlowFixMe[incompatible-use]
|
|
301
|
+
expect(match[1].type).toEqual("paragraph");
|
|
302
|
+
// $FlowFixMe[incompatible-use]
|
|
303
|
+
expect(match[2].type).toEqual("paragraph");
|
|
304
|
+
// $FlowFixMe[incompatible-use]
|
|
305
|
+
expect(match[2]).toEqual(n);
|
|
306
|
+
matchedText += content;
|
|
307
|
+
numMatches++;
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
expect(numMatches).toEqual(2);
|
|
312
|
+
expect(matchedText).toEqual("CGH");
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it("mixed combinators", () => {
|
|
316
|
+
const selector = Selector.parse("list + paragraph > em");
|
|
317
|
+
const tree = parseTree();
|
|
318
|
+
const tt = new TreeTransformer(tree);
|
|
319
|
+
let numMatches = 0;
|
|
320
|
+
let matchedText = "";
|
|
321
|
+
|
|
322
|
+
tt.traverse((n, state, content) => {
|
|
323
|
+
const match = selector.match(state);
|
|
324
|
+
if (match !== null) {
|
|
325
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
326
|
+
expect(match).toHaveLength(3);
|
|
327
|
+
// $FlowFixMe[incompatible-use]
|
|
328
|
+
expect(match[0].type).toEqual("list");
|
|
329
|
+
// $FlowFixMe[incompatible-use]
|
|
330
|
+
expect(match[1].type).toEqual("paragraph");
|
|
331
|
+
// $FlowFixMe[incompatible-use]
|
|
332
|
+
expect(match[1]).toEqual(state.parent());
|
|
333
|
+
// $FlowFixMe[incompatible-use]
|
|
334
|
+
expect(match[2].type).toEqual("em");
|
|
335
|
+
// $FlowFixMe[incompatible-use]
|
|
336
|
+
expect(match[2]).toEqual(n);
|
|
337
|
+
matchedText += content;
|
|
338
|
+
numMatches++;
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
expect(numMatches).toEqual(1);
|
|
343
|
+
expect(matchedText).toEqual("G");
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("mixed combinators 2", () => {
|
|
347
|
+
const selector = Selector.parse("list ~ paragraph em");
|
|
348
|
+
const tree = parseTree();
|
|
349
|
+
const tt = new TreeTransformer(tree);
|
|
350
|
+
let numMatches = 0;
|
|
351
|
+
let matchedText = "";
|
|
352
|
+
|
|
353
|
+
tt.traverse((n, state, content) => {
|
|
354
|
+
const match = selector.match(state);
|
|
355
|
+
if (match !== null) {
|
|
356
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
357
|
+
expect(match).toHaveLength(3);
|
|
358
|
+
// $FlowFixMe[incompatible-use]
|
|
359
|
+
expect(match[0].type).toEqual("list");
|
|
360
|
+
// $FlowFixMe[incompatible-use]
|
|
361
|
+
expect(match[1].type).toEqual("paragraph");
|
|
362
|
+
// $FlowFixMe[incompatible-use]
|
|
363
|
+
expect(match[1]).toEqual(state.parent());
|
|
364
|
+
// $FlowFixMe[incompatible-use]
|
|
365
|
+
expect(match[2].type).toEqual("em");
|
|
366
|
+
// $FlowFixMe[incompatible-use]
|
|
367
|
+
expect(match[2]).toEqual(n);
|
|
368
|
+
matchedText += content;
|
|
369
|
+
numMatches++;
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
expect(numMatches).toEqual(1);
|
|
374
|
+
expect(matchedText).toEqual("G");
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it("mixed combinators 3", () => {
|
|
378
|
+
const selector = Selector.parse("paragraph > em + text");
|
|
379
|
+
const tree = parseTree();
|
|
380
|
+
const tt = new TreeTransformer(tree);
|
|
381
|
+
let numMatches = 0;
|
|
382
|
+
let matchedText = "";
|
|
383
|
+
|
|
384
|
+
tt.traverse((n, state, content) => {
|
|
385
|
+
const match = selector.match(state);
|
|
386
|
+
if (match !== null) {
|
|
387
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
388
|
+
expect(match).toHaveLength(3);
|
|
389
|
+
// $FlowFixMe[incompatible-use]
|
|
390
|
+
expect(match[0].type).toEqual("paragraph");
|
|
391
|
+
// $FlowFixMe[incompatible-use]
|
|
392
|
+
expect(match[0]).toEqual(state.parent());
|
|
393
|
+
// $FlowFixMe[incompatible-use]
|
|
394
|
+
expect(match[1].type).toEqual("em");
|
|
395
|
+
// $FlowFixMe[incompatible-use]
|
|
396
|
+
expect(match[1]).toEqual(state.previousSibling());
|
|
397
|
+
// $FlowFixMe[incompatible-use]
|
|
398
|
+
expect(match[2].type).toEqual("text");
|
|
399
|
+
// $FlowFixMe[incompatible-use]
|
|
400
|
+
expect(match[2]).toEqual(n);
|
|
401
|
+
matchedText += content;
|
|
402
|
+
numMatches++;
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
expect(numMatches).toEqual(1);
|
|
407
|
+
expect(matchedText).toEqual("H");
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
it("mixed combinators 4", () => {
|
|
411
|
+
const selector = Selector.parse("paragraph em ~ text");
|
|
412
|
+
const tree = parseTree();
|
|
413
|
+
const tt = new TreeTransformer(tree);
|
|
414
|
+
let numMatches = 0;
|
|
415
|
+
let matchedText = "";
|
|
416
|
+
|
|
417
|
+
tt.traverse((n, state, content) => {
|
|
418
|
+
const match = selector.match(state);
|
|
419
|
+
if (match !== null) {
|
|
420
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
421
|
+
expect(match).toHaveLength(3);
|
|
422
|
+
// $FlowFixMe[incompatible-use]
|
|
423
|
+
expect(match[0].type).toEqual("paragraph");
|
|
424
|
+
// $FlowFixMe[incompatible-use]
|
|
425
|
+
expect(match[0]).toEqual(state.parent());
|
|
426
|
+
// $FlowFixMe[incompatible-use]
|
|
427
|
+
expect(match[1].type).toEqual("em");
|
|
428
|
+
// $FlowFixMe[incompatible-use]
|
|
429
|
+
expect(match[1]).toEqual(state.previousSibling());
|
|
430
|
+
// $FlowFixMe[incompatible-use]
|
|
431
|
+
expect(match[2].type).toEqual("text");
|
|
432
|
+
// $FlowFixMe[incompatible-use]
|
|
433
|
+
expect(match[2]).toEqual(n);
|
|
434
|
+
matchedText += content;
|
|
435
|
+
numMatches++;
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
expect(numMatches).toEqual(1);
|
|
440
|
+
expect(matchedText).toEqual("H");
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
it("selector list", () => {
|
|
444
|
+
const selector = Selector.parse("paragraph, list");
|
|
445
|
+
const tree = parseTree();
|
|
446
|
+
const tt = new TreeTransformer(tree);
|
|
447
|
+
let numMatches = 0;
|
|
448
|
+
let matchedText = "";
|
|
449
|
+
|
|
450
|
+
tt.traverse((n, state, content) => {
|
|
451
|
+
const match = selector.match(state);
|
|
452
|
+
if (match !== null) {
|
|
453
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
454
|
+
expect(match).toHaveLength(1);
|
|
455
|
+
// $FlowFixMe[incompatible-use]
|
|
456
|
+
expect(match[0]).toEqual(n);
|
|
457
|
+
expect(
|
|
458
|
+
n.type === "paragraph" || n.type === "list",
|
|
459
|
+
).toBeTruthy();
|
|
460
|
+
matchedText += content;
|
|
461
|
+
numMatches++;
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
expect(numMatches).toEqual(4);
|
|
466
|
+
expect(matchedText).toEqual("BCDEFGH");
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
it("selector list 2", () => {
|
|
470
|
+
const selector = Selector.parse("heading, paragraph text, list>text");
|
|
471
|
+
const tree = parseTree();
|
|
472
|
+
const tt = new TreeTransformer(tree);
|
|
473
|
+
let numMatches = 0;
|
|
474
|
+
let matchedText = "";
|
|
475
|
+
|
|
476
|
+
tt.traverse((n, state, content) => {
|
|
477
|
+
const match = selector.match(state);
|
|
478
|
+
if (match !== null) {
|
|
479
|
+
expect(Array.isArray(match)).toBeTruthy();
|
|
480
|
+
if (n.type === "heading") {
|
|
481
|
+
expect(match).toHaveLength(1);
|
|
482
|
+
// $FlowFixMe[incompatible-use]
|
|
483
|
+
expect(match[0]).toEqual(n);
|
|
484
|
+
} else {
|
|
485
|
+
expect(match).toHaveLength(2);
|
|
486
|
+
// $FlowFixMe[incompatible-use]
|
|
487
|
+
expect(match[1]).toEqual(n);
|
|
488
|
+
expect(n.type).toEqual("text");
|
|
489
|
+
}
|
|
490
|
+
matchedText += content;
|
|
491
|
+
numMatches++;
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
expect(numMatches).toEqual(8);
|
|
496
|
+
expect(matchedText).toEqual("ABCDEFGH");
|
|
497
|
+
});
|
|
498
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import * as PureMarkdown from "@khanacademy/pure-markdown";
|
|
3
|
+
|
|
4
|
+
import Rule from "../rule.js";
|
|
5
|
+
import TreeTransformer from "../tree-transformer.js";
|
|
6
|
+
|
|
7
|
+
describe("Gorgon lint Rules class", () => {
|
|
8
|
+
const markdown = `
|
|
9
|
+
## This Heading is in Title Case
|
|
10
|
+
|
|
11
|
+
This paragraph contains forbidden words. Poop!
|
|
12
|
+
|
|
13
|
+
This paragraph contains an unescaped $ sign.
|
|
14
|
+
|
|
15
|
+
#### This heading skipped a level
|
|
16
|
+
`;
|
|
17
|
+
|
|
18
|
+
const ruleDescriptions = [
|
|
19
|
+
{
|
|
20
|
+
name: "heading-title-case",
|
|
21
|
+
selector: "heading",
|
|
22
|
+
pattern: "\\s[A-Z][a-z]",
|
|
23
|
+
message: `Title case in heading:
|
|
24
|
+
Only capitalize the first word of headings.`,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
name: "profanity",
|
|
28
|
+
pattern: "/poop|crap/i",
|
|
29
|
+
message: `Profanity:
|
|
30
|
+
this is a family website!`,
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: "unescaped-dollar",
|
|
34
|
+
selector: "unescapedDollar",
|
|
35
|
+
message: `Unescaped '$':
|
|
36
|
+
If writing math, pair with another $.
|
|
37
|
+
Otherwise escape it by writing \\$.`,
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: "heading-level-skip",
|
|
41
|
+
selector: "heading ~ heading",
|
|
42
|
+
lint: function (state, content, nodes) {
|
|
43
|
+
const currentHeading = nodes[1];
|
|
44
|
+
const previousHeading = nodes[0];
|
|
45
|
+
|
|
46
|
+
expect(nodes).toHaveLength(2);
|
|
47
|
+
expect(nodes[1]).toEqual(state.currentNode());
|
|
48
|
+
|
|
49
|
+
// A heading can have a level less than, the same as
|
|
50
|
+
// or one more than the previous heading. But going up
|
|
51
|
+
// by 2 or more levels is not right
|
|
52
|
+
if (currentHeading.level > previousHeading.level + 1) {
|
|
53
|
+
return `Skipped heading level:
|
|
54
|
+
this heading is level ${currentHeading.level} but
|
|
55
|
+
the previous heading was level ${previousHeading.level}`;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
let rules = [];
|
|
63
|
+
|
|
64
|
+
function parseTree() {
|
|
65
|
+
return PureMarkdown.parse(markdown);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
it("makeRules() factory method", () => {
|
|
69
|
+
rules = ruleDescriptions.map((r) => Rule.makeRule(r));
|
|
70
|
+
expect(rules).toHaveLength(ruleDescriptions.length);
|
|
71
|
+
rules.forEach((r) => expect(r instanceof Rule).toBeTruthy());
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("check() method", () => {
|
|
75
|
+
const tree = parseTree();
|
|
76
|
+
const tt = new TreeTransformer(tree);
|
|
77
|
+
const warnings = [];
|
|
78
|
+
|
|
79
|
+
tt.traverse((node, state, content) => {
|
|
80
|
+
rules.forEach((r) => {
|
|
81
|
+
const lint = r.check(node, state, content);
|
|
82
|
+
if (lint) {
|
|
83
|
+
warnings.push(lint);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
expect(warnings).toHaveLength(4);
|
|
89
|
+
expect(warnings[0].rule).toEqual(ruleDescriptions[0].name);
|
|
90
|
+
expect(warnings[0].message).toEqual(ruleDescriptions[0].message);
|
|
91
|
+
|
|
92
|
+
expect(warnings[1].rule).toEqual(ruleDescriptions[1].name);
|
|
93
|
+
expect(warnings[1].message).toEqual(ruleDescriptions[1].message);
|
|
94
|
+
expect(warnings[1].start).toEqual(2);
|
|
95
|
+
expect(warnings[1].end).toEqual(6);
|
|
96
|
+
|
|
97
|
+
expect(warnings[2].rule).toEqual(ruleDescriptions[2].name);
|
|
98
|
+
expect(warnings[2].message).toEqual(ruleDescriptions[2].message);
|
|
99
|
+
|
|
100
|
+
expect(warnings[3].rule).toEqual(ruleDescriptions[3].name);
|
|
101
|
+
});
|
|
102
|
+
});
|