tsutils icon indicating copy to clipboard operation
tsutils copied to clipboard

use identifierToKeywordKind when available (TypeScript 5+)

Open pmcelhaney opened this issue 1 year ago • 6 comments

As of TypeScript 5.0, node.originalKeywordKind is deprecated.

As of TypeScript 5.2, it has been removed.

This fix uses the suggested alternative, idenifierToKeywordKind(node) when available. So it should work with TypeScript 5 without sacrificing backwards compatibility.

Fixes #155

pmcelhaney avatar Aug 29 '23 13:08 pmcelhaney

Here's a work-around until it's fixed: Install patch-package, add "postinstall": "patch-package" to your npm scripts, and copy this code to a file named ./patches/tsutils+3.21.0.patch.

diff --git a/node_modules/tsutils/util/usage.js b/node_modules/tsutils/util/usage.js
index fa24e64..9818b0a 100644
--- a/node_modules/tsutils/util/usage.js
+++ b/node_modules/tsutils/util/usage.js
@@ -25,7 +25,7 @@ function getUsageDomain(node) {
     const parent = node.parent;
     switch (parent.kind) {
         case ts.SyntaxKind.TypeReference:
-            return node.originalKeywordKind !== ts.SyntaxKind.ConstKeyword ? 2 /* Type */ : undefined;
+            return ts.identifierToKeywordKind(node) !== ts.SyntaxKind.ConstKeyword ? 2 /* Type */ : undefined;
         case ts.SyntaxKind.ExpressionWithTypeArguments:
             return parent.parent.token === ts.SyntaxKind.ImplementsKeyword ||
                 parent.parent.parent.kind === ts.SyntaxKind.InterfaceDeclaration
@@ -116,7 +116,7 @@ function getDeclarationDomain(node) {
         case ts.SyntaxKind.ModuleDeclaration:
             return 1 /* Namespace */;
         case ts.SyntaxKind.Parameter:
-            if (node.parent.parent.kind === ts.SyntaxKind.IndexSignature || node.originalKeywordKind === ts.SyntaxKind.ThisKeyword)
+            if (node.parent.parent.kind === ts.SyntaxKind.IndexSignature || ts.identifierToKeywordKind(node) === ts.SyntaxKind.ThisKeyword)
                 return;
         // falls through
         case ts.SyntaxKind.BindingElement:
@@ -525,7 +525,7 @@ class UsageWalker {
                 case ts.SyntaxKind.Parameter:
                     if (node.parent.kind !== ts.SyntaxKind.IndexSignature &&
                         (node.name.kind !== ts.SyntaxKind.Identifier ||
-                            node.name.originalKeywordKind !== ts.SyntaxKind.ThisKeyword))
+                            ts.identifierToKeywordKind(node.name) !== ts.SyntaxKind.ThisKeyword))
                         this._handleBindingName(node.name, false, false);
                     break;
                 case ts.SyntaxKind.EnumMember:
diff --git a/node_modules/tsutils/util/util.js b/node_modules/tsutils/util/util.js
index 0109fea..81aa517 100644
--- a/node_modules/tsutils/util/util.js
+++ b/node_modules/tsutils/util/util.js
@@ -37,7 +37,7 @@ function isKeywordKind(kind) {
 }
 exports.isKeywordKind = isKeywordKind;
 function isThisParameter(parameter) {
-    return parameter.name.kind === ts.SyntaxKind.Identifier && parameter.name.originalKeywordKind === ts.SyntaxKind.ThisKeyword;
+    return parameter.name.kind === ts.SyntaxKind.Identifier && ts.identifierToKeywordKind(parameter.name) === ts.SyntaxKind.ThisKeyword;
 }
 exports.isThisParameter = isThisParameter;
 function getModifier(node, kind) {

pmcelhaney avatar Sep 05 '23 15:09 pmcelhaney

Good work!

@pmcelhaney For the hotfix I had to use "postinstall" instead of "post-install". Other than that: awesome!

@ajafff Can this be merged? It's preventing many people to upgrade to TS 5.2. Thanks :-)

willemmulder avatar Sep 08 '23 13:09 willemmulder

this tsutils project seems to be dead & not maintained anymore 😢 @ajafff could you give access to some maintainers ?

Shuunen avatar Oct 19 '23 14:10 Shuunen

another solution would be that @pmcelhaney publish a fixed version on npm like "@pmcelhaney/tsutils" and then make a PR on the needed projects

Shuunen avatar Oct 19 '23 15:10 Shuunen

This project is dead and you are probably not affected. Short summary:

  • I thought I was affected, because "eslint-typescript" was using "tsutils" in our project
  • A quick research showed that the eslint team figured out that "tsutils" is dead a long time ago
  • The eslint team therefore forked the project as "ts-api-utils"
  • Nothing in a modern eslint toolchain should depend on tsutils, they removed everything in February
  • Our project was still using tsutils as dependency of eslint, and I am not sure why. Removing and re-adding eslint resulted in the correct "ts-api-utils" dependency
  • Everything works fine since then :)

tux21b avatar Oct 19 '23 15:10 tux21b

Thanks @tux21b for these details, so the issue some of us have here is linked to the fact that eslint-plugin-etc is depending on this tsutils instead of this newer package ts-api-utils

Shuunen avatar Oct 19 '23 16:10 Shuunen