2 var Deprecation
, Emitter
, grim
;
4 Emitter
= require('emissary').Emitter
;
6 Deprecation
= require('./deprecation');
8 if (global
.__grim__
== null) {
9 grim
= global
.__grim__
= {
11 includeDeprecatedAPIs: true,
12 getDeprecations: function() {
13 var deprecation
, deprecations
, deprecationsByLineNumber
, deprecationsByPackage
, fileName
, lineNumber
, packageName
, _ref
;
15 _ref
= grim
.deprecations
;
16 for (fileName
in _ref
) {
17 deprecationsByLineNumber
= _ref
[fileName
];
18 for (lineNumber
in deprecationsByLineNumber
) {
19 deprecationsByPackage
= deprecationsByLineNumber
[lineNumber
];
20 for (packageName
in deprecationsByPackage
) {
21 deprecation
= deprecationsByPackage
[packageName
];
22 deprecations
.push(deprecation
);
28 getDeprecationsLength: function() {
29 return this.getDeprecations().length
;
31 clearDeprecations: function() {
32 grim
.deprecations
= {};
34 logDeprecations: function() {
35 var deprecation
, deprecations
, _i
, _len
;
36 deprecations
= this.getDeprecations();
37 deprecations
.sort(function(a
, b
) {
38 return b
.getCallCount() - a
.getCallCount();
40 console
.warn("\nCalls to deprecated functions\n-----------------------------");
41 for (_i
= 0, _len
= deprecations
.length
; _i
< _len
; _i
++) {
42 deprecation
= deprecations
[_i
];
43 console
.warn("(" + (deprecation
.getCallCount()) + ") " + (deprecation
.getOriginName()) + " : " + (deprecation
.getMessage()), deprecation
);
46 deprecate: function(message
, metadata
) {
47 var deprecation
, deprecationSite
, error
, fileName
, lineNumber
, originalPrepareStackTrace
, originalStackTraceLimit
, packageName
, stack
, _base
, _base1
, _base2
, _ref
;
48 originalStackTraceLimit
= Error
.stackTraceLimit
;
49 Error
.stackTraceLimit
= 7;
51 Error
.captureStackTrace(error
);
52 Error
.stackTraceLimit
= originalStackTraceLimit
;
53 originalPrepareStackTrace
= Error
.prepareStackTrace
;
54 Error
.prepareStackTrace = function(error
, stack
) {
57 stack
= error
.stack
.slice(1);
58 Error
.prepareStackTrace
= originalPrepareStackTrace
;
59 deprecationSite
= stack
[0];
60 fileName
= deprecationSite
.getFileName();
61 lineNumber
= deprecationSite
.getLineNumber();
62 packageName
= (_ref
= metadata
!= null ? metadata
.packageName : void 0) != null ? _ref : "";
63 if ((_base
= grim
.deprecations
)[fileName
] == null) {
66 if ((_base1
= grim
.deprecations
[fileName
])[lineNumber
] == null) {
67 _base1
[lineNumber
] = {};
69 if ((_base2
= grim
.deprecations
[fileName
][lineNumber
])[packageName
] == null) {
70 _base2
[packageName
] = new Deprecation(message
);
72 deprecation
= grim
.deprecations
[fileName
][lineNumber
][packageName
];
73 deprecation
.addStack(stack
, metadata
);
74 grim
.emit("updated", deprecation
);
76 addSerializedDeprecation: function(serializedDeprecation
) {
77 var deprecation
, fileName
, lineNumber
, message
, packageName
, stack
, stacks
, _base
, _base1
, _base2
, _i
, _len
, _ref
, _ref1
, _ref2
;
78 deprecation
= Deprecation
.deserialize(serializedDeprecation
);
79 message
= deprecation
.getMessage();
80 fileName
= deprecation
.fileName
, lineNumber
= deprecation
.lineNumber
;
81 stacks
= deprecation
.getStacks();
82 packageName
= (_ref
= (_ref1
= stacks
[0]) != null ? (_ref2
= _ref1
.metadata
) != null ? _ref2
.packageName : void 0 : void 0) != null ? _ref : "";
83 if ((_base
= grim
.deprecations
)[fileName
] == null) {
86 if ((_base1
= grim
.deprecations
[fileName
])[lineNumber
] == null) {
87 _base1
[lineNumber
] = {};
89 if ((_base2
= grim
.deprecations
[fileName
][lineNumber
])[packageName
] == null) {
90 _base2
[packageName
] = new Deprecation(message
, fileName
, lineNumber
);
92 deprecation
= grim
.deprecations
[fileName
][lineNumber
][packageName
];
93 for (_i
= 0, _len
= stacks
.length
; _i
< _len
; _i
++) {
95 deprecation
.addStack(stack
, stack
.metadata
);
97 grim
.emit("updated", deprecation
);
100 Emitter
.extend(grim
);
103 module
.exports
= global
.__grim__
;