tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PreviewFunction.js (2435B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
      4 
      5 import { Component } from "devtools/client/shared/vendor/react";
      6 import {
      7  span,
      8  button,
      9 } from "devtools/client/shared/vendor/react-dom-factories";
     10 import PropTypes from "devtools/client/shared/vendor/react-prop-types";
     11 
     12 import { formatDisplayName } from "../../utils/pause/frames/index";
     13 
     14 const IGNORED_SOURCE_URLS = ["debugger eval code"];
     15 
     16 export default class PreviewFunction extends Component {
     17  static get propTypes() {
     18    return {
     19      func: PropTypes.object.isRequired,
     20    };
     21  }
     22 
     23  renderFunctionName(func) {
     24    const { l10n } = this.context;
     25    const name = formatDisplayName(func, undefined, l10n);
     26    return span(
     27      {
     28        className: "function-name",
     29      },
     30      name
     31    );
     32  }
     33 
     34  renderParams(func) {
     35    const { parameterNames = [] } = func;
     36 
     37    return parameterNames
     38      .filter(Boolean)
     39      .map((param, i, arr) => {
     40        const elements = [
     41          span(
     42            {
     43              className: "param",
     44              key: param,
     45            },
     46            param
     47          ),
     48        ];
     49        // if this isn't the last param, add a comma
     50        if (i !== arr.length - 1) {
     51          elements.push(
     52            span(
     53              {
     54                className: "delimiter",
     55                key: i,
     56              },
     57              ", "
     58            )
     59          );
     60        }
     61        return elements;
     62      })
     63      .flat();
     64  }
     65 
     66  jumpToDefinitionButton(func) {
     67    const { location } = func;
     68 
     69    if (!location?.url || IGNORED_SOURCE_URLS.includes(location.url)) {
     70      return null;
     71    }
     72 
     73    const lastIndex = location.url.lastIndexOf("/");
     74    return button({
     75      className: "jump-definition",
     76      draggable: "false",
     77      title: `${location.url.slice(lastIndex + 1)}:${location.line}`,
     78    });
     79  }
     80 
     81  render() {
     82    const { func } = this.props;
     83    return span(
     84      {
     85        className: "function-signature",
     86      },
     87      this.renderFunctionName(func),
     88      span(
     89        {
     90          className: "paren",
     91        },
     92        "("
     93      ),
     94      this.renderParams(func),
     95      span(
     96        {
     97          className: "paren",
     98        },
     99        ")"
    100      ),
    101      this.jumpToDefinitionButton(func)
    102    );
    103  }
    104 }
    105 
    106 PreviewFunction.contextTypes = {
    107  l10n: PropTypes.object,
    108 };