Skip to content

Commit

Permalink
Fixes false positives from object prototypes
Browse files Browse the repository at this point in the history
  • Loading branch information
mattorchard committed Jun 7, 2022
1 parent ea8491c commit 6b21166
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 0.4.2
- Fixes false positives from object prototype fields

## 0.4.1
- Readme and copy updates

Expand Down
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ function warnOnce() {
}

function createExpectedCountMap(countMapOrNameList) {
if (!Array.isArray(countMapOrNameList)) {
return countMapOrNameList;
}
const expectedCountMap = {};
for (const functionName of countMapOrNameList) {
expectedCountMap[functionName] = 1;
const expectedCountMap = new Map();
if (Array.isArray(countMapOrNameList)) {
countMapOrNameList.forEach(functionName =>
expectedCountMap.set(functionName, 1)
);
} else {
Object.entries(countMapOrNameList).forEach(([key, value]) =>
expectedCountMap.set(key, value)
);
}
return expectedCountMap;
}
Expand Down Expand Up @@ -61,8 +64,9 @@ function getCalleeNames(node) {

function assertThatNodeHasExpectedGenerics({ context, expectedCountMap, node, nodeType }) {
const possibleNames = getCalleeNames(node);
const matchingRuleName = possibleNames.find(possibleName => expectedCountMap[possibleName]);
const expectedCount = expectedCountMap[matchingRuleName];
const matchingRuleName = possibleNames.find(possibleName => expectedCountMap.has(possibleName));
if (!matchingRuleName) return;
const expectedCount = expectedCountMap.get(matchingRuleName);
if (!expectedCount) return;

const actualCount = getParamsLength(node.typeParameters) || getParamsLength(node.typeArguments) || 0;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "eslint-plugin-require-explicit-generics",
"description": "Force configured functions to include explicit generics",
"version": "0.4.1",
"version": "0.4.2",
"main": "index.js",
"homepage": "https://github.com/mattorchard/eslint-plugin-require-explicit-generics",
"repository": {
Expand Down

0 comments on commit 6b21166

Please sign in to comment.