Skip to content

style: run deno fmt #15

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
68 changes: 34 additions & 34 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
name: CI
on: [push, pull_request]
jobs:
CI:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Test
run: deno test --allow-net --allow-read=.
- name: Publish on tag
run: deno run -A jsr:@david/[email protected]
- name: Get tag version
if: startsWith(github.ref, 'refs/tags/')
id: get_tag_version
run: echo ::set-output name=TAG_VERSION::${GITHUB_REF/refs\/tags\//}
- uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- name: npm build
run: deno task build:npm ${{steps.get_tag_version.outputs.TAG_VERSION}}
- name: npm publish
if: startsWith(github.ref, 'refs/tags/')
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
cd npm
npm publish --access public
name: CI
on: [push, pull_request]
jobs:
CI:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Test
run: deno test --allow-net --allow-read=.
- name: Publish on tag
run: deno run -A jsr:@david/[email protected]
- name: Get tag version
if: startsWith(github.ref, 'refs/tags/')
id: get_tag_version
run: echo ::set-output name=TAG_VERSION::${GITHUB_REF/refs\/tags\//}
- uses: actions/setup-node@v4
with:
node-version: "20.x"
registry-url: "https://registry.npmjs.org"
- name: npm build
run: deno task build:npm ${{steps.get_tag_version.outputs.TAG_VERSION}}
- name: npm publish
if: startsWith(github.ref, 'refs/tags/')
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
cd npm
npm publish --access public
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ console.log(tsFormatter.formatText({
}));
```

Using with plugins on npm (ex. [@dprint/json](https://www.npmjs.com/package/@dprint/json)):
Using with plugins on npm (ex.
[@dprint/json](https://www.npmjs.com/package/@dprint/json)):

```ts
import { createFromBuffer } from "@dprint/formatter";
Expand Down
9 changes: 7 additions & 2 deletions common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export interface FormatRequest {

export interface Host {
setInstance(wasmInstance: WebAssembly.Instance): void;
setHostFormatter(formatWithHost: ((request: FormatRequest) => string) | undefined): void;
setHostFormatter(
formatWithHost: ((request: FormatRequest) => string) | undefined,
): void;
createImportObject(): WebAssembly.Imports;
}

Expand Down Expand Up @@ -54,7 +56,10 @@ export interface Formatter {
* @returns The formatted text.
* @throws If there is an error formatting.
*/
formatText(request: FormatRequest, formatWithHost?: (request: FormatRequest) => string): string;
formatText(
request: FormatRequest,
formatWithHost?: (request: FormatRequest) => string,
): string;
}

/** Configuration specified for use across plugins. */
Expand Down
20 changes: 14 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export async function createStreaming(
);
}
if (
typeof WebAssembly.instantiateStreaming === "function"
&& response.headers.get("content-type") === "application/wasm"
typeof WebAssembly.instantiateStreaming === "function" &&
response.headers.get("content-type") === "application/wasm"
) {
// deno-lint-ignore no-explicit-any
const module = await WebAssembly.compileStreaming(response as any);
Expand All @@ -58,7 +58,9 @@ export function createFromBuffer(wasmModuleBuffer: BufferSource): Formatter {
return createFromWasmModule(wasmModule);
}

export function createFromWasmModule(wasmModule: WebAssembly.Module): Formatter {
export function createFromWasmModule(
wasmModule: WebAssembly.Module,
): Formatter {
const version = getModuleVersionOrThrow(wasmModule);
if (version === 3) {
const host = v3.createHost();
Expand All @@ -81,13 +83,19 @@ export function createFromWasmModule(wasmModule: WebAssembly.Module): Formatter
function getModuleVersionOrThrow(module: WebAssembly.Module): 3 | 4 {
const version = getModuleVersion(module);
if (version == null) {
throw new Error("Couldn't determine dprint plugin version. Maybe the js-formatter version is too old?");
throw new Error(
"Couldn't determine dprint plugin version. Maybe the js-formatter version is too old?",
);
} else if (version === 3 || version === 4) {
return version;
} else if (version > 4) {
throw new Error(`Unsupported new dprint plugin version '${version}'. Maybe the js-formatter version is too old?`);
throw new Error(
`Unsupported new dprint plugin version '${version}'. Maybe the js-formatter version is too old?`,
);
} else {
throw new Error(`Unsupported old dprint plugin version '${version}'. Please upgrade the plugin.`);
throw new Error(
`Unsupported old dprint plugin version '${version}'. Please upgrade the plugin.`,
);
}
}

Expand Down
12 changes: 10 additions & 2 deletions mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { assertEquals } from "@std/assert";
import * as fs from "node:fs";
import { createFromBuffer, createStreaming, type Formatter, type GlobalConfiguration } from "./mod.ts";
import {
createFromBuffer,
createStreaming,
type Formatter,
type GlobalConfiguration,
} from "./mod.ts";

Deno.test("it should create streaming", async () => {
const formatter = await createStreaming(
Expand Down Expand Up @@ -157,7 +162,10 @@ Deno.test("should support v4", () => {
});

// some special config in this plugin
formatter.setConfig({}, { "file_extensions": ["asdf"], "file_names": ["some_name"] });
formatter.setConfig({}, {
"file_extensions": ["asdf"],
"file_names": ["some_name"],
});
assertEquals(formatter.getFileMatchingInfo(), {
fileExtensions: ["asdf"],
fileNames: ["some_name"],
Expand Down
5 changes: 4 additions & 1 deletion scripts/build_npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ await build({
postBuild: () => {
for (const location of wasmFileLocations) {
Deno.mkdirSync(location, { recursive: true });
Deno.copyFileSync("test/test_plugin_v4.wasm", location + "/test_plugin_v4.wasm");
Deno.copyFileSync(
"test/test_plugin_v4.wasm",
location + "/test_plugin_v4.wasm",
);
}
},
package: {
Expand Down
62 changes: 47 additions & 15 deletions v3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { FormatRequest, Formatter, GlobalConfiguration, Host, PluginInfo } from "./common.ts";
import type {
FormatRequest,
Formatter,
GlobalConfiguration,
Host,
PluginInfo,
} from "./common.ts";

const decoder = new TextDecoder();
const encoder = new TextEncoder();
Expand All @@ -8,7 +14,8 @@ const encoder = new TextEncoder();
*/
export function createHost(): Host {
let instance: WebAssembly.Instance;
let hostFormatter: ((request: FormatRequest) => string) | undefined = undefined;
let hostFormatter: ((request: FormatRequest) => string) | undefined =
undefined;

let overrideConfig = {};
let filePath = "";
Expand Down Expand Up @@ -37,11 +44,20 @@ export function createHost(): Host {
resetSharedBuffer(length);
},
"host_read_buffer": (pointer: number, length: number) => {
sharedBuffer.set(getWasmBufferAtPointer(instance, pointer, length), sharedBufferIndex);
sharedBuffer.set(
getWasmBufferAtPointer(instance, pointer, length),
sharedBufferIndex,
);
sharedBufferIndex += length;
},
"host_write_buffer": (pointer: number, index: number, length: number) => {
getWasmBufferAtPointer(instance, pointer, length).set(sharedBuffer.slice(index, index + length));
"host_write_buffer": (
pointer: number,
index: number,
length: number,
) => {
getWasmBufferAtPointer(instance, pointer, length).set(
sharedBuffer.slice(index, index + length),
);
},
"host_take_file_path": () => {
filePath = decoder.decode(sharedBuffer);
Expand Down Expand Up @@ -108,13 +124,13 @@ export function createFromInstance(
const pluginSchemaVersion = get_plugin_schema_version();
const expectedPluginSchemaVersion = 3;
if (
pluginSchemaVersion !== 2
&& pluginSchemaVersion !== expectedPluginSchemaVersion
pluginSchemaVersion !== 2 &&
pluginSchemaVersion !== expectedPluginSchemaVersion
) {
throw new Error(
`Not compatible plugin. `
+ `Expected schema ${expectedPluginSchemaVersion}, `
+ `but plugin had ${pluginSchemaVersion}.`,
`Not compatible plugin. ` +
`Expected schema ${expectedPluginSchemaVersion}, ` +
`but plugin had ${pluginSchemaVersion}.`,
);
}

Expand All @@ -136,7 +152,9 @@ export function createFromInstance(
},
getFileMatchingInfo() {
const length = get_plugin_info();
const pluginInfo = JSON.parse(receiveString(wasmInstance, length)) as PluginInfo;
const pluginInfo = JSON.parse(
receiveString(wasmInstance, length),
) as PluginInfo;
return {
// deno-lint-ignore no-explicit-any
fileExtensions: (pluginInfo as any).fileExtensions ?? [],
Expand Down Expand Up @@ -229,7 +247,11 @@ function sendString(wasmInstance: WebAssembly.Instance, text: string) {
let index = 0;
while (index < length) {
const writeCount = Math.min(length - index, memoryBufferSize);
const wasmBuffer = getWasmBufferAtPointer(wasmInstance, memoryBufferPointer, writeCount);
const wasmBuffer = getWasmBufferAtPointer(
wasmInstance,
memoryBufferPointer,
writeCount,
);
wasmBuffer.set(encodedText.slice(index, index + writeCount));
exports.add_to_shared_bytes_from_buffer(writeCount);
index += writeCount;
Expand All @@ -249,19 +271,29 @@ function receiveString(wasmInstance: WebAssembly.Instance, length: number) {
while (index < length) {
const readCount = Math.min(length - index, memoryBufferSize);
exports.set_buffer_with_shared_bytes(index, readCount);
const wasmBuffer = getWasmBufferAtPointer(wasmInstance, memoryBufferPointer, readCount);
const wasmBuffer = getWasmBufferAtPointer(
wasmInstance,
memoryBufferPointer,
readCount,
);
buffer.set(wasmBuffer, index);
index += readCount;
}
return decoder.decode(buffer);
}

function getWasmMemoryBufferPointer(wasmInstance: WebAssembly.Instance): number {
function getWasmMemoryBufferPointer(
wasmInstance: WebAssembly.Instance,
): number {
// deno-lint-ignore no-explicit-any
return (wasmInstance.exports as any).get_wasm_memory_buffer();
}

function getWasmBufferAtPointer(wasmInstance: WebAssembly.Instance, pointer: number, length: number) {
function getWasmBufferAtPointer(
wasmInstance: WebAssembly.Instance,
pointer: number,
length: number,
) {
return new Uint8Array(
// deno-lint-ignore no-explicit-any
(wasmInstance.exports.memory as any).buffer,
Expand Down
36 changes: 29 additions & 7 deletions v4.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { FormatRequest, Formatter, GlobalConfiguration, Host, PluginInfo } from "./common.ts";
import type {
FormatRequest,
Formatter,
GlobalConfiguration,
Host,
PluginInfo,
} from "./common.ts";
import type { FileMatchingInfo } from "./mod.ts";

const decoder = new TextDecoder();
Expand All @@ -25,7 +31,8 @@ export function createHost(): Host {
}

let instance: WebAssembly.Instance;
let hostFormatter: ((request: FormatRequest) => string) | undefined = undefined;
let hostFormatter: ((request: FormatRequest) => string) | undefined =
undefined;
let formattedText = "";
let errorText = "";

Expand Down Expand Up @@ -57,7 +64,11 @@ export function createHost(): Host {
const iovecBufPtr = dataView.getUint32(iovsOffset, true);
const iovecBufLen = dataView.getUint32(iovsOffset + 4, true);

const buf = new Uint8Array(wasmMemoryBuffer, iovecBufPtr, iovecBufLen);
const buf = new Uint8Array(
wasmMemoryBuffer,
iovecBufPtr,
iovecBufLen,
);

if (fd === 1 || fd === 2) {
// just write both stdout and stderr to stderr
Expand All @@ -77,7 +88,9 @@ export function createHost(): Host {
dprint: {
"host_has_cancelled": () => 0,
"host_write_buffer": (pointer: number) => {
getWasmBufferAtPointer(instance, pointer, sharedBuffer.length).set(sharedBuffer);
getWasmBufferAtPointer(instance, pointer, sharedBuffer.length).set(
sharedBuffer,
);
},
"host_format": (
filePathPtr: number,
Expand All @@ -90,9 +103,14 @@ export function createHost(): Host {
fileBytesLen: number,
) => {
const filePath = receiveString(filePathPtr, filePathLen);
const overrideConfigRaw = receiveString(overrideConfigPtr, overrideConfigLen);
const overrideConfigRaw = receiveString(
overrideConfigPtr,
overrideConfigLen,
);

const overrideConfig = overrideConfigRaw === "" ? {} : JSON.parse(overrideConfigRaw);
const overrideConfig = overrideConfigRaw === ""
? {}
: JSON.parse(overrideConfigRaw);
const fileText = receiveString(fileBytesPtr, fileBytesLen);
const bytesRange = rangeStart === 0 && rangeEnd === fileBytesLen
? undefined
Expand Down Expand Up @@ -248,7 +266,11 @@ export function createFromInstance(
}
}

function getWasmBufferAtPointer(wasmInstance: WebAssembly.Instance, pointer: number, length: number) {
function getWasmBufferAtPointer(
wasmInstance: WebAssembly.Instance,
pointer: number,
length: number,
) {
return new Uint8Array(
// deno-lint-ignore no-explicit-any
(wasmInstance.exports.memory as any).buffer,
Expand Down