Skip to content

Commit

Permalink
Remove mixed enums; add ast-grep (#116)
Browse files Browse the repository at this point in the history
* Add AST Grep

* Add first ast-grep rule and CI

* Remove mixed enums

* [Automated] Update generated code

* Update deno client
  • Loading branch information
zephraph authored Feb 7, 2025
1 parent 78abe5d commit 019ef30
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 132 deletions.
11 changes: 11 additions & 0 deletions mise.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[tools]
deno = "2.1.8"
"npm:@ast-grep/cli" = "0.32.2"
rust = { version = "1.78.0", postinstall = "rustup component add rustfmt clippy rust-analyzer" }

[settings]
Expand Down Expand Up @@ -94,6 +95,16 @@ description = "Run deno lint"
dir = "src/clients/deno"
run = ["deno lint", "deno check ."]

[tasks."lint:ast-grep"]
description = "Run ast-grep lint"
run = """
{% set format = '' %}
{% if get_env(name='CI', default='') == 'true' %}
{% set format = ' --format=github' %}
{% endif %}
sg scan {{format}} .
"""

[tasks."lint"]
description = "Run all linting tasks"
depends = ["lint:*"]
Expand Down
49 changes: 26 additions & 23 deletions schemas/WebViewMessage.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 20 additions & 15 deletions schemas/WebViewOptions.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions schemas/WebViewRequest.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 26 additions & 23 deletions schemas/WebViewResponse.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added sg/rules/.gitkeep
Empty file.
20 changes: 20 additions & 0 deletions sg/rules/no-mixed-enums.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
id: no-mixed-enum
language: rust
severity: error
message: Don't mix call style and object style enum variants
rule:
matches: enum
all:
- has:
stopBy: end
all:
- kind: field_declaration_list
- has:
stopBy: end
all:
- kind: ordered_field_declaration_list
utils:
enum:
any:
- pattern: enum $VARIANT{$$$BODY}
- pattern: pub enum $VARIANT{$$$BODY}
2 changes: 2 additions & 0 deletions sgconfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ruleDirs:
- ./sg/rules
3 changes: 1 addition & 2 deletions src/clients/deno/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,10 @@ export class WebView implements Disposable {
$type: "getSize",
include_decorations: includeDecorations,
});
const { width, height, scale_factor: scaleFactor } = returnResult(
return returnResult(
result,
"size",
);
return { width, height, scaleFactor };
}

/**
Expand Down
33 changes: 17 additions & 16 deletions src/clients/deno/schemas/WebViewMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ export type Notification =
$type: "closed";
};

export type SizeWithScale = {
/** The height of the window in logical pixels. */
height: number;
/** The ratio between physical and logical sizes. */
scaleFactor: number;
/** The width of the window in logical pixels. */
width: number;
};

/**
* Types that can be returned from webview results.
*/
Expand All @@ -41,14 +50,7 @@ export type ResultType =
| {
$type: "size";

value: {
/** The height of the window in logical pixels. */
height: number;
/** The ratio between physical and logical sizes. */
scale_factor: number;
/** The width of the window in logical pixels. */
width: number;
};
value: SizeWithScale;
};

/**
Expand Down Expand Up @@ -99,18 +101,17 @@ export const Notification: z.ZodType<Notification> = z.discriminatedUnion(
],
);

export const SizeWithScale: z.ZodType<SizeWithScale> = z.object({
height: z.number(),
scaleFactor: z.number(),
width: z.number(),
});

export const ResultType: z.ZodType<ResultType> = z.discriminatedUnion("$type", [
z.object({ $type: z.literal("string"), value: z.string() }),
z.object({ $type: z.literal("boolean"), value: z.boolean() }),
z.object({ $type: z.literal("float"), value: z.number() }),
z.object({
$type: z.literal("size"),
value: z.object({
height: z.number(),
scale_factor: z.number(),
width: z.number(),
}),
}),
z.object({ $type: z.literal("size"), value: SizeWithScale }),
]);

export const Response: z.ZodType<Response> = z.discriminatedUnion("$type", [
Expand Down
19 changes: 14 additions & 5 deletions src/clients/deno/schemas/WebViewOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// DO NOT EDIT: This file is auto-generated by generate-schema/index.ts
import { z } from "npm:zod";

export type Size = {
/** The height of the window in logical pixels. */
height: number;
/** The width of the window in logical pixels. */
width: number;
};

/**
* The content to load into the webview.
*/
Expand All @@ -19,10 +26,7 @@ export type WebViewContent =
};

export type WindowSizeStates = "maximized" | "fullscreen";
export type WindowSize = WindowSizeStates | {
height: number;
width: number;
};
export type WindowSize = WindowSizeStates | Size;

/**
* Options for creating a webview.
Expand Down Expand Up @@ -70,6 +74,11 @@ export type WebViewOptions = {
userAgent?: string;
};

export const Size: z.ZodType<Size> = z.object({
height: z.number(),
width: z.number(),
});

export const WebViewContent: z.ZodType<WebViewContent> = z.union([
z.object({
headers: z.record(z.string(), z.string()).optional(),
Expand All @@ -84,7 +93,7 @@ export const WindowSizeStates: z.ZodType<WindowSizeStates> = z.enum([
]);
export const WindowSize: z.ZodType<WindowSize> = z.union([
WindowSizeStates,
z.object({ height: z.number(), width: z.number() }),
Size,
]);

export const WebViewOptions: z.ZodType<WebViewOptions> = z.object({
Expand Down
Loading

0 comments on commit 019ef30

Please sign in to comment.