-
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 1 commit
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ node_modules/ | |
coverage/ | ||
lib/ | ||
*.log | ||
package-lock.json | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import { | |
} from '@typescript-eslint/experimental-utils'; | ||
import { | ||
DescribeAlias, | ||
TopCase, | ||
JestFunctionCallExpressionWithIdentifierCallee, | ||
JestFunctionName, | ||
TestCaseName, | ||
|
@@ -52,6 +53,32 @@ const testDescription = (argument: ArgumentLiteral): string | null => { | |
return argument.quasis[0].value.raw; | ||
}; | ||
|
||
const jestTopFunctioName = ( | ||
node: CallExpressionWithCorrectCalleeAndArguments, | ||
counter: number, | ||
) => { | ||
const description = testDescription(node.arguments[0]); | ||
if (description === null) { | ||
return null; | ||
} | ||
|
||
if (counter !== 1) { | ||
G-Rath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return null; | ||
} | ||
|
||
const firstCharacter = description.charAt(0); | ||
|
||
if (!firstCharacter) { | ||
gayathrigs27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return null; | ||
} | ||
|
||
if (firstCharacter === firstCharacter.toUpperCase()) { | ||
return node.callee.name; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
const jestFunctionName = ( | ||
node: CallExpressionWithCorrectCalleeAndArguments, | ||
) => { | ||
|
@@ -93,7 +120,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, | ||
}, | ||
}, | ||
|
@@ -103,14 +130,25 @@ export default createRule({ | |
} as const, | ||
defaultOptions: [{ ignore: [] } as { ignore: readonly JestFunctionName[] }], | ||
create(context, [{ ignore }]) { | ||
let counter: number = 0 | ||
return { | ||
|
||
CallExpression(node) { | ||
if (!isJestFunctionWithLiteralArg(node)) { | ||
return; | ||
} | ||
|
||
if (isTestCase || isDescribe) { | ||
gayathrigs27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
counter += 1 | ||
} | ||
|
||
const topMethod = jestTopFunctioName(node, counter); | ||
|
||
const topMethodIgnore = 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. The only value being used in this conditional that is computed as part of the That way we won't be iterating over |
||
|
||
const erroneousMethod = jestFunctionName(node); | ||
|
||
if (erroneousMethod && !ignore.includes(node.callee.name)) { | ||
if (erroneousMethod && !ignore.includes(node.callee.name) && topMethodIgnore!==true) { | ||
context.report({ | ||
messageId: 'unexpectedLowercase', | ||
data: { method: erroneousMethod }, | ||
|
@@ -137,4 +175,4 @@ export default createRule({ | |
}, | ||
}; | ||
}, | ||
}); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.