Skip to content

chore(website): add new action to playground to fix all eslint errors #6529

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

Merged
Merged
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
28 changes: 28 additions & 0 deletions packages/website/src/components/editor/LoadedEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,34 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
}
}, 150),
),
sandboxInstance.editor.addAction({
id: 'fix-eslint-problems',
label: 'Fix eslint problems',
keybindings: [
sandboxInstance.monaco.KeyMod.CtrlCmd |
sandboxInstance.monaco.KeyCode.KeyS,
],
contextMenuGroupId: 'snippets',
contextMenuOrder: 1.5,
run(editor) {
const editorModel = editor.getModel();
if (editorModel) {
const fixed = webLinter.fix(editor.getValue());
if (fixed.fixed) {
editorModel.pushEditOperations(
null,
[
{
range: editorModel.getFullModelRange(),
text: fixed.output,
},
],
() => null,
);
}
}
},
}),
tabs.eslintrc.onDidChangeContent(
debounce(() => {
onChange({ eslintrc: tabs.eslintrc.getValue() });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function createProvideCodeActions(
for (const marker of context.markers) {
const messages = fixes.get(createURI(marker)) ?? [];
for (const message of messages) {
const editOperation = createEditOperation(model, message);
actions.push({
title: message.message + (message.code ? ` (${message.code})` : ''),
diagnostics: [marker],
Expand All @@ -34,7 +35,11 @@ export function createProvideCodeActions(
edits: [
{
resource: model.uri,
edit: createEditOperation(model, message),
// monaco for ts >= 4.8
// @ts-expect-error types are wrong
textEdit: editOperation,
// monaco for ts < 4.8
edit: editOperation,
},
],
},
Expand Down
14 changes: 11 additions & 3 deletions packages/website/src/components/linter/WebLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,20 @@ export class WebLinter {
});
}

lint(code: string): TSESLint.Linter.LintMessage[] {
return this.linter.verify(code, {
get eslintConfig(): TSESLint.Linter.Config {
return {
parser: PARSER_NAME,
parserOptions: this.parserOptions,
rules: this.rules,
});
};
}

lint(code: string): TSESLint.Linter.LintMessage[] {
return this.linter.verify(code, this.eslintConfig);
}

fix(code: string): TSESLint.Linter.FixReport {
return this.linter.verifyAndFix(code, this.eslintConfig, { fix: true });
}

updateRules(rules: TSESLint.Linter.RulesRecord): void {
Expand Down