-
Notifications
You must be signed in to change notification settings - Fork 241
feat(rules): option for rule to allow uppercase for top level describes #428
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
Changes from all commits
d70e3b7
55564fc
803a485
0808838
81caecb
ed10ea8
44f1429
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,11 @@ import { | |
JestFunctionCallExpressionWithIdentifierCallee, | ||
JestFunctionName, | ||
TestCaseName, | ||
TopCase, | ||
createRule, | ||
isDescribe, | ||
isTestCase, | ||
isTopMostJestFunction, | ||
} from './utils'; | ||
|
||
type ArgumentLiteral = TSESTree.Literal | TSESTree.TemplateLiteral; | ||
|
@@ -52,6 +54,27 @@ const testDescription = (argument: ArgumentLiteral): string | null => { | |
return argument.quasis[0].value.raw; | ||
}; | ||
|
||
const jestTopFunctioName = ( | ||
node: CallExpressionWithCorrectCalleeAndArguments, | ||
isTop: boolean, | ||
) => { | ||
const description = testDescription(node.arguments[0]); | ||
if (isTop !== true) { | ||
return null; | ||
} | ||
|
||
if (description === null) { | ||
return null; | ||
} | ||
const firstCharacter = description.charAt(0); | ||
|
||
if (firstCharacter === firstCharacter.toUpperCase()) { | ||
return node.callee.name; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
const jestFunctionName = ( | ||
node: CallExpressionWithCorrectCalleeAndArguments, | ||
allowedPrefixes: readonly string[], | ||
|
@@ -105,7 +128,7 @@ export default createRule< | |
properties: { | ||
ignore: { | ||
type: 'array', | ||
items: { enum: ['describe', 'test', 'it'] }, | ||
items: { enum: ['describe', 'test', 'it', 'top'] }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if you're going to do it this way, it would be better to use a boolean option since That would also be a good position to later extend it to be an array accepting the same properties as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My understanding is that if I gave ignore=top, the lowercase rule will not apply to the first parent element(describe/test/it) the inner describe/test should have this rule.
will be a valid one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to be clear, this is the sort of early return I had in mind:
Since |
||
additionalItems: false, | ||
}, | ||
allowedPrefixes: { | ||
|
@@ -118,13 +141,20 @@ export default createRule< | |
}, | ||
], | ||
} as const, | ||
|
||
defaultOptions: [{ ignore: [], allowedPrefixes: [] }], | ||
create(context, [{ ignore = [], allowedPrefixes = [] }]) { | ||
return { | ||
CallExpression(node) { | ||
if (!isJestFunctionWithLiteralArg(node)) { | ||
return; | ||
} | ||
|
||
const topMethod = jestTopFunctioName(node, isTopMostJestFunction(node)); | ||
if (topMethod && ignore.includes(TopCase.top)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return; | ||
} | ||
|
||
const erroneousMethod = jestFunctionName(node, allowedPrefixes); | ||
|
||
if (erroneousMethod && !ignore.includes(node.callee.name)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -550,7 +550,15 @@ export enum TestCaseProperty { | |
'todo' = 'todo', | ||
} | ||
|
||
export type JestFunctionName = DescribeAlias | TestCaseName | HookName; | ||
export enum TopCase { | ||
'top' = 'top', | ||
} | ||
|
||
export type JestFunctionName = | ||
| DescribeAlias | ||
| TestCaseName | ||
| HookName | ||
| TopCase; | ||
|
||
export interface JestFunctionIdentifier<FunctionName extends JestFunctionName> | ||
extends TSESTree.Identifier { | ||
|
@@ -652,6 +660,13 @@ export const isDescribe = ( | |
); | ||
}; | ||
|
||
export function isTopMostJestFunction(node: TSESTree.CallExpression): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer that this be inside the rule, rather than in For now, it'll just be cleaner to have it in the rule :) |
||
if (node.parent && node.parent.parent && node.parent.parent.parent === null) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
const collectReferences = (scope: TSESLint.Scope.Scope) => { | ||
const locals = new Set(); | ||
const unresolved = new Set(); | ||
|
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.
Why do we need
build
?