mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
fix: improve type safety for browser-compatible modules (#995)
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
parent
ca602b56f6
commit
91cd23ab81
5
.github/workflows/ci.yml
vendored
5
.github/workflows/ci.yml
vendored
@ -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
|
||||
|
||||
|
@ -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";
|
||||
}
|
||||
|
9
browser-compat.tsconfig.json
Normal file
9
browser-compat.tsconfig.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": [
|
||||
"DOM",
|
||||
"DOM.Iterable",
|
||||
"ESNext"
|
||||
]
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user