-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding eslint rule for converting toBe(true) to toBeTrue() etc (#…
…2911) * feat: adding eslint rule for converting toBe(true) to toBeTrue() etc * minor * minor * feat: prefer resolveTo and rejectWith instead of returnValue(Promise.resolve()) (#2912) * feat: prefer resolveTo and rejectWith instead of returnValue(Promise.resolve()) * feat: added deepFreeze to not pollute global mock data (#2913) * feat: added deepFreeze to not pollute global mock data * feat: added deepFreeze to not pollute global mock data - Part 2 (#2914) * feat: added deepFreeze to not pollute global mock data - Part 2 * feat: added deepFreeze to not pollute global mock data - Part 3 (#2915) * feat: added deepFreeze to not pollute global mock data - Part 3 * feat: added deepFreeze to not pollute global mock data - Part 4 (#2916) * feat: added deepFreeze to not pollute global mock data - Part 4 * feat: added deepFreeze to not pollute global mock data - Part 5 (#2917) * feat: added deepFreeze to not pollute global mock data - Part 5 * feat: added deepFreeze to not pollute global mock data - Part 6 (#2918) * feat: added deepFreeze to not pollute global mock data - Part 6 * feat: added deepFreeze to not pollute global mock data - Part 7 (#2919) * feat: added deepFreeze to not pollute global mock data - Part 7 * minor * feat: added deepFreeze to not pollute global mock data - Part 8 (#2922) * feat: added deepFreeze to not pollute global mock data - Part 8 * feat: added deepFreeze to not pollute global mock data - Part 9 (#2924) * feat: added deepFreeze to not pollute global mock data - Part 9 * feat: added deepFreeze to not pollute global mock data - Part 10 (#2925) * feat: added deepFreeze to not pollute global mock data - Part 10 * feat: added deepFreeze to not pollute global mock data - Part 11 (#2927) * feat: added deepFreeze to not pollute global mock data - Part 11 * feat: added deepFreeze to not pollute global mock data - Part 12 (#2929) * feat: added deepFreeze to not pollute global mock data - Part 12 * feat: added deepFreeze to not pollute global mock data - Part 13 (#2930) * feat: added deepFreeze to not pollute global mock data - Part 13 * minor * feat: added deepFreeze to not pollute global mock data - Part 14 (#2933) * feat: added deepFreeze to not pollute global mock data - Part 14 * feat: added deepFreeze to not pollute global mock data - Part 15 (#2934) * feat: added deepFreeze to not pollute global mock data - Part 15 * test: fixing tests - part 1 (#2939) * test: fixing tests - part 1 * test: fixing tests - part 2 (#2940) * test: fixing tests - part 2 * test: fixing tests - part 3 (#2941) * test: fixing tests - part 3 * test: fixing tests - part 4 (#2943) * test: fixing tests - part 4 * test: fixing tests - part 5 (#2945) * minor
- Loading branch information
1 parent
630e384
commit ae37934
Showing
263 changed files
with
3,888 additions
and
3,092 deletions.
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.