Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rules/spell-checker): add multilang support #68

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.pipe(istanbul.hookRequire()) // Force `require` to return covered files
.on('finish', function () {
gulp.src(['test/*.js', '!test/performance.js'])
.pipe(mocha())
.pipe(mocha({ timeout: 100000 }))
.pipe(istanbul.writeReports()) // Creating the reports after tests runned
.on('end', cb);
});
Expand Down
111 changes: 88 additions & 23 deletions rules/spell-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,54 @@ function getGloabalsSkipsWords() {
});
}

var spell = new Spellchecker(),
dictionary = null,
dictionaryLang,
function makeRegexpFilter(symbols) {
return new RegExp('(\\\\[sSwdDB0nfrtv])|\\\\[0-7][0-7][0-7]|\\\\x[0-9A-F][0-9A-F]|\\\\u[0-9A-F][0-9A-F][0-9A-F][0-9A-F]|[^0-9' + symbols + ' \']', 'g')
}

var spells = {},
dictionaries = {},
skipWords = lodash.union(
...getGloabalsSkipsWords(),
defaultSettings.skipWords,
Object.getOwnPropertyNames(String.prototype),
Object.getOwnPropertyNames(JSON),
Object.getOwnPropertyNames(Math)
);

),
spellingTypeMap = {
Comment: 'comments',
String: 'strings',
Template: 'templates',
Identifier: 'identifiers',
},
defaultLanguage = 'en_US',
enSymbols = 'a-zA-Z',
ruSymbols = 'а-яА-ЯёЁ',
capitalEnSymbolsRegexp = /([A-Z])/g,
enRegexpFilter = {
all: makeRegexpFilter(enSymbols),
capital: capitalEnSymbolsRegexp
},
langRegexpFilter = {
'en_US': enRegexpFilter,
'en_GB': enRegexpFilter,
'en_AU': enRegexpFilter,
'en_CA': enRegexpFilter,
'ru_RU': {
all: makeRegexpFilter(ruSymbols),
capital: /([А-ЯЁ])/g,
}
};

// ESLint 3 had "eslint.version" in context. ESLint 4 does not have one.
function isEslint4OrAbove(context) {
return !('eslint' in context);
}

var langScheme = {
type: 'string',
enum: ['en_US', 'en_GB', 'en_AU', 'en_CA', 'ru_RU']
}

module.exports = {
// meta (object) contains metadata for the rule:
meta: {
Expand Down Expand Up @@ -77,7 +108,18 @@ module.exports = {
default: true
},
lang: {
type: 'string',
anyOf: [
langScheme,
{
type: 'object',
properties: {
comments: langScheme,
strings: langScheme,
identifiers: langScheme,
templates: langScheme
}
}
],
default: 'en_US'
},
skipWords: {
Expand Down Expand Up @@ -129,48 +171,71 @@ module.exports = {
minLength: 1
},
options = lodash.assign(defaultOptions, context.options[0]),
lang = options.lang || 'en_US';

lang = {};

if (dictionaryLang !== lang) { //Dictionary will only be initialized if changed
dictionaryLang = lang;
initializeDictionary(lang);
if (lodash.isString(options.lang) || lodash.isNil(options.lang)) {
var commonLang = options.lang || defaultLanguage;
lang = {
comments: commonLang,
strings: commonLang,
identifiers: commonLang,
templates: commonLang
}
} else {
var enabledSpellingTypes = lodash.values(spellingTypeMap).filter(spellingType => options[spellingType]);
enabledSpellingTypes.forEach(spellingType => {
lang[spellingType] = options.lang[spellingType] || defaultLanguage;
})
}

initializeDictionary(lang)

options.skipWords = new Set(lodash.union(options.skipWords, skipWords)
.map(function (string) {
return string.toLowerCase();
}));

options.skipIfMatch = lodash.union(options.skipIfMatch, defaultSettings.skipIfMatch);

function initializeDictionary(language) {
dictionary = spell.parse({
aff: fs.readFileSync(__dirname + '/utils/dicts/' + language + '.aff'),
dic: fs.readFileSync(__dirname + '/utils/dicts/' + language + '.dic')
});

spell.use(dictionary);
function initializeDictionary(languages) {
lodash.forEach(languages, language => {
if (!spells[language]) {
spells[language] = new Spellchecker()
if (!dictionaries[language]) {
dictionaries[language] = spells[language].parse({
aff: fs.readFileSync(__dirname + '/utils/dicts/' + language + '.aff'),
dic: fs.readFileSync(__dirname + '/utils/dicts/' + language + '.dic')
});
}
spells[language].use(dictionaries[language])
}
})
}

function isSpellingError(aWord) {
return !options.skipWords.has(aWord) && !spell.check(aWord);
function makeIsSpellingError(spell) {
return function isSpellingError(aWord) {
return !options.skipWords.has(aWord) && !spell.check(aWord);
}
}

function checkSpelling(aNode, value, spellingType) {
if(!hasToSkip(value)) {
// Regular expression matches regexp metacharacters, and any special char
var regexp = /(\\[sSwdDB0nfrtv])|\\[0-7][0-7][0-7]|\\x[0-9A-F][0-9A-F]|\\u[0-9A-F][0-9A-F][0-9A-F][0-9A-F]|[^0-9a-zA-Z ']/g,
var spellingTypeLang = lang[spellingTypeMap[spellingType]],
regexp = langRegexpFilter[spellingTypeLang].all,
capitalRegexp = langRegexpFilter[spellingTypeLang].capital,
nodeWords = value.replace(regexp, ' ')
.replace(/([A-Z])/g, ' $1').split(' '),
.replace(capitalRegexp, ' $1').split(' '),
spell = spells[spellingTypeLang],
isSpellingError = makeIsSpellingError(spell),
errors;
errors = nodeWords
.filter(hasToSkipWord)
.filter(isSpellingError)
.filter(function(aWord) {
// Split words by numbers for special cases such as test12anything78variable and to include 2nd and 3rd ordinals
// also for Proper names we convert to lower case in second pass.
var splitByNumberWords = aWord.replace(/[0-9']/g, ' ').replace(/([A-Z])/g, ' $1').toLowerCase().split(' ');
var splitByNumberWords = aWord.replace(/[0-9']/g, ' ').replace(capitalRegexp, ' $1').toLowerCase().split(' ');
return splitByNumberWords.some(isSpellingError);
})
.forEach(function(aWord) {
Expand Down
Loading