Skip to content

Commit

Permalink
fix(no-use-context): removing imports should also remove the associat… (
Browse files Browse the repository at this point in the history
  • Loading branch information
Rel1cx authored Jan 28, 2025
1 parent 31aa77f commit c3b1797
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 412 deletions.
2 changes: 1 addition & 1 deletion examples/dual-react-dom-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"prepare": "pnpm run build"
},
"devDependencies": {
"@eslint-react/eslint-plugin": "^1.25.0",
"@eslint-react/eslint-plugin": "workspace:*",
"@eslint/js": "^9.19.0",
"@tsconfig/node22": "^22.0.0",
"@tsconfig/strictest": "^2.0.5",
Expand Down
2 changes: 1 addition & 1 deletion examples/next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"react-dom": "^19.0.0"
},
"devDependencies": {
"@eslint-react/eslint-plugin": "^1.25.0",
"@eslint-react/eslint-plugin": "workspace:*",
"@eslint/config-inspector": "^1.0.0",
"@eslint/js": "^9.19.0",
"@next/eslint-plugin-next": "^15.1.6",
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-react-dom-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"react-dom": "^19.0.0"
},
"devDependencies": {
"@eslint-react/eslint-plugin": "^1.25.0",
"@eslint-react/eslint-plugin": "workspace:*",
"@eslint/config-inspector": "^1.0.0",
"@eslint/js": "^9.19.0",
"@tsconfig/node22": "^22.0.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-react-dom-js-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"react-dom": "^19.0.0"
},
"devDependencies": {
"@eslint-react/eslint-plugin": "^1.25.0",
"@eslint-react/eslint-plugin": "workspace:*",
"@eslint/config-inspector": "^1.0.0",
"@eslint/js": "^9.19.0",
"@types/react": "^19.0.8",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@babel/eslint-parser": "^7.26.5",
"@babel/preset-env": "^7.26.7",
"@babel/preset-react": "^7.26.3",
"@eslint-react/eslint-plugin": "^1.25.0",
"@eslint-react/eslint-plugin": "workspace:*",
"@eslint/config-inspector": "^1.0.0",
"@eslint/js": "^9.19.0",
"@types/babel__core": "~7.20.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"react-dom": "^19.0.0"
},
"devDependencies": {
"@eslint-react/eslint-plugin": "^1.25.0",
"@eslint-react/eslint-plugin": "workspace:*",
"@eslint/config-inspector": "^1.0.0",
"@eslint/js": "^9.19.0",
"@tsconfig/node22": "^22.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ruleTester.run(RULE_NAME, rule, {
{ messageId: "noUseContext" },
],
output: /* tsx */ `
import { use, } from 'react'
import { use } from 'react'
export const Component = () => {
const value = use(MyContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { compare } from "compare-versions";
import type { CamelCase } from "string-ts";
import { isMatching } from "ts-pattern";

import { createRule } from "../utils";
import { createRule, getAssociatedTokens } from "../utils";

export const RULE_NAME = "no-use-context";

Expand Down Expand Up @@ -86,7 +86,13 @@ export default createRule<[], MessageID>({
node: specifier,
fix(fixer) {
if (isUseImported) {
return fixer.replaceText(specifier, " ".repeat(specifier.range[1] - specifier.range[0]));
return [
fixer.remove(specifier),
...getAssociatedTokens(
context,
specifier,
).map((token) => fixer.remove(token)),
];
}
return fixer.replaceText(specifier.imported, "use");
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { RuleContext } from "@eslint-react/shared";
import type { TSESTree } from "@typescript-eslint/types";

export function getAssociatedTokens(
context: RuleContext,
node: TSESTree.Node,
) {
{
const tokenBefore = context.sourceCode.getTokenBefore(node);
const tokenAfter = context.sourceCode.getTokenAfter(node);
const tokens = [];

// If this is not the only entry, then the line above this one
// will become the last line, and should not have a trailing comma.
if (tokenAfter?.value !== "," && tokenBefore?.value === ",") {
tokens.push(tokenBefore);
}

// If this is not the last entry, then we need to remove the comma from this line.
if (tokenAfter?.value === ",") {
tokens.push(tokenAfter);
}

return tokens;
}
}
1 change: 1 addition & 0 deletions packages/plugins/eslint-plugin-react-x/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./create-rule";
export * from "./get-associated-tokens";
Loading

0 comments on commit c3b1797

Please sign in to comment.