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

oprajs / opra / 14419802659

12 Apr 2025 12:55PM UTC coverage: 82.088% (+4.2%) from 77.908%
14419802659

push

github

web-flow
Merge pull request #27 from oprajs/dev

Dev

3458 of 4432 branches covered (78.02%)

Branch coverage included in aggregate %.

16 of 16 new or added lines in 10 files covered. (100.0%)

1793 existing lines in 129 files now uncovered.

29424 of 35625 relevant lines covered (82.59%)

183.2 hits per line

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

86.15
/packages/common/src/document/common/document-node.ts
1
import type { Type } from 'ts-gems';
1✔
2
import { OpraSchema } from '../../schema/index.js';
1✔
3
import type { ApiDocument } from '../api-document.js';
1✔
4
import { DATATYPE_METADATA, kDataTypeMap, kTypeNSMap } from '../constants.js';
1✔
5
import type { ComplexType } from '../data-type/complex-type.js';
1✔
6
import type { DataType } from '../data-type/data-type.js';
1✔
7
import type { EnumType } from '../data-type/enum-type.js';
1✔
8
import type { MappedType } from '../data-type/mapped-type.js';
1✔
9
import type { MixinType } from '../data-type/mixin-type.js';
1✔
10
import type { SimpleType } from '../data-type/simple-type.js';
1✔
11
import type { DataTypeMap } from './data-type-map.js';
1✔
12
import type { DocumentElement } from './document-element.js';
1✔
13

1✔
14
/**
1✔
15
 * @class DocumentNode
1✔
16
 */
1✔
17
export class DocumentNode {
1✔
18
  protected [kDataTypeMap]?: DataTypeMap;
1✔
19
  protected _document?: ApiDocument;
1✔
20
  readonly parent?: DocumentNode;
1✔
21
  readonly element: DocumentElement;
1✔
22

1✔
23
  constructor(element: DocumentElement, parent?: DocumentNode) {
1✔
24
    this.element = element;
11,118✔
25
    this.parent = parent;
11,118✔
26
  }
11,118✔
27

1✔
28
  getDocument(): ApiDocument {
1✔
29
    if (this._document) return this._document;
10,201✔
30
    if (this.element[kTypeNSMap]) return this.element as unknown as ApiDocument;
10,201✔
31
    if (this.parent) return (this._document = this.parent.getDocument());
4,369!
UNCOV
32
    // istanbul ignore next
×
UNCOV
33
    throw new Error('ApiDocument not found in document tree');
×
UNCOV
34
  }
×
35

1✔
36
  hasDataType(
1✔
UNCOV
37
    nameOrCtor: string | Type | Function | object | any[],
×
UNCOV
38
    scope?: string | '*',
×
UNCOV
39
  ): boolean {
×
40
    return !!this.findDataType(nameOrCtor, scope);
×
UNCOV
41
  }
×
42

1✔
43
  findDataType(
1✔
44
    nameOrCtor: string | Type | Function | object | any[],
13,297✔
45
    scope?: string | '*',
13,297✔
46
  ): DataType | undefined {
13,297✔
47
    const dt = this[kDataTypeMap]?.get(nameOrCtor);
13,297✔
48
    if (dt && dt.inScope(scope)) return dt;
13,297✔
49
    return this.parent
13,290✔
50
      ? this.parent.findDataType(nameOrCtor, scope)
13,290!
UNCOV
51
      : undefined;
×
52
  }
13,297✔
53

1✔
54
  /**
1✔
55
   * Returns DataType instance by name or Constructor. Returns undefined if not found
1✔
56
   */
1✔
57
  getDataType(
1✔
58
    nameOrCtor: string | Type | Function | object | any[],
1,892✔
59
    scope?: string | '*',
1,892✔
60
  ): DataType {
1,892✔
61
    const dt = this.findDataType(nameOrCtor, scope);
1,892✔
62
    if (dt) return dt;
1,892✔
63
    let name = '';
1✔
64
    if (typeof nameOrCtor === 'function') {
1,892!
65
      const metadata = Reflect.getMetadata(DATATYPE_METADATA, nameOrCtor);
×
66
      name = metadata.name;
×
67
    } else if (typeof nameOrCtor === 'object') {
1,892!
68
      const metadata = nameOrCtor[DATATYPE_METADATA];
×
69
      name = metadata?.name;
×
UNCOV
70
    }
×
71
    if (!name) {
1✔
72
      if (nameOrCtor && typeof nameOrCtor === 'string') name = nameOrCtor;
1!
73
      else if (typeof nameOrCtor === 'function') name = nameOrCtor.name;
×
74
    }
1✔
75
    if (dt)
1✔
76
      throw new TypeError(
1,892!
UNCOV
77
        `Data type${name ? ' (' + name + ')' : ''} is not in requested scope (${scope})`,
✔
78
      );
1✔
79
    throw new TypeError(`Unknown data type${name ? ' (' + name + ')' : ''}`);
1,892!
80
  }
1,892✔
81

1✔
82
  getDataTypeNameWithNs(dataType: DataType): string | undefined {
1✔
83
    if (!dataType.name) return;
4,755✔
84
    const ns = this.getDocument().getDataTypeNs(dataType);
4,146✔
85
    return ns ? ns + ':' + dataType.name : dataType.name;
4,755✔
86
  }
4,755✔
87

1✔
88
  /**
1✔
89
   * Returns ComplexType instance by name or Constructor.
1✔
90
   * Returns undefined if not found
1✔
91
   * Throws error if data type is not a ComplexType
1✔
92
   */
1✔
93
  getComplexType(
1✔
94
    nameOrCtor: string | Type | Function,
1,229✔
95
    scope?: string | '*',
1,229✔
96
  ): ComplexType {
1,229✔
97
    const t = this.getDataType(nameOrCtor, scope);
1,229✔
98
    if (t.kind === OpraSchema.ComplexType.Kind) return t as ComplexType;
1,229✔
99
    throw new TypeError(`Data type "${t.name}" is not a ComplexType`);
2✔
100
  }
2✔
101

1✔
102
  /**
1✔
103
   * Returns SimpleType instance by name or Constructor.
1✔
104
   * Returns undefined if not found
1✔
105
   * Throws error if data type is not a SimpleType
1✔
106
   */
1✔
107
  getSimpleType(nameOrCtor: string | Type, scope?: string | '*'): SimpleType {
1✔
108
    const t = this.getDataType(nameOrCtor, scope);
71✔
109
    if (t.kind === OpraSchema.SimpleType.Kind) return t as SimpleType;
71✔
110
    throw new TypeError(`Data type "${t.name || t}" is not a SimpleType`);
71!
111
  }
71✔
112

1✔
113
  /**
1✔
114
   * Returns EnumType instance by name or Constructor.
1✔
115
   * Returns undefined if not found
1✔
116
   * Throws error if data type is not a EnumType
1✔
117
   */
1✔
118
  getEnumType(
1✔
119
    nameOrCtor: string | object | any[],
10✔
120
    scope?: string | '*',
10✔
121
  ): EnumType {
10✔
122
    const t = this.getDataType(nameOrCtor, scope);
10✔
123
    if (t.kind === OpraSchema.EnumType.Kind) return t as EnumType;
10!
124
    throw new TypeError(`Data type "${t.name || t}" is not a EnumType`);
10✔
125
  }
10✔
126

1✔
127
  /**
1✔
128
   * Returns EnumType instance by name or Constructor.
1✔
129
   * Returns undefined if not found
1✔
130
   * Throws error if data type is not a MappedType
1✔
131
   */
1✔
132
  getMappedType(
1✔
133
    nameOrCtor: string | object | any[],
8✔
134
    scope?: string | '*',
8✔
135
  ): MappedType {
8✔
136
    const t = this.getDataType(nameOrCtor, scope);
8✔
137
    if (t.kind === OpraSchema.MappedType.Kind) return t as MappedType;
8!
138
    throw new TypeError(`Data type "${t.name || t}" is not a MappedType`);
8✔
139
  }
8✔
140

1✔
141
  /**
1✔
142
   * Returns EnumType instance by name or Constructor.
1✔
143
   * Returns undefined if not found
1✔
144
   * Throws error if data type is not a MixinType
1✔
145
   */
1✔
146
  getMixinType(
1✔
147
    nameOrCtor: string | object | any[],
3✔
148
    scope?: string | '*',
3✔
149
  ): MixinType {
3✔
150
    const t = this.getDataType(nameOrCtor, scope);
3✔
151
    if (t.kind === OpraSchema.MixinType.Kind) return t as MixinType;
3!
152
    throw new TypeError(`Data type "${t.name || t}" is not a MixinType`);
3✔
153
  }
3✔
154
}
1✔
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