mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 14:52:01 +00:00
Move default options into WASM interface (#1453)
This commit is contained in:
parent
091d36cd30
commit
cd2099f772
20 changed files with 324 additions and 170 deletions
|
@ -164,17 +164,17 @@ pub fn convert(
|
|||
// flake8-quotes
|
||||
"quotes" | "inline-quotes" | "inline_quotes" => match value.trim() {
|
||||
"'" | "single" => flake8_quotes.inline_quotes = Some(Quote::Single),
|
||||
"\"" | "double" => flake8_quotes.inline_quotes = Some(Quote::Single),
|
||||
"\"" | "double" => flake8_quotes.inline_quotes = Some(Quote::Double),
|
||||
_ => eprintln!("Unexpected '{key}' value: {value}"),
|
||||
},
|
||||
"multiline-quotes" | "multiline_quotes" => match value.trim() {
|
||||
"'" | "single" => flake8_quotes.multiline_quotes = Some(Quote::Single),
|
||||
"\"" | "double" => flake8_quotes.multiline_quotes = Some(Quote::Single),
|
||||
"\"" | "double" => flake8_quotes.multiline_quotes = Some(Quote::Double),
|
||||
_ => eprintln!("Unexpected '{key}' value: {value}"),
|
||||
},
|
||||
"docstring-quotes" | "docstring_quotes" => match value.trim() {
|
||||
"'" | "single" => flake8_quotes.docstring_quotes = Some(Quote::Single),
|
||||
"\"" | "double" => flake8_quotes.docstring_quotes = Some(Quote::Single),
|
||||
"\"" | "double" => flake8_quotes.docstring_quotes = Some(Quote::Double),
|
||||
_ => eprintln!("Unexpected '{key}' value: {value}"),
|
||||
},
|
||||
"avoid-escape" | "avoid_escape" => match parser::parse_bool(value.as_ref()) {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { useCallback, useEffect, useState } from "react";
|
||||
import { persist, restore } from "./settings";
|
||||
import { DEFAULT_SETTINGS_SOURCE, DEFAULT_PYTHON_SOURCE } from "../constants";
|
||||
import { DEFAULT_PYTHON_SOURCE } from "../constants";
|
||||
import init, { check, Check, currentVersion, defaultSettings } from "../pkg";
|
||||
import { ErrorMessage } from "./ErrorMessage";
|
||||
import Header from "./Header";
|
||||
import init, { check, current_version, Check } from "../pkg";
|
||||
import { persist, restore, stringify } from "./settings";
|
||||
import SettingsEditor from "./SettingsEditor";
|
||||
import SourceEditor from "./SourceEditor";
|
||||
import Themes from "./Themes";
|
||||
|
@ -52,6 +52,10 @@ export default function Editor() {
|
|||
}, [initialized, settingsSource, pythonSource]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (settingsSource == null || pythonSource == null) {
|
||||
const payload = restore();
|
||||
if (payload) {
|
||||
|
@ -59,18 +63,18 @@ export default function Editor() {
|
|||
setSettingsSource(settingsSource);
|
||||
setPythonSource(pythonSource);
|
||||
} else {
|
||||
setSettingsSource(DEFAULT_SETTINGS_SOURCE);
|
||||
setSettingsSource(stringify(defaultSettings()));
|
||||
setPythonSource(DEFAULT_PYTHON_SOURCE);
|
||||
}
|
||||
}
|
||||
}, [settingsSource, pythonSource]);
|
||||
}, [initialized, settingsSource, pythonSource]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
setVersion(current_version());
|
||||
setVersion(currentVersion());
|
||||
}, [initialized]);
|
||||
|
||||
const handleShare = useCallback(() => {
|
||||
|
|
|
@ -1,38 +1,22 @@
|
|||
import lzstring from "lz-string";
|
||||
import { OptionGroup } from "../ruff_options";
|
||||
|
||||
export type Settings = { [K: string]: any };
|
||||
|
||||
/**
|
||||
* Parse an encoded value from the options export.
|
||||
*
|
||||
* TODO(charlie): Use JSON for the default values.
|
||||
* Stringify a settings object to JSON.
|
||||
*/
|
||||
function parse(value: any): any {
|
||||
if (value == "None") {
|
||||
return null;
|
||||
}
|
||||
return JSON.parse(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The default settings for the playground.
|
||||
*/
|
||||
export function defaultSettings(availableOptions: OptionGroup[]): Settings {
|
||||
const settings: Settings = {};
|
||||
for (const group of availableOptions) {
|
||||
if (group.name == "globals") {
|
||||
for (const field of group.fields) {
|
||||
settings[field.name] = parse(field.default);
|
||||
}
|
||||
export function stringify(settings: Settings): string {
|
||||
return JSON.stringify(
|
||||
settings,
|
||||
(k, v) => {
|
||||
if (v instanceof Map) {
|
||||
return Object.fromEntries(v.entries());
|
||||
} else {
|
||||
settings[group.name] = {};
|
||||
for (const field of group.fields) {
|
||||
settings[group.name][field.name] = parse(field.default);
|
||||
return v;
|
||||
}
|
||||
}
|
||||
}
|
||||
return settings;
|
||||
},
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import { defaultSettings } from "./Editor/settings";
|
||||
import { AVAILABLE_OPTIONS } from "./ruff_options";
|
||||
|
||||
export const DEFAULT_PYTHON_SOURCE =
|
||||
|
@ -32,9 +31,3 @@ export const DEFAULT_PYTHON_SOURCE =
|
|||
"# 13\n" +
|
||||
"# 21\n" +
|
||||
"# 34\n";
|
||||
|
||||
export const DEFAULT_SETTINGS_SOURCE = JSON.stringify(
|
||||
defaultSettings(AVAILABLE_OPTIONS),
|
||||
null,
|
||||
2
|
||||
);
|
||||
|
|
|
@ -5,7 +5,7 @@ use schemars::JsonSchema;
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(
|
||||
Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, JsonSchema,
|
||||
Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions, JsonSchema,
|
||||
)]
|
||||
#[serde(
|
||||
deny_unknown_fields,
|
||||
|
@ -51,7 +51,7 @@ pub struct Options {
|
|||
pub allow_star_arg_any: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Hash, Default)]
|
||||
#[derive(Debug, Default, Hash)]
|
||||
#[allow(clippy::struct_excessive_bools)]
|
||||
pub struct Settings {
|
||||
pub mypy_init_return: bool,
|
||||
|
@ -60,14 +60,24 @@ pub struct Settings {
|
|||
pub allow_star_arg_any: bool,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
pub fn from_options(options: Options) -> Self {
|
||||
impl From<Options> for Settings {
|
||||
fn from(options: Options) -> Self {
|
||||
Self {
|
||||
mypy_init_return: options.mypy_init_return.unwrap_or_default(),
|
||||
suppress_dummy_args: options.suppress_dummy_args.unwrap_or_default(),
|
||||
suppress_none_returning: options.suppress_none_returning.unwrap_or_default(),
|
||||
allow_star_arg_any: options.allow_star_arg_any.unwrap_or_default(),
|
||||
mypy_init_return: options.mypy_init_return.unwrap_or(false),
|
||||
suppress_dummy_args: options.suppress_dummy_args.unwrap_or(false),
|
||||
suppress_none_returning: options.suppress_none_returning.unwrap_or(false),
|
||||
allow_star_arg_any: options.allow_star_arg_any.unwrap_or(false),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Settings> for Options {
|
||||
fn from(settings: Settings) -> Self {
|
||||
Self {
|
||||
mypy_init_return: Some(settings.mypy_init_return),
|
||||
suppress_dummy_args: Some(settings.suppress_dummy_args),
|
||||
suppress_none_returning: Some(settings.suppress_none_returning),
|
||||
allow_star_arg_any: Some(settings.allow_star_arg_any),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ use schemars::JsonSchema;
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(
|
||||
Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, JsonSchema,
|
||||
Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions, JsonSchema,
|
||||
)]
|
||||
#[serde(
|
||||
deny_unknown_fields,
|
||||
|
@ -26,15 +26,23 @@ pub struct Options {
|
|||
pub extend_immutable_calls: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Hash, Default)]
|
||||
#[derive(Debug, Default, Hash)]
|
||||
pub struct Settings {
|
||||
pub extend_immutable_calls: Vec<String>,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn from_options(options: Options) -> Self {
|
||||
impl From<Options> for Settings {
|
||||
fn from(options: Options) -> Self {
|
||||
Self {
|
||||
extend_immutable_calls: options.extend_immutable_calls.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Settings> for Options {
|
||||
fn from(settings: Settings) -> Self {
|
||||
Self {
|
||||
extend_immutable_calls: Some(settings.extend_immutable_calls),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,11 +27,18 @@ pub struct Settings {
|
|||
pub max_string_length: usize,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
pub fn from_options(options: Options) -> Self {
|
||||
impl From<Options> for Settings {
|
||||
fn from(options: Options) -> Self {
|
||||
Self {
|
||||
max_string_length: options.max_string_length.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Settings> for Options {
|
||||
fn from(settings: Settings) -> Self {
|
||||
Self {
|
||||
max_string_length: Some(settings.max_string_length),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,16 +29,14 @@ mod tests {
|
|||
let mut checks = test_path(
|
||||
Path::new("./resources/test/fixtures/flake8_import_conventions/custom.py"),
|
||||
&Settings {
|
||||
flake8_import_conventions:
|
||||
flake8_import_conventions::settings::Settings::from_options(
|
||||
flake8_import_conventions::settings::Options {
|
||||
flake8_import_conventions: flake8_import_conventions::settings::Options {
|
||||
aliases: None,
|
||||
extend_aliases: Some(FxHashMap::from_iter([
|
||||
("dask.array".to_string(), "da".to_string()),
|
||||
("dask.dataframe".to_string(), "dd".to_string()),
|
||||
])),
|
||||
},
|
||||
),
|
||||
}
|
||||
.into(),
|
||||
..Settings::for_rule(CheckCode::ICN001)
|
||||
},
|
||||
)?;
|
||||
|
@ -52,9 +50,7 @@ mod tests {
|
|||
let mut checks = test_path(
|
||||
Path::new("./resources/test/fixtures/flake8_import_conventions/remove_default.py"),
|
||||
&Settings {
|
||||
flake8_import_conventions:
|
||||
flake8_import_conventions::settings::Settings::from_options(
|
||||
flake8_import_conventions::settings::Options {
|
||||
flake8_import_conventions: flake8_import_conventions::settings::Options {
|
||||
aliases: Some(FxHashMap::from_iter([
|
||||
("altair".to_string(), "alt".to_string()),
|
||||
("matplotlib.pyplot".to_string(), "plt".to_string()),
|
||||
|
@ -62,8 +58,8 @@ mod tests {
|
|||
("seaborn".to_string(), "sns".to_string()),
|
||||
])),
|
||||
extend_aliases: None,
|
||||
},
|
||||
),
|
||||
}
|
||||
.into(),
|
||||
..Settings::for_rule(CheckCode::ICN001)
|
||||
},
|
||||
)?;
|
||||
|
@ -77,16 +73,14 @@ mod tests {
|
|||
let mut checks = test_path(
|
||||
Path::new("./resources/test/fixtures/flake8_import_conventions/override_default.py"),
|
||||
&Settings {
|
||||
flake8_import_conventions:
|
||||
flake8_import_conventions::settings::Settings::from_options(
|
||||
flake8_import_conventions::settings::Options {
|
||||
flake8_import_conventions: flake8_import_conventions::settings::Options {
|
||||
aliases: None,
|
||||
extend_aliases: Some(FxHashMap::from_iter([(
|
||||
"numpy".to_string(),
|
||||
"nmp".to_string(),
|
||||
)])),
|
||||
},
|
||||
),
|
||||
}
|
||||
.into(),
|
||||
..Settings::for_rule(CheckCode::ICN001)
|
||||
},
|
||||
)?;
|
||||
|
|
|
@ -84,14 +84,6 @@ fn resolve_aliases(options: Options) -> FxHashMap<String, String> {
|
|||
aliases
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn from_options(options: Options) -> Self {
|
||||
Self {
|
||||
aliases: resolve_aliases(options),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
|
@ -99,3 +91,20 @@ impl Default for Settings {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Options> for Settings {
|
||||
fn from(options: Options) -> Self {
|
||||
Self {
|
||||
aliases: resolve_aliases(options),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Settings> for Options {
|
||||
fn from(settings: Settings) -> Self {
|
||||
Self {
|
||||
aliases: Some(settings.aliases),
|
||||
extend_aliases: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,12 @@ pub enum Quote {
|
|||
Double,
|
||||
}
|
||||
|
||||
impl Default for Quote {
|
||||
fn default() -> Self {
|
||||
Self::Double
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, JsonSchema,
|
||||
)]
|
||||
|
@ -74,24 +80,35 @@ pub struct Settings {
|
|||
pub avoid_escape: bool,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn from_options(options: Options) -> Self {
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
inline_quotes: options.inline_quotes.unwrap_or(Quote::Double),
|
||||
multiline_quotes: options.multiline_quotes.unwrap_or(Quote::Double),
|
||||
docstring_quotes: options.docstring_quotes.unwrap_or(Quote::Double),
|
||||
inline_quotes: Quote::default(),
|
||||
multiline_quotes: Quote::default(),
|
||||
docstring_quotes: Quote::default(),
|
||||
avoid_escape: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Options> for Settings {
|
||||
fn from(options: Options) -> Self {
|
||||
Self {
|
||||
inline_quotes: options.inline_quotes.unwrap_or_default(),
|
||||
multiline_quotes: options.multiline_quotes.unwrap_or_default(),
|
||||
docstring_quotes: options.docstring_quotes.unwrap_or_default(),
|
||||
avoid_escape: options.avoid_escape.unwrap_or(true),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
impl From<Settings> for Options {
|
||||
fn from(settings: Settings) -> Self {
|
||||
Self {
|
||||
inline_quotes: Quote::Double,
|
||||
multiline_quotes: Quote::Double,
|
||||
docstring_quotes: Quote::Double,
|
||||
avoid_escape: true,
|
||||
inline_quotes: Some(settings.inline_quotes),
|
||||
multiline_quotes: Some(settings.multiline_quotes),
|
||||
docstring_quotes: Some(settings.docstring_quotes),
|
||||
avoid_escape: Some(settings.avoid_escape),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,14 +40,6 @@ pub struct Settings {
|
|||
pub ban_relative_imports: Strictness,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn from_options(options: Options) -> Self {
|
||||
Self {
|
||||
ban_relative_imports: options.ban_relative_imports.unwrap_or(Strictness::Parents),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
|
@ -55,3 +47,19 @@ impl Default for Settings {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Options> for Settings {
|
||||
fn from(options: Options) -> Self {
|
||||
Self {
|
||||
ban_relative_imports: options.ban_relative_imports.unwrap_or(Strictness::Parents),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Settings> for Options {
|
||||
fn from(settings: Settings) -> Self {
|
||||
Self {
|
||||
ban_relative_imports: Some(settings.ban_relative_imports),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,16 +22,23 @@ pub struct Options {
|
|||
pub ignore_variadic_names: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Hash, Default)]
|
||||
#[derive(Debug, Default, Hash)]
|
||||
pub struct Settings {
|
||||
pub ignore_variadic_names: bool,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
pub fn from_options(options: Options) -> Self {
|
||||
impl From<Options> for Settings {
|
||||
fn from(options: Options) -> Self {
|
||||
Self {
|
||||
ignore_variadic_names: options.ignore_variadic_names.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Settings> for Options {
|
||||
fn from(settings: Settings) -> Self {
|
||||
Self {
|
||||
ignore_variadic_names: Some(settings.ignore_variadic_names),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -123,8 +123,23 @@ pub struct Settings {
|
|||
pub extra_standard_library: BTreeSet<String>,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn from_options(options: Options) -> Self {
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
combine_as_imports: false,
|
||||
force_wrap_aliases: false,
|
||||
split_on_trailing_comma: true,
|
||||
force_single_line: false,
|
||||
single_line_exclusions: BTreeSet::new(),
|
||||
known_first_party: BTreeSet::new(),
|
||||
known_third_party: BTreeSet::new(),
|
||||
extra_standard_library: BTreeSet::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Options> for Settings {
|
||||
fn from(options: Options) -> Self {
|
||||
Self {
|
||||
combine_as_imports: options.combine_as_imports.unwrap_or(false),
|
||||
force_wrap_aliases: options.force_wrap_aliases.unwrap_or(false),
|
||||
|
@ -142,17 +157,17 @@ impl Settings {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
impl From<Settings> for Options {
|
||||
fn from(settings: Settings) -> Self {
|
||||
Self {
|
||||
combine_as_imports: false,
|
||||
force_wrap_aliases: false,
|
||||
split_on_trailing_comma: true,
|
||||
force_single_line: false,
|
||||
single_line_exclusions: BTreeSet::new(),
|
||||
known_first_party: BTreeSet::new(),
|
||||
known_third_party: BTreeSet::new(),
|
||||
extra_standard_library: BTreeSet::new(),
|
||||
combine_as_imports: Some(settings.combine_as_imports),
|
||||
force_wrap_aliases: Some(settings.force_wrap_aliases),
|
||||
split_on_trailing_comma: Some(settings.split_on_trailing_comma),
|
||||
force_single_line: Some(settings.force_single_line),
|
||||
single_line_exclusions: Some(settings.single_line_exclusions.into_iter().collect()),
|
||||
known_first_party: Some(settings.known_first_party.into_iter().collect()),
|
||||
known_third_party: Some(settings.known_third_party.into_iter().collect()),
|
||||
extra_standard_library: Some(settings.extra_standard_library.into_iter().collect()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,14 +7,20 @@ use wasm_bindgen::prelude::*;
|
|||
|
||||
use crate::autofix::Fix;
|
||||
use crate::checks::CheckCode;
|
||||
use crate::directives;
|
||||
use crate::checks_gen::CheckCodePrefix;
|
||||
use crate::linter::check_path;
|
||||
use crate::rustpython_helpers::tokenize;
|
||||
use crate::settings::configuration::Configuration;
|
||||
use crate::settings::options::Options;
|
||||
use crate::settings::types::PythonVersion;
|
||||
use crate::settings::{flags, Settings};
|
||||
use crate::source_code_locator::SourceCodeLocator;
|
||||
use crate::source_code_style::SourceCodeStyleDetector;
|
||||
use crate::{
|
||||
directives, flake8_annotations, flake8_bugbear, flake8_errmsg, flake8_import_conventions,
|
||||
flake8_quotes, flake8_tidy_imports, flake8_unused_arguments, isort, mccabe, pep8_naming,
|
||||
pydocstyle, pyupgrade,
|
||||
};
|
||||
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
|
@ -62,11 +68,65 @@ pub fn run() {
|
|||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn current_version() -> JsValue {
|
||||
#[allow(non_snake_case)]
|
||||
pub fn currentVersion() -> JsValue {
|
||||
JsValue::from(VERSION)
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[allow(non_snake_case)]
|
||||
pub fn defaultSettings() -> Result<JsValue, JsValue> {
|
||||
Ok(serde_wasm_bindgen::to_value(&Options {
|
||||
// Propagate defaults.
|
||||
allowed_confusables: Some(Vec::default()),
|
||||
dummy_variable_rgx: Some("^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$".to_string()),
|
||||
extend_ignore: Some(Vec::default()),
|
||||
extend_select: Some(Vec::default()),
|
||||
external: Some(Vec::default()),
|
||||
fixable: Some(Vec::default()),
|
||||
ignore: Some(Vec::default()),
|
||||
line_length: Some(88),
|
||||
select: Some(vec![CheckCodePrefix::E, CheckCodePrefix::F]),
|
||||
target_version: Some(PythonVersion::default()),
|
||||
unfixable: Some(Vec::default()),
|
||||
// Ignore a bunch of options that don't make sense in a single-file editor.
|
||||
cache_dir: None,
|
||||
exclude: None,
|
||||
extend: None,
|
||||
extend_exclude: None,
|
||||
fix: None,
|
||||
fix_only: None,
|
||||
force_exclude: None,
|
||||
format: None,
|
||||
ignore_init_module_imports: None,
|
||||
per_file_ignores: None,
|
||||
required_version: None,
|
||||
respect_gitignore: None,
|
||||
show_source: None,
|
||||
src: None,
|
||||
update_check: None,
|
||||
// Use default options for all plugins.
|
||||
flake8_annotations: Some(flake8_annotations::settings::Settings::default().into()),
|
||||
flake8_bugbear: Some(flake8_bugbear::settings::Settings::default().into()),
|
||||
flake8_errmsg: Some(flake8_errmsg::settings::Settings::default().into()),
|
||||
flake8_quotes: Some(flake8_quotes::settings::Settings::default().into()),
|
||||
flake8_tidy_imports: Some(flake8_tidy_imports::settings::Settings::default().into()),
|
||||
flake8_import_conventions: Some(
|
||||
flake8_import_conventions::settings::Settings::default().into(),
|
||||
),
|
||||
flake8_unused_arguments: Some(
|
||||
flake8_unused_arguments::settings::Settings::default().into(),
|
||||
),
|
||||
isort: Some(isort::settings::Settings::default().into()),
|
||||
mccabe: Some(mccabe::settings::Settings::default().into()),
|
||||
pep8_naming: Some(pep8_naming::settings::Settings::default().into()),
|
||||
pydocstyle: Some(pydocstyle::settings::Settings::default().into()),
|
||||
pyupgrade: Some(pyupgrade::settings::Settings::default().into()),
|
||||
})?)
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[allow(non_snake_case)]
|
||||
pub fn check(contents: &str, options: JsValue) -> Result<JsValue, JsValue> {
|
||||
let options: Options = serde_wasm_bindgen::from_value(options).map_err(|e| e.to_string())?;
|
||||
let configuration =
|
||||
|
|
|
@ -30,16 +30,24 @@ pub struct Settings {
|
|||
pub max_complexity: usize,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn from_options(options: &Options) -> Self {
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self { max_complexity: 10 }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Options> for Settings {
|
||||
fn from(options: Options) -> Self {
|
||||
Self {
|
||||
max_complexity: options.max_complexity.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self { max_complexity: 10 }
|
||||
impl From<Settings> for Options {
|
||||
fn from(settings: Settings) -> Self {
|
||||
Self {
|
||||
max_complexity: Some(settings.max_complexity),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,8 +76,18 @@ pub struct Settings {
|
|||
pub staticmethod_decorators: Vec<String>,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn from_options(options: Options) -> Self {
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
ignore_names: IGNORE_NAMES.map(String::from).to_vec(),
|
||||
classmethod_decorators: CLASSMETHOD_DECORATORS.map(String::from).to_vec(),
|
||||
staticmethod_decorators: STATICMETHOD_DECORATORS.map(String::from).to_vec(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Options> for Settings {
|
||||
fn from(options: Options) -> Self {
|
||||
Self {
|
||||
ignore_names: options
|
||||
.ignore_names
|
||||
|
@ -92,12 +102,12 @@ impl Settings {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
impl From<Settings> for Options {
|
||||
fn from(settings: Settings) -> Self {
|
||||
Self {
|
||||
ignore_names: IGNORE_NAMES.map(String::from).to_vec(),
|
||||
classmethod_decorators: CLASSMETHOD_DECORATORS.map(String::from).to_vec(),
|
||||
staticmethod_decorators: STATICMETHOD_DECORATORS.map(String::from).to_vec(),
|
||||
ignore_names: Some(settings.ignore_names),
|
||||
classmethod_decorators: Some(settings.classmethod_decorators),
|
||||
staticmethod_decorators: Some(settings.staticmethod_decorators),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use ruff_macros::ConfigurationOptions;
|
|||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash, JsonSchema)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash, JsonSchema)]
|
||||
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
|
||||
pub enum Convention {
|
||||
/// Use Google-style docstrings.
|
||||
|
@ -37,10 +37,18 @@ pub struct Settings {
|
|||
pub convention: Option<Convention>,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn from_options(options: Options) -> Self {
|
||||
impl From<Options> for Settings {
|
||||
fn from(options: Options) -> Self {
|
||||
Self {
|
||||
convention: options.convention,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Settings> for Options {
|
||||
fn from(settings: Settings) -> Self {
|
||||
Self {
|
||||
convention: settings.convention,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,15 +29,23 @@ pub struct Options {
|
|||
pub keep_runtime_typing: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Hash, Default)]
|
||||
#[derive(Debug, Default, Hash)]
|
||||
pub struct Settings {
|
||||
pub keep_runtime_typing: bool,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn from_options(options: &Options) -> Self {
|
||||
impl From<Options> for Settings {
|
||||
fn from(options: Options) -> Self {
|
||||
Self {
|
||||
keep_runtime_typing: options.keep_runtime_typing.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Settings> for Options {
|
||||
fn from(settings: Settings) -> Self {
|
||||
Self {
|
||||
keep_runtime_typing: Some(settings.keep_runtime_typing),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -153,58 +153,56 @@ impl Settings {
|
|||
src: config
|
||||
.src
|
||||
.unwrap_or_else(|| vec![project_root.to_path_buf()]),
|
||||
target_version: config.target_version.unwrap_or(PythonVersion::Py310),
|
||||
target_version: config.target_version.unwrap_or_default(),
|
||||
update_check: config.update_check.unwrap_or(true),
|
||||
// Plugins
|
||||
flake8_annotations: config
|
||||
.flake8_annotations
|
||||
.map(flake8_annotations::settings::Settings::from_options)
|
||||
.map(std::convert::Into::into)
|
||||
.unwrap_or_default(),
|
||||
flake8_bugbear: config
|
||||
.flake8_bugbear
|
||||
.map(flake8_bugbear::settings::Settings::from_options)
|
||||
.map(std::convert::Into::into)
|
||||
.unwrap_or_default(),
|
||||
flake8_errmsg: config
|
||||
.flake8_errmsg
|
||||
.map(flake8_errmsg::settings::Settings::from_options)
|
||||
.map(std::convert::Into::into)
|
||||
.unwrap_or_default(),
|
||||
flake8_import_conventions: config
|
||||
.flake8_import_conventions
|
||||
.map(flake8_import_conventions::settings::Settings::from_options)
|
||||
.map(std::convert::Into::into)
|
||||
.unwrap_or_default(),
|
||||
flake8_quotes: config
|
||||
.flake8_quotes
|
||||
.map(flake8_quotes::settings::Settings::from_options)
|
||||
.map(std::convert::Into::into)
|
||||
.unwrap_or_default(),
|
||||
flake8_tidy_imports: config
|
||||
.flake8_tidy_imports
|
||||
.map(flake8_tidy_imports::settings::Settings::from_options)
|
||||
.map(std::convert::Into::into)
|
||||
.unwrap_or_default(),
|
||||
flake8_unused_arguments: config
|
||||
.flake8_unused_arguments
|
||||
.map(flake8_unused_arguments::settings::Settings::from_options)
|
||||
.map(std::convert::Into::into)
|
||||
.unwrap_or_default(),
|
||||
isort: config
|
||||
.isort
|
||||
.map(isort::settings::Settings::from_options)
|
||||
.map(std::convert::Into::into)
|
||||
.unwrap_or_default(),
|
||||
mccabe: config
|
||||
.mccabe
|
||||
.as_ref()
|
||||
.map(mccabe::settings::Settings::from_options)
|
||||
.map(std::convert::Into::into)
|
||||
.unwrap_or_default(),
|
||||
pep8_naming: config
|
||||
.pep8_naming
|
||||
.map(pep8_naming::settings::Settings::from_options)
|
||||
.map(std::convert::Into::into)
|
||||
.unwrap_or_default(),
|
||||
pydocstyle: config
|
||||
.pydocstyle
|
||||
.map(pydocstyle::settings::Settings::from_options)
|
||||
.map(std::convert::Into::into)
|
||||
.unwrap_or_default(),
|
||||
pyupgrade: config
|
||||
.pyupgrade
|
||||
.as_ref()
|
||||
.map(pyupgrade::settings::Settings::from_options)
|
||||
.map(std::convert::Into::into)
|
||||
.unwrap_or_default(),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -31,6 +31,12 @@ pub enum PythonVersion {
|
|||
Py311,
|
||||
}
|
||||
|
||||
impl Default for PythonVersion {
|
||||
fn default() -> Self {
|
||||
Self::Py310
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for PythonVersion {
|
||||
type Err = anyhow::Error;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue