Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 43a9142

Browse files
committed
Merge pull request #7190 from adobe/nj/deprecate-global-cm
Add deprecation warning for global CodeMirror the first time it's called from a given location
2 parents 4ab72eb + 941fdd2 commit 43a9142

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

src/brackets.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,22 @@ define(function (require, exports, module) {
100100
ColorUtils = require("utils/ColorUtils"),
101101
CodeInspection = require("language/CodeInspection"),
102102
NativeApp = require("utils/NativeApp"),
103+
DeprecationWarning = require("utils/DeprecationWarning"),
103104
ViewCommandHandlers = require("view/ViewCommandHandlers"),
104105
_ = require("thirdparty/lodash");
105-
106+
107+
// DEPRECATED: In future we want to remove the global CodeMirror, but for now we
108+
// expose our required CodeMirror globally so as to avoid breaking extensions in the
109+
// interim.
110+
var CodeMirror = require("thirdparty/CodeMirror2/lib/codemirror");
111+
112+
Object.defineProperty(window, "CodeMirror", {
113+
get: function () {
114+
DeprecationWarning.deprecationWarning('Use brackets.getModule("thirdparty/CodeMirror2/lib/codemirror") instead of global CodeMirror.', true);
115+
return CodeMirror;
116+
}
117+
});
118+
106119
// Load modules that self-register and just need to get included in the main project
107120
require("command/DefaultMenus");
108121
require("document/ChangedDocumentTracker");

src/utils/DeprecationWarning.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,35 @@ define(function (require, exports, module) {
5858
* Show deprecation message with the call stack if it
5959
* has never been displayed before.
6060
* @param {!string} message The deprecation message to be displayed.
61+
* @param {boolean=} oncePerCaller If true, displays the message once for each unique call location.
62+
* If false (the default), only displays the message once no matter where it's called from.
63+
* Note that setting this to true can cause a slight performance hit (because it has to generate
64+
* a stack trace), so don't set this for functions that you expect to be called from performance-
65+
* sensitive code (e.g. tight loops).
6166
*/
62-
function deprecationWarning(message) {
63-
// If we have displayed this message before, then don't
64-
// show it again.
65-
if (!message || displayedWarnings[message]) {
67+
function deprecationWarning(message, oncePerCaller) {
68+
// If oncePerCaller isn't set, then only show the message once no matter who calls it.
69+
if (!message || (!oncePerCaller && displayedWarnings[message])) {
6670
return;
6771
}
6872

69-
console.warn(message + "\n" + _trimStack(new Error().stack));
70-
displayedWarnings[message] = true;
73+
// Don't show the warning again if we've already gotten it from the current caller.
74+
// The true caller location is the fourth line in the stack trace:
75+
// * 0 is the word "Error"
76+
// * 1 is this function
77+
// * 2 is the caller of this function (the one throwing the deprecation warning)
78+
// * 3 is the actual caller of the deprecated function.
79+
var stack = new Error().stack,
80+
callerLocation = stack.split("\n")[3];
81+
if (oncePerCaller && displayedWarnings[message] && displayedWarnings[message][callerLocation]) {
82+
return;
83+
}
84+
85+
console.warn(message + "\n" + _trimStack(stack));
86+
if (!displayedWarnings[message]) {
87+
displayedWarnings[message] = {};
88+
}
89+
displayedWarnings[message][callerLocation] = true;
7190
}
7291

7392
// Define public API

0 commit comments

Comments
 (0)