forked from emberjs/ember-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject-inspector.js
1361 lines (1214 loc) · 37.5 KB
/
object-inspector.js
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable ember/no-private-routing-service */
import DebugPort from './debug-port';
import bound from 'ember-debug/utils/bound-method';
import {
isComputed,
isDescriptor,
getDescriptorFor,
typeOf,
} from 'ember-debug/utils/type-check';
import { compareVersion } from 'ember-debug/utils/version';
import { inspect as emberInspect } from 'ember-debug/utils/ember/debug';
import Ember, { EmberObject } from 'ember-debug/utils/ember';
import { cacheFor, guidFor } from 'ember-debug/utils/ember/object/internals';
import { _backburner, join } from 'ember-debug/utils/ember/runloop';
import emberNames from './utils/ember-object-names';
import getObjectName from './utils/get-object-name';
import { EmberLoader } from 'ember-debug/utils/ember/loader';
const { meta: emberMeta, VERSION, CoreObject, ObjectProxy } = Ember;
const GlimmerComponent = (() => {
try {
return EmberLoader.require('@glimmer/component').default;
} catch (e) {
// ignore, return undefined
}
})();
let tagValue, tagValidate, track, tagForProperty;
try {
// Try to load the most recent library
let GlimmerValidator = EmberLoader.require('@glimmer/validator');
tagValue = GlimmerValidator.value || GlimmerValidator.valueForTag;
tagValidate = GlimmerValidator.validate || GlimmerValidator.validateTag;
track = GlimmerValidator.track;
// patch tagFor to add debug info, older versions already have _propertyKey
const tagFor = GlimmerValidator.tagFor;
GlimmerValidator.tagFor = function (...args) {
const tag = tagFor.call(this, ...args);
const [obj, key] = args;
if (
(!tag._propertyKey || !tag._object) &&
typeof obj === 'object' &&
typeof key === 'string'
) {
tag._propertyKey = key;
tag._object = obj;
}
return tag;
};
const trackedData = GlimmerValidator.trackedData;
GlimmerValidator.trackedData = function (...args) {
const r = trackedData.call(this, ...args);
if (r.getter && args.length === 2) {
const [key] = args;
const getter = r.getter;
r.getter = function (self) {
GlimmerValidator.tagFor(self, key);
return getter.call(this, self);
};
}
return r;
};
} catch (e) {
try {
// Fallback to the previous implementation
let GlimmerReference = EmberLoader.require('@glimmer/reference');
tagValue = GlimmerReference.value;
tagValidate = GlimmerReference.validate;
} catch (e) {
// ignore
}
}
try {
let metal = EmberLoader.require('@ember/-internals/metal');
tagForProperty = metal.tagForProperty;
// If track was not already loaded, use metal's version (the previous version)
track = track || metal.track;
} catch (e) {
// ignore
}
const HAS_GLIMMER_TRACKING = tagValue && tagValidate && track && tagForProperty;
const keys = Object.keys || Ember.keys;
/**
* Determine the type and get the value of the passed property
* @param {*} object The parent object we will look for `key` on
* @param {string} key The key for the property which points to a computed, EmberObject, etc
* @param {*} computedValue A value that has already been computed with calculateCP
* @return {{inspect: (string|*), type: string}|{computed: boolean, inspect: string, type: string}|{inspect: string, type: string}}
*/
function inspectValue(object, key, computedValue) {
let string;
const value = computedValue;
if (arguments.length === 3 && computedValue === undefined) {
return { type: `type-undefined`, inspect: 'undefined' };
}
// TODO: this is not very clean. We should refactor calculateCP, etc, rather than passing computedValue
if (computedValue !== undefined) {
return { type: `type-${typeOf(value)}`, inspect: inspect(value) };
}
if (value instanceof EmberObject) {
return { type: 'type-ember-object', inspect: value.toString() };
} else if (isComputed(object, key)) {
string = '<computed>';
return { type: 'type-descriptor', inspect: string };
} else if (isDescriptor(value)) {
return { type: 'type-descriptor', inspect: value.toString() };
} else {
return { type: `type-${typeOf(value)}`, inspect: inspect(value) };
}
}
function inspect(value) {
if (typeof value === 'function') {
return 'function() { ... }';
} else if (value instanceof EmberObject) {
return value.toString();
} else if (typeOf(value) === 'array') {
if (value.length === 0) {
return '[]';
} else if (value.length === 1) {
return `[ ${inspect(value[0])} ]`;
} else {
return `[ ${inspect(value[0])}, ... ]`;
}
} else if (value instanceof Error) {
return `Error: ${value.message}`;
} else if (value === null) {
return 'null';
} else if (typeOf(value) === 'date') {
return value.toString();
} else if (typeof value === 'object') {
// `Ember.inspect` is able to handle this use case,
// but it is very slow as it loops over all props,
// so summarize to just first 2 props
// if it defines a toString, we use that instead
if (
typeof value.toString === 'function' &&
value.toString !== Object.prototype.toString &&
value.toString !== Function.prototype.toString
) {
try {
return `<Object:${value.toString()}>`;
} catch (e) {
//
}
}
let ret = [];
let v;
let count = 0;
let broken = false;
for (let key in value) {
if (!('hasOwnProperty' in value) || value.hasOwnProperty(key)) {
if (count++ > 1) {
broken = true;
break;
}
v = value[key];
if (v === 'toString') {
continue;
} // ignore useless items
if (typeOf(v).includes('function')) {
v = 'function() { ... }';
}
if (typeOf(v) === 'array') {
v = `[Array : ${v.length}]`;
}
if (typeOf(v) === 'object') {
v = '[Object]';
}
ret.push(`${key}: ${v}`);
}
}
let suffix = ' }';
if (broken) {
suffix = ' ...}';
}
return `{ ${ret.join(', ')}${suffix}`;
} else {
return emberInspect(value);
}
}
function isMandatorySetter(descriptor) {
if (descriptor.set && descriptor.set === Ember.MANDATORY_SETTER_FUNCTION) {
return true;
}
if (
descriptor.set &&
Function.prototype.toString
.call(descriptor.set)
.includes('You attempted to update')
) {
return true;
}
return false;
}
function getTagTrackedTags(tag, ownTag, level = 0) {
const props = [];
// do not include tracked properties from dependencies
if (!tag || level > 1) {
return props;
}
const subtags = tag.subtags || (Array.isArray(tag.subtag) ? tag.subtag : []);
if (tag.subtag && !Array.isArray(tag.subtag)) {
if (tag.subtag._propertyKey) props.push(tag.subtag);
props.push(...getTagTrackedTags(tag.subtag, ownTag, level + 1));
}
if (subtags) {
subtags.forEach((t) => {
if (t === ownTag) return;
if (t._propertyKey) props.push(t);
props.push(...getTagTrackedTags(t, ownTag, level + 1));
});
}
return props;
}
function getTrackedDependencies(object, property, tagInfo) {
const tag = tagInfo.tag;
const proto = Object.getPrototypeOf(object);
if (!proto) return [];
const cpDesc = emberMeta(object).peekDescriptors(property);
const dependentKeys = [];
if (cpDesc) {
dependentKeys.push(
...(cpDesc._dependentKeys || []).map((k) => ({ name: k }))
);
}
if (HAS_GLIMMER_TRACKING) {
const ownTag = tagForProperty(object, property);
const tags = getTagTrackedTags(tag, ownTag);
const mapping = {};
let maxRevision = tagValue(tag);
tags.forEach((t) => {
const p =
(t._object ? getObjectName(t._object) + '.' : '') + t._propertyKey;
const [objName, prop] = p.split('.');
mapping[objName] = mapping[objName] || new Set();
const value = tagValue(t);
if (prop) {
mapping[objName].add([prop, value]);
}
});
const hasChange =
(tagInfo.revision && maxRevision !== tagInfo.revision) || false;
const names = new Set();
Object.entries(mapping).forEach(([objName, props]) => {
if (names.has(objName)) {
return;
}
names.add(objName);
if (props.size > 1) {
dependentKeys.push({ name: objName });
props.forEach((p) => {
const changed = hasChange && p[1] > tagInfo.revision;
const obj = {
child: p[0],
};
if (changed) {
obj.changed = true;
}
dependentKeys.push(obj);
});
}
if (props.size === 1) {
const p = [...props][0];
const changed = hasChange && p[1] > tagInfo.revision;
const obj = {
name: objName + '.' + p[0],
};
if (changed) {
obj.changed = true;
}
dependentKeys.push(obj);
}
if (props.size === 0) {
dependentKeys.push({ name: objName });
}
});
}
return [...dependentKeys];
}
export default class extends DebugPort {
get adapter() {
return this.namespace?.adapter;
}
get port() {
return this.namespace?.port;
}
currentObject = null;
updateCurrentObject() {
Object.values(this.sentObjects).forEach((obj) => {
if (obj instanceof CoreObject && obj.isDestroyed) {
this.dropObject(guidFor(obj));
}
});
if (this.currentObject) {
const { object, mixinDetails, objectId } = this.currentObject;
mixinDetails.forEach((mixin, mixinIndex) => {
mixin.properties.forEach((item) => {
if (item.overridden) {
return true;
}
try {
let cache = cacheFor(object, item.name);
if (item.isExpensive && !cache) return true;
if (item.value.type === 'type-function') return true;
let value = null;
let changed = false;
const values = (this.objectPropertyValues[objectId] =
this.objectPropertyValues[objectId] || {});
const tracked = (this.trackedTags[objectId] =
this.trackedTags[objectId] || {});
const desc = Object.getOwnPropertyDescriptor(object, item.name);
const isSetter = desc && isMandatorySetter(desc);
if (HAS_GLIMMER_TRACKING && item.canTrack && !isSetter) {
let tagInfo = tracked[item.name] || {
tag: tagForProperty(object, item.name),
revision: 0,
};
if (!tagInfo.tag) return;
changed = !tagValidate(tagInfo.tag, tagInfo.revision);
if (changed) {
tagInfo.tag = track(() => {
value = object.get?.(item.name) || object[item.name];
});
}
tracked[item.name] = tagInfo;
} else {
value = calculateCP(object, item, {});
if (values[item.name] !== value) {
changed = true;
values[item.name] = value;
}
}
if (changed) {
value = inspectValue(object, item.name, value);
value.isCalculated = true;
let dependentKeys = null;
if (tracked[item.name]) {
dependentKeys = getTrackedDependencies(
object,
item.name,
tracked[item.name]
);
tracked[item.name].revision = tagValue(tracked[item.name].tag);
}
this.sendMessage('updateProperty', {
objectId,
property:
Array.isArray(object) && !Number.isNaN(parseInt(item.name))
? parseInt(item.name)
: item.name,
value,
mixinIndex,
dependentKeys,
});
}
} catch (e) {
// dont do anything
}
});
});
}
}
init() {
super.init();
this.sentObjects = {};
_backburner.on('end', bound(this, this.updateCurrentObject));
}
willDestroy() {
super.willDestroy();
for (let objectId in this.sentObjects) {
this.releaseObject(objectId);
}
_backburner.off('end', bound(this, this.updateCurrentObject));
}
sentObjects = {};
parentObjects = {};
objectPropertyValues = {};
trackedTags = {};
_errorsFor = {};
static {
this.prototype.portNamespace = 'objectInspector';
this.prototype.messages = {
digDeeper(message) {
this.digIntoObject(message.objectId, message.property);
},
releaseObject(message) {
this.releaseObject(message.objectId);
},
calculate(message) {
let value;
value = this.valueForObjectProperty(
message.objectId,
message.property,
message.mixinIndex
);
if (value) {
this.sendMessage('updateProperty', value);
message.isCalculated = true;
}
this.sendMessage('updateErrors', {
objectId: message.objectId,
errors: errorsToSend(this._errorsFor[message.objectId]),
});
},
saveProperty(message) {
let value = message.value;
if (message.dataType && message.dataType === 'date') {
value = new Date(value);
}
this.saveProperty(message.objectId, message.property, value);
},
sendToConsole(message) {
this.sendToConsole(message.objectId, message.property);
},
sendControllerToConsole(message) {
const container = this.namespace?.owner;
this.sendValueToConsole(container.lookup(`controller:${message.name}`));
},
sendRouteHandlerToConsole(message) {
const container = this.namespace?.owner;
this.sendValueToConsole(container.lookup(`route:${message.name}`));
},
sendContainerToConsole() {
const container = this.namespace?.owner;
this.sendValueToConsole(container);
},
/**
* Lookup the router instance, and find the route with the given name
* @param message The message sent
* @param {string} messsage.name The name of the route to lookup
*/
inspectRoute(message) {
const container = this.namespace?.owner;
const router = container.lookup('router:main');
const routerLib = router._routerMicrolib || router.router;
// 3.9.0 removed intimate APIs from router
// https://github.com/emberjs/ember.js/pull/17843
// https://deprecations.emberjs.com/v3.x/#toc_remove-handler-infos
if (compareVersion(VERSION, '3.9.0') !== -1) {
// Ember >= 3.9.0
this.sendObject(routerLib.getRoute(message.name));
} else {
// Ember < 3.9.0
this.sendObject(routerLib.getHandler(message.name));
}
},
inspectController(message) {
const container = this.namespace?.owner;
this.sendObject(container.lookup(`controller:${message.name}`));
},
inspectById(message) {
const obj = this.sentObjects[message.objectId];
if (obj) {
this.sendObject(obj);
}
},
inspectByContainerLookup(message) {
const container = this.namespace?.owner;
this.sendObject(container.lookup(message.name));
},
traceErrors(message) {
let errors = this._errorsFor[message.objectId];
toArray(errors).forEach((error) => {
let stack = error.error;
if (stack && stack.stack) {
stack = stack.stack;
} else {
stack = error;
}
this.adapter.log(
`Object Inspector error for ${error.property}`,
stack
);
});
},
};
}
canSend(val) {
return (
val &&
(val instanceof EmberObject ||
val instanceof Object ||
typeOf(val) === 'object' ||
typeOf(val) === 'array')
);
}
saveProperty(objectId, prop, val) {
let object = this.sentObjects[objectId];
join(() => {
if (object.set) {
object.set(prop, val);
} else {
object[prop] = val;
}
});
}
sendToConsole(objectId, prop) {
let object = this.sentObjects[objectId];
let value;
if (prop === null || prop === undefined) {
value = this.sentObjects[objectId];
} else {
value = calculateCP(object, { name: prop }, {});
}
this.sendValueToConsole(value);
}
sendValueToConsole(value) {
window.$E = value;
if (value instanceof Error) {
value = value.stack;
}
let args = [value];
if (value instanceof EmberObject) {
args.unshift(inspect(value));
}
this.adapter.log('Ember Inspector ($E): ', ...args);
}
digIntoObject(objectId, property) {
let parentObject = this.sentObjects[objectId];
let object = calculateCP(parentObject, { name: property }, {});
if (this.canSend(object)) {
const currentObject = this.currentObject;
let details = this.mixinsForObject(object);
this.parentObjects[details.objectId] = currentObject;
this.sendMessage('updateObject', {
parentObject: objectId,
property,
objectId: details.objectId,
name: getObjectName(object),
details: details.mixins,
errors: details.errors,
});
}
}
sendObject(object) {
if (!this.canSend(object)) {
throw new Error(
`Can't inspect ${object}. Only Ember objects and arrays are supported.`
);
}
let details = this.mixinsForObject(object);
this.sendMessage('updateObject', {
objectId: details.objectId,
name: getObjectName(object),
details: details.mixins,
errors: details.errors,
});
}
retainObject(object) {
let meta = emberMeta(object);
let guid = guidFor(object);
meta._debugReferences = meta._debugReferences || 0;
meta._debugReferences++;
this.sentObjects[guid] = object;
return guid;
}
releaseObject(objectId) {
let object = this.sentObjects[objectId];
if (!object) {
return;
}
let meta = emberMeta(object);
let guid = guidFor(object);
meta._debugReferences--;
if (meta._debugReferences === 0) {
this.dropObject(guid);
}
}
dropObject(objectId) {
if (this.parentObjects[objectId]) {
this.currentObject = this.parentObjects[objectId];
}
delete this.parentObjects[objectId];
delete this.sentObjects[objectId];
delete this.objectPropertyValues[objectId];
delete this.trackedTags[objectId];
if (this.currentObject && this.currentObject.objectId === objectId) {
this.currentObject = null;
}
delete this._errorsFor[objectId];
this.sendMessage('droppedObject', { objectId });
}
/**
* This function, and the rest of Ember Inspector, currently refer to the
* output entirely as mixins. However, this is no longer accurate! This has
* been refactored to return a list of objects that represent both the classes
* themselves and their mixins. For instance, the following class definitions:
*
* ```js
* class Foo extends EmberObject {}
*
* class Bar extends Foo {}
*
* class Baz extends Bar.extend(Mixin1, Mixin2) {}
*
* let obj = Baz.create();
* ```
*
* Will result in this in the inspector:
*
* ```
* - Own Properties
* - Baz
* - Mixin1
* - Mixin2
* - Bar
* - Foo
* - EmberObject
* ```
*
* The "mixins" returned by this function directly represent these things too.
* Each class object consists of the actual own properties of that class's
* prototype, and is followed by the mixins (if any) that belong to that
* class. Own Properties represents the actual own properties of the object
* itself.
*
* TODO: The rest of the Inspector should be updated to reflect this new data
* model, and these functions should be updated with new names. Mixins should
* likely be embedded _on_ the class definitions, but this was designed to be
* backwards compatible.
*/
mixinDetailsForObject(object) {
const mixins = [];
const own = ownMixins(object);
const objectMixin = {
id: guidFor(object),
name: getObjectName(object),
properties: ownProperties(object, own),
};
mixins.push(objectMixin);
// insert ember mixins
for (let mixin of own) {
let name = (
mixin[Ember.NAME_KEY] ||
mixin.ownerConstructor ||
emberNames.get(mixin) ||
''
).toString();
if (!name && typeof mixin.toString === 'function') {
try {
name = mixin.toString();
if (name === '(unknown)') {
name = '(unknown mixin)';
}
} catch (e) {
name = '(Unable to convert Object to string)';
}
}
const mix = {
properties: propertiesForMixin(mixin),
name,
isEmberMixin: true,
id: guidFor(mixin),
};
mixins.push(mix);
}
const proto = Object.getPrototypeOf(object);
if (proto && proto !== Object.prototype) {
mixins.push(...this.mixinDetailsForObject(proto));
}
return mixins;
}
mixinsForObject(object) {
if (
object instanceof ObjectProxy &&
object.content &&
!object._showProxyDetails
) {
object = object.content;
}
if (
object instanceof Ember.ArrayProxy &&
object.content &&
!object._showProxyDetails
) {
object = object.slice(0, 101);
}
let mixinDetails = this.mixinDetailsForObject(object);
mixinDetails[0].name = 'Own Properties';
mixinDetails[0].expand = true;
if (mixinDetails[1] && !mixinDetails[1].isEmberMixin) {
mixinDetails[1].expand = true;
}
fixMandatorySetters(mixinDetails);
applyMixinOverrides(mixinDetails);
let propertyInfo = null;
let debugInfo = getDebugInfo(object);
if (debugInfo) {
propertyInfo = getDebugInfo(object).propertyInfo;
mixinDetails = customizeProperties(mixinDetails, propertyInfo);
}
let expensiveProperties = null;
if (propertyInfo) {
expensiveProperties = propertyInfo.expensiveProperties;
}
let objectId = this.retainObject(object);
let errorsForObject = (this._errorsFor[objectId] = {});
const tracked = (this.trackedTags[objectId] =
this.trackedTags[objectId] || {});
calculateCPs(
object,
mixinDetails,
errorsForObject,
expensiveProperties,
tracked
);
this.currentObject = { object, mixinDetails, objectId };
let errors = errorsToSend(errorsForObject);
return { objectId, mixins: mixinDetails, errors };
}
valueForObjectProperty(objectId, property, mixinIndex) {
let object = this.sentObjects[objectId],
value;
if (object.isDestroying) {
value = '<DESTROYED>';
} else {
value = calculateCP(
object,
{ name: property },
this._errorsFor[objectId]
);
}
if (!value || !(value instanceof CalculateCPError)) {
value = inspectValue(object, property, value);
value.isCalculated = true;
return { objectId, property, value, mixinIndex };
}
}
inspect = inspect;
inspectValue = inspectValue;
}
function ownMixins(object) {
// TODO: We need to expose an API for getting _just_ the own mixins directly
let meta = emberMeta(object);
let parentMeta = meta.parent;
let mixins = new Set();
// Filter out anonymous mixins that are directly in a `class.extend`
let baseMixins =
object.constructor &&
object.constructor.PrototypeMixin &&
object.constructor.PrototypeMixin.mixins;
meta.forEachMixins((m) => {
// Find mixins that:
// - Are not in the parent classes
// - Are not primitive (has mixins, doesn't have properties)
// - Don't include any of the base mixins from a class extend
if (
(!parentMeta || !parentMeta.hasMixin(m)) &&
!m.properties &&
m.mixins &&
(!baseMixins || !m.mixins.some((m) => baseMixins.includes(m)))
) {
mixins.add(m);
}
});
return mixins;
}
function ownProperties(object, ownMixins) {
let meta = emberMeta(object);
if (Array.isArray(object)) {
// slice to max 101, for performance and so that the object inspector will show a `more items` indicator above 100
object = object.slice(0, 101);
}
let props = Object.getOwnPropertyDescriptors(object);
delete props.constructor;
// meta has the correct descriptors for CPs
meta.forEachDescriptors((name, desc) => {
// only for own properties
if (props[name]) {
props[name] = desc;
}
});
// remove properties set by mixins
// especially for Object.extend(mixin1, mixin2), where a new class is created which holds the merged properties
// if all properties are removed, it will be marked as useless mixin and will not be shown
ownMixins.forEach((m) => {
if (m.mixins) {
m.mixins.forEach((mix) => {
Object.keys(mix.properties || {}).forEach((k) => {
const pDesc = Object.getOwnPropertyDescriptor(mix.properties, k);
if (pDesc && props[k] && pDesc.get && pDesc.get === props[k].get) {
delete props[k];
}
if (
pDesc &&
props[k] &&
'value' in pDesc &&
pDesc.value === props[k].value
) {
delete props[k];
}
if (pDesc && props[k] && pDesc._getter === props[k]._getter) {
delete props[k];
}
});
});
}
});
Object.keys(props).forEach((k) => {
if (typeof props[k].value === 'function') {
return;
}
props[k].isDescriptor = true;
});
// Clean the properties, removing private props and bindings, etc
return addProperties([], props);
}
function propertiesForMixin(mixin) {
let properties = [];
if (mixin.mixins) {
mixin.mixins.forEach((mixin) => {
if (mixin.properties) {
addProperties(properties, mixin.properties);
}
});
}
return properties;
}
function addProperties(properties, hash) {
for (let prop in hash) {
if (!hash.hasOwnProperty(prop)) {
continue;
}
if (isInternalProperty(prop)) {
continue;
}
// remove `fooBinding` type props
if (prop.match(/Binding$/)) {
continue;
}
// when mandatory setter is removed, an `undefined` value may be set
const desc =
getDescriptorFor(hash, prop) ||
Object.getOwnPropertyDescriptor(hash, prop);
if (!desc) continue;
if (
hash[prop] === undefined &&
desc.value === undefined &&
!desc.get &&
!desc._getter
) {
continue;
}
let options = { isMandatorySetter: isMandatorySetter(desc) };
if (typeof hash[prop] === 'object' && hash[prop] !== null) {
options.isService =
!('type' in hash[prop]) && hash[prop].type === 'service';
if (!options.isService) {
if (hash[prop].constructor) {
options.isService = hash[prop].constructor.isServiceFactory;
}
}
if (!options.isService) {
options.isService = desc.value instanceof Ember.Service;
}
}
if (options.isService) {
replaceProperty(properties, prop, inspectValue(hash, prop), options);
continue;
}
if (isComputed(hash, prop)) {
options.isComputed = true;
options.dependentKeys = (desc._dependentKeys || []).map((key) =>
key.toString()
);
if (typeof desc.get === 'function') {
options.code = Function.prototype.toString.call(desc.get);
}
if (typeof desc._getter === 'function') {
options.isCalculated = true;
options.code = Function.prototype.toString.call(desc._getter);
}
if (!options.code) {
options.code = '';
}
options.readOnly = desc._readOnly;
options.auto = desc._auto;
options.canTrack = options.code !== '';
}
if (desc.get) {
options.isGetter = true;
options.canTrack = true;
if (!desc.set) {
options.readOnly = true;
}