eslint-plugin-spellcheck
Advanced tools
Comparing version 0.0.7 to 0.0.8
(function () { | ||
'use strict'; | ||
var gulp = require('gulp'), | ||
eslint = require('gulp-eslint'), | ||
istanbul = require('gulp-istanbul'), | ||
mocha = require('gulp-mocha'); | ||
gulp.task('quality', function() { | ||
gulp.src(['rules/*.js']) | ||
.pipe(eslint({ | ||
rules: { | ||
'no-console': 0, | ||
'quotes': [2, 'single'] | ||
}, | ||
globals: { | ||
'require': false, | ||
'module': false, | ||
'console': false, | ||
'process': true | ||
} | ||
})) | ||
.pipe(eslint.format()); | ||
}); | ||
gulp.task('test', function (cb) { | ||
@@ -38,3 +19,3 @@ gulp.src(['rules/*.js']) | ||
gulp.task('default', ['quality', 'test']); | ||
gulp.task('default', ['test']); | ||
}()); |
29
index.js
@@ -1,25 +0,6 @@ | ||
(function(){ | ||
'use strict'; | ||
module.exports = { | ||
rules: { | ||
'spell-checker': require('./rules/spell-checker') | ||
}, | ||
rulesConfig: { | ||
'spell-checker': [1 , { | ||
comments: true, | ||
strings: true, | ||
identifiers: true, | ||
lang: 'en_US', | ||
skipWords: [ | ||
'dict', | ||
'aff', | ||
'hunspellchecker', | ||
'hunspell', | ||
'utils' | ||
] | ||
} | ||
] | ||
} | ||
}; | ||
})(); | ||
module.exports = { | ||
rules: { | ||
'spell-checker': require('./rules/spell-checker') | ||
} | ||
}; |
{ | ||
"name": "eslint-plugin-spellcheck", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "ESLint rules to spell check js files", | ||
@@ -16,19 +16,21 @@ "main": "index.js", | ||
"homepage": "https://github.com/sotaduy/eslint-plugin-spellcheck", | ||
"scripts": { | ||
"test": "gulp", | ||
"lint": "eslint -c .eslintrc.json rules/*.js" | ||
}, | ||
"dependencies": { | ||
"globals": "^6.4.1", | ||
"hunspell-spellchecker": "^1.0.0", | ||
"lodash": "^3.3.1" | ||
"globals": "^9.9.0", | ||
"hunspell-spellchecker": "^1.0.2", | ||
"lodash": "^4.14.2" | ||
}, | ||
"devDependencies": { | ||
"chai": "^1.10.0", | ||
"coveralls": "^2.11.2", | ||
"eslint": "^0.13.0", | ||
"eslint-tester": "^0.5.0", | ||
"gulp": "^3.8.10", | ||
"gulp-eslint": "^0.3.0", | ||
"gulp-istanbul": "0.6.0", | ||
"gulp-mocha": "^2.0.0", | ||
"istanbul": "^0.3.5", | ||
"mocha": "2.1.0", | ||
"shelljs": "^0.3.0", | ||
"chai": "^3.5.0", | ||
"coveralls": "^2.11.12", | ||
"eslint": "^3.2.2", | ||
"gulp": "^3.9.1", | ||
"gulp-istanbul": "1.0.0", | ||
"gulp-mocha": "^3.0.0", | ||
"istanbul": "^0.4.4", | ||
"mocha": "3.0.2", | ||
"shelljs": "^0.7.3", | ||
"shelljs-nodecli": "^0.1.1" | ||
@@ -35,0 +37,0 @@ }, |
@@ -11,3 +11,3 @@ # eslint-plugin-spellcheck | ||
"strings": <<Boolean>>, default: true | ||
Check Spelling inside comments | ||
Check Spelling inside strings | ||
@@ -21,2 +21,5 @@ "identifiers": <<Boolean>>, default: true | ||
"lang": <<String>>, default: "en_US" | ||
Choose the language you want to use. Options are: "en_US", "en_CA", and "en_GB" | ||
"skipWords": <<Array Of Strings>> default: [] | ||
@@ -27,2 +30,3 @@ Array of words that will not be checked. | ||
Array of Regular Expressions that if matched will not be checked. | ||
i.e: "^[-\\w]+\/[-\\w\\.]+$" will ignore MIME types. | ||
```` | ||
@@ -55,5 +59,6 @@ | ||
{ | ||
"comments": "true", | ||
"strings": "true", | ||
"identifiers": "true", | ||
"comments": true, | ||
"strings": true, | ||
"identifiers": true, | ||
"lang": "en_US", | ||
"skipWords": [ | ||
@@ -67,3 +72,4 @@ "dict", | ||
"skipIfMatch": [ | ||
"http://[^s]*" | ||
"http://[^s]*", | ||
"^[-\\w]+\/[-\\w\\.]+$" //For MIME Types | ||
] | ||
@@ -92,5 +98,6 @@ } | ||
{ | ||
"comments": "true", | ||
"strings": "true", | ||
"identifiers": "true", | ||
"comments": true, | ||
"strings": true, | ||
"identifiers": true, | ||
"lang": "en_US", | ||
"skipWords": [ | ||
@@ -106,5 +113,5 @@ "dict", | ||
] | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
``` |
@@ -0,7 +1,11 @@ | ||
// Native modules | ||
var fs = require('fs'); | ||
// 3rd party dependencies | ||
var lodash = require('lodash'), | ||
fs = require('fs'), | ||
Spellchecker = require('hunspell-spellchecker'), | ||
spell = new Spellchecker(), | ||
dictionary, | ||
globals = require('globals'), | ||
Spellchecker = require('hunspell-spellchecker'), | ||
globals = require('globals'); | ||
var spell = new Spellchecker(), | ||
dictionary, | ||
skipWords = lodash.union( | ||
@@ -15,85 +19,170 @@ lodash.keys(globals.builtin), | ||
lodash.keys(globals.shelljs) | ||
); | ||
); | ||
module.exports = function(context) { | ||
'use strict'; | ||
var defaultOptions = { | ||
comments: true, | ||
strings: true, | ||
identifiers: true, | ||
templates: true, | ||
skipWords: [], | ||
skipIfMatch: [] | ||
module.exports = { | ||
// meta (object) contains metadata for the rule: | ||
meta: { | ||
// docs (object) is required for core rules of ESLint. | ||
// In a custom rule or plugin, you can omit docs or include any properties that you need in it. | ||
docs: { | ||
// provides the short description of the rule in the rules index | ||
description: 'spell check', | ||
// specifies the heading under which the rule is listed in the rules index | ||
category: 'Possible Errors', | ||
// is whether the 'extends': 'eslint:recommended' property in a configuration file enables the rule | ||
recommended: false | ||
}, | ||
// fixable (string) is either 'code' or 'whitespace' if the --fix option on the command line automatically fixes problems reported by the rule | ||
// Important: Without the fixable property, ESLint does not apply fixes even if the rule implements fix functions. Omit the fixable property if the rule is not fixable. | ||
fixable: 'code', | ||
// specifies the options so ESLint can prevent invalid rule configurations | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
comments: { | ||
type: 'boolean', | ||
default: true | ||
}, | ||
strings: { | ||
type: 'boolean', | ||
default: true | ||
}, | ||
identifiers: { | ||
type: 'boolean', | ||
default: true | ||
}, | ||
templates: { | ||
type: 'boolean', | ||
default: true | ||
}, | ||
lang: { | ||
type: 'string', | ||
default: 'en_US' | ||
}, | ||
skipWords: { | ||
type: 'array', | ||
default: [ | ||
'dict', | ||
'aff', | ||
'hunspellchecker', | ||
'hunspell', | ||
'utils' | ||
] | ||
}, | ||
skipIfMatch: { | ||
type: 'array', | ||
default: [] | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
] | ||
}, | ||
options = lodash.assign(defaultOptions, context.options[0]), | ||
lang = options.lang || 'en_US'; | ||
dictionary = spell.parse({ | ||
aff: fs.readFileSync(__dirname + '/utils/dicts/' + lang + '.aff'), | ||
dic: fs.readFileSync(__dirname + '/utils/dicts/' + lang + '.dic') | ||
}); | ||
// create (function) returns an object with methods that ESLint calls to “visit” nodes while traversing the abstract syntax tree (AST as defined by ESTree) of JavaScript code: | ||
create: function(context) { | ||
/* | ||
if a key is a node type, ESLint calls that visitor function while going down the tree | ||
if a key is a node type plus :exit, ESLint calls that visitor function while going up the tree | ||
if a key is an event name, ESLint calls that handler function for code path analysis | ||
*/ | ||
spell.use(dictionary); | ||
'use strict'; | ||
var defaultOptions = { | ||
comments: true, | ||
strings: true, | ||
identifiers: true, | ||
templates: true, | ||
skipWords: [], | ||
skipIfMatch: [] | ||
}, | ||
options = lodash.assign(defaultOptions, context.options[0]), | ||
lang = options.lang || 'en_US'; | ||
options.skipWords = lodash.union(options.skipWords, skipWords) | ||
.map(function (string) { | ||
return string.toLowerCase(); | ||
dictionary = spell.parse({ | ||
aff: fs.readFileSync(__dirname + '/utils/dicts/' + lang + '.aff'), | ||
dic: fs.readFileSync(__dirname + '/utils/dicts/' + lang + '.dic') | ||
}); | ||
function checkSpelling(aNode, value, spellingType) { | ||
if(!hasToSkip(value)) { | ||
var nodeWords = value.replace(/[^a-zA-Z ]/g, ' ').replace(/([A-Z])/g, ' $1').toLowerCase().split(' '); | ||
nodeWords | ||
.filter(function(aWord) { | ||
return !lodash.includes(options.skipWords, aWord) && !spell.check(aWord); | ||
}) | ||
.forEach(function(aWord) { | ||
context.report( | ||
aNode, | ||
'You have a misspelled word: {{word}} on {{spellingType}}', | ||
{ word: aWord, | ||
spellingType: spellingType} | ||
); | ||
spell.use(dictionary); | ||
options.skipWords = lodash.union(options.skipWords, skipWords) | ||
.map(function (string) { | ||
return string.toLowerCase(); | ||
}); | ||
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, | ||
nodeWords = value.replace(regexp, ' ') | ||
.replace(/([A-Z])/g, ' $1').split(' '); | ||
nodeWords | ||
.filter(function(aWord) { | ||
return !lodash.includes(options.skipWords, aWord) && !spell.check(aWord); | ||
}) | ||
.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(' '); | ||
return splitByNumberWords.some(function (aWord) { | ||
return !lodash.includes(options.skipWords, aWord) && !spell.check(aWord); | ||
}); | ||
}) | ||
.forEach(function(aWord) { | ||
context.report( | ||
aNode, | ||
'You have a misspelled word: {{word}} on {{spellingType}}', { | ||
word: aWord, | ||
spellingType: spellingType | ||
}); | ||
}); | ||
} | ||
} | ||
function checkComment(aNode) { | ||
if(options.comments) { | ||
checkSpelling(aNode, aNode.value, 'Comment'); | ||
} | ||
} | ||
} | ||
function checkComment(aNode) { | ||
if(options.comments) { | ||
checkSpelling(aNode, aNode.value, 'Comment'); | ||
function checkLiteral(aNode){ | ||
if(options.strings && typeof aNode.value === 'string') { | ||
checkSpelling(aNode, aNode.value, 'String'); | ||
} | ||
} | ||
} | ||
function checkTemplateElement(aNode){ | ||
if(options.templates && typeof aNode.value.raw === 'string') { | ||
checkSpelling(aNode, aNode.value.raw, 'Template'); | ||
} | ||
} | ||
function checkLiteral(aNode){ | ||
if(options.strings && typeof aNode.value === 'string') { | ||
checkSpelling(aNode, aNode.value, 'String'); | ||
function checkIdentifier(aNode) { | ||
if(options.identifiers) { | ||
checkSpelling(aNode, aNode.name, 'Identifier'); | ||
} | ||
} | ||
} | ||
function checkTemplateElement(aNode){ | ||
if(options.templates && typeof aNode.value.raw === 'string') { | ||
checkSpelling(aNode, aNode.value.raw, 'Template'); | ||
/* Returns true if the string in value has to be skipped for spell checking */ | ||
function hasToSkip(value) { | ||
return lodash.includes(options.skipWords, value) || | ||
lodash.find(options.skipIfMatch, function (aPattern) { | ||
return value.match(aPattern); | ||
}); | ||
} | ||
} | ||
function checkIdentifier(aNode) { | ||
if(options.identifiers) { | ||
checkSpelling(aNode, aNode.name, 'Identifier'); | ||
} | ||
return { | ||
'BlockComment': checkComment, | ||
'LineComment': checkComment, | ||
'Literal': checkLiteral, | ||
'TemplateElement': checkTemplateElement, | ||
'Identifier': checkIdentifier | ||
}; | ||
} | ||
/* Returns true if the string in value has to be skipped for spell checking */ | ||
function hasToSkip(value) { | ||
return lodash.includes(options.skipWords, value) || | ||
lodash.find(options.skipIfMatch, function (aPattern) { | ||
return value.match(aPattern); | ||
}); | ||
} | ||
return { | ||
'BlockComment': checkComment, | ||
'LineComment': checkComment, | ||
'Literal': checkLiteral, | ||
'TemplateElement': checkTemplateElement, | ||
'Identifier': checkIdentifier | ||
}; | ||
}; |
@@ -5,4 +5,4 @@ //------------------------------------------------------------------------------ | ||
var eslint = require('../node_modules/eslint/lib/eslint'), | ||
ESLintTester = require('eslint-tester'); | ||
var rule = require('../rules/spell-checker'), | ||
RuleTester = require('eslint').RuleTester; | ||
@@ -13,11 +13,21 @@ //------------------------------------------------------------------------------ | ||
var eslintTester = new ESLintTester(eslint); | ||
eslintTester.addRuleTest('rules/spell-checker', { | ||
var ruleTester = new RuleTester({ | ||
env: { | ||
'es6': true | ||
} | ||
}); | ||
ruleTester.run('spellcheck/spell-checker', rule, { | ||
valid: [ | ||
'var a = 1 // This is a comment', | ||
'var this2 = 1 // This shouldn\'t fail, not the first or the 2nd time', | ||
'var test12anything78variable = 1 // This shouldn\'t fail, not the first or \'the\' 3rd time', | ||
'var a = 2 /* This is a Block Comment */', | ||
'var a = 2 //Array', | ||
'var angular = thisIsATest(of_a_snake_case)', | ||
'var a = new RegExp(`\^Card\\sreader\\xAAnot\\uFFFFconnected\\sin\\s${ numberOfSeconds }s\\.`);', | ||
'var a = new RegExp(`\^Card\\sreader\\snot\\sconnected\\sin\\s${ numberOfSeconds }s\\.`);', | ||
'var a = function testingCamelCase(each){};', | ||
'var a = RegExp', | ||
'var a = "January"', | ||
'var a = \'Hello how are you this is a string\'', | ||
@@ -27,3 +37,3 @@ 'var a = \'ArrayBuffer\'', | ||
code: 'var url = "http://examplus.com"', | ||
args:[2, {skipWords: ['url'], skipIfMatch:['http://[^\s]*']}] | ||
options:[{skipWords: ['url'], skipIfMatch:['http://[^\s]*']}] | ||
}, | ||
@@ -35,3 +45,3 @@ | ||
code: 'var a = 1 // tsih is a comment srting dict', | ||
args:[2, {skipWords: ['dict']}], | ||
options:[{skipWords: ['dict']}], | ||
errors: [ | ||
@@ -43,3 +53,3 @@ { message: 'You have a misspelled word: tsih on Comment'}, | ||
code: 'var ajhasd = \'liasdfuhn\' // tsih is a comment srting dict', | ||
args:[2, {strings: false, identifiers: false, skipWords: ['dict']}], | ||
options:[{strings: false, identifiers: false, skipWords: ['dict']}], | ||
errors: [ | ||
@@ -51,3 +61,3 @@ { message: 'You have a misspelled word: tsih on Comment'}, | ||
code: 'var a = \'liasdfuhn\' // tsih is a comment srting dict', | ||
args:[2, {comments: false, strings: true, skipWords: ['dict']}], | ||
options:[{comments: false, strings: true, skipWords: ['dict']}], | ||
errors: [ | ||
@@ -58,3 +68,3 @@ { message: 'You have a misspelled word: liasdfuhn on String'}] | ||
code: 'var a = 1 // tsih is a comment srting dict', | ||
args:[2, {skipWords: ['dict']}], | ||
options:[{skipWords: ['dict']}], | ||
errors: [ | ||
@@ -66,3 +76,3 @@ { message: 'You have a misspelled word: tsih on Comment'}, | ||
code: 'var url = "http://examplus.com"', | ||
args:[2, {skipWords: ['url']}], | ||
options:[{skipWords: ['url']}], | ||
errors: [ | ||
@@ -79,2 +89,8 @@ { message: 'You have a misspelled word: http on String'}, | ||
{ | ||
code: 'var test12anthing78variable = 1 // This shuldn\'t fail, not the first or the 3rd time ', | ||
errors: [ | ||
{ message: 'You have a misspelled word: test12anthing78variable on Identifier'}, | ||
{ message: 'You have a misspelled word: shuldn\'t on Comment'}] | ||
}, | ||
{ | ||
code: 'var angular = tsihIsATest(of_a_snake_case_srting)', | ||
@@ -93,3 +109,3 @@ errors: [ | ||
code: 'var a = 1 // colour cheque behaviour tsih', | ||
args:[2, {lang: 'en_GB', skipWords: ['dict']}], | ||
options:[{lang: 'en_GB', skipWords: ['dict']}], | ||
errors: [ | ||
@@ -100,3 +116,3 @@ { message: 'You have a misspelled word: tsih on Comment'}] | ||
code: 'var a = 1 // color is a comment behavior dict', | ||
args:[2, {lang: 'en_GB', skipWords: ['dict']}], | ||
options:[{lang: 'en_GB', skipWords: ['dict']}], | ||
errors: [ | ||
@@ -103,0 +119,0 @@ { message: 'You have a misspelled word: color on Comment'}, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1873110
10
15
313
111
54002
+ Addedglobals@9.18.0(transitive)
+ Addedlodash@4.17.21(transitive)
- Removedglobals@6.4.1(transitive)
- Removedlodash@3.10.1(transitive)
Updatedglobals@^9.9.0
Updatedhunspell-spellchecker@^1.0.2
Updatedlodash@^4.14.2