Skip to content

Commit

Permalink
add checkbox to enable all rules or not
Browse files Browse the repository at this point in the history
  • Loading branch information
magurotuna committed May 5, 2024
1 parent 5f35f18 commit 5a76d29
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
13 changes: 11 additions & 2 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::diagnostic::LintDiagnostic;
use crate::linter::LintFileOptions;
use crate::linter::LinterBuilder;
use crate::rules::get_all_rules;
use crate::rules::get_recommended_rules;
use deno_ast::MediaType;
use deno_ast::ModuleSpecifier;
Expand All @@ -17,9 +18,17 @@ use wasm_bindgen::prelude::*;
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[wasm_bindgen]
pub fn run(filename: String, source_code: String) -> Result<String, String> {
pub fn run(
filename: String,
source_code: String,
enable_all_rules: bool,
) -> Result<String, String> {
let linter = LinterBuilder::default()
.rules(get_recommended_rules())
.rules(if enable_all_rules {
get_all_rules()
} else {
get_recommended_rules()
})
.build();
let specifier = ModuleSpecifier::parse(&format!("file:///{filename}"))
.map_err(|e| e.to_string())?;
Expand Down
7 changes: 6 additions & 1 deletion www/islands/Linter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { unreachable } from "@std/assert/unreachable";
type Props = {
source: Signal<string>;
language: Signal<SupportedLanguages>;
enableAllRules: Signal<boolean>;
};

type Display = {
Expand Down Expand Up @@ -57,7 +58,11 @@ export default function Linter(props: Props) {

let result;
try {
result = run(filename.value, props.source.value);
result = run(
filename.value,
props.source.value,
props.enableAllRules.value,
);
} catch (e) {
return {
kind: "LintError",
Expand Down
26 changes: 24 additions & 2 deletions www/islands/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function Playground() {
const source = useSignal(defaultSource);
const language = useSignal<SupportedLanguages>("TypeScript");
const isDarkMode = useSignal(false);
const enableAllRules = useSignal(false);

useEffect(() => {
if (!IS_BROWSER) {
Expand All @@ -36,7 +37,7 @@ export default function Playground() {

return (
<div class="flex flex-col w-full h-full md:px-9 md:gap-4">
<div class="flex">
<div class="flex gap-4">
<select
class="dark:bg-[#1e1e1e] border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2.5 dark:border-gray-700 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
defaultValue="typescript"
Expand All @@ -52,6 +53,23 @@ export default function Playground() {
<option value={language}>{language}</option>
))}
</select>
<div class="flex items-center">
<input
type="checkbox"
id="enable-all-rules"
name="all"
checked={enableAllRules.value}
onChange={(e) => {
if (e.target !== null) {
const target = e.target as HTMLInputElement;
enableAllRules.value = target.checked;
}
}}
/>
<label htmlFor="enable-all-rules" class="ml-2">
Enable all rules
</label>
</div>
</div>
<div class="flex w-full h-full md:gap-4">
<MonacoEditor
Expand All @@ -62,7 +80,11 @@ export default function Playground() {
isDarkMode={isDarkMode}
/>
<div class="w-1/2 h-full">
<Linter source={source} language={language} />
<Linter
source={source}
language={language}
enableAllRules={enableAllRules}
/>
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion www/static/deno_lint.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function instantiateWithInstance(opts?: InstantiateOptions): Promise<Inst
/**
* @param {string} filename
* @param {string} source_code
* @param {boolean} enable_all_rules
* @returns {string}
*/
export function run(filename: string, source_code: string): string;
export function run(filename: string, source_code: string, enable_all_rules: boolean): string;
7 changes: 4 additions & 3 deletions www/static/deno_lint.generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// deno-fmt-ignore-file
/// <reference types="./deno_lint.generated.d.ts" />

// source-hash: fef76a37136f707f86dd3e60c4b7b2ab08d42c77
// source-hash: 23d7e72f1076e5a0ba8ef15124c69c56112dc8d0
let wasm;

const cachedTextDecoder = typeof TextDecoder !== "undefined"
Expand Down Expand Up @@ -125,9 +125,10 @@ function takeObject(idx) {
/**
* @param {string} filename
* @param {string} source_code
* @param {boolean} enable_all_rules
* @returns {string}
*/
export function run(filename, source_code) {
export function run(filename, source_code, enable_all_rules) {
let deferred4_0;
let deferred4_1;
try {
Expand All @@ -144,7 +145,7 @@ export function run(filename, source_code) {
wasm.__wbindgen_realloc,
);
const len1 = WASM_VECTOR_LEN;
wasm.run(retptr, ptr0, len0, ptr1, len1);
wasm.run(retptr, ptr0, len0, ptr1, len1, enable_all_rules);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
Expand Down
Binary file modified www/static/deno_lint_bg.wasm
Binary file not shown.

0 comments on commit 5a76d29

Please sign in to comment.