-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.js
More file actions
80 lines (76 loc) · 2.13 KB
/
Copy path42.js
File metadata and controls
80 lines (76 loc) · 2.13 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
// LICENSE : MIT
"use strict";
/**
* Get parents of node.
* The parent nodes are returned in order from the closest parent to the outer ones.
* @param node
* @returns {Array}
*/
function getParents(node) {
var result = [];
var parent = node.parent;
while (parent != null) {
result.push(parent);
parent = parent.parent;
}
return result;
}
/**
* Return true if `node` is wrapped any one of `types`.
* @param {TxtNode} node is target node
* @param {string[]} types are wrapped target node
* @returns {boolean}
*/
function isNodeWrapped(node, types) {
var parents = getParents(node);
var parentsTypes = parents.map(function(parent) {
return parent.type;
});
return types.some(function(type) {
return parentsTypes.some(function(parentType) {
return parentType === type;
});
});
}
/**
* @param {RuleContext} context
*/
module.exports = function(context) {
var exports = {};
// When `Node`'s type is `Str` come, call this callback.
/*
e.g.)
# Header
This is Str.
Todo: quick fix this.
*/
// "This is Str." and "Todo: quick fix this." are `Str` type.
// This callback function is called twice.
exports[context.Syntax.Str] = function(node) {
var Syntax = context.Syntax;
if (isNodeWrapped(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote])) {
return;
}
// get text from node
var text = context.getSource(node);
// does text contain "todo:"?
if (/todo:/i.test(text)) {
context.report(node, new context.RuleError("found TODO: '" + text + "'"));
}
};
// When `node`'s type is `ListItem` come, call this callback.
/*
e.g.)
# Header
- list 1
- [ ] todo
*/
// `List` is "- list 1" and - [ ] todo", so called this callback twice.
exports[context.Syntax.ListItem] = function(node) {
var text = context.getSource(node);
if (/\[\s+\]\s/i.test(text)) {
context.report(node, new context.RuleError("found TODO: '" + text + "'"));
}
};
return exports;
};