-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
234 lines (195 loc) · 7.49 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
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
/**
* @author Gaurav Nelson
* @copyright 2017 Gaurav Nelson
* @module ibmstyleguide
* @fileoverview
* check input against list of words
*/
'use strict';
/* Dependencies. */
var keys = require('object-keys');
var difference = require('lodash.difference');
var nlcstToString = require('nlcst-to-string');
var search = require('nlcst-search');
var position = require('unist-util-position');
var findBefore = require('unist-util-find-before');
var findAfter = require('unist-util-find-after');
var patterns = require('./index.json');
/* Expose. */
module.exports = ibmstyleguide;
var list = keys(patterns);
//to remove passed values from the array, used to find and remove empty vlaues
function removeA(arr) {
var what, a = arguments,
L = a.length,
ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax = arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
// to check number of words in an array
function countWords(s) {
s = s.replace(/(^\s*)|(\s*$)/gi, ""); //exclude start and end white-space
s = s.replace(/[ ]{2,}/gi, " "); //2 or more space to 1
s = s.replace(/\n /, "\n"); // exclude newline with a start spacing
return s.split(' ').length;
}
//to check if all values in the array are same
var contains = function (needle) {
// Per spec, the way to identify NaN is that it is not equal to itself
var findNaN = needle !== needle;
var indexOf;
if (!findNaN && typeof Array.prototype.indexOf === 'function') {
indexOf = Array.prototype.indexOf;
} else {
indexOf = function (needle) {
var i = -1,
index = -1;
for (i = 0; i < this.length; i++) {
var item = this[i];
if ((findNaN && item !== item) || item === needle) {
index = i;
break;
}
}
return index;
};
}
return indexOf.call(this, needle) > -1;
};
function ibmstyleguide(options) {
var ignore = (options || {}).ignore || [];
var phrases = difference(list, ignore);
return transformer;
//allow dashes to compare words with hyphen
function transformer(tree, file) {
search(tree, phrases, finder, {
allowDashes: true
});
function finder(match, index, parent, phrase) {
var pattern = patterns[phrase];
var replace = pattern.replace;
var value = nlcstToString(match);
var data = pattern.data;
var reason;
var message;
var matchedWord = value.toString().toLowerCase().trim(); //this is what triggered the error
var replaceWord = replace.toString().toLowerCase().trim(); // this is correct value
var fMulti = matchedWord.match(/^.(\w)+/g); //find the first word in matched word
var LMulti = matchedWord.match(/\s(\w+)$/g); //find the last word in matched word
//if matched word is a single word, there is no last word (null)
if (LMulti != null) {
LMulti = LMulti.toString().trim();
}
var finalMatches = [];
//check if the replace word contains the matched words
//in this case we need to also lookout for words before and after matched words.
if (replaceWord.includes(matchedWord)) {
//lets get the repace word in an array
var checkValueArr = [];
checkValueArr = replaceWord.split(" ");
//remove empty values
removeA(checkValueArr, "");
//remove instances where replace = match and it is a single word
if (countWords(replaceWord) == 1) {
//do nothing, this means matched word and replaced word are same e.g. Cloud and cloud
} else {
if (countWords(matchedWord) == 1) {
//that is lats word is same as the first word, which means we are dealing with just one word
LMulti = fMulti;
}
//find how many words are before the matched word in replace and how many are after that
//get position of the first word in replace words
var firstWordIndex = checkValueArr.findIndex(function (x) {
return x == fMulti;
})
//get position of last word in replace words
var lastWordIndex = checkValueArr.findIndex(function (x) {
return x == LMulti;
})
var noOfWordsAfter;
var noOfWordsBefore;
//how many words are before the matched words in replace word
noOfWordsBefore = firstWordIndex;
if (lastWordIndex < 0) {
//for single word there are no words after it.
noOfWordsAfter = 0;
} else {
if (noOfWordsBefore != 0) {
noOfWordsAfter = checkValueArr.length - lastWordIndex - 1;
} else {
noOfWordsAfter = checkValueArr.length - 1;
}
}
if (noOfWordsBefore > 0) {
var sampleSizeArray = checkValueArr.slice(0, noOfWordsBefore);
sampleSizeArray.reverse();
var previousNode = findBefore(parent, match[0], 'WordNode');
for (var i = 0; i < sampleSizeArray.length; i++) {
if (previousNode != null) {
var compareValue1 = nlcstToString(previousNode).toLowerCase();
compareValue1 = compareValue1.replace(/\s+/g, '');
var compareValue2 = sampleSizeArray[i];
compareValue2 = compareValue2.replace(/\s+/g, '');
if (compareValue1 == compareValue2) {
finalMatches.push(true);
} else finalMatches.push(false);
previousNode = findBefore(parent, previousNode, 'WordNode');
} else {
//this means it is the first word, we will show error
finalMatches.push(false);
}
}
}
if (noOfWordsAfter > 0) {
var sampleSizeArray = checkValueArr.slice(noOfWordsAfter);
var afterNode = findAfter(parent, match[0], 'WordNode');
for (var i = 0; i < sampleSizeArray.length; i++) {
if (afterNode != null) {
var compareValue1 = nlcstToString(afterNode).toLowerCase();
compareValue1 = compareValue1.replace(/\s+/g, '');
var compareValue2 = sampleSizeArray[i];
compareValue2 = compareValue2.replace(/\s+/g, '');
if (compareValue1 == compareValue2) {
finalMatches.push(true);
} else finalMatches.push(false);
afterNode = findAfter(parent, afterNode, 'WordNode');
}
}
}
}
var checkifAllTrue = contains.call(finalMatches, false);
if (!checkifAllTrue) {
//this means the matched word and the words before or after it are same
//therefore do nothing
} else {
reason = data[0]
message = file.warn(reason, {
start: position.start(match[0]),
end: position.end(match[match.length - 1])
});
message.ruleId = phrase.replace(/\s+/g, '-').toLowerCase();
message.source = "ibmstyleguide";
message.actual = value;
message.expected = replace;
}
}
// replaced words and matched words are different thus do the normal error reporting
else {
reason = data[0]
message = file.warn(reason, {
start: position.start(match[0]),
end: position.end(match[match.length - 1])
});
message.ruleId = phrase.replace(/\s+/g, '-').toLowerCase();
message.source = "ibmstyleguide";
message.actual = value;
message.expected = replace;
}
}
}
}