-
Notifications
You must be signed in to change notification settings - Fork 6
/
textlint-rule-prh.js
166 lines (157 loc) · 5.5 KB
/
textlint-rule-prh.js
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
// LICENSE : MIT
"use strict";
import { RuleHelper } from "textlint-rule-helper";
/**
* RegExp#flags polyfill
*/
if (RegExp.prototype.flags === undefined) {
Object.defineProperty(RegExp.prototype, "flags", {
configurable: true,
get: function() {
return this.toString().match(/[gimuy]*$/)[0];
}
});
}
const prh = require("prh");
const path = require("path");
const untildify = require("untildify");
function createPrhEngine(rulePaths, baseDir) {
if (rulePaths.length === 0) {
return null;
}
const expandedRulePaths = rulePaths.map(rulePath => untildify(rulePath));
const prhEngine = prh.fromYAMLFilePath(path.resolve(baseDir, expandedRulePaths[0]));
expandedRulePaths.slice(1).forEach(ruleFilePath => {
const config = prh.fromYAMLFilePath(path.resolve(baseDir, ruleFilePath));
prhEngine.merge(config);
});
return prhEngine;
}
function createPrhEngineFromContents(yamlContents) {
if (yamlContents.length === 0) {
return null;
}
const dummyFilePath = "";
const prhEngine = prh.fromYAML(dummyFilePath, yamlContents[0]);
yamlContents.slice(1).forEach(content => {
const config = prh.fromYAML(dummyFilePath, content);
prhEngine.merge(config);
});
return prhEngine;
}
function mergePrh(...engines) {
const engines_ = engines.filter(engine => !!engine);
const mainEngine = engines_[0];
engines_.slice(1).forEach(engine => {
mainEngine.merge(engine);
});
return mainEngine;
}
const assertOptions = options => {
if (typeof options.ruleContents === "undefined" && typeof options.rulePaths === "undefined") {
throw new Error(`textlint-rule-prh require Rule Options.
Please set .textlinrc:
{
"rules": {
"prh": {
"rulePaths" :["path/to/prh.yml"]
}
}
}
`);
}
};
/**
* for each diff of changeSet
* @param {ChangeSet} changeSet
* @param {string} str
* @param {function({
matchStartIndex: number,
matchEndIndex: number,
actual: string
expected: string
})}onChangeOfMatch
*/
const forEachChange = (changeSet, str, onChangeOfMatch) => {
const sortedDiffs = changeSet.diffs.sort(function(a, b) {
return a.index - b.index;
});
let delta = 0;
sortedDiffs.forEach(function(diff) {
const result = diff.expected.replace(/\$([0-9]{1,2})/g, function(match, g1) {
const index = parseInt(g1);
if (index === 0 || diff.matches.length - 1 < index) {
return match;
}
return diff.matches[index] || "";
});
// matchStartIndex/matchEndIndex value is original position, not replaced position
// textlint use original position
const matchStartIndex = diff.index;
const matchEndIndex = matchStartIndex + diff.matches[0].length;
// actual => expected
const actual = str.slice(diff.index + delta, diff.index + delta + diff.matches[0].length);
onChangeOfMatch({
matchStartIndex,
matchEndIndex,
actual: actual,
expected: result
});
str = str.slice(0, diff.index + delta) + result + str.slice(diff.index + delta + diff.matches[0].length);
delta += result.length - diff.matches[0].length;
});
};
const getConfigBaseDir = context => {
if (typeof context.getConfigBaseDir === "function") {
return context.getConfigBaseDir() || process.cwd();
}
// Old fallback that use deprecated `config` value
// https://github.com/textlint/textlint/issues/294
const textlintRcFilePath = context.config ? context.config.configFile : null;
// .textlinrc directory
return textlintRcFilePath ? path.dirname(textlintRcFilePath) : process.cwd();
};
function reporter(context, options = {}) {
assertOptions(options);
// .textlinrc directory
const textlintRCDir = getConfigBaseDir(context);
// create prh config
const rulePaths = options.rulePaths || [];
const ruleContents = options.ruleContents || [];
// yaml file + yaml contents
const prhEngineContent = createPrhEngineFromContents(ruleContents);
const prhEngineFiles = createPrhEngine(rulePaths, textlintRCDir);
const prhEngine = mergePrh(prhEngineFiles, prhEngineContent);
const helper = new RuleHelper(context);
const { Syntax, getSource, report, fixer, RuleError } = context;
return {
[Syntax.Str](node) {
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
return;
}
const text = getSource(node);
// to get position from index
// https://github.com/prh/prh/issues/29
const dummyFilePath = "";
const makeChangeSet = prhEngine.makeChangeSet(dummyFilePath, text);
forEachChange(makeChangeSet, text, ({ matchStartIndex, matchEndIndex, actual, expected }) => {
// If result is not changed, should not report
if (actual === expected) {
return;
}
const messages = actual + " => " + expected;
report(
node,
new RuleError(messages, {
index: matchStartIndex,
fix: fixer.replaceTextRange([matchStartIndex, matchEndIndex], expected)
})
);
});
}
};
}
module.exports = {
linter: reporter,
fixer: reporter
};