Skip to content

Commit

Permalink
Fixed false positive issue and updated the dependencies (#10)
Browse files Browse the repository at this point in the history
* updated packages and fixed issues caused by updates.

* prettier changes

* Removed unneeded peer dependency.

* updated prettier settings

Co-authored-by: Derek Batenhorst <derek.batenhorst@buildertrend.com>
  • Loading branch information
daryl0313 and Derek Batenhorst authored Jan 24, 2022
1 parent 6df38fb commit 0f2d1e0
Show file tree
Hide file tree
Showing 13 changed files with 540 additions and 173 deletions.
5 changes: 4 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
{}
{
"tabWidth": 2,
"endOfLine": "auto"
}
3 changes: 2 additions & 1 deletion eslint-plugin-enterprise-extras.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
],
"settings": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
"editor.formatOnSave": true,
"editor.tabSize": 2
}
}
601 changes: 479 additions & 122 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"name": "@buildertrend/eslint-plugin-enterprise-extras",
"description": "Extra eslint rules for enterprise environments focusing on React and Typescript",
"version": "2.0.1",
"version": "3.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"/dist"
],
"dependencies": {
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"eslint": "^7.9.0"
"@typescript-eslint/eslint-plugin": "^5.10.0",
"@typescript-eslint/parser": "^5.10.0",
"eslint": "^7.32.0"
},
"devDependencies": {
"@types/node": "14.14.35",
"jest": "26.6.3",
"prettier": "2.3.1",
"rimraf": "3.0.2",
"ts-jest": "26.5.4",
"typescript": "4.0.6"
"typescript": "4.4.4"
},
"peerDependencies": {
"eslint": "^7"
Expand Down
9 changes: 6 additions & 3 deletions src/rules/no-href-assignment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ESLintUtils, TSESTree } from "@typescript-eslint/experimental-utils";
import { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
import { Declaration, Type } from "typescript";

type MessageIds = "avoidHref";
Expand All @@ -14,7 +14,11 @@ type Options = [];
*/
const getParentObjectOfMemberExpression = (
memberExp: TSESTree.MemberExpression
): TSESTree.Expression | TSESTree.Identifier | undefined => {
):
| TSESTree.Expression
| TSESTree.Identifier
| TSESTree.PrivateIdentifier
| undefined => {
const memberExpObj = memberExp.object;

if (memberExpObj.type === "MemberExpression") {
Expand Down Expand Up @@ -50,7 +54,6 @@ export default ESLintUtils.RuleCreator(
type: "suggestion",
fixable: "code",
docs: {
category: "Best Practices",
recommended: "error",
description: "Prefer using location.assign() to make testing easier",
},
Expand Down
6 changes: 3 additions & 3 deletions src/rules/no-unhandled-scheduling.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ESLintUtils, TSESTree } from "@typescript-eslint/experimental-utils";
import { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
import { isAsExpression, isIdentifier } from "../utils/type-guards";

type MessageIds = "noUnhandledScheduling";
Expand All @@ -15,7 +15,6 @@ export default ESLintUtils.RuleCreator(
meta: {
type: "suggestion",
docs: {
category: "Best Practices",
recommended: "warn",
description:
"When using Javascript scheduling (`setTimeout` or `setInterval`), it is recommended to support cancelling of the task, especially within React components.",
Expand All @@ -29,7 +28,8 @@ export default ESLintUtils.RuleCreator(
defaultOptions: [],
create: function (context) {
const reportError = (callExpression: TSESTree.CallExpression) => {
let expression: TSESTree.Expression = callExpression.callee;
let expression: TSESTree.Expression | TSESTree.PrivateIdentifier =
callExpression.callee;

// Drill down expression if on global/window objects
if (expression.type === "MemberExpression") {
Expand Down
18 changes: 9 additions & 9 deletions src/rules/private-component-methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ESLintUtils, TSESTree } from "@typescript-eslint/experimental-utils";
import { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
import { isMethod } from "../utils/type-guards";

type MessageIds = "privateMethods";
Expand All @@ -23,7 +23,7 @@ const reactLifecycleMethods = new Set([
]);

const isLifecycleMethod = (
method: TSESTree.ClassProperty | TSESTree.MethodDefinition
method: TSESTree.PropertyDefinition | TSESTree.MethodDefinition
) => {
return (
method.key.type === "Identifier" &&
Expand All @@ -40,7 +40,6 @@ export default ESLintUtils.RuleCreator(
type: "suggestion",
fixable: "code",
docs: {
category: "Best Practices",
recommended: "error",
description:
"Non-lifecycle methods for React class components should be private to help find unused handlers",
Expand All @@ -66,12 +65,13 @@ export default ESLintUtils.RuleCreator(
!isLifecycleMethod(classEl)
);
})
.forEach((b: TSESTree.ClassProperty | TSESTree.MethodDefinition) =>
context.report({
node: b.key,
messageId: "privateMethods",
fix: (fixer) => fixer.insertTextBefore(b, "private "),
})
.forEach(
(b: TSESTree.PropertyDefinition | TSESTree.MethodDefinition) =>
context.report({
node: b.key,
messageId: "privateMethods",
fix: (fixer) => fixer.insertTextBefore(b, "private "),
})
);
},
};
Expand Down
7 changes: 5 additions & 2 deletions src/rules/unregister-events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ESLintUtils, TSESTree } from "@typescript-eslint/experimental-utils";
import { ESLintUtils, TSESTree } from "@typescript-eslint/utils";

type MessageIds = "unregisterEventsInClass" | "unregisterEventsInHook";
type Options = [];
Expand All @@ -17,7 +17,6 @@ export default ESLintUtils.RuleCreator(
meta: {
type: "problem",
docs: {
category: "Possible Errors",
recommended: "error",
description:
"After registering event listeners in React components, event handlers should be unregistered when the component is unmounted.",
Expand Down Expand Up @@ -151,6 +150,10 @@ export default ESLintUtils.RuleCreator(
pushRemoveSubscription,
"ClassDeclaration[superClass.property.name=/Component|PureComponent/] ClassProperty[key.name='componentWillUnmount'] CallExpression[callee.name='removeEventListener']":
pushRemoveSubscription,
"ClassDeclaration[superClass.property.name=/Component|PureComponent/] PropertyDefinition[key.name='componentWillUnmount'] CallExpression[callee.object.name=/window|document/][callee.property.name='removeEventListener']":
pushRemoveSubscription,
"ClassDeclaration[superClass.property.name=/Component|PureComponent/] PropertyDefinition[key.name='componentWillUnmount'] CallExpression[callee.name='removeEventListener']":
pushRemoveSubscription,

// Track event listener registrations made in hook components
"VariableDeclarator[id.name=/^[A-Z].+/] CallExpression[callee.name='addEventListener']":
Expand Down
14 changes: 7 additions & 7 deletions src/utils/type-guards.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TSESTree } from "@typescript-eslint/experimental-utils";
import { TSESTree } from "@typescript-eslint/utils";

export const isClassProperty = (
export const isPropertyDefinition = (
node: TSESTree.Node
): node is TSESTree.ClassProperty => {
return node.type === "ClassProperty";
): node is TSESTree.PropertyDefinition => {
return node.type === "PropertyDefinition";
};

export const isMethodDefinition = (
Expand All @@ -15,17 +15,17 @@ export const isMethodDefinition = (
const funcExpRegex = /^(Arrow)?FunctionExpression$/;
export const isMethod = (
node: TSESTree.ClassElement
): node is TSESTree.ClassProperty | TSESTree.MethodDefinition => {
): node is TSESTree.PropertyDefinition | TSESTree.MethodDefinition => {
return (
(isClassProperty(node) &&
(isPropertyDefinition(node) &&
node.value &&
funcExpRegex.test(node.value.type)) ||
isMethodDefinition(node)
);
};

export const isIdentifier = (
node: TSESTree.Expression
node: TSESTree.Expression | TSESTree.PrivateIdentifier
): node is TSESTree.Identifier => {
return node.type === "Identifier";
};
Expand Down
2 changes: 1 addition & 1 deletion tests/rules/no-href-assignment.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import rule from "../../src/rules/no-href-assignment";
import { ESLintUtils } from "@typescript-eslint/experimental-utils";
import { ESLintUtils } from "@typescript-eslint/utils";
import { resolve, join } from "path";

const ruleTester = new ESLintUtils.RuleTester({
Expand Down
2 changes: 1 addition & 1 deletion tests/rules/no-unhandled-scheduling.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import rule from "../../src/rules/no-unhandled-scheduling";
import { ESLintUtils } from "@typescript-eslint/experimental-utils";
import { ESLintUtils } from "@typescript-eslint/utils";
import { resolve, join } from "path";

const ruleTester = new ESLintUtils.RuleTester({
Expand Down
2 changes: 1 addition & 1 deletion tests/rules/private-component-methods.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import rule from "../../src/rules/private-component-methods";
import { ESLintUtils } from "@typescript-eslint/experimental-utils";
import { ESLintUtils } from "@typescript-eslint/utils";
import { resolve, join } from "path";

const ruleTester = new ESLintUtils.RuleTester({
Expand Down
34 changes: 17 additions & 17 deletions tests/rules/unregister-events.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import rule from "../../src/rules/unregister-events";
import { ESLintUtils } from "@typescript-eslint/experimental-utils";
import { ESLintUtils } from "@typescript-eslint/utils";
import { resolve, join } from "path";

const ruleTester = new ESLintUtils.RuleTester({
Expand Down Expand Up @@ -29,11 +29,11 @@ ruleTester.run("unregister-events", rule, {
};
componentDidMount() {
window.addEventListener("scroll", handler);
window.addEventListener("scroll", this.handler);
}
componentWillUnmount() {
window.removeEventListener("scroll", handler);
window.removeEventListener("scroll", this.handler);
}
render() {
Expand All @@ -48,11 +48,11 @@ ruleTester.run("unregister-events", rule, {
};
componentDidMount() {
window.addEventListener("scroll", handler);
window.addEventListener("scroll", this.handler);
}
componentWillUnmount() {
removeEventListener("scroll", handler);
removeEventListener("scroll", this.handler);
}
render() {
Expand All @@ -67,11 +67,11 @@ ruleTester.run("unregister-events", rule, {
};
componentDidMount = () => {
window.addEventListener("scroll", handler);
window.addEventListener("scroll", this.handler);
}
componentWillUnmount = () => {
window.removeEventListener("scroll", handler);
window.removeEventListener("scroll", this.handler);
}
render() {
Expand All @@ -86,11 +86,11 @@ ruleTester.run("unregister-events", rule, {
};
componentDidMount() {
window.addEventListener("scroll", handler);
window.addEventListener("scroll", this.handler);
}
componentWillUnmount() {
window.removeEventListener("scroll", handler);
window.removeEventListener("scroll", this.handler);
}
render() {
Expand All @@ -105,11 +105,11 @@ ruleTester.run("unregister-events", rule, {
};
componentDidMount() {
addEventListener("scroll", handler);
addEventListener("scroll", this.handler);
}
componentWillUnmount() {
removeEventListener("scroll", handler);
removeEventListener("scroll", this.handler);
}
render() {
Expand Down Expand Up @@ -229,11 +229,11 @@ ruleTester.run("unregister-events", rule, {
};
componentWillUnmount() {
window.removeEventListener("scroll", handler);
window.removeEventListener("scroll", this.handler);
}
render() {
return <div onClick={() => addEventListener("scroll", handler)} />;
return <div onClick={() => addEventListener("scroll", this.handler)} />;
}
}
`,
Expand All @@ -247,7 +247,7 @@ ruleTester.run("unregister-events", rule, {
};
componentDidMount() {
window.addEventListener("scroll", handler);
window.addEventListener("scroll", this.handler);
}
render() {
Expand All @@ -269,7 +269,7 @@ ruleTester.run("unregister-events", rule, {
};
componentDidMount = () => {
window.addEventListener("scroll", handler);
window.addEventListener("scroll", this.handler);
}
render() {
Expand All @@ -291,11 +291,11 @@ ruleTester.run("unregister-events", rule, {
};
componentDidMount() {
window.addEventListener("scroll", handler);
window.addEventListener("scroll", this.handler);
}
componentWillUnmount() {
// window.removeEventListener("scroll", handler);
// window.removeEventListener("scroll", this.handler);
}
render() {
Expand Down

0 comments on commit 0f2d1e0

Please sign in to comment.