-
Notifications
You must be signed in to change notification settings - Fork 623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: print warning on use of deprecated API #4200
Merged
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
81312d7
feat: `warnDeprecatedApi()`
iuioiua 8d6f032
refactor: fixes and tweaks
iuioiua d6ab55b
refactor: tweaks and improvements
iuioiua 7ec3cf1
refactor: further work
iuioiua 5238cd2
refactor
iuioiua 03154b4
Merge branch 'main' into warn-deprecated-api
iuioiua 37c767a
fix
iuioiua b70dfec
tweaks
iuioiua 7d0dcf0
tweak
iuioiua 8338c67
tweak
iuioiua ad518c8
tweaks
iuioiua 3e0d3d8
tweak
iuioiua 425294f
add test
iuioiua 91cd789
tweaks
iuioiua c291a22
make compatible with Deploy
iuioiua 0337b2e
tweak
iuioiua 78ba272
fix
iuioiua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does having an |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
/** | ||
* Internal utilities for the public API of the Deno Standard Library. | ||
* | ||
* Note: for internal use only. | ||
* | ||
* @module | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
// deno-lint-ignore no-explicit-any | ||
const { Deno } = globalThis as any; | ||
|
||
const ALREADY_WARNED_DEPRECATED = new Set<string>(); | ||
|
||
interface WarnDeprecatedApiConfig { | ||
/** The name of the deprecated API. */ | ||
apiName: string; | ||
/** The stack trace of the deprecated API. */ | ||
stack: string; | ||
/** The version in which the API will be removed. */ | ||
removalVersion: string; | ||
/** An optional message to print. */ | ||
suggestion?: string; | ||
} | ||
|
||
/** | ||
* Prints a warning message to the console for the given deprecated API. | ||
* | ||
* These warnings can be disabled by setting `NO_DEPRECATION_WARNINGS=1` | ||
* in the current process. | ||
*/ | ||
export function warnOnDeprecatedApi(config: WarnDeprecatedApiConfig) { | ||
const variable = "NO_DEPRECATION_WARNINGS"; | ||
if ( | ||
Deno?.permissions.querySync({ name: "env", variable }).state === | ||
iuioiua marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"granted" && Deno?.env.get(variable) === "1" | ||
) return; | ||
|
||
const key = config.apiName + config.stack; | ||
if (ALREADY_WARNED_DEPRECATED.has(key)) return; | ||
|
||
// If we haven't warned yet, let's do some processing of the stack trace | ||
// to make it more useful. | ||
const stackLines = config.stack.split("\n"); | ||
stackLines.shift(); | ||
|
||
let isFromRemoteDependency = false; | ||
const firstStackLine = stackLines[0]; | ||
if (firstStackLine && !firstStackLine.includes("file:")) { | ||
isFromRemoteDependency = true; | ||
} | ||
|
||
ALREADY_WARNED_DEPRECATED.add(key); | ||
console.log( | ||
"%cWarning", | ||
"color: yellow; font-weight: bold;", | ||
); | ||
console.log( | ||
`%c\u251c Use of deprecated "${config.apiName}" API.`, | ||
"color: yellow;", | ||
); | ||
console.log("%c\u2502", "color: yellow;"); | ||
console.log( | ||
`%c\u251c This API will be removed in version ${config.removalVersion} of the Deno Standard Library.`, | ||
"color: yellow;", | ||
); | ||
console.log("%c\u2502", "color: yellow;"); | ||
console.log( | ||
`%c\u251c Suggestion: ${config.suggestion}`, | ||
"color: yellow;", | ||
); | ||
if (isFromRemoteDependency) { | ||
console.log("%c\u2502", "color: yellow;"); | ||
console.log( | ||
`%c\u251c Suggestion: It appears this API is used by a remote dependency.`, | ||
"color: yellow;", | ||
); | ||
console.log( | ||
"%c\u2502 Try upgrading to the latest version of that dependency.", | ||
"color: yellow;", | ||
); | ||
} | ||
|
||
console.log("%c\u2502", "color: yellow;"); | ||
console.log("%c\u2502", "color: yellow;"); | ||
console.log( | ||
"%c\u251c Set `NO_DEPRECATION_WARNINGS=1` to disable these deprecation warnings.", | ||
"color: yellow;", | ||
); | ||
console.log("%c\u2514 Stack trace:", "color: yellow;"); | ||
for (let i = 0; i < stackLines.length; i++) { | ||
console.log( | ||
`%c ${i == stackLines.length - 1 ? "\u2514" : "\u251c"}\u2500 ${ | ||
stackLines[i].trim() | ||
}`, | ||
"color: yellow;", | ||
); | ||
} | ||
console.log(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed for cases where we must still continue to test deprecated APIs.