Skip to content

Commit 4d53a1f

Browse files
authored
Move deprecate from Debug to deprecatedCompat (#51522)
1 parent f4ddc1a commit 4d53a1f

File tree

8 files changed

+447
-433
lines changed

8 files changed

+447
-433
lines changed

src/compiler/debug.ts

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ts from "./_namespaces/ts";
22
import {
33
AnyFunction, AssertionLevel, BigIntLiteralType, CheckMode, compareValues, EmitFlags, every, FlowFlags, FlowLabel, FlowNode,
4-
FlowNodeBase, FlowSwitchClause, formatStringFromArgs, getEffectiveModifierFlagsNoCache, getEmitFlags, getOwnKeys,
4+
FlowNodeBase, FlowSwitchClause, getEffectiveModifierFlagsNoCache, getEmitFlags, getOwnKeys,
55
getParseTreeNode, getSourceFileOfNode, getSourceTextOfNodeFromSourceFile, hasProperty, idText, IntrinsicType,
66
isArrayTypeNode, isBigIntLiteral, isCallSignatureDeclaration, isConditionalTypeNode, isConstructorDeclaration,
77
isConstructorTypeNode, isConstructSignatureDeclaration, isDefaultClause, isFunctionTypeNode, isGeneratedIdentifier,
@@ -14,7 +14,7 @@ import {
1414
ObjectFlags, ObjectType, RelationComparisonResult, Signature, SignatureCheckMode,
1515
SignatureFlags, SnippetKind, SortedReadonlyArray, stableSort, Symbol, SymbolFlags, symbolName, SyntaxKind,
1616
TransformFlags, Type, TypeFacts, TypeFlags, TypeMapKind, TypeMapper, unescapeLeadingUnderscores, VarianceFlags,
17-
version, Version, zipWith,
17+
zipWith,
1818
} from "./_namespaces/ts";
1919

2020
/** @internal */
@@ -31,33 +31,16 @@ export interface LoggingHost {
3131
log(level: LogLevel, s: string): void;
3232
}
3333

34-
/** @internal */
35-
export interface DeprecationOptions {
36-
message?: string;
37-
error?: boolean;
38-
since?: Version | string;
39-
warnAfter?: Version | string;
40-
errorAfter?: Version | string;
41-
typeScriptVersion?: Version | string;
42-
name?: string;
43-
}
44-
4534
/** @internal */
4635
export namespace Debug {
47-
let typeScriptVersion: Version | undefined;
48-
4936
/* eslint-disable prefer-const */
5037
let currentAssertionLevel = AssertionLevel.None;
5138
export let currentLogLevel = LogLevel.Warning;
5239
export let isDebugging = false;
5340
export let loggingHost: LoggingHost | undefined;
54-
export let enableDeprecationWarnings = true;
5541
/* eslint-enable prefer-const */
5642

5743
type AssertionKeys = MatchingKeys<typeof Debug, AnyFunction>;
58-
export function getTypeScriptVersion() {
59-
return typeScriptVersion ?? (typeScriptVersion = new Version(version));
60-
}
6144

6245
export function shouldLog(level: LogLevel): boolean {
6346
return currentLogLevel <= level;
@@ -697,58 +680,6 @@ export namespace Debug {
697680
isDebugInfoEnabled = true;
698681
}
699682

700-
function formatDeprecationMessage(name: string, error: boolean | undefined, errorAfter: Version | undefined, since: Version | undefined, message: string | undefined) {
701-
let deprecationMessage = error ? "DeprecationError: " : "DeprecationWarning: ";
702-
deprecationMessage += `'${name}' `;
703-
deprecationMessage += since ? `has been deprecated since v${since}` : "is deprecated";
704-
deprecationMessage += error ? " and can no longer be used." : errorAfter ? ` and will no longer be usable after v${errorAfter}.` : ".";
705-
deprecationMessage += message ? ` ${formatStringFromArgs(message, [name], 0)}` : "";
706-
return deprecationMessage;
707-
}
708-
709-
function createErrorDeprecation(name: string, errorAfter: Version | undefined, since: Version | undefined, message: string | undefined) {
710-
const deprecationMessage = formatDeprecationMessage(name, /*error*/ true, errorAfter, since, message);
711-
return () => {
712-
throw new TypeError(deprecationMessage);
713-
};
714-
}
715-
716-
function createWarningDeprecation(name: string, errorAfter: Version | undefined, since: Version | undefined, message: string | undefined) {
717-
let hasWrittenDeprecation = false;
718-
return () => {
719-
if (enableDeprecationWarnings && !hasWrittenDeprecation) {
720-
log.warn(formatDeprecationMessage(name, /*error*/ false, errorAfter, since, message));
721-
hasWrittenDeprecation = true;
722-
}
723-
};
724-
}
725-
726-
export function createDeprecation(name: string, options: DeprecationOptions & { error: true }): () => never;
727-
export function createDeprecation(name: string, options?: DeprecationOptions): () => void;
728-
export function createDeprecation(name: string, options: DeprecationOptions = {}) {
729-
const version = typeof options.typeScriptVersion === "string" ? new Version(options.typeScriptVersion) : options.typeScriptVersion ?? getTypeScriptVersion();
730-
const errorAfter = typeof options.errorAfter === "string" ? new Version(options.errorAfter) : options.errorAfter;
731-
const warnAfter = typeof options.warnAfter === "string" ? new Version(options.warnAfter) : options.warnAfter;
732-
const since = typeof options.since === "string" ? new Version(options.since) : options.since ?? warnAfter;
733-
const error = options.error || errorAfter && version.compareTo(errorAfter) <= 0;
734-
const warn = !warnAfter || version.compareTo(warnAfter) >= 0;
735-
return error ? createErrorDeprecation(name, errorAfter, since, options.message) :
736-
warn ? createWarningDeprecation(name, errorAfter, since, options.message) :
737-
noop;
738-
}
739-
740-
function wrapFunction<F extends (...args: any[]) => any>(deprecation: () => void, func: F): F {
741-
return function (this: unknown) {
742-
deprecation();
743-
return func.apply(this, arguments);
744-
} as F;
745-
}
746-
747-
export function deprecate<F extends (...args: any[]) => any>(func: F, options?: DeprecationOptions): F {
748-
const deprecation = createDeprecation(options?.name ?? getFunctionName(func), options);
749-
return wrapFunction(deprecation, func);
750-
}
751-
752683
export function formatVariance(varianceFlags: VarianceFlags) {
753684
const variance = varianceFlags & VarianceFlags.VarianceMask;
754685
let result =

0 commit comments

Comments
 (0)