eslint-plugin-mongodb
Advanced tools
Comparing version 0.0.3 to 0.1.0
{ | ||
"name": "eslint-plugin-mongodb", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Lint your MongoDB queries.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -6,4 +6,4 @@ # eslint-plugin-mongodb | ||
** Disclaimer ** | ||
This is a work in progress. Use it only if you whish to be involved in this | ||
**Disclaimer:** | ||
This is a work in progress. Use it only if you wish to be involved in this | ||
project evolution by reporting bugs or even sending PRs. | ||
@@ -13,3 +13,3 @@ | ||
# Usage | ||
## Usage | ||
@@ -45,6 +45,38 @@ 1. Install `eslint` as a dev-dependency: | ||
# Rules | ||
## Settings | ||
## no-replace | ||
In order to recognize MongoDB native driver queries, this plugin check for | ||
function calls. By using [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings) | ||
you can specify your own patterns, here are the defaults: | ||
```json | ||
{ | ||
"settings": { | ||
"mongodb": { | ||
"callPatterns": { | ||
"query": [ | ||
"(\\.|^)db\\.collection\\([^\\)]+\\)\\.(find|findOne|)", | ||
], | ||
"update": [ | ||
"(\\.|^)db\\.collection\\([^\\)]+\\)\\.(update|findAndModify)", | ||
], | ||
"insert": [ | ||
"(\\.|^)db\\.collection\\([^\\)]+\\)\\.insert", | ||
], | ||
"remove": [ | ||
"(\\.|^)db\\.collection\\([^\\)]+\\)\\.remove", | ||
], | ||
} | ||
} | ||
} | ||
}) | ||
``` | ||
Note that the above are strings representing regular expressions. It will be | ||
cast with the `RegExp` constructor so you have to escape your escapes ;). | ||
## Rules | ||
### no-replace | ||
Default: `'no_replace': 1` | ||
@@ -54,3 +86,3 @@ | ||
## check-numeric-updates | ||
### check-numeric-updates | ||
@@ -62,3 +94,3 @@ Default: `'check-numeric-updates': 2` | ||
## check-updates-calls | ||
### check-updates-calls | ||
@@ -69,3 +101,3 @@ Default: `'check-updates-calls': 2` | ||
## check-rename-updates | ||
### check-rename-updates | ||
@@ -76,3 +108,3 @@ Default: `'check-rename-updates': 2` | ||
## check-unset-updates | ||
### check-unset-updates | ||
@@ -83,3 +115,3 @@ Default: `'check-unset-updates': 2` | ||
## check-current-date-updates | ||
### check-current-date-updates | ||
@@ -90,4 +122,13 @@ Default: `'check-current-date-updates': 2` | ||
## How to create a new rule | ||
### check-minmax-updates | ||
Default: `'check-minmax-updates': 2` | ||
Check `$min` and `$max` update operator usage. | ||
## Contributing | ||
Feel free to push your code if you agree with publishing under the MIT license. | ||
### How to create a new rule | ||
Avoid wasting your time and follow those steps to suggest a new rule: | ||
@@ -100,7 +141,8 @@ - create and issue prefixed by [rule] and followed by it's name | ||
## Contributing | ||
Feel free to push your code if you agree with publishing under the MIT license. | ||
### Changing a specific rule behavior | ||
Create and issue prefixed by [rule] and let us know what should change. | ||
## Stats | ||
[![NPM](https://nodei.co/npm/eslint-plugin-mongodb.png?downloads=true&stars=true)](https://nodei.co/npm/eslint-plugin-mongodb/) | ||
[![NPM](https://nodei.co/npm-dl/eslint-plugin-mongodb.png)](https://nodei.co/npm/eslint-plugin-mongodb/) |
@@ -11,2 +11,3 @@ 'use strict'; | ||
'check-current-date-updates': require('./lib/rules/check-current-date-updates'), | ||
'check-minmax-updates': require('./lib/rules/check-minmax-updates'), | ||
}, | ||
@@ -20,3 +21,4 @@ rulesConfig: { | ||
'check-current-date-updates': 2, | ||
'check-minmax-updates': 2, | ||
}, | ||
}; |
@@ -7,3 +7,3 @@ 'use strict'; | ||
return utils.lookupCall(context, utils.CALL_PATTERNS.UPDATE, | ||
return utils.lookupCall(context, utils.getCallPattern('update', context.settings), | ||
function(callSource, args) { | ||
@@ -10,0 +10,0 @@ if((!args[1]) || 'ObjectExpression' !== args[1].type || |
@@ -10,4 +10,12 @@ 'use strict'; | ||
ruleTester.run('check-current-date-updates', rule, { | ||
valid: [ | ||
"db.collection('users').update({}, { $currentDate: true });", | ||
valid: [{ | ||
code: "userCollection.update({}, { $currentDate: true });", | ||
settings: { | ||
mongodb: { | ||
callPatterns: { | ||
update: ['^(user|place|session)Collection\\.(update|findAndModify)$'], | ||
} | ||
}, | ||
}, | ||
}, | ||
"mongoClient.db.collection('users').update({}, { $currentDate: { $type: 'timestamp' } });", | ||
@@ -18,3 +26,10 @@ "mongoClient.db.collection('users').update({}, { $currentDate: { $type: 'date' } });", | ||
invalid: [{ | ||
code: "db.collection('users').update({}, { $currentDate: 'true' });", | ||
code: "userCollection.update({}, { $currentDate: 'true' });", | ||
settings: { | ||
mongodb: { | ||
callPatterns: { | ||
update: ['^(user|place|session)Collection\\.(update|findAndModify)$'], | ||
} | ||
}, | ||
}, | ||
errors: [{ | ||
@@ -21,0 +36,0 @@ message: 'Expected $currentDate operator value to be a boolean or an object.', |
@@ -9,14 +9,15 @@ 'use strict'; | ||
QUERY: [ | ||
/(\.|^)db\.collection\([^\)]+\)\.(find|findOne|)/, | ||
/(\.|^)db\.collection\([^\)]+\)\.(find|findOne|)$/, | ||
], | ||
UPDATE: [ | ||
/(\.|^)db\.collection\([^\)]+\)\.(update|findAndModify)/, | ||
/(\.|^)db\.collection\([^\)]+\)\.(update|findAndModify)$/, | ||
], | ||
INSERT: [ | ||
/(\.|^)db\.collection\([^\)]+\)\.insert/, | ||
/(\.|^)db\.collection\([^\)]+\)\.insert$/, | ||
], | ||
REMOVE: [ | ||
/(\.|^)db\.collection\([^\)]+\)\.remove/, | ||
/(\.|^)db\.collection\([^\)]+\)\.remove$/, | ||
], | ||
}, | ||
getCallPattern: getCallPattern, | ||
nodeIsDynamic: nodeIsDynamic, | ||
@@ -30,4 +31,17 @@ nodeWillBeString: nodeWillBeString, | ||
nodeIsString: nodeIsString, | ||
nodeIsTrue: nodeIsTrue, | ||
}; | ||
function getCallPattern(type, settings) { | ||
if( | ||
settings && settings.mongodb && settings.mongodb.callPatterns && | ||
settings.mongodb.callPatterns[type] | ||
) { | ||
return settings.mongodb.callPatterns[type].map(function(pattern) { | ||
return new RegExp(pattern); | ||
}); | ||
} | ||
return utils.CALL_PATTERNS[type.toUpperCase()]; | ||
} | ||
function lookupCall(context, callPatterns, cb) { | ||
@@ -34,0 +48,0 @@ return { |
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
30702
22
567
139