Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Feature: Add debug support. #22

Open
8 tasks
ryan-roemer opened this issue Mar 27, 2020 · 0 comments
Open
8 tasks

Feature: Add debug support. #22

ryan-roemer opened this issue Mar 27, 2020 · 0 comments
Labels
feature New feature or request

Comments

@ryan-roemer
Copy link
Member

ryan-roemer commented Mar 27, 2020

Some ideas:

  • Lazy require debug without it being a dependency.
  • Use node12+ debug logger in util
  • Things to log
    • require(A_VAR)
    • require.resolve(A_VAR)
    • import(A_VAR)
    • ignores matches
    • allowMissing matches

Possible starting point diff:

diff --git a/lib/debug.js b/lib/debug.js
new file mode 100644
index 0000000..b9d36e2
--- /dev/null
+++ b/lib/debug.js
@@ -0,0 +1,15 @@
+"use strict";
+
+/**
+ * Simple debugger using lazy http://npm.im/debug
+ */
+const NOOP = () => {};
+
+let debug;
+try {
+  debug = require("debug")
+} catch (err) {
+  debug = () => NOOP;
+}
+
+module.exports = debug;
diff --git a/lib/extract.js b/lib/extract.js
index 0a26c6b..0bc6c7b 100644
--- a/lib/extract.js
+++ b/lib/extract.js
@@ -3,8 +3,11 @@
 /**
  * Extract dependencies from code.
  */
-
 const walk = require("acorn-node/walk");
+const debug = require("debug");
+
+// Track non-literal imports
+const debugNonLiteral = debug("trace-deps:non-literal");
 
 // Extract all dependency strings from require/import statements.
 const getDeps = (ast) => {
@@ -183,10 +186,12 @@ const getDeps = (ast) => {
     //   value: four
     //   raw: '"four"'
     // ```
-    ExportNamedDeclaration({ source } = {}) {
-      source = source || {};
+    ExportNamedDeclaration(node = {}) {
+      const source = node.source || {};
       if (source.type === "Literal") {
         deps.add(source.value);
+      } else {
+        debugNonLiteral(JSON.stringify(node));
       }
     },
 
@@ -207,6 +212,8 @@ const getDeps = (ast) => {
       source = source || {};
       if (source.type === "Literal") {
         deps.add(source.value);
+      } else {
+        debugNonLiteral(JSON.stringify(source));
       }
     }
 
diff --git a/package.json b/package.json
index be11426..93bf173 100644
--- a/package.json
+++ b/package.json
@@ -33,6 +33,7 @@
     "chai": "^4.2.0",
     "chai-as-promised": "^7.1.1",
     "codecov": "^3.6.5",
+    "debug": "^4.1.1",
     "eslint": "^6.8.0",
     "eslint-config-formidable": "^4.0.0",
     "eslint-plugin-filenames": "^1.3.2",
@ryan-roemer ryan-roemer added the feature New feature or request label Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant