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

Commit df4198e

Browse files
author
Narciso Jaramillo
committed
Add deprecation warning for global CodeMirror the first time it's called from a given location
1 parent 098d360 commit df4198e

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

src/brackets.js

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

src/utils/DeprecationWarning.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,31 @@ 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.
6163
*/
62-
function deprecationWarning(message) {
64+
function deprecationWarning(message, oncePerCall) {
65+
// The true caller location is the fourth line in the stack trace:
66+
// * 0 is the word "Error"
67+
// * 1 is this function
68+
// * 2 is the caller of this function (the one throwing the deprecation warning)
69+
// * 3 is the actual caller of the deprecated function.
70+
var stack = new Error().stack,
71+
callerLocation = stack.split("\n")[3];
72+
6373
// If we have displayed this message before, then don't
6474
// show it again.
65-
if (!message || displayedWarnings[message]) {
75+
if (!message ||
76+
(!oncePerCall && displayedWarnings[message]) ||
77+
(oncePerCall && displayedWarnings[message] && displayedWarnings[message][callerLocation])) {
6678
return;
6779
}
6880

69-
console.warn(message + "\n" + _trimStack(new Error().stack));
70-
displayedWarnings[message] = true;
81+
console.warn(message + "\n" + _trimStack(stack));
82+
if (!displayedWarnings[message]) {
83+
displayedWarnings[message] = {};
84+
}
85+
displayedWarnings[message][callerLocation] = true;
7186
}
7287

7388
// Define public API

0 commit comments

Comments
 (0)