Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

docs(website): added checkbox to toggle linter in playground #3699

Merged
merged 3 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion website/src/playground/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export default function PlaygroundLoader({
/>
</TabPanel>
<TabPanel>
<DiagnosticsTab errors={errors} />
<DiagnosticsTab errors={errors} lintingEnabled={settings.enabledLinting} />
</TabPanel>
<TabPanel>
<SyntaxTab ast={ast} cst={cst} ref={astPanelCodeMirrorRef} />
Expand Down
19 changes: 19 additions & 0 deletions website/src/playground/components/SettingsPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default function SettingsPane({
typescript: isTypeScript,
jsx: isJsx,
enabledNurseryRules,
enabledLinting,
},
}: Props) {
const setIsTypeScript = createSetter(setPlaygroundState, "typescript");
Expand All @@ -51,6 +52,7 @@ export default function SettingsPane({
setPlaygroundState,
"enabledNurseryRules",
);
const setEnabledLinting = createSetter(setPlaygroundState, "enabledLinting");

const [isCollapsed, setIsCollapsed] = useState(isCollapsedStore.getBoolean());

Expand Down Expand Up @@ -88,6 +90,8 @@ export default function SettingsPane({
<LinterSettings
enabledNurseryRules={enabledNurseryRules}
setEnabledNurseryRules={setEnabledNurseryRules}
enabledLinting={enabledLinting}
setEnabledLinting={setEnabledLinting}
/>
<SyntaxSettings
sourceType={sourceType}
Expand Down Expand Up @@ -284,20 +288,35 @@ function FormatterSettings({
function LinterSettings({
enabledNurseryRules,
setEnabledNurseryRules,
enabledLinting,
setEnabledLinting,
}: {
enabledNurseryRules: boolean;
setEnabledNurseryRules: (value: boolean) => void;
enabledLinting: boolean;
setEnabledLinting: (value: boolean) => void;
}) {
return (
<>
<h2>Linter</h2>
<section>
<div className="field-row">
<input
id="linting-enabled"
name="linting-enabled"
type="checkbox"
checked={enabledLinting}
onChange={(e) => setEnabledLinting(e.target.checked)}
/>
<label htmlFor="linting-enabled">Linter enabled</label>
</div>
<div className="field-row">
<input
id="nursery-rules"
aria-describedby="nursery-rules-description"
name="nursery-rules"
type="checkbox"
disabled={!enabledLinting}
checked={enabledNurseryRules}
onChange={(e) => setEnabledNurseryRules(e.target.checked)}
/>
Expand Down
6 changes: 5 additions & 1 deletion website/src/playground/tabs/DiagnosticsTab.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
interface Props {
errors: string;
lintingEnabled: boolean;
}

export default function DiagnosticsTab({ errors }: Props) {
export default function DiagnosticsTab({ errors, lintingEnabled }: Props) {
if (!lintingEnabled) {
return <div className="empty-panel">Linter is currently disabled</div>;
}
PatrM marked this conversation as resolved.
Show resolved Hide resolved
if (errors === "") {
return <div className="empty-panel">No diagnostics present</div>;
}
Expand Down
3 changes: 3 additions & 0 deletions website/src/playground/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface PlaygroundState {
jsx: boolean;
cursorPosition: number;
enabledNurseryRules: boolean;
enabledLinting: boolean;
}

// change `lineWidth` and `indentWidth` to string type, just to fits our `usePlaygroundState` fallback usage
Expand All @@ -70,6 +71,7 @@ export const defaultRomeConfig: RomeConfiguration = {
jsx: true,
cursorPosition: 0,
enabledNurseryRules: true,
enabledLinting: true,
};

export interface PlaygroundProps {
Expand All @@ -92,6 +94,7 @@ export type PlaygroundSettings = Pick<
| "typescript"
| "jsx"
| "enabledNurseryRules"
| "enabledLinting"
>;

export type Tree = ReturnType<typeof parser.parse>;
Expand Down
3 changes: 3 additions & 0 deletions website/src/playground/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ export function usePlaygroundState(
enabledNurseryRules:
searchParams.get("enabledNurseryRules") === "true" ||
defaultRomeConfig.enabledNurseryRules,
enabledLinting:
searchParams.get("enabledLinting") === "true" ||
defaultRomeConfig.enabledLinting,
});
const [playgroundState, setPlaygroundState] = useState(
initState(initialSearchParams),
Expand Down
15 changes: 12 additions & 3 deletions website/src/playground/workers/romeWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import init, {
Configuration,
DiagnosticPrinter,
RomePath,
RuleCategories,
Workspace,
} from "@rometools/wasm-web";
import {
Expand Down Expand Up @@ -83,6 +84,7 @@ self.addEventListener("message", async (e) => {
sourceType,
cursorPosition,
enabledNurseryRules,
enabledLinting,
trailingComma,
} = playgroundState;

Expand All @@ -95,7 +97,7 @@ self.addEventListener("message", async (e) => {
indentSize: indentWidth,
},
linter: {
enabled: true,
enabled: enabledLinting,
},
javascript: {
formatter: {
Expand All @@ -110,7 +112,7 @@ self.addEventListener("message", async (e) => {
};
if (enabledNurseryRules) {
configuration.linter = {
enabled: true,
enabled: enabledLinting,
rules: {
nursery: {
noConstAssign: "error",
Expand Down Expand Up @@ -160,9 +162,16 @@ self.addEventListener("message", async (e) => {
path,
});

const categories: RuleCategories = [];
if (configuration.formatter?.enabled) {
categories.push("Syntax");
}
if (configuration.linter?.enabled) {
categories.push("Lint");
}
const diagnostics = workspace.pullDiagnostics({
path,
categories: ["Syntax", "Lint"],
categories: categories,
max_diagnostics: Number.MAX_SAFE_INTEGER,
});

Expand Down