Skip to content

Commit

Permalink
perf: remove semver as dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Feb 17, 2023
1 parent d37da96 commit 040d488
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@
"lint-staged": {
"*": "prettier --ignore-unknown --write"
},
"dependencies": {
"semver": "^7.3.8"
},
"devDependencies": {
"@types/semver": "^7.3.13",
"@typescript-eslint/eslint-plugin": "^5.48.2",
"@typescript-eslint/parser": "^5.48.2",
"@typescript/vfs": "^1.4.0",
Expand Down
9 changes: 3 additions & 6 deletions pnpm-lock.yaml

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

8 changes: 4 additions & 4 deletions src/nodes/typeGuards/union.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as semver from "semver";
import * as ts from "typescript";

import { isTsVersionAtLeast } from "../../utils.js";
import {
isJSDocNamespaceDeclaration,
isJsxTagNamePropertyAccess,
Expand Down Expand Up @@ -72,7 +72,7 @@ export function isBindingOrAssignmentElementRestIndicator(
return true;
}

if (semver.satisfies(ts.version, ">=4.4")) {
if (isTsVersionAtLeast(4, 4)) {
return ts.isDotDotDotToken(node);
}

Expand Down Expand Up @@ -308,7 +308,7 @@ export function isHasJSDoc(node: ts.Node): node is ts.HasJSDoc {
return true;
}

if (semver.satisfies(ts.version, ">=4.4")) {
if (isTsVersionAtLeast(4, 4)) {
return ts.isClassStaticBlockDeclaration(node);
}

Expand Down Expand Up @@ -381,7 +381,7 @@ export function isJSDocComment(node: ts.Node): node is ts.JSDocComment {
return true;
}

if (semver.satisfies(ts.version, ">=4.4")) {
if (isTsVersionAtLeast(4, 4)) {
return (
ts.isJSDocLink(node) ||
ts.isJSDocLinkCode(node) ||
Expand Down
4 changes: 2 additions & 2 deletions src/syntax.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as semver from "semver";
import * as ts from "typescript";
import { describe, expect, it } from "vitest";

Expand All @@ -7,8 +6,9 @@ import {
isNumericPropertyName,
isValidPropertyAccess,
} from "./syntax.js";
import { isTsVersionAtLeast } from "./utils.js";

const isTS4dot4 = semver.satisfies(ts.version, ">=4.4");
const isTS4dot4 = isTsVersionAtLeast(4, 4);

describe("isAssignmentKind", () => {
const tests: [boolean, ts.SyntaxKind][] = [
Expand Down
5 changes: 3 additions & 2 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
// Original license MIT:
// /~https://github.com/ajafff/tsutils/blob/26b195358ec36d59f00333115aa3ffd9611ca78b/LICENSE

import * as semver from "semver";
import * as ts from "typescript";

import { isTsVersionAtLeast } from "./utils.js";

export type ForEachTokenCallback = (token: ts.Node) => void;

/**
Expand All @@ -19,7 +20,7 @@ export function forEachToken(
callback: ForEachTokenCallback,
sourceFile: ts.SourceFile = node.getSourceFile()
): void {
const isTS4dot3 = semver.satisfies(ts.version, ">=4.3");
const isTS4dot3 = isTsVersionAtLeast(4, 3);

const queue = [];
while (true) {
Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as ts from "typescript";

const [tsMajor, tsMinor] = ts.versionMajorMinor
.split(".")
.map((raw) => Number.parseInt(raw, 10));

export function isTsVersionAtLeast(major: number, minor = 0): boolean {
return major > tsMajor || (major === tsMajor && minor >= tsMinor);
}

0 comments on commit 040d488

Please sign in to comment.