debugger icon indicating copy to clipboard operation
debugger copied to clipboard

[PrettyPrint] auto-pretty-print minified sources

Open jasonLaster opened this issue 9 years ago • 19 comments

The old debugger would automatically pretty print minified sources. This was a nice feature, which removed an extra "pretty print" action.

jasonLaster avatar Sep 26 '17 20:09 jasonLaster

This could be done via an action on select source?

codehag avatar Sep 27 '17 15:09 codehag

The way i see it working is, when we load the source text we can check for a: min.js, b: a line w/ > 1000 chars

and if either is true we can fire the togglePrettyPrint action

jasonLaster avatar Sep 27 '17 15:09 jasonLaster

There is an isMinified function which the old debugger uses as heuristics to decide, we could use that .. https://searchfox.org/mozilla-central/source/devtools/client/debugger/utils.js#74

bomsy avatar Sep 27 '17 15:09 bomsy

lets steal it:

 function (key, text) {
    if (this._minifiedCache.has(key)) {
      return this._minifiedCache.get(key);
    }

    let isMinified;
    let lineEndIndex = 0;
    let lineStartIndex = 0;
    let lines = 0;
    let indentCount = 0;
    let overCharLimit = false;

    // Strip comments.
    text = text.replace(/\/\*[\S\s]*?\*\/|\/\/(.+|\n)/g, "");

    while (lines++ < SAMPLE_SIZE) {
      lineEndIndex = text.indexOf("\n", lineStartIndex);
      if (lineEndIndex == -1) {
        break;
      }
      if (/^\s+/.test(text.slice(lineStartIndex, lineEndIndex))) {
        indentCount++;
      }
      // For files with no indents but are not minified.
      if ((lineEndIndex - lineStartIndex) > CHARACTER_LIMIT) {
        overCharLimit = true;
        break;
      }
      lineStartIndex = lineEndIndex + 1;
    }

    isMinified =
      ((indentCount / lines) * 100) < INDENT_COUNT_THRESHOLD || overCharLimit;

    this._minifiedCache.set(key, isMinified);
    return isMinified;
  }

jasonLaster avatar Sep 27 '17 15:09 jasonLaster

@jasonLaster can I take this one?

peterschussheim avatar Oct 02 '17 15:10 peterschussheim

sure, it's yours

jasonLaster avatar Oct 02 '17 22:10 jasonLaster

:wave: @peterschussheim, just checking in to see how it's going and if you have any questions.

Also, if you're currently busy, feel free to /unclaim the issue. You can always /claim another one later. And, this is OSS, we're guilt free!

Also, always happy to chat on slack or in a 'hangout!

jasonLaster avatar Nov 04 '17 21:11 jasonLaster

Hey @jasonLaster, I apologize for forgetting about this one. I am going to refresh my memory on this one during the afternoon and will update shortly after. 👍🏼

peterschussheim avatar Nov 06 '17 17:11 peterschussheim

/unclaim

peterschussheim avatar Nov 08 '17 14:11 peterschussheim

Done :+1:

claim avatar Nov 08 '17 14:11 claim

/claim

bomsy avatar Nov 08 '17 14:11 bomsy

Thanks for claiming the issue! :wave:

Here are some links for getting setup, contributing, and developing. We're always happy to answer questions in slack! If you become busy, feel free to /unclaim it.

:fox_face: Debugger team!

claim avatar Nov 08 '17 14:11 claim

/unclaim

bomsy avatar Nov 09 '17 21:11 bomsy

Done :+1:

claim avatar Nov 09 '17 21:11 claim

diff --git a/src/components/Editor/index.js b/src/components/Editor/index.js
index d4dfb348..a6b1b36f 100644
--- a/src/components/Editor/index.js
+++ b/src/components/Editor/index.js
@@ -9,7 +9,8 @@ import ReactDOM from "react-dom";
 import { connect } from "react-redux";
 import classnames from "classnames";
 import { debugGlobal } from "devtools-launchpad";
-import { isLoaded } from "../../utils/source";
+import { isLoaded, shouldPrettyPrint, isMinified } from "../../utils/source";
+import { isOriginalId } from "devtools-source-map";
 import { isFirefox } from "devtools-config";
 import { SourceEditor } from "devtools-source-editor";

@@ -88,6 +89,7 @@ type Props = {
   setContextMenu: (string, any) => void,
   continueToHere: number => void,
   toggleBreakpoint: number => void,
+  togglePrettyPrint: string => void,
   addOrToggleDisabledBreakpoint: number => void,
   jumpToMappedLocation: any => void,
   traverseResults: (boolean, Object) => void
@@ -224,7 +226,9 @@ class Editor extends PureComponent<Props, State> {
   }

   componentWillUpdate(nextProps) {
-    this.setText(nextProps);
+    if (nextProps.selectedSource !== this.props.selectedSource) {
+      this.setText(nextProps);
+    }
     this.setSize(nextProps);
   }

@@ -470,7 +474,7 @@ class Editor extends PureComponent<Props, State> {
   }

   setText(props) {
-    const { selectedSource, sourceMetaData } = props;
+    const { selectedSource, sourceMetaData, togglePrettyPrint } = props;
     if (!this.state.editor) {
       return;
     }
@@ -488,6 +492,15 @@ class Editor extends PureComponent<Props, State> {
     }

     if (selectedSource) {
+      // auto-prettyprint the source
+      const selected = selectedSource.toJS();
+      if (
+        selected.text &&
+        !isOriginalId(selected.id) &&
+        (isMinified(selected.id, selected.text) || shouldPrettyPrint(selected))
+      ) {
+        togglePrettyPrint(selectedSource.get("id"));
+      }
       return showSourceText(
         this.state.editor,
         selectedSource.toJS(),
diff --git a/src/utils/source.js b/src/utils/source.js
index f1e2e378..d175d855 100644
--- a/src/utils/source.js
+++ b/src/utils/source.js
@@ -200,6 +200,60 @@ function getSourceLineCount(source: Source) {
   return source.text != undefined ? source.text.split("\n").length : 0;
 }

+// Used to detect minification for automatic pretty printing
+const SAMPLE_SIZE = 50; // no of lines
+const INDENT_COUNT_THRESHOLD = 5; // percentage
+const CHARACTER_LIMIT = 250; // line character limit
+let _minifiedCache = new Map();
+
+/**
+ *
+ * Checks if a source is minified based on some heuristics
+ * @param key
+ * @param text
+ * @return boolean
+ * @memberof utils/source
+ * @static
+ */
+
+function isMinified(key, text) {
+  if (_minifiedCache.has(key)) {
+    return _minifiedCache.get(key);
+  }
+
+  let isMinified;
+  let lineEndIndex = 0;
+  let lineStartIndex = 0;
+  let lines = 0;
+  let indentCount = 0;
+  let overCharLimit = false;
+
+  // Strip comments.
+  text = text.replace(/\/\*[\S\s]*?\*\/|\/\/(.+|\n)/g, "");
+
+  while (lines++ < SAMPLE_SIZE) {
+    lineEndIndex = text.indexOf("\n", lineStartIndex);
+    if (lineEndIndex == -1) {
+      break;
+    }
+    if (/^\s+/.test(text.slice(lineStartIndex, lineEndIndex))) {
+      indentCount++;
+    }
+    // For files with no indents but are not minified.
+    if (lineEndIndex - lineStartIndex > CHARACTER_LIMIT) {
+      overCharLimit = true;
+      break;
+    }
+    lineStartIndex = lineEndIndex + 1;
+  }
+
+  isMinified =
+    indentCount / lines * 100 < INDENT_COUNT_THRESHOLD || overCharLimit;
+
+  _minifiedCache.set(key, isMinified);
+  return isMinified;
+}
+
 /**
  *
  * Returns Code Mirror mode for source content type
@@ -264,6 +318,7 @@ function isLoaded(source: Source) {
 }

 export {
+  isMinified,
   isJavaScript,
   isPretty,
   isThirdParty,
(END)

bomsy avatar Nov 11 '17 17:11 bomsy

A good STR is

  1. go to http://devtools-html.github.io/debugger-examples/examples/todomvc/
  2. select bower_components/backbone/local-storage

ER should pretty print

jasonLaster avatar Nov 13 '17 17:11 jasonLaster

/claim

assafdd avatar Nov 13 '17 19:11 assafdd

Thanks for claiming the issue! :wave:

Here are some links for getting setup, contributing, and developing. We're always happy to answer questions in slack! If you become busy, feel free to /unclaim it.

:fox_face: Debugger team!

claim avatar Nov 13 '17 19:11 claim

Yay!

jasonLaster avatar Dec 15 '17 16:12 jasonLaster