forked from sapegin/textlint-rule-terminology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
207 lines (182 loc) · 5.25 KB
/
index.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
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
// @ts-check
const fs = require('fs');
const path = require('path');
const stripJsonComments = require('strip-json-comments');
const { RuleHelper } = require('textlint-rule-helper');
const { upperFirst } = require('lodash');
const DEFAULT_OPTIONS = {
terms: [],
skip: ['BlockQuote'],
defaultTerms: true,
exclude: [],
};
const sentenceStartRegExp = /\w+[.?!]\)? $/;
function reporter(context, opts = {}) {
const options = { ...DEFAULT_OPTIONS, ...opts };
const terms = getTerms(options.defaultTerms, options.terms, options.exclude);
// Match all words (plain strings) with a single regexp
const words = terms.filter(rule => typeof rule === 'string');
const exactWordRules = [[getMultipleWordRegExp(words), words]];
// Create a separate regexp of each array rule ([pattern, replacement])
const advancedRules = terms.filter(rule => typeof rule !== 'string');
const rules = [...exactWordRules, ...advancedRules];
const helper = new RuleHelper(context);
const { Syntax, RuleError, report, fixer, getSource } = context;
return {
[Syntax.Str](node) {
if (
helper.isChildNode(
node,
options.skip.map(rule => Syntax[rule])
)
) {
return false;
}
return new Promise(resolve => {
const text = getSource(node);
rules.forEach(([pattern, replacements]) => {
const regExp =
typeof pattern === 'string' ? getAdvancedRegExp(pattern) : pattern;
let match;
// eslint-disable-next-line no-cond-assign
while ((match = regExp.exec(text))) {
const index = match.index;
const matched = match[0];
let replacement = getReplacement(pattern, replacements, matched);
// Capitalize word in the beginning of a sentense if the original word was capitalized
const textBeforeMatch = text.substring(0, index);
const isSentenceStart =
index === 0 || sentenceStartRegExp.test(textBeforeMatch);
if (isSentenceStart && upperFirst(matched) === matched) {
replacement = upperFirst(replacement);
}
// Skip correct spelling
if (matched === replacement) {
continue;
}
const range = [index, index + matched.length];
const fix = fixer.replaceTextRange(range, replacement);
const message = `Incorrect usage of the term: “${matched.trim()}”, use “${replacement.trim()}” instead`;
report(node, new RuleError(message, { index, fix }));
}
});
resolve();
});
},
};
}
/**
* @param {boolean} defaultTerms
* @param {string | Array} terms
* @param {Array} [exclude]
*/
function getTerms(defaultTerms, terms, exclude) {
const defaults = defaultTerms
? loadJson(path.resolve(__dirname, 'terms.json'))
: [];
const extras = typeof terms === 'string' ? loadJson(terms) : terms;
// Order matters, the first term to match is used. We prioritize user 'extras' before defaults
const listTerms = [...(Array.isArray(extras) ? extras : []), ...defaults];
// Filter on all terms
if (Array.isArray(exclude)) {
return listTerms.filter(term => {
if (Array.isArray(term)) {
return !exclude.includes(term[0]);
}
return !exclude.includes(term);
});
}
return listTerms;
}
/**
* @param {string} filepath
*/
function loadJson(filepath) {
const json = readTermsFile(path.resolve(filepath));
return JSON.parse(stripJsonComments(json));
}
/**
* @param {string} filepath
*/
function readTermsFile(filepath) {
try {
return fs.readFileSync(filepath, 'utf8');
} catch (err) {
if (err.code === 'ENOENT') {
throw new Error(`Terms file not found: ${filepath}`);
} else {
throw err;
}
}
}
/**
* Match exact word in the middle of the text
* @param {string} pattern
*/
function getExactMatchRegExp(pattern) {
return new RegExp(
// 1. Beginning of the string, or any character that isn't "-" or alphanumeric
// 2. Exact match of the pattern
// 3. Space, ". ", "." at the end of the string, end of the string
`(?<=^|[^-\\w])\\b${pattern}\\b(?= |\\. |\\.$|$)`,
'ig'
);
}
/**
* Match any of given words exactly in the middle of the text
* @param {string[]} words
*/
function getMultipleWordRegExp(words) {
return getExactMatchRegExp(`(?:${words.join('|')})`);
}
/**
* Match pattern on word boundaries in the middle of the text unless the pattern
* has look behinds
* @param {string} pattern
*/
function getAdvancedRegExp(pattern) {
if (
// Look behind: (?<=...) and (?<!...)
pattern.startsWith('(?<') ||
// Positive look ahead: (?=...)
pattern.includes('(?=') ||
// Negative look ahead: (?!...)
pattern.includes('(?!')
) {
return new RegExp(pattern, 'ig');
}
return getExactMatchRegExp(pattern);
}
/**
* @param {string} pattern
* @param {string[]} replacements
* @param {string} matched
*/
function getReplacement(pattern, replacements, matched) {
if (Array.isArray(replacements)) {
return findWord(replacements, matched);
}
return `xyz ${matched} xyz`
.replace(new RegExp(pattern, 'i'), replacements)
.slice(4, -4);
}
/**
* @param {string[]} items
* @param {string} match
*/
function findWord(items, match) {
const lowerCaseMatch = match.toLowerCase();
return items.find(word => word.toLowerCase() === lowerCaseMatch);
}
module.exports = {
linter: reporter,
fixer: reporter,
test: {
getTerms,
findWord,
getMultipleWordRegExp,
getExactMatchRegExp,
getAdvancedRegExp,
getReplacement,
},
};