Skip to content

Commit ef8cfa5

Browse files
committedOct 19, 2024
fix(valid-title): Accept string variables as a valid title
Fixes #295 Fixes #243 Fixes #312 Fixes #320 I give in, I've got enough reports of this from users I'm tired of dealing with it and decided to just support basic semantic analysis. No doubt at some point someone is going to expect it to work with full type information cause they'll have some weird use case where they import some title string function from a util or something dumb like that, but at least this will quell the nonsense for most simple cases.
1 parent 79a9681 commit ef8cfa5

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed
 

‎docs/rules/valid-title.md

+6
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,19 @@ test(123, () => {})
4646
test.describe(String(/.+/), () => {})
4747
test.describe(myFunction, () => {})
4848
test.describe(6, function () {})
49+
50+
const title = 123
51+
test(title, () => {})
4952
```
5053

5154
Examples of **correct** code for this rule:
5255

5356
```javascript
5457
test('is a string', () => {})
5558
test.describe('is a string', () => {})
59+
60+
const title = 'is a string'
61+
test(title, () => {})
5662
```
5763

5864
Examples of **correct** code when `ignoreTypeOfDescribeName` is `true`:

‎src/rules/valid-title.test.ts

+33-15
Original file line numberDiff line numberDiff line change
@@ -607,17 +607,6 @@ runRuleTester('title-must-be-string', rule, {
607607
],
608608
options: [{ ignoreTypeOfTestName: false }],
609609
},
610-
{
611-
code: 'const foo = "my-title"; test(foo, () => {});',
612-
errors: [
613-
{
614-
column: 30,
615-
line: 1,
616-
messageId: 'titleMustBeString',
617-
},
618-
],
619-
options: [{ ignoreTypeOfTestName: false }],
620-
},
621610
{
622611
code: 'test(123, () => {});',
623612
errors: [
@@ -731,6 +720,22 @@ runRuleTester('title-must-be-string', rule, {
731720
],
732721
options: [{ ignoreTypeOfStepName: false }],
733722
},
723+
// Basic semantic analysis
724+
{
725+
code: javascript`
726+
const title = 123;
727+
test(title, () => {
728+
expect(1).toBe(1);
729+
});
730+
`,
731+
errors: [
732+
{
733+
column: 15,
734+
line: 1,
735+
messageId: 'titleMustBeString',
736+
},
737+
],
738+
},
734739
// Global aliases
735740
{
736741
code: 'it(String(/.+/), () => {});',
@@ -768,10 +773,6 @@ runRuleTester('title-must-be-string', rule, {
768773
code: 'test(String(/.+/), () => {});',
769774
options: [{ ignoreTypeOfTestName: true }],
770775
},
771-
{
772-
code: 'const foo = "my-title"; test(foo, () => {});',
773-
options: [{ ignoreTypeOfTestName: true }],
774-
},
775776
{
776777
code: 'test.describe(myFunction, () => {});',
777778
options: [{ ignoreTypeOfDescribeName: true }],
@@ -780,6 +781,23 @@ runRuleTester('title-must-be-string', rule, {
780781
code: 'test.describe(skipFunction, () => {});',
781782
options: [{ disallowedWords: [], ignoreTypeOfDescribeName: true }],
782783
},
784+
// Basic semantic analysis
785+
{
786+
code: 'const foo = "my-title"; test(foo, () => {});',
787+
options: [{ ignoreTypeOfTestName: true }],
788+
},
789+
{
790+
code: 'const foo = "my-title"; test(foo, () => {});',
791+
options: [{ ignoreTypeOfTestName: false }],
792+
},
793+
{
794+
code: javascript`
795+
const title = "is a string";
796+
test(title, () => {
797+
expect(1).toBe(1);
798+
});
799+
`,
800+
},
783801
// Global aliases
784802
{
785803
code: 'it("is a string", () => {});',

‎src/rules/valid-title.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import ESTree from 'estree'
2-
import { getStringValue, isStringNode, StringNode } from '../utils/ast'
2+
import {
3+
dereference,
4+
getStringValue,
5+
isStringNode,
6+
StringNode,
7+
} from '../utils/ast'
38
import { createRule } from '../utils/createRule'
49
import { parseFnCall } from '../utils/parseFnCall'
510

@@ -118,9 +123,12 @@ export default createRule({
118123
return
119124
}
120125

121-
const [argument] = node.arguments
126+
let argument: ESTree.Node = node.arguments[0]
122127
if (!argument) return
123128

129+
// Attempt to dereference the argument if it's a variable
130+
argument = dereference(context, argument) ?? argument
131+
124132
if (!isStringNode(argument)) {
125133
if (
126134
argument.type === 'BinaryExpression' &&

0 commit comments

Comments
 (0)
Please sign in to comment.