All files / near-membrane-dom/dist custom-devtools-formatter.mjs.js

100% Statements 20/20
100% Branches 2/2
100% Functions 0/0
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269                      37x     37x 37x 37x 37x 37x 37x 37x 37x     37x     37x     37x     37x     37x     37x     37x                                                                                                                                                                                                                                                                                                                                                                   37x 37x 1x 1x                                                                                          
import { LOCKER_UNMINIFIED_FLAG, SymbolFor, ArrayIsArray, ReflectDefineProperty, isNearMembraneProxy, ReflectApply, ArrayProtoPush, ObjectProtoToString, ObjectKeys, TO_STRING_BRAND_SYMBOL, ArrayProtoIncludes, ArrayProtoUnshift, TO_STRING_BRAND_STRING, ArrayProtoFilter, NumberIsInteger, ReflectOwnKeys, ArrayProtoMap, StringCtor, MathMin, NumberIsNaN, NumberCtor, CHAR_ELLIPSIS, StringProtoSlice, getNearMembraneProxySerializedValue, TO_STRING_BRAND_NUMBER, TO_STRING_BRAND_BOOLEAN, TO_STRING_BRAND_BIG_INT, ArrayProtoIndexOf, ArrayProtoSplice, isObject, NumberIsFinite, JSONStringify } from '@locker/near-membrane-shared';
import { rootWindow } from '@locker/near-membrane-shared-dom';
 
// This package is bundled by third-parties that have their own build time
// replacement logic. Instead of customizing each build system to be aware
// of this package we implement a two phase debug mode by performing small
// runtime checks to determine phase one, our code is unminified, and
// phase two, the user opted-in to custom devtools formatters. Phase one
// is used for light weight initialization time debug while phase two is
// reserved for post initialization runtime.
// istanbul ignore else: not avoidable via tests
if (LOCKER_UNMINIFIED_FLAG) {
  // We passed the phase one gate so we know our code is unminified and we can
  // install Locker's custom devtools formatter.
  let lockerDebugModeSymbolFlag = true;
  const LOCKER_DEBUG_MODE_SYMBOL = SymbolFor('@@lockerDebugMode');
  const MAX_ARRAY_DISPLAY = 100;
  const MAX_OBJECT_DISPLAY = 5;
  const MAX_STRING_DISPLAY = 100;
  const MID_STRING_DISPLAY = MAX_STRING_DISPLAY / 2;
  const headerCSSText = 'display: inline-block; margin-bottom: 3px; margin-left: -3px; word-break: break-all; word-wrap: wrap;';
  const bodyItemStyleObject = {
    style: 'margin-left:15px; margin-bottom: 3px;'
  };
  const bodyStyleObject = {
    style: 'display: inline-block; margin-left:12px; word-break: break-all; word-wrap: wrap;'
  };
  const keyEnumerableStringStyleObject = {
    style: 'color: #9d288c; font-weight: bold'
  };
  const keyNonEnumerableOrSymbolStyleObject = {
    style: 'color: #b17ab0'
  };
  const primitiveBlueColorStyleObject = {
    style: 'color: #16239f'
  };
  const primitiveGreenColorStyleObject = {
    style: 'color: #236d25'
  };
  const primitiveGreyColorStyleObject = {
    style: 'color: #606367'
  };
  const primitiveOrangeColorStyleObject = {
    style: 'color: #b82619'
  };
  // istanbul ignore next: currently unreachable via tests
  const formatValue = function formatValue(value) {
    if (value === null || value === undefined) {
      return ['span', primitiveGreyColorStyleObject, `${value}`];
    }
    if (typeof value === 'boolean') {
      return ['span', primitiveBlueColorStyleObject, value];
    }
    if (typeof value === 'number') {
      return NumberIsFinite(value) ? ['span', primitiveBlueColorStyleObject, value] : ['span', primitiveBlueColorStyleObject, `${value >= 0 ? '' : '-'}Infinity`];
    }
    if (typeof value === 'bigint') {
      return ['span', primitiveGreenColorStyleObject, `${value}n`];
    }
    if (typeof value === 'string') {
      let string = value;
      const {
        length
      } = string;
      if (length > MAX_STRING_DISPLAY) {
        const firstChunk = ReflectApply(StringProtoSlice, string, [0, MID_STRING_DISPLAY]);
        const lastChunk = ReflectApply(StringProtoSlice, string, [length - MID_STRING_DISPLAY - 1, length]);
        string = firstChunk + CHAR_ELLIPSIS + lastChunk;
      }
      // @TODO: Default to using single quotes on main header and double
      // quotes on body.
      return ['span', primitiveOrangeColorStyleObject, JSONStringify(string)];
    }
    if (ArrayIsArray(value)) {
      return ['span', {}, `Array(${value.length})`];
    }
    if (isObject(value)) {
      return ['span', {}, `{${CHAR_ELLIPSIS}}`];
    }
    // Symbol will be coerced to a string.
    return ['span', primitiveOrangeColorStyleObject, StringCtor(value)];
  };
  // istanbul ignore next: currently unreachable via tests
  const formatHeader = function formatHeader(object, config) {
    const isChildElement = config == null ? void 0 : config.isChildElement;
    const formattedHeader = [];
    let formattedHeaderOffset = 0;
    if (isChildElement) {
      formattedHeader[formattedHeaderOffset++] = ['span', keyEnumerableStringStyleObject, config.childKey];
      formattedHeader[formattedHeaderOffset++] = ['span', {}, ': '];
    }
    const brand = ReflectApply(ObjectProtoToString, object, []);
    let keys = ObjectKeys(object);
    if (brand === TO_STRING_BRAND_SYMBOL) {
      if (!ReflectApply(ArrayProtoIncludes, keys, ['description'])) {
        ReflectApply(ArrayProtoUnshift, keys, ['description']);
      }
    } else if (brand === TO_STRING_BRAND_STRING) {
      const {
        length
      } = object;
      keys = ReflectApply(ArrayProtoFilter, keys, [key => {
        const possibleIndex = typeof key === 'string' ? +key : -1;
        return possibleIndex < 0 || possibleIndex >= length || !NumberIsInteger(possibleIndex);
      }]);
    }
    const ownKeysRaw = ReflectOwnKeys(object);
    const ownKeys = ReflectApply(ArrayProtoMap, ownKeysRaw, [StringCtor]);
    const {
      length: ownKeysLength
    } = ownKeys;
    if (ArrayIsArray(object)) {
      formattedHeader[formattedHeaderOffset++] = ['span', {}, `(${object.length}) [`];
      for (let i = 0, length = MathMin(ownKeysLength, MAX_ARRAY_DISPLAY); i < length; i += 1) {
        const ownKeyRaw = ownKeysRaw[i];
        const ownKey = ownKeys[i];
        const value = object[ownKeyRaw];
        if (ownKey !== 'length') {
          if (!NumberIsNaN(NumberCtor(ownKey))) {
            formattedHeader[formattedHeaderOffset++] = ['span', {}, i ? ', ' : ''];
            formattedHeader[formattedHeaderOffset++] = formatValue(value);
          } else {
            formattedHeader[formattedHeaderOffset++] = ['span', {}, i ? ', ' : ''];
            formattedHeader[formattedHeaderOffset++] = ['span', primitiveGreyColorStyleObject, StringCtor(ownKey)];
            formattedHeader[formattedHeaderOffset++] = ['span', {}, ': '];
            formattedHeader[formattedHeaderOffset++] = formatValue(value);
          }
        }
      }
      if (ownKeysLength > MAX_ARRAY_DISPLAY) {
        formattedHeader[formattedHeaderOffset++] = ['span', null, ['span', {}, `, ${CHAR_ELLIPSIS}`]];
      }
      formattedHeader[formattedHeaderOffset++] = ['span', {}, ']'];
      return formattedHeader;
    }
    let boxedHeaderEntry;
    let headerOpening = '{';
    // eslint-disable-next-line default-case
    switch (brand) {
      case TO_STRING_BRAND_BIG_INT:
      case TO_STRING_BRAND_BOOLEAN:
      case TO_STRING_BRAND_NUMBER:
      case TO_STRING_BRAND_STRING:
      case TO_STRING_BRAND_SYMBOL:
        {
          let colorStyleObject = primitiveBlueColorStyleObject;
          if (brand === TO_STRING_BRAND_BIG_INT) {
            colorStyleObject = primitiveGreenColorStyleObject;
          } else if (brand === TO_STRING_BRAND_SYMBOL) {
            colorStyleObject = primitiveOrangeColorStyleObject;
          }
          headerOpening = `${ReflectApply(StringProtoSlice, brand, [8, -1])} {`;
          boxedHeaderEntry = ['span', colorStyleObject, `${StringCtor(getNearMembraneProxySerializedValue(object))}`];
          break;
        }
    }
    formattedHeader[formattedHeaderOffset++] = ['span', {}, headerOpening];
    if (boxedHeaderEntry) {
      formattedHeader[formattedHeaderOffset++] = boxedHeaderEntry;
      if (ownKeysLength) {
        formattedHeader[formattedHeaderOffset++] = ['span', {}, ', '];
      }
    }
    for (let i = 0, length = MathMin(ownKeysLength, MAX_OBJECT_DISPLAY); i < length; i += 1) {
      const ownKeyRaw = ownKeysRaw[i];
      const ownKey = ownKeys[i];
      const value = object[ownKeyRaw];
      formattedHeader[formattedHeaderOffset++] = ['span', {}, i ? ', ' : ''];
      formattedHeader[formattedHeaderOffset++] = ['span', primitiveGreyColorStyleObject, ownKey];
      formattedHeader[formattedHeaderOffset++] = ['span', {}, ': '];
      formattedHeader[formattedHeaderOffset++] = formatValue(value);
    }
    if (ownKeysLength > MAX_OBJECT_DISPLAY) {
      formattedHeader[formattedHeaderOffset++] = ['span', null, ['span', {}, `, ${CHAR_ELLIPSIS}`]];
    }
    formattedHeader[formattedHeaderOffset++] = ['span', {}, '}'];
    return formattedHeader;
  };
  // istanbul ignore next: currently unreachable via tests
  const formatBody = function formatBody(object) {
    // @TODO: Arrays are broken into groups of 100.
    const ownKeysRaw = ReflectOwnKeys(object);
    const ownKeys = ReflectApply(ArrayProtoMap, ownKeysRaw, [StringCtor]);
    // Put 'length' at the end of array.
    const isArray = ArrayIsArray(object);
    if (isArray) {
      const lengthIndex = ReflectApply(ArrayProtoIndexOf, ownKeys, ['length']);
      const lengthKeyRaw = ReflectApply(ArrayProtoSplice, ownKeysRaw, [lengthIndex, 1])[0];
      ReflectApply(ArrayProtoPush, ownKeysRaw, [lengthKeyRaw]);
      const lengthKey = ReflectApply(ArrayProtoSplice, ownKeys, [lengthIndex, 1])[0];
      ReflectApply(ArrayProtoPush, ownKeys, [lengthKey]);
    }
    const formattedBody = [];
    let formattedBodyOffset = 0;
    for (let i = 0, {
        length
      } = ownKeys; i < length; i += 1) {
      const ownKeyRaw = ownKeysRaw[i];
      const ownKey = ownKeys[i];
      const value = object[ownKeyRaw];
      if (isObject(value)) {
        formattedBody[formattedBodyOffset++] = ['div', {}, ['object', {
          object: value,
          config: {
            childKey: StringCtor(ownKey),
            isChildElement: true
          }
        }]];
      } else {
        let currentKeyStyle = keyEnumerableStringStyleObject;
        if (isArray && ownKey === 'length') {
          currentKeyStyle = keyNonEnumerableOrSymbolStyleObject;
        }
        formattedBody[formattedBodyOffset++] = ['div', bodyItemStyleObject, ['span', currentKeyStyle, ownKey], ['span', {}, ': '], formatValue(value)];
      }
    }
    return formattedBody;
  };
  let {
    devtoolsFormatters
  } = rootWindow;
  if (!ArrayIsArray(devtoolsFormatters)) {
    devtoolsFormatters = [];
    ReflectDefineProperty(rootWindow, 'devtoolsFormatters', {
      __proto__: null,
      configurable: true,
      value: devtoolsFormatters,
      writable: true
    });
  }
  // Append our custom formatter to the array of devtools formatters.
  // istanbul ignore next: currently unreachable via tests
  devtoolsFormatters[devtoolsFormatters.length] = {
    // istanbul ignore next: currently unreachable via tests
    header(object, config) {
      if (lockerDebugModeSymbolFlag) {
        // We passed the second phase gate so we know that the user has
        // opted-in to custom devtools formatters. Close the gate and
        // define the @@lockerDebugMode symbol on window.
        lockerDebugModeSymbolFlag = false;
        ReflectDefineProperty(rootWindow, LOCKER_DEBUG_MODE_SYMBOL, {
          __proto__: null,
          configurable: true,
          value: true,
          writable: true
        });
      }
      if (!isNearMembraneProxy(object)) {
        return null;
      }
      const headerDiv = ['div', {
        style: `${headerCSSText}${config != null && config.isChildElement ? '' : 'font-style: italic;'}`
      }];
      ReflectApply(ArrayProtoPush, headerDiv, formatHeader(object, config));
      return ['div', {}, headerDiv];
    },
    // istanbul ignore next: currently unreachable via tests
    hasBody() {
      return true;
    },
    // istanbul ignore next: currently unreachable via tests
    body(object) {
      const bodyDiv = ['div', bodyStyleObject];
      ReflectApply(ArrayProtoPush, bodyDiv, formatBody(object));
      return bodyDiv;
    }
  };
}