Skip to content

Commit

Permalink
fix: improve type safety for browser-compatible modules (#995)
Browse files Browse the repository at this point in the history
Co-authored-by: Yoshiya Hinosawa <[email protected]>
  • Loading branch information
jsejcksn and kt3k authored Jul 6, 2021
1 parent ca602b5 commit 91cd23a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions _util/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
9 changes: 9 additions & 0 deletions browser-compat.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"lib": [
"DOM",
"DOM.Iterable",
"ESNext"
]
}
}
6 changes: 5 additions & 1 deletion fmt/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion path/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion path/posix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions path/win32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion testing/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 91cd23a

Please sign in to comment.