Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
refactor: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 7, 2020
1 parent a8965b5 commit 1a2b7b6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
12 changes: 11 additions & 1 deletion core/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
str2regexpStr,
isHttpURL,
hashURL,
normalizeFilepath
normalizeFilepath,
isValidDenoDocument
} from "./util";

test("core / util / pathExists", async () => {
Expand Down Expand Up @@ -54,3 +55,12 @@ test("core / util / normalizeFilepath", () => {

expect(normalizeFilepath("d:\\path\\to\\file")).toEqual("D:\\path\\to\\file");
});

test("core / util / isValidDenoDocument", () => {
expect(isValidDenoDocument("foo")).toBe(false);
expect(isValidDenoDocument("bar")).toBe(false);
expect(isValidDenoDocument("javascript")).toBe(true);
expect(isValidDenoDocument("javascriptreact")).toBe(true);
expect(isValidDenoDocument("typescript")).toBe(true);
expect(isValidDenoDocument("typescriptreact")).toBe(true);
});
9 changes: 9 additions & 0 deletions core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ export function hashURL(url: URL): string {
.update(url.pathname + url.search)
.digest("hex");
}

export function isValidDenoDocument(languageID: string): boolean {
return [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
].includes(languageID);
}
4 changes: 2 additions & 2 deletions server/src/language/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
Location
} from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";
import * as ts from "typescript";
import { URI } from "vscode-uri";

import { getDenoTypesHintsFromDocument } from "../deno_types";
import { pathExists } from "../../../core/util";

export class Definition {
constructor(connection: IConnection, documents: TextDocuments<TextDocument>) {
Expand All @@ -33,7 +33,7 @@ export class Definition {
position.character >= start.character &&
position.character <= end.character
) {
if (ts.sys.fileExists(typeComment.filepath)) {
if ((await pathExists(typeComment.filepath)) === true) {
locations.push(
Location.create(
URI.file(typeComment.filepath).toString(),
Expand Down
4 changes: 2 additions & 2 deletions server/src/language/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { localize } from "vscode-nls-i18n";

import { Bridge } from "../bridge";
import { ModuleResolver } from "../../../core/module_resolver";
import { pathExists, isHttpURL } from "../../../core/util";
import { pathExists, isHttpURL, isValidDenoDocument } from "../../../core/util";
import { ImportMap } from "../../../core/import_map";
import { getImportModules } from "../../../core/deno_deps";

Expand Down Expand Up @@ -108,7 +108,7 @@ export class Diagnostics {
documents.onDidChangeContent(params => this.diagnosis(params.document));
}
async generate(document: TextDocument): Promise<Diagnostic[]> {
if (!["typescript", "typescriptreact"].includes(document.languageId)) {
if (!isValidDenoDocument(document.languageId)) {
return [];
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/language/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
Location
} from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";
import * as ts from "typescript";
import { URI } from "vscode-uri";

import { getDenoTypesHintsFromDocument } from "../deno_types";
import { pathExists } from "../../../core/util";

export class References {
constructor(connection: IConnection, documents: TextDocuments<TextDocument>) {
Expand All @@ -33,7 +33,7 @@ export class References {
position.character >= start.character &&
position.character <= end.character
) {
if (ts.sys.fileExists(typeComment.filepath)) {
if ((await pathExists(typeComment.filepath)) === true) {
locations.push(
Location.create(
URI.file(typeComment.filepath).toString(),
Expand Down

0 comments on commit 1a2b7b6

Please sign in to comment.