-
Notifications
You must be signed in to change notification settings - Fork 15
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: adding eslint rule for converting toBe(true) to toBeTrue() etc #2911
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6eb0a36
feat: adding eslint rule for converting toBe(true) to toBeTrue() etc
suyashpatil78 6552491
Merge branch 'FYLE-86cv5nv19' of github.com:fylein/fyle-mobile-app in…
suyashpatil78 f678d34
minor
suyashpatil78 3e8f3b6
Merge branch 'FYLE-86cv5nv19' of github.com:fylein/fyle-mobile-app in…
suyashpatil78 e306edb
minor
suyashpatil78 a111842
feat: prefer resolveTo and rejectWith instead of returnValue(Promise.…
suyashpatil78 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
eslint-custom-rules/rules/eslint-plugin-prefer-jasmine-matchers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Use toBeNull() instead of toBe(null) or toEqual(null) | ||
// Use toBeUndefined() instead of toBe(undefined) or toEqual(undefined) | ||
// Use toBeTrue() instead of toBe(true) or toEqual(true) | ||
// Use toBeFalse() instead of toBe(false) or toEqual(false) | ||
// Use toBeNaN() instead of toBe(NaN) or toEqual(NaN) | ||
|
||
module.exports = { | ||
meta: { | ||
type: "suggestion", | ||
docs: { | ||
description: "Enforce using toBeTrue(), toBeFalse(), toBeNull(), and toBeUndefined() instead of toBe() or toEqual() with true, false, null, or undefined", | ||
category: "Best Practices", | ||
recommended: true, | ||
}, | ||
fixable: "code", | ||
schema: [], | ||
}, | ||
|
||
create: function (context) { | ||
return { | ||
CallExpression(node) { | ||
const isToBe = node.callee.property && node.callee.property.name === "toBe"; | ||
const isToEqual = node.callee.property && node.callee.property.name === "toEqual"; | ||
|
||
if ((isToBe || isToEqual) && node.arguments.length === 1) { | ||
const argValue = node.arguments[0].value; | ||
if (argValue === true) { | ||
context.report({ | ||
node, | ||
message: "Prefer using toBeTrue() instead of toBe(true) or toEqual(true)", | ||
fix: function (fixer) { | ||
const replacementText = 'toBeTrue()'; | ||
const sourceCode = context.getSourceCode(); | ||
const nodeText = sourceCode.getText(node); | ||
|
||
// Find the correct part of the code to replace | ||
const match = nodeText.match(/\.toBe\((true)\)|\.toEqual\((true)\)/); | ||
if (match) { | ||
const start = node.callee.property.range[0]; | ||
const end = node.callee.property.range[1] + 6; // Adjust end position | ||
return fixer.replaceTextRange([start, end], replacementText); | ||
} | ||
|
||
return null; // No match found, no fix needed | ||
} | ||
}); | ||
} else if (argValue === false) { | ||
context.report({ | ||
node, | ||
message: "Prefer using toBeFalse() instead of toBe(false) or toEqual(false)", | ||
fix: function (fixer) { | ||
const replacementText = 'toBeFalse()'; | ||
const sourceCode = context.getSourceCode(); | ||
const nodeText = sourceCode.getText(node); | ||
|
||
// Find the correct part of the code to replace | ||
const match = nodeText.match(/\.toBe\((false)\)|\.toEqual\((false)\)/); | ||
if (match) { | ||
const start = node.callee.property.range[0]; | ||
const end = node.callee.property.range[1] + 7; // Adjust end position | ||
return fixer.replaceTextRange([start, end], replacementText); | ||
} | ||
|
||
return null; // No match found, no fix needed | ||
} | ||
}); | ||
} else if (argValue === null) { | ||
context.report({ | ||
node, | ||
message: "Prefer using toBeNull() instead of toBe(null) or toEqual(null)", | ||
fix: function (fixer) { | ||
const replacementText = 'toBeNull()'; | ||
const sourceCode = context.getSourceCode(); | ||
const nodeText = sourceCode.getText(node); | ||
|
||
// Find the correct part of the code to replace | ||
const match = nodeText.match(/\.toBe\((null)\)|\.toEqual\((null)\)/); | ||
if (match) { | ||
const start = node.callee.property.range[0]; | ||
const end = node.callee.property.range[1] + 6; // Adjust end position | ||
return fixer.replaceTextRange([start, end], replacementText); | ||
} | ||
|
||
return null; // No match found, no fix needed | ||
} | ||
}); | ||
} else if (argValue === undefined && node.arguments[0].name === 'undefined') { | ||
// Comparing the name of the argument to undefined since every variable is undefined by default in AST | ||
context.report({ | ||
node, | ||
message: "Prefer using toBeUndefined() instead of toBe(undefined) or toEqual(undefined)", | ||
fix: function (fixer) { | ||
const replacementText = 'toBeUndefined()'; | ||
const sourceCode = context.getSourceCode(); | ||
const nodeText = sourceCode.getText(node); | ||
|
||
// Find the correct part of the code to replace | ||
const match = nodeText.match(/\.toBe\((undefined)\)|\.toEqual\((undefined)\)/); | ||
if (match) { | ||
const start = node.callee.property.range[0]; | ||
const end = node.callee.property.range[1] + 11; // Adjust end position | ||
return fixer.replaceTextRange([start, end], replacementText); | ||
} | ||
|
||
return null; // No match found, no fix needed | ||
} | ||
}); | ||
} else if (node.arguments[0].name === 'NaN') { | ||
// Since NaN === NaN returns false, we are comparing the name of the argument to NaN | ||
context.report({ | ||
node, | ||
message: "Prefer using toBeNaN() instead of toBe(NaN) or toEqual(NaN)", | ||
fix: function (fixer) { | ||
const replacementText = 'toBeNaN()'; | ||
const sourceCode = context.getSourceCode(); | ||
const nodeText = sourceCode.getText(node); | ||
|
||
// Find the correct part of the code to replace | ||
const match = nodeText.match(/\.toBe\((NaN)\)|\.toEqual\((NaN)\)/); | ||
if (match) { | ||
const start = node.callee.property.range[0]; | ||
const end = node.callee.property.range[1] + 5; // Adjust end position | ||
return fixer.replaceTextRange([start, end], replacementText); | ||
} | ||
|
||
return null; // No match found, no fix needed | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |
52 changes: 52 additions & 0 deletions
52
eslint-custom-rules/rules/eslint-plugin-prefer-resolve-to-reject-with.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const SpyStrategyCall = `CallExpression:matches( | ||
[callee.object.property.name=and], | ||
[callee.object.callee.property.name=withArgs] | ||
)`.replace(/\s+/g, ' '); | ||
|
||
const ReturnStrategy = `${SpyStrategyCall}[callee.property.name=returnValue]`; | ||
|
||
// Matches Promise.{resolve,reject}(X) | ||
const PromiseCall = 'CallExpression[callee.object.name=Promise]'; | ||
const SettledPromise = `${PromiseCall}[callee.property.name=/resolve|reject/]`; | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: "Enforce using resolveTo() and rejectWith() instead of Promise.resolve() and Promise.reject() in Jasmine tests", | ||
category: "Best Practices", | ||
recommended: true, | ||
}, | ||
fixable: 'code', | ||
schema: [] | ||
}, | ||
|
||
create: context => ({ | ||
[`${ReturnStrategy} > ${SettledPromise}.arguments:first-child`] (promiseCall) { | ||
const returnStrategyCall = promiseCall.parent; | ||
const returnValueMethod = returnStrategyCall.callee.property; | ||
const preferredMethod = promiseCall.callee.property.name === 'resolve' | ||
? 'resolveTo' : 'rejectWith'; | ||
|
||
context.report({ | ||
message: `Prefer ${preferredMethod}`, | ||
loc: { | ||
start: returnValueMethod.loc.start, | ||
end: returnStrategyCall.loc.end | ||
}, | ||
fix (fixer) { | ||
const code = context.getSourceCode(); | ||
return [ | ||
// Replace Promise constructor call with its arguments | ||
fixer.remove(promiseCall.callee), | ||
fixer.remove(code.getTokenAfter(promiseCall.callee)), | ||
fixer.remove(code.getLastToken(promiseCall)), | ||
|
||
// Replace returnValue method with resolveTo or rejectWith | ||
fixer.replaceText(returnValueMethod, preferredMethod) | ||
]; | ||
} | ||
}) | ||
} | ||
}) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't want these rules for *.spec.ts files