Skip to content

Commit

Permalink
feat(prefer-lowercase-title): introduce lowercaseFirstCharacterOnly o…
Browse files Browse the repository at this point in the history
…ption (#190)
  • Loading branch information
so1ve committed May 23, 2023
1 parent d7c1266 commit a234637
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
6 changes: 5 additions & 1 deletion docs/rules/prefer-lowercase-title.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Examples of **incorrect** code for this rule:

```js
test('IT WORKS', () => {
test('It works', () => {
// ...
})
```
Expand Down Expand Up @@ -52,6 +52,10 @@ test('it works', () => {
"ignoreTopLevelDescribe":{
"type":"boolean",
"default":false
},
"lowercaseFirstCharacterOnly":{
"type":"boolean",
"default":true
}
},
"additionalProperties":false
Expand Down
15 changes: 15 additions & 0 deletions src/rules/prefer-lowercase-title.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ describe(RULE_NAME, () => {
}
}
]
},
{
code: 'test(`SFC Compile`, function () {})',
output: 'test(`sfc compile`, function () {})',
errors: [
{
messageId: 'lowerCaseTitle',
data: {
method: TestCaseName.test
}
}
],
options: [{
lowercaseFirstCharacterOnly: false
}]
}
]
})
Expand Down
22 changes: 15 additions & 7 deletions src/rules/prefer-lowercase-title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default createEslintRule<[
ignore: string[];
allowedPrefixes: string[];
ignoreTopLevelDescribe: boolean;
lowercaseFirstCharacterOnly: boolean;
}>
], MessageIds>({
name: RULE_NAME,
Expand Down Expand Up @@ -77,16 +78,20 @@ export default createEslintRule<[
ignoreTopLevelDescribe: {
type: 'boolean',
default: false
},
lowercaseFirstCharacterOnly: {
type: 'boolean',
default: true
}
},
additionalProperties: false
}
]
},
defaultOptions: [
{ ignore: [], allowedPrefixes: [], ignoreTopLevelDescribe: false }
{ ignore: [], allowedPrefixes: [], ignoreTopLevelDescribe: false, lowercaseFirstCharacterOnly: true }
],
create: (context, [{ ignore = [], allowedPrefixes = [], ignoreTopLevelDescribe = [] }]) => {
create: (context, [{ ignore = [], allowedPrefixes = [], ignoreTopLevelDescribe = false, lowercaseFirstCharacterOnly = false }]) => {
const ignores = populateIgnores(ignore)
let numberOfDescribeBlocks = 0

Expand Down Expand Up @@ -117,10 +122,11 @@ export default createEslintRule<[

const firstCharacter = description.charAt(0)

if (!firstCharacter ||
firstCharacter === firstCharacter.toLowerCase() ||
ignores.includes(vitestFnCall.name as IgnorableFunctionExpressions))
return
if (
ignores.includes(vitestFnCall.name as IgnorableFunctionExpressions) ||
(lowercaseFirstCharacterOnly && (!firstCharacter || firstCharacter === firstCharacter.toLowerCase())) ||
(!lowercaseFirstCharacterOnly && description === description.toLowerCase())
) return

context.report({
messageId: 'lowerCaseTitle',
Expand All @@ -138,7 +144,9 @@ export default createEslintRule<[
firstArgument.range[1] - 1
]

const newDescription = description.substring(0, 1).toLowerCase() + description.substring(1)
const newDescription = lowercaseFirstCharacterOnly
? description.substring(0, 1).toLowerCase() + description.substring(1)
: description.toLowerCase()

return [fixer.replaceTextRange(rangeIgnoreQuotes, newDescription)]
}
Expand Down

0 comments on commit a234637

Please sign in to comment.