Skip to content

Mugambi-Ian/mugambi-mongodb-eslint-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mugambi mongodb eslint plugin

Installation

To install run the following command in your project root

npm i https://github.com/Mugambi-Ian/mugambi-mongodb-eslint-plugin

and add the following lines to your .eslintrc.json file

 "plugins": ["@mugambi-mongodb",...],
  "rules": {
    "@mugambi-mongodb/check-wrong-comparison": "error"

Logic

  • This rule(s) were written to help prevent the comparing of two ObjectId classes using the === operator.

  • This rule(s) have been tested for assignment expressions and if statements.

Example (TS)

import {ObjectId} from 'mongodb';

type test = ObjectId | false | null;
let x: test = new ObjectId();
let y: test = new ObjectId();
let a = false;

a = x === y; //trigger error
if (x === y) {//trigger error
  console.log('error');
}

a = x.equals(y);
if (x.equals(y)) {
  console.log('safe');
}

x = null;

a = x === y; //trigger error
if (x === y) { //trigger error
  console.log('error');
}

x = new ObjectId();
y = false;

a = x === y; //trigger error
if (x === y) {//trigger error
  console.log('error');
}

y = x;

a = x === y; //trigger error
if (x === y) {//trigger error
  console.log('error');
}

a = x.equals(y);
if (x.equals(y)) {
  console.log('safe');
}

console.log(a);

Known Issues

Currently, the rule will be enforced for variables initialized from a class with the name ObjectId.

The targeted behavior would be to ensure that only mongoDb ObjectId variables would enforce this.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published