Skip to content

Commit

Permalink
fix: drop dependency on tsutils
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Jan 28, 2023
1 parent bf971ab commit 7a63d89
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
9 changes: 8 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@
],
"sourceType": "module"
},
"ignorePatterns": ["/build/", "/coverage/", "/lib/", "/**/*.cjs", "/**/*.js"],
"ignorePatterns": [
"/build/",
"/coverage/",
"/lib/",
"/**/*.cjs",
"/**/*.js",
"/src/util/tsutils.ts"
],
"rules": {
"functional/prefer-immutable-types": "off",
"import/no-relative-parent-imports": "error"
Expand Down
4 changes: 2 additions & 2 deletions GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ yarn add -D eslint eslint-plugin-functional

```sh
# Install with npm
npm install -D eslint @typescript-eslint/parser tsutils eslint-plugin-functional
npm install -D eslint @typescript-eslint/parser eslint-plugin-functional

# Install with yarn
yarn add -D eslint @typescript-eslint/parser tsutils eslint-plugin-functional
yarn add -D eslint @typescript-eslint/parser eslint-plugin-functional
```

## Usage
Expand Down
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,14 @@
"tsc-prog": "^2.2.1",
"tsconfig-paths": "^4.1.2",
"tslib": "^2.4.1",
"tsutils": "^3.21.0",
"typescript": "^4.9.4",
"word-wrap": "^1.2.3"
},
"peerDependencies": {
"eslint": "^8.0.0",
"tsutils": "^3.0.0",
"typescript": ">=4.0.2"
},
"peerDependenciesMeta": {
"tsutils": {
"optional": true
},
"typescript": {
"optional": true
}
Expand Down
8 changes: 2 additions & 6 deletions src/rules/no-conditional-statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { ESLintUtils, TSESLint, TSESTree } from "@typescript-eslint/utils";
import type { JSONSchema4 } from "json-schema";
import type { Type } from "typescript";

import tsutils from "~/conditional-imports/tsutils";
import type { RuleResult } from "~/util/rule";
import { createRule, getTypeOfNode } from "~/util/rule";
import { unionTypeParts } from "~/util/tsutils";
import {
isBlockStatement,
isBreakStatement,
Expand Down Expand Up @@ -243,16 +243,12 @@ function isExhaustiveTypeSwitchViolation(
node: TSESTree.SwitchStatement,
context: TSESLint.RuleContext<keyof typeof errorMessages, Options>
): boolean {
if (tsutils === undefined) {
return true;
}

const discriminantType = getTypeOfNode(node.discriminant, context);
if (discriminantType === null || !discriminantType.isUnion()) {
return true;
}

const unionTypes = tsutils.unionTypeParts(discriminantType);
const unionTypes = unionTypeParts(discriminantType);
const caseTypes = node.cases.reduce<ReadonlySet<Type>>(
(types, c) => new Set([...types, getTypeOfNode(c.test!, context)!]),
new Set()
Expand Down
11 changes: 0 additions & 11 deletions src/util/conditional-imports/tsutils.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/util/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { ESLintUtils } from "@typescript-eslint/utils";
import type { Rule } from "eslint";
import type { ImmutabilityOverrides } from "is-immutable-type";
import { getTypeImmutability, Immutability } from "is-immutable-type";
import { isIdentifier } from "tsutils";
import type { Node as TSNode, Type, TypeNode } from "typescript";

import ts from "~/conditional-imports/typescript";
import { getImmutabilityOverrides } from "~/settings";
import type { ESFunction } from "~/util/node-types";
import { isIdentifier } from "~/util/tsutils";

// eslint-disable-next-line @typescript-eslint/naming-convention, no-underscore-dangle -- This is a special var.
const __VERSION__ = "0.0.0-development";
Expand Down
29 changes: 29 additions & 0 deletions src/util/tsutils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Need functions from tsutils.
* Copied here so they can be patched if needed.
* Should in future be removed and replaced with a new lib.
*
* @see https://github.com/typescript-eslint/typescript-eslint/issues/5552
*/

import type tsType from "typescript";

import ts from "~/conditional-imports/typescript";

export function isUnionType(type: tsType.Type): type is tsType.UnionType {
if (ts === undefined) {
return false;
}
return (type.flags & ts.TypeFlags.Union) !== 0;
}

export function unionTypeParts(type: tsType.Type): tsType.Type[] {
return isUnionType(type) ? type.types : [type];
}

export function isIdentifier(node: tsType.Node): node is tsType.Identifier {
if (ts === undefined) {
return false;
}
return node.kind === ts.SyntaxKind.Identifier;
}

0 comments on commit 7a63d89

Please sign in to comment.