Skip to content

Commit 96c364a

Browse files
ricardozv28MichaelDeBoeyG-Rath
authored
fix(getQueryNodeFrom): move Identifier type checks to getInnerNodeFrom (#255)
Co-authored-by: Michaël De Boey <[email protected]> Co-authored-by: Gareth Jones <[email protected]>
1 parent 48fce31 commit 96c364a

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

src/__tests__/__fixtures__/createBannedAttributeTestCases.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ export default ({ preferred, negatedPreferred, attribute }) => {
3939
],
4040
output: `const el = screen.getByText("foo"); expect(el).${negatedPreferred}`,
4141
},
42+
{
43+
code: `const el = screen.getByRole("button"); expect(el).not.${preferred}`,
44+
errors: [
45+
{
46+
message: `Use ${negatedPreferred} instead of not.${preferred}`,
47+
},
48+
],
49+
output: `const el = screen.getByRole("button"); expect(el).${negatedPreferred}`,
50+
},
4251
]
4352
: [];
4453
const directChecks = /-/.test(attribute)
@@ -63,13 +72,22 @@ export default ({ preferred, negatedPreferred, attribute }) => {
6372
output: `expect(getByText('foo')).${[negatedPreferred]}`,
6473
},
6574
{
66-
code: `expect(getByText('foo').${attribute}).toBe(true)`,
75+
code: `const el = getByText('foo'); expect(el.${attribute}).toBe(true)`,
6776
errors: [
6877
{
6978
message: `Use ${preferred} instead of checking .${attribute} directly`,
7079
},
7180
],
72-
output: `expect(getByText('foo')).${[preferred]}`,
81+
output: `const el = getByText('foo'); expect(el).${[preferred]}`,
82+
},
83+
{
84+
code: `const el = getByRole('button'); expect(el.${attribute}).toBe(true)`,
85+
errors: [
86+
{
87+
message: `Use ${preferred} instead of checking .${attribute} directly`,
88+
},
89+
],
90+
output: `const el = getByRole('button'); expect(el).${[preferred]}`,
7391
},
7492
];
7593

@@ -203,6 +221,14 @@ export default ({ preferred, negatedPreferred, attribute }) => {
203221
},
204222
],
205223
},
224+
{
225+
code: `const el = getByRole("button", { name: 'My Button' }); expect(el).toHaveProperty('${attribute}', foo)`,
226+
errors: [
227+
{
228+
message: `Use ${preferred} instead of toHaveProperty('${attribute}', foo)`,
229+
},
230+
],
231+
},
206232
],
207233
};
208234
};

src/assignment-ast.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ import { queries } from "./queries";
55
* await someAsyncFunc() => someAsyncFunc()
66
* someElement as HTMLDivElement => someElement
77
*
8+
* @param {Object} context - Context for a rule
89
* @param {Object} expression - An expression node
910
* @returns {Object} - A node
1011
*/
11-
export function getInnerNodeFrom(expression) {
12+
export function getInnerNodeFrom(context, expression) {
1213
switch (expression.type) {
14+
case "Identifier":
15+
return getAssignmentForIdentifier(context, expression.name);
1316
case "TSAsExpression":
14-
return getInnerNodeFrom(expression.expression);
17+
return getInnerNodeFrom(context, expression.expression);
1518
case "AwaitExpression":
16-
return getInnerNodeFrom(expression.argument);
19+
return getInnerNodeFrom(context, expression.argument);
1720
case "MemberExpression":
18-
return getInnerNodeFrom(expression.object);
21+
return getInnerNodeFrom(context, expression.object);
1922
default:
2023
return expression;
2124
}
@@ -37,7 +40,7 @@ export function getAssignmentForIdentifier(context, identifierName) {
3740
let assignmentNode;
3841
if (init) {
3942
// let foo = bar;
40-
assignmentNode = getInnerNodeFrom(init);
43+
assignmentNode = getInnerNodeFrom(context, init);
4144
} else {
4245
// let foo;
4346
// foo = bar;
@@ -47,7 +50,7 @@ export function getAssignmentForIdentifier(context, identifierName) {
4750
if (!assignmentRef) {
4851
return;
4952
}
50-
assignmentNode = getInnerNodeFrom(assignmentRef.writeExpr);
53+
assignmentNode = getInnerNodeFrom(context, assignmentRef.writeExpr);
5154
}
5255
return assignmentNode;
5356
}
@@ -61,10 +64,7 @@ export function getAssignmentForIdentifier(context, identifierName) {
6164
* @returns {Object} - Object with query, queryArg & isDTLQuery
6265
*/
6366
export function getQueryNodeFrom(context, nodeWithValueProp) {
64-
const queryNode =
65-
nodeWithValueProp.type === "Identifier"
66-
? getAssignmentForIdentifier(context, nodeWithValueProp.name)
67-
: getInnerNodeFrom(nodeWithValueProp);
67+
const queryNode = getInnerNodeFrom(context, nodeWithValueProp);
6868

6969
if (!queryNode || !queryNode.callee) {
7070
return {

0 commit comments

Comments
 (0)