From 040d4885097e296ea71bfc45115d7fc0a39b2ae9 Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Fri, 17 Feb 2023 23:39:26 +1300 Subject: [PATCH] perf: remove semver as dependency --- package.json | 4 ---- pnpm-lock.yaml | 9 +++------ src/nodes/typeGuards/union.ts | 8 ++++---- src/syntax.test.ts | 4 ++-- src/tokens.ts | 5 +++-- src/utils.ts | 9 +++++++++ 6 files changed, 21 insertions(+), 18 deletions(-) create mode 100644 src/utils.ts diff --git a/package.json b/package.json index e60a6555..db8f5f31 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 52712e69..e6de579c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,7 +1,6 @@ lockfileVersion: 5.4 specifiers: - '@types/semver': ^7.3.13 '@typescript-eslint/eslint-plugin': ^5.48.2 '@typescript-eslint/parser': ^5.48.2 '@typescript/vfs': ^1.4.0 @@ -31,18 +30,13 @@ specifiers: prettier: ^2.8.3 prettier-plugin-packagejson: ^2.4.2 release-it: ^15.6.0 - semver: ^7.3.8 sentences-per-line: ^0.2.1 should-semantic-release: ^0.0.4 tsup: ^6.5.0 typescript: 4.9.5 vitest: ^0.28.0 -dependencies: - semver: 7.3.8 - devDependencies: - '@types/semver': 7.3.13 '@typescript-eslint/eslint-plugin': 5.48.2_azmbqzqvrlvblbdtiwxwvyvjjy '@typescript-eslint/parser': 5.48.2_et5x32uxl7z5ldub3ye5rhlyqm '@typescript/vfs': 1.4.0 @@ -5063,6 +5057,7 @@ packages: engines: {node: '>=10'} dependencies: yallist: 4.0.0 + dev: true /macos-release/3.1.0: resolution: {integrity: sha512-/M/R0gCDgM+Cv1IuBG1XGdfTFnMEG6PZeT+KGWHO/OG+imqmaD9CH5vHBTycEM3+Kc4uG2Il+tFAuUWLqQOeUA==} @@ -6419,6 +6414,7 @@ packages: hasBin: true dependencies: lru-cache: 6.0.0 + dev: true /sentences-per-line/0.2.1: resolution: {integrity: sha512-6hlyKBwqoaZJ5+RBTKNNem2kBGAboh9e9KfFw5KYKA+64xaTYWbv5C6XnOudx8xk1Sg6f/4yalhJtCZFSLWIsQ==} @@ -7533,6 +7529,7 @@ packages: /yallist/4.0.0: resolution: {integrity: sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==} + dev: true /yaml/1.10.2: resolution: {integrity: sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==} diff --git a/src/nodes/typeGuards/union.ts b/src/nodes/typeGuards/union.ts index 4d88e5d2..0d02d056 100644 --- a/src/nodes/typeGuards/union.ts +++ b/src/nodes/typeGuards/union.ts @@ -1,6 +1,6 @@ -import * as semver from "semver"; import * as ts from "typescript"; +import { isTsVersionAtLeast } from "../../utils.js"; import { isJSDocNamespaceDeclaration, isJsxTagNamePropertyAccess, @@ -72,7 +72,7 @@ export function isBindingOrAssignmentElementRestIndicator( return true; } - if (semver.satisfies(ts.version, ">=4.4")) { + if (isTsVersionAtLeast(4, 4)) { return ts.isDotDotDotToken(node); } @@ -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); } @@ -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) || diff --git a/src/syntax.test.ts b/src/syntax.test.ts index 83053b2d..f1aa8f95 100644 --- a/src/syntax.test.ts +++ b/src/syntax.test.ts @@ -1,4 +1,3 @@ -import * as semver from "semver"; import * as ts from "typescript"; import { describe, expect, it } from "vitest"; @@ -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][] = [ diff --git a/src/tokens.ts b/src/tokens.ts index ba257caa..9ae5b1e1 100644 --- a/src/tokens.ts +++ b/src/tokens.ts @@ -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; /** @@ -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) { diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 00000000..83788040 --- /dev/null +++ b/src/utils.ts @@ -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); +}