-
Notifications
You must be signed in to change notification settings - Fork 630
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 10 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
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,16 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
import { warnDeprecatedApi } from "../warn_deprecated_api.ts"; | ||
|
||
function fn() { | ||
warnDeprecatedApi({ | ||
name: "fn()", | ||
stack: new Error().stack!, | ||
version: "1.0.0", | ||
message: "Use `y` instead.", | ||
}); | ||
console.log("Hello, world!"); | ||
} | ||
|
||
for (let i = 0; i < 2; i++) { | ||
fn(); | ||
} |
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,47 @@ | ||
// 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. */ | ||
name: string; | ||
/** The stack trace of the deprecated API. */ | ||
stack: string; | ||
/** The version in which the API will be removed. */ | ||
version: string; | ||
/** An optional message to print. */ | ||
message?: 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 warnDeprecatedApi(config: WarnDeprecatedApiConfig) { | ||
const variable = "NO_DEPRECATION_WARNINGS"; | ||
if ( | ||
Deno?.permissions.querySync({ name: "env", variable }).state === | ||
"granted" && Deno?.env.get(variable) === "1" | ||
) return; | ||
|
||
// Remove the prepending "Error\n" from the stack output. | ||
const stack = config.stack.slice(6); | ||
const key = config.name + stack; | ||
iuioiua marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (ALREADY_WARNED_DEPRECATED.has(key)) return; | ||
ALREADY_WARNED_DEPRECATED.add(key); | ||
|
||
console.log( | ||
"%cWarning", | ||
"color: yellow;", | ||
`Use of deprecated API \`${config.name}\`. This API will be removed in ${config.version} of the Deno Standard Library.`, | ||
config.message ?? "", | ||
"\n" + stack, | ||
); | ||
} |
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.
Does having an
internal
folder interfere with workspaces functionality, @lucacasonato?