-
Notifications
You must be signed in to change notification settings - Fork 577
/
check_deprecation.ts
127 lines (113 loc) · 3.5 KB
/
check_deprecation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { VERSION } from "../version.ts";
import * as semver from "../semver/mod.ts";
import * as colors from "../fmt/colors.ts";
import { doc } from "https://deno.land/x/deno_doc@0.59.0/mod.ts";
import { walk } from "../fs/walk.ts";
import { toFileUrl } from "../path/mod.ts";
const EXTENSIONS = [".mjs", ".js", ".ts"];
const EXCLUDED_PATHS = [
".git",
"dotenv/testdata",
"fs/testdata",
"http/testdata",
"http/_negotiation",
"crypto/_wasm",
"crypto/_fnv",
"encoding/_yaml",
"encoing/_toml",
"_tools",
"_util",
];
const ROOT = new URL("../", import.meta.url);
const FAIL_FAST = Deno.args.includes("--fail-fast");
const DEPRECATION_IN_FORMAT_REGEX =
/^\(will be removed in (?<version>\d+\.\d+\.\d+)\)/;
const DEPRECATION_AFTER_FORMAT_REGEX =
/^\(will be removed after (?<version>\d+\.\d+\.\d+)\)/;
let shouldFail = false;
// add three minor version to current version
const DEFAULT_DEPRECATED_VERSION = semver.increment(
semver.increment(
semver.increment(
VERSION,
"minor",
)!,
"minor",
)!,
"minor",
);
const DEPRECATION_IN_FORMAT =
`(will be removed in ${DEFAULT_DEPRECATED_VERSION})`;
for await (
const { path } of walk(ROOT, {
includeDirs: false,
exts: EXTENSIONS,
skip: EXCLUDED_PATHS.map((path) => new RegExp(path + "$")),
})
) {
// deno_doc only takes urls.
const url = toFileUrl(path);
const docs = await doc(url.href);
for (const d of docs) {
const tags = d.jsDoc?.tags;
if (tags) {
for (const tag of tags) {
switch (tag.kind) {
case "deprecated": {
const message = tag.doc;
if (!message) {
console.error(
colors.red("Error"),
`${
colors.bold("@deprecated")
} tag must have a version: ${path}:${d.location.line}`,
);
shouldFail = true;
if (FAIL_FAST) Deno.exit(1);
continue;
}
const { version: afterVersion } =
DEPRECATION_AFTER_FORMAT_REGEX.exec(message)?.groups || {};
if (afterVersion) {
if (semver.lt(afterVersion, VERSION)) {
console.warn(
colors.yellow("Warn"),
`${
colors.bold("@deprecated")
} tag is expired and export should be removed: ${path}:${d.location.line}`,
);
}
continue;
}
const { version: inVersion } =
DEPRECATION_IN_FORMAT_REGEX.exec(message)?.groups || {};
if (!inVersion) {
console.error(
colors.red("Error"),
`${
colors.bold("@deprecated")
} tag version is missing. Append '${DEPRECATION_IN_FORMAT}' after @deprecated tag: ${path}:${d.location.line}`,
);
shouldFail = true;
if (FAIL_FAST) Deno.exit(1);
continue;
}
if (!semver.gt(inVersion, VERSION)) {
console.error(
colors.red("Error"),
`${
colors.bold("@deprecated")
} tag is expired and export must be removed: ${path}:${d.location.line}`,
);
if (FAIL_FAST) Deno.exit(1);
shouldFail = true;
continue;
}
}
}
}
}
}
}
if (shouldFail) Deno.exit(1);