Repository: depcheck/depcheck
Pull request #725 (2022-06-03 00:40:49)

Add support for some tsconfig.json fields
Detect dependencies for the "types" and "extends" fields in tsconfig.json

Fixes https://github.com/depcheck/depcheck/issues/720


codecov-commenter: # [Codecov](https://codecov.io/gh/depcheck/depcheck/pull/725?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck) Report
> Merging [#725](https://codecov.io/gh/depcheck/depcheck/pull/725?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck) (f31886d) into [main](https://codecov.io/gh/depcheck/depcheck/commit/73c2bec4ae85404c01249b805e7dafe4f8499c65?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck) (73c2bec) will **decrease** coverage by `1.41%`.
> The diff coverage is `46.43%`.

[![Impacted file tree graph](https://codecov.io/gh/depcheck/depcheck/pull/725/graphs/tree.svg?width=650&height=150&src=pr&token=pgMcKAUD3w&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck)](https://codecov.io/gh/depcheck/depcheck/pull/725?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck)

```diff
@@            Coverage Diff             @@
##             main     #725      +/-   ##
==========================================
- Coverage   98.64%   97.23%   -1.40%     
==========================================
  Files          49       50       +1     
  Lines        1023     1046      +23     
==========================================
+ Hits         1009     1017       +8     
- Misses         14       29      +15     
```


| [Impacted Files](https://codecov.io/gh/depcheck/depcheck/pull/725?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck) | Coverage Δ | |
|---|---|---|
| [src/constants.js](https://codecov.io/gh/depcheck/depcheck/pull/725/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck#diff-c3JjL2NvbnN0YW50cy5qcw==) | `100.00% <ø> (ø)` | |
| [src/utils/configuration-reader.js](https://codecov.io/gh/depcheck/depcheck/pull/725/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck#diff-c3JjL3V0aWxzL2NvbmZpZ3VyYXRpb24tcmVhZGVyLmpz) | `100.00% <ø> (ø)` | |
| [src/parser/tsconfig.js](https://codecov.io/gh/depcheck/depcheck/pull/725/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck#diff-c3JjL3BhcnNlci90c2NvbmZpZy5qcw==) | `13.34% <13.34%> (ø)` | |
| [src/cli.js](https://codecov.io/gh/depcheck/depcheck/pull/725/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck#diff-c3JjL2NsaS5qcw==) | `95.56% <75.00%> (-4.44%)` | :arrow_down: |
| [src/detector/importDeclaration.js](https://codecov.io/gh/depcheck/depcheck/pull/725/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck#diff-c3JjL2RldGVjdG9yL2ltcG9ydERlY2xhcmF0aW9uLmpz) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/depcheck/depcheck/pull/725?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/depcheck/depcheck/pull/725?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck). Last update [3d1a45f...f31886d](https://codecov.io/gh/depcheck/depcheck/pull/725?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck).


Patch:
diff --git a/src/component.json b/src/component.json
index 4158694..6b19f92 100644
--- a/src/component.json
+++ b/src/component.json
@@ -7,6 +7,7 @@
     "graphql",
     "jsx",
     "sass",
+    "tsconfig",
     "typescript",
     "vue",
     "svelte"
diff --git a/src/constants.js b/src/constants.js
index 694e308..1e8d2f2 100644
--- a/src/constants.js
+++ b/src/constants.js
@@ -53,6 +53,7 @@ export const defaultOptions = {
     '**/*.litcoffee': availableParsers.coffee,
     '**/*.coffee.md': availableParsers.coffee,
     '**/*.graphql': availableParsers.graphql,
+    '**/tsconfig*.json': availableParsers.tsconfig,
     '**/*.ts': availableParsers.typescript,
     '**/*.tsx': availableParsers.typescript,
     '**/*.sass': availableParsers.sass,
diff --git a/src/parser/tsconfig.js b/src/parser/tsconfig.js
new file mode 100644
index 0000000..148c307
--- /dev/null
+++ b/src/parser/tsconfig.js
@@ -0,0 +1,26 @@
+const requirePackageName = require('require-package-name');
+const { readFileSync } = require('fs');
+
+export default function tsconfigParser(filePath, deps) {
+  const content = readFileSync(filePath, { encoding: 'utf8' });
+  const foundDeps = [];
+  const tsconfigJson = JSON.parse(content);
+  const types = tsconfigJson.compilerOptions?.types;
+  if (types) {
+    types.forEach((pkg) => {
+      const typesPkg = `@types/${pkg}`;
+      if (
+        !deps.includes(typesPkg) &&
+        (deps.includes(pkg) || /[@/]/.test(pkg))
+      ) {
+        foundDeps.push(pkg);
+      } else {
+        foundDeps.push(typesPkg);
+      }
+    });
+  }
+  if (tsconfigJson.extends) {
+    foundDeps.push(tsconfigJson.extends);
+  }
+  return foundDeps.map((p) => requirePackageName(p)).filter(Boolean);
+}


Test patch:
diff --git a/test/fake_modules/tsconfig/package.json b/test/fake_modules/tsconfig/package.json
new file mode 100644
index 0000000..a387619
--- /dev/null
+++ b/test/fake_modules/tsconfig/package.json
@@ -0,0 +1,9 @@
+{
+  "name": "tsconfig-test-package",
+  "dependencies": {},
+  "devDependencies": {
+    "@mybrand/tsconfig": "*",
+    "@types/node": "*",
+    "@types/unused": "*"
+  }
+}
diff --git a/test/fake_modules/tsconfig/tsconfig.build.json b/test/fake_modules/tsconfig/tsconfig.build.json
new file mode 100644
index 0000000..30252af
--- /dev/null
+++ b/test/fake_modules/tsconfig/tsconfig.build.json
@@ -0,0 +1,3 @@
+{
+  "extends": "tsconfig-base/tsconfig.base.json"
+}
diff --git a/test/fake_modules/tsconfig/tsconfig.json b/test/fake_modules/tsconfig/tsconfig.json
new file mode 100644
index 0000000..6267e91
--- /dev/null
+++ b/test/fake_modules/tsconfig/tsconfig.json
@@ -0,0 +1,6 @@
+{
+  "compilerOptions": {
+    "types": ["node", "jest"]
+  },
+  "extends": "@mybrand/tsconfig"
+}
diff --git a/test/spec.js b/test/spec.js
index 1e651ac..5927f45 100755
--- a/test/spec.js
+++ b/test/spec.js
@@ -223,6 +223,26 @@ export default [
     },
     expectedErrorCode: -1,
   },
+  {
+    name: 'support tsconfig extends and types fields',
+    module: 'tsconfig',
+    options: {},
+    expected: {
+      dependencies: [],
+      devDependencies: ['@types/unused'],
+      missing: {
+        '@types/jest': ['tsconfig.json'],
+        'tsconfig-base': ['tsconfig.build.json'],
+      },
+      using: {
+        '@mybrand/tsconfig': ['tsconfig.json'],
+        '@types/jest': ['tsconfig.json'],
+        '@types/node': ['tsconfig.json'],
+        'tsconfig-base': ['tsconfig.build.json'],
+      },
+    },
+    expectedErrorCode: -1,
+  },
   {
     name: 'support SASS/SCSS syntax',
     module: 'sass',