• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Haixing-Hu / js-common-util / 1e98184f-0522-4515-a343-e44e919e1313

08 Dec 2023 07:22AM UTC coverage: 55.49% (-10.3%) from 65.775%
1e98184f-0522-4515-a343-e44e919e1313

push

circleci

Haixing-Hu
style: fix coding style

274 of 513 branches covered (0.0%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

47 existing lines in 7 files now uncovered.

287 of 498 relevant lines covered (57.63%)

12.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.1
/src/get-declaring-class.js
1
////////////////////////////////////////////////////////////////////////////////
2
//
3
//    Copyright (c) 2022 - 2023.
4
//    Haixing Hu, Qubit Co. Ltd.
5
//
6
//    All rights reserved.
7
//
8
////////////////////////////////////////////////////////////////////////////////
9

10
/**
11
 * Gets the declaring class of the specified property from the ancestor tree of
12
 * the specified class.
13
 *
14
 * All classes in the ancestor tree of the specified class must have a default
15
 * constructor.
16
 *
17
 * If the specified property is declared in multiple places in the ancestor tree
18
 * of the specified class, this function returns the earliest declaring class.
19
 *
20
 * @param {function} Class
21
 *     The constructor of the specified class.
22
 * @param {string} property
23
 *     The name of the specified property.
24
 * @return {function}
25
 *     The earliest declaring class of the specified property in the ancestor
26
 *     tree of the specified class; or `null` if there is no such declaring
27
 *     class.
28
 * @deprecated This function **CANNOT** correctly handle the class fields, and
29
 * we haven't found a correct way to implement it. Do **NOT** use it.
30
 */
31
function getDeclaringClass(Class, property) {
32
  if (typeof Class !== 'function') {
21✔
33
    throw new TypeError('The parameter "Class" must be the constructor of a class.');
3✔
34
  }
35
  if (typeof property !== 'string') {
18!
UNCOV
36
    throw new TypeError('The parameter "property" must be a string.');
×
37
  }
38
  let Current = Class;
18✔
39
  let Last = null;
18✔
40
  while (Current !== Function.prototype) {
18✔
41
    // Tests whether property is defined in the prototype of Current
42
    if (Object.hasOwn(Current.prototype, property)) {
32✔
43
      return Current;
10✔
44
    }
45
    // Tests whether property is defined in the instance of Current
46
    const instance = new Current();
22✔
47
    if (!(property in instance)) {
22✔
48
      return Last;
5✔
49
    }
50
    Last = Current;
17✔
51
    // Go backward in the ancestor tree
52
    Current = Object.getPrototypeOf(Current);
17✔
53
  }
54
  if (Object.hasOwn(Last.prototype, property)) {
3!
UNCOV
55
    return Last;
×
56
  } else if (Object.hasOwn(Object.prototype, property)) {
3✔
57
    // deal with the special cases that the property is declared in Object
58
    return Object;
1✔
59
  } else {
60
    return Last;
2✔
61
  }
62
}
63

64
export default getDeclaringClass;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc