Skip to content

Commit

Permalink
add failed tests for MemberExpression (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedBaset authored Sep 27, 2024
2 parents 0dc4f27 + 0bb4595 commit 0fee186
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"eslint-plugin-eslint-plugin": "^6.2.0",
"tailwindcss": "~3.3.3",
"tsup": "^8.2.4",
"typescript": "^5.5.4",
"typescript": "^5.6.2",
"typescript-eslint": "^8.0.0",
"vitest": "^2.0.5"
},
Expand Down
80 changes: 40 additions & 40 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 81 additions & 5 deletions src/rules/no-phyisical-properties/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,20 +319,55 @@ vitest.describe(RULE_NAME, () => {
],
},
{
name: "Outside the scope",
skip: true,
name: "MemberExpression",
code: `
const cls = "left-2";
function Comp() {
return <div className={cls} />
const styles = { main: "left-2" };
return <div className={styles.main} />
}
`,
errors: [{ messageId: IDENTIFIER_USED }],
output: `
const cls = "start-2";
function Comp() {
return <div className={cls} />
const styles = { main: "start-2" };
return <div className={styles.main} />
}
`,
},
{
skip: true,
name: "MemberExpression with computed property",
code: `
const styles = { main: "left-2" };
function Comp() {
return <div className={styles[main]} />
}
`,
errors: [{ messageId: IDENTIFIER_USED }],
output: `
const styles = { main: "start-2" };
function Comp() {
return <div className={styles[main]} />
}
`,
},
{
skip: true,
name: "MemberExpression deeply",
code: `
const styles = { main: { title: "left-2" } };
function Comp() {
return <div className={styles.main.title} />
}
`,
errors: [{ messageId: IDENTIFIER_USED }],
output: `
const styles = { main: { title: "start-2" } };
function Comp() {
return <div className={styles.main.title} />
}
`,
},
{
name: "Reassignment in a nested scope",
Expand All @@ -355,6 +390,47 @@ vitest.describe(RULE_NAME, () => {
{ messageId: IDENTIFIER_USED },
],
},
{
skip: true,
name: "Reassignment object in a nested scope",
code: `
let styles = { main: "left-2" };
function Comp() {
styles = { main: "text-left", hey: "pl-2" };
return <div className={styles.main} />
}
`,
output: `
let styles = { main: "start-2" };
function Comp() {
styles = { main: "text-start", hey: "pl-2" };
return <div className={styles.main} />
}
`,
errors: [
{ messageId: IDENTIFIER_USED },
{ messageId: IDENTIFIER_USED },
],
},
{
skip: true,
name: "Reassignment object property in a nested scope",
code: `
const styles = { main: "left-2" };
function Comp() {
styles.main = "text-left";
return <div className={styles.main} />
}
`,
output: `
const styles = { main: "start-2" };
function Comp() {
styles.main = "text-start";
return <div className={styles.main} />
}
`,
errors: [{ messageId: IDENTIFIER_USED }],
},
{
name: "Don't conflict with other vars with the same name",
code: `
Expand Down
20 changes: 20 additions & 0 deletions src/utils/ast.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// the ast.ts exports are tested in `test.ts` but this file is only to 100% coverage

import { describe, expect, it } from "vitest";
import { extractTokensFromNode } from "./ast.js";
import type { Context } from "../rules/no-phyisical-properties/rule.js";
import type { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";

describe("ast", () => {
it("non JSXAttribute", () => {
expect(
extractTokensFromNode(
{
type: "VariableDeclaration" as AST_NODE_TYPES.VariableDeclaration,
} as TSESTree.Node,
{} as Context,
"fixer"
)
).toEqual([]);
});
});
28 changes: 14 additions & 14 deletions src/utils/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export function extractTokensFromNode(
ctx: Context,
runner: "checker" | "fixer"
): Token[] {
const run = (exp: Exp) => extractTokensFromExpression(exp, ctx, runner);

if (node.type === "JSXAttribute") {
// value: Literal | JSXExpressionContainer | JSXElement | JSXFragment | null
const value = node.value;
Expand All @@ -45,17 +43,18 @@ export function extractTokensFromNode(
return extractTokensFromExpression(expression, ctx, runner);
}

if (value.type === "JSXElement" || value.type === "JSXSpreadChild") {
return [];
}
// if (value.type === "JSXElement" || value.type === "JSXSpreadChild") {
// return [];
// }

return [];
// return [];
}

if (is(node, "VariableDeclarator")) {
if (!node.init) return [];
return run(node.init);
}
// Handled somewhere else > find the call of `getDefinitions`
// if (is(node, "VariableDeclarator")) {
// if (!node.init) return [];
// return run(node.init);
// }

// if (is(node, "ArrowFunctionExpression")) return run(node);

Expand Down Expand Up @@ -170,14 +169,15 @@ function extractTokensFromExpression(

const writes = getDefinitions(exp, ctx, scope).filter(
(r) => r?.type === "Literal" || r?.type === "Identifier"
// || r?.type === "ObjectExpression" ||
// r?.type === "AssignmentExpression"
);

return writes.flatMap((n) => rerun(n, true));
}

if (is(exp, "MemberExpression")) {
return [];
}
// if (is(exp, "MemberExpression") && is(exp.property, "Identifier")) {
// return [];
// }

/*
if (is(exp, "ArrowFunctionExpression")) {
Expand Down
Loading

0 comments on commit 0fee186

Please sign in to comment.