Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve type safety for browser-compatible modules #995

Merged
merged 7 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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