Skip to content

Commit

Permalink
fix(getQueryNodeFrom): move Identifier type checks to `getInnerNode…
Browse files Browse the repository at this point in the history
…From` (#255)

Co-authored-by: Michaël De Boey <info@michaeldeboey.be>
Co-authored-by: Gareth Jones <jones258@gmail.com>
  • Loading branch information
3 people committed Jun 4, 2023
1 parent 48fce31 commit 96c364a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
30 changes: 28 additions & 2 deletions src/__tests__/__fixtures__/createBannedAttributeTestCases.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ export default ({ preferred, negatedPreferred, attribute }) => {
],
output: `const el = screen.getByText("foo"); expect(el).${negatedPreferred}`,
},
{
code: `const el = screen.getByRole("button"); expect(el).not.${preferred}`,
errors: [
{
message: `Use ${negatedPreferred} instead of not.${preferred}`,
},
],
output: `const el = screen.getByRole("button"); expect(el).${negatedPreferred}`,
},
]
: [];
const directChecks = /-/.test(attribute)
Expand All @@ -63,13 +72,22 @@ export default ({ preferred, negatedPreferred, attribute }) => {
output: `expect(getByText('foo')).${[negatedPreferred]}`,
},
{
code: `expect(getByText('foo').${attribute}).toBe(true)`,
code: `const el = getByText('foo'); expect(el.${attribute}).toBe(true)`,
errors: [
{
message: `Use ${preferred} instead of checking .${attribute} directly`,
},
],
output: `expect(getByText('foo')).${[preferred]}`,
output: `const el = getByText('foo'); expect(el).${[preferred]}`,
},
{
code: `const el = getByRole('button'); expect(el.${attribute}).toBe(true)`,
errors: [
{
message: `Use ${preferred} instead of checking .${attribute} directly`,
},
],
output: `const el = getByRole('button'); expect(el).${[preferred]}`,
},
];

Expand Down Expand Up @@ -203,6 +221,14 @@ export default ({ preferred, negatedPreferred, attribute }) => {
},
],
},
{
code: `const el = getByRole("button", { name: 'My Button' }); expect(el).toHaveProperty('${attribute}', foo)`,
errors: [
{
message: `Use ${preferred} instead of toHaveProperty('${attribute}', foo)`,
},
],
},
],
};
};
20 changes: 10 additions & 10 deletions src/assignment-ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import { queries } from "./queries";
* await someAsyncFunc() => someAsyncFunc()
* someElement as HTMLDivElement => someElement
*
* @param {Object} context - Context for a rule
* @param {Object} expression - An expression node
* @returns {Object} - A node
*/
export function getInnerNodeFrom(expression) {
export function getInnerNodeFrom(context, expression) {
switch (expression.type) {
case "Identifier":
return getAssignmentForIdentifier(context, expression.name);
case "TSAsExpression":
return getInnerNodeFrom(expression.expression);
return getInnerNodeFrom(context, expression.expression);
case "AwaitExpression":
return getInnerNodeFrom(expression.argument);
return getInnerNodeFrom(context, expression.argument);
case "MemberExpression":
return getInnerNodeFrom(expression.object);
return getInnerNodeFrom(context, expression.object);
default:
return expression;
}
Expand All @@ -37,7 +40,7 @@ export function getAssignmentForIdentifier(context, identifierName) {
let assignmentNode;
if (init) {
// let foo = bar;
assignmentNode = getInnerNodeFrom(init);
assignmentNode = getInnerNodeFrom(context, init);
} else {
// let foo;
// foo = bar;
Expand All @@ -47,7 +50,7 @@ export function getAssignmentForIdentifier(context, identifierName) {
if (!assignmentRef) {
return;
}
assignmentNode = getInnerNodeFrom(assignmentRef.writeExpr);
assignmentNode = getInnerNodeFrom(context, assignmentRef.writeExpr);
}
return assignmentNode;
}
Expand All @@ -61,10 +64,7 @@ export function getAssignmentForIdentifier(context, identifierName) {
* @returns {Object} - Object with query, queryArg & isDTLQuery
*/
export function getQueryNodeFrom(context, nodeWithValueProp) {
const queryNode =
nodeWithValueProp.type === "Identifier"
? getAssignmentForIdentifier(context, nodeWithValueProp.name)
: getInnerNodeFrom(nodeWithValueProp);
const queryNode = getInnerNodeFrom(context, nodeWithValueProp);

if (!queryNode || !queryNode.callee) {
return {
Expand Down

0 comments on commit 96c364a

Please sign in to comment.