diff --git a/lib/dictionary.js b/lib/dictionary.js index cc5dc24..20d9f0a 100644 --- a/lib/dictionary.js +++ b/lib/dictionary.js @@ -1,6 +1,7 @@ 'use strict'; const chalk = require('chalk'); +const { ERROR_TOO_MANY_ERRORS } = require('yandex-speller'); const debug = require('./debug'); const exitCodes = require('./exit-codes'); const utils = require('./utils'); @@ -132,8 +133,7 @@ module.exports = { const dictionary = this.get(); data.forEach(function(typo) { - // ERROR_TOO_MANY_ERRORS = 4 - if (typo.code === 4 || this.isTypo(typo.word, dictionary)) { + if (typo.code === ERROR_TOO_MANY_ERRORS || this.isTypo(typo.word, dictionary)) { result.push(typo); } }, this); diff --git a/lib/report/index.js b/lib/report/index.js index fd82d6a..f013487 100644 --- a/lib/report/index.js +++ b/lib/report/index.js @@ -11,22 +11,23 @@ const stats = { total: 0, }; const reports = []; -const reportNames = {}; +const reportNames = new Set(); const buffer = []; module.exports = { addReports(names) { names.forEach(function(name) { - const moduleName = pth.extname(name) === '.js' ? name : './report/' + name; - if (reportNames[moduleName]) { + const moduleName = pth.extname(name) === '.js' ? name : './' + name; + + if (reportNames.has(moduleName)) { return; } try { - reportNames[moduleName] = true; reports.push(require(moduleName)); + reportNames.add(moduleName); } catch (e) { - console.error(chalk.red('Can\'t load report module "' + moduleName + '".')); + console.error(chalk.red(`Can't load report module "${moduleName}".`)); console.error(chalk.red(e)); } }); @@ -37,17 +38,17 @@ module.exports = { }); }, oneach(hasError, data, dictionary) { + stats.total++; + const hasTypos = Boolean(data && data.data && data.data.length); - stats.total++; + if (hasTypos) { + stats.hasTypos = true; + } if (hasError || hasTypos) { stats.errors++; - if (hasTypos) { - stats.hasTypos = true; - } - buffer.push([hasError, data]); } else { stats.ok++; diff --git a/lib/utils.js b/lib/utils.js index 8103ded..24b874c 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -5,6 +5,7 @@ const isutf8 = require('isutf8'); const minimatch = require('minimatch'); const pth = require('path'); const stripJsonComments = require('strip-json-comments'); +const { ERROR_TOO_MANY_ERRORS } = require('yandex-speller'); const isWin = process.platform === 'win32'; module.exports = { @@ -177,7 +178,7 @@ module.exports = { * @returns {boolean} */ hasManyErrors(data) { - return data.some(el => el.code === 4); // ERROR_TOO_MANY_ERRORS + return data.some(el => el.code === ERROR_TOO_MANY_ERRORS); }, /** * Get uptime in sec. diff --git a/lib/yaspeller.js b/lib/yaspeller.js index 800ed18..a65af0d 100644 --- a/lib/yaspeller.js +++ b/lib/yaspeller.js @@ -15,7 +15,6 @@ const yaspellerApi = require('yandex-speller'); const md = new MarkdownIt(); const printDebug = require('../lib/debug').print; const MAX_LEN_TEXT = 10000; // Max length of text for Yandex.Speller API -const TOO_MANY_ERRORS = 4; function getMaxRequest(settings) { return settings.maxRequest || 2; @@ -327,14 +326,19 @@ function checkSitemap(url, commonCallback, settings, callback) { */ function addPositions(text, data) { data.forEach(function(item) { - if (item.code === TOO_MANY_ERRORS || item.position) { + if (item.code === yaspellerApi.ERROR_TOO_MANY_ERRORS || item.position) { return; } const result = []; const letters = '[^a-zA-Zа-яА-ЯЁёҐґЄєІіЇї]'; + let word = item.word; - text.replace(new RegExp(item.word + '(?:' + letters + '|$)', 'g'), function($0, index) { + if (item.code === yaspellerApi.ERROR_REPEATED_WORD) { + word = item.word + '\\s+' + item.word; + } + + text.replace(new RegExp(word + '(?:' + letters + '|$)', 'mg'), function($0, index) { const prevSymbol = text[index - 1]; if (prevSymbol && prevSymbol.search(letters) === -1) { return; @@ -473,15 +477,15 @@ function prepareText(text) { text = fixLineEndings(text); text = removeSpecialSymbols(text); - return text.trim(); + return text; } function fixLineEndings(text) { - return text.replace(/\r\n/g, '\n') // Fix Windows + return text + .replace(/\r\n/g, '\n') // Fix Windows .replace(/\r/g, '\n') // Fix MacOS .replace(/\s+\n/g, '\n') // Trailling spaces - .replace(/\s+/g, ' ') // Repeat spaces - .replace(/\n+/g, '\n'); // Repeat line endings + .trimRight(); } function removeSpecialSymbols(text) { @@ -495,17 +499,15 @@ function removeSpecialSymbols(text) { } function getErrors() { - return yaspellerApi.errorCodes.filter(function(el) { - return el.code !== TOO_MANY_ERRORS; - }).map(function(el) { - return { + return yaspellerApi.errors + .filter(el => el.code !== yaspellerApi.ERROR_TOO_MANY_ERRORS) + .map(el => ({ code: el.code, title: el.text - }; - }).concat({ - code: 100, // ERROR_EYO - title: 'Letter Ё (Yo)' - }); + })).concat({ + code: 100, // ERROR_EYO + title: 'Letter Ё (Yo)' + }); } module.exports = { diff --git a/package-lock.json b/package-lock.json index 6498a2e..5393d44 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2843,9 +2843,9 @@ } }, "yandex-speller": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/yandex-speller/-/yandex-speller-4.0.0.tgz", - "integrity": "sha512-fDhxRnDL57UPzWxKwPoBJj3Fd8swv74g2pv653cZzcY7KrZZqovZa989Z4LGSAx6PHTPILmsUWFtp9zIuH8aXA==" + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/yandex-speller/-/yandex-speller-4.1.0.tgz", + "integrity": "sha512-z13o3GCMxm5g/MU28JCIdu9SXsAoShroqAheFciS3K0wa2JRkA1y5qASZuavJuaCl0KG5RhTjo8gdkbyp6QC+w==" }, "yargs": { "version": "13.3.2", diff --git a/package.json b/package.json index 6cf54cb..d70fe0f 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "node-fetch": "^2.6.0", "strip-json-comments": "^3.0.1", "xml2js": "^0.4.23", - "yandex-speller": "^4.0.0" + "yandex-speller": "^4.1.0" }, "devDependencies": { "chai": "^4.2.0",