fix: improve type safety for browser-compatible modules (#995)

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
Jesse Jackson 2021-07-05 21:15:37 -05:00 committed by GitHub
parent ca602b56f6
commit 91cd23ab81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 36 additions and 8 deletions

View File

@ -33,6 +33,11 @@ jobs:
- name: Run tests
run: deno test --unstable --allow-all
- name: Type check browser compatible modules
shell: bash
run: |
git grep --name-only "// This module is browser compatible." | grep -v ".github/workflows" | xargs deno cache --config browser-compat.tsconfig.json
- name: Generate lcov
run: deno coverage --unstable --lcov ./cov > cov.lcov

View File

@ -2,12 +2,14 @@
// This module is browser compatible.
export const osType = (() => {
if (globalThis.Deno != null) {
// deno-lint-ignore no-explicit-any
const { Deno } = globalThis as any;
if (typeof Deno?.build?.os === "string") {
return Deno.build.os;
}
// deno-lint-ignore no-explicit-any
const navigator = (globalThis as any).navigator;
const { navigator } = globalThis as any;
if (navigator?.appVersion?.includes?.("Win") ?? false) {
return "windows";
}

View File

@ -0,0 +1,9 @@
{
"compilerOptions": {
"lib": [
"DOM",
"DOM.Iterable",
"ESNext"
]
}
}

View File

@ -12,7 +12,11 @@
//
// This module is browser compatible.
const noColor = globalThis.Deno?.noColor ?? true;
// deno-lint-ignore no-explicit-any
const { Deno } = globalThis as any;
const noColor = typeof Deno?.noColor === "boolean"
? Deno.noColor as boolean
: true;
interface Code {
open: string;

View File

@ -21,7 +21,7 @@ export interface GlobOptions {
/** Whether globstar should be case insensitive. */
caseInsensitive?: boolean;
/** Operating system. Defaults to the native OS. */
os?: typeof Deno.build.os;
os?: "darwin" | "linux" | "windows";
}
export type GlobToRegExpOptions = GlobOptions;

View File

@ -30,7 +30,9 @@ export function resolve(...pathSegments: string[]): string {
if (i >= 0) path = pathSegments[i];
else {
if (globalThis.Deno == null) {
// deno-lint-ignore no-explicit-any
const { Deno } = globalThis as any;
if (typeof Deno?.cwd !== "function") {
throw new TypeError("Resolved a relative path without a CWD.");
}
path = Deno.cwd();

View File

@ -34,15 +34,19 @@ export function resolve(...pathSegments: string[]): string {
for (let i = pathSegments.length - 1; i >= -1; i--) {
let path: string;
// deno-lint-ignore no-explicit-any
const { Deno } = globalThis as any;
if (i >= 0) {
path = pathSegments[i];
} else if (!resolvedDevice) {
if (globalThis.Deno == null) {
if (typeof Deno?.cwd !== "function") {
throw new TypeError("Resolved a drive-letter-less path without a CWD.");
}
path = Deno.cwd();
} else {
if (globalThis.Deno == null) {
if (
typeof Deno?.env?.get !== "function" || typeof Deno?.cwd !== "function"
) {
throw new TypeError("Resolved a relative path without a CWD.");
}
// Windows has the concept of drive-specific current working

View File

@ -34,7 +34,9 @@ export class AssertionError extends Error {
* @param v Value to be formatted
*/
export function _format(v: unknown): string {
return globalThis.Deno
// deno-lint-ignore no-explicit-any
const { Deno } = globalThis as any;
return typeof Deno?.inspect === "function"
? Deno.inspect(v, {
depth: Infinity,
sorted: true,