mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 22:55:08 +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
|
// flake8-quotes
|
||||||
"quotes" | "inline-quotes" | "inline_quotes" => match value.trim() {
|
"quotes" | "inline-quotes" | "inline_quotes" => match value.trim() {
|
||||||
"'" | "single" => flake8_quotes.inline_quotes = Some(Quote::Single),
|
"'" | "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}"),
|
_ => eprintln!("Unexpected '{key}' value: {value}"),
|
||||||
},
|
},
|
||||||
"multiline-quotes" | "multiline_quotes" => match value.trim() {
|
"multiline-quotes" | "multiline_quotes" => match value.trim() {
|
||||||
"'" | "single" => flake8_quotes.multiline_quotes = Some(Quote::Single),
|
"'" | "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}"),
|
_ => eprintln!("Unexpected '{key}' value: {value}"),
|
||||||
},
|
},
|
||||||
"docstring-quotes" | "docstring_quotes" => match value.trim() {
|
"docstring-quotes" | "docstring_quotes" => match value.trim() {
|
||||||
"'" | "single" => flake8_quotes.docstring_quotes = Some(Quote::Single),
|
"'" | "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}"),
|
_ => eprintln!("Unexpected '{key}' value: {value}"),
|
||||||
},
|
},
|
||||||
"avoid-escape" | "avoid_escape" => match parser::parse_bool(value.as_ref()) {
|
"avoid-escape" | "avoid_escape" => match parser::parse_bool(value.as_ref()) {
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
import { persist, restore } from "./settings";
|
import { DEFAULT_PYTHON_SOURCE } from "../constants";
|
||||||
import { DEFAULT_SETTINGS_SOURCE, DEFAULT_PYTHON_SOURCE } from "../constants";
|
import init, { check, Check, currentVersion, defaultSettings } from "../pkg";
|
||||||
import { ErrorMessage } from "./ErrorMessage";
|
import { ErrorMessage } from "./ErrorMessage";
|
||||||
import Header from "./Header";
|
import Header from "./Header";
|
||||||
import init, { check, current_version, Check } from "../pkg";
|
import { persist, restore, stringify } from "./settings";
|
||||||
import SettingsEditor from "./SettingsEditor";
|
import SettingsEditor from "./SettingsEditor";
|
||||||
import SourceEditor from "./SourceEditor";
|
import SourceEditor from "./SourceEditor";
|
||||||
import Themes from "./Themes";
|
import Themes from "./Themes";
|
||||||
|
@ -52,6 +52,10 @@ export default function Editor() {
|
||||||
}, [initialized, settingsSource, pythonSource]);
|
}, [initialized, settingsSource, pythonSource]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!initialized) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (settingsSource == null || pythonSource == null) {
|
if (settingsSource == null || pythonSource == null) {
|
||||||
const payload = restore();
|
const payload = restore();
|
||||||
if (payload) {
|
if (payload) {
|
||||||
|
@ -59,18 +63,18 @@ export default function Editor() {
|
||||||
setSettingsSource(settingsSource);
|
setSettingsSource(settingsSource);
|
||||||
setPythonSource(pythonSource);
|
setPythonSource(pythonSource);
|
||||||
} else {
|
} else {
|
||||||
setSettingsSource(DEFAULT_SETTINGS_SOURCE);
|
setSettingsSource(stringify(defaultSettings()));
|
||||||
setPythonSource(DEFAULT_PYTHON_SOURCE);
|
setPythonSource(DEFAULT_PYTHON_SOURCE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [settingsSource, pythonSource]);
|
}, [initialized, settingsSource, pythonSource]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setVersion(current_version());
|
setVersion(currentVersion());
|
||||||
}, [initialized]);
|
}, [initialized]);
|
||||||
|
|
||||||
const handleShare = useCallback(() => {
|
const handleShare = useCallback(() => {
|
||||||
|
|
|
@ -1,38 +1,22 @@
|
||||||
import lzstring from "lz-string";
|
import lzstring from "lz-string";
|
||||||
import { OptionGroup } from "../ruff_options";
|
|
||||||
|
|
||||||
export type Settings = { [K: string]: any };
|
export type Settings = { [K: string]: any };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse an encoded value from the options export.
|
* Stringify a settings object to JSON.
|
||||||
*
|
|
||||||
* TODO(charlie): Use JSON for the default values.
|
|
||||||
*/
|
*/
|
||||||
function parse(value: any): any {
|
export function stringify(settings: Settings): string {
|
||||||
if (value == "None") {
|
return JSON.stringify(
|
||||||
return null;
|
settings,
|
||||||
}
|
(k, v) => {
|
||||||
return JSON.parse(value);
|
if (v instanceof Map) {
|
||||||
}
|
return Object.fromEntries(v.entries());
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
settings[group.name] = {};
|
return v;
|
||||||
for (const field of group.fields) {
|
|
||||||
settings[group.name][field.name] = parse(field.default);
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
2
|
||||||
return settings;
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import { defaultSettings } from "./Editor/settings";
|
|
||||||
import { AVAILABLE_OPTIONS } from "./ruff_options";
|
import { AVAILABLE_OPTIONS } from "./ruff_options";
|
||||||
|
|
||||||
export const DEFAULT_PYTHON_SOURCE =
|
export const DEFAULT_PYTHON_SOURCE =
|
||||||
|
@ -32,9 +31,3 @@ export const DEFAULT_PYTHON_SOURCE =
|
||||||
"# 13\n" +
|
"# 13\n" +
|
||||||
"# 21\n" +
|
"# 21\n" +
|
||||||
"# 34\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};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(
|
#[derive(
|
||||||
Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, JsonSchema,
|
Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions, JsonSchema,
|
||||||
)]
|
)]
|
||||||
#[serde(
|
#[serde(
|
||||||
deny_unknown_fields,
|
deny_unknown_fields,
|
||||||
|
@ -51,7 +51,7 @@ pub struct Options {
|
||||||
pub allow_star_arg_any: Option<bool>,
|
pub allow_star_arg_any: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Hash, Default)]
|
#[derive(Debug, Default, Hash)]
|
||||||
#[allow(clippy::struct_excessive_bools)]
|
#[allow(clippy::struct_excessive_bools)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
pub mypy_init_return: bool,
|
pub mypy_init_return: bool,
|
||||||
|
@ -60,14 +60,24 @@ pub struct Settings {
|
||||||
pub allow_star_arg_any: bool,
|
pub allow_star_arg_any: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl From<Options> for Settings {
|
||||||
#[allow(clippy::needless_pass_by_value)]
|
fn from(options: Options) -> Self {
|
||||||
pub fn from_options(options: Options) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
mypy_init_return: options.mypy_init_return.unwrap_or_default(),
|
mypy_init_return: options.mypy_init_return.unwrap_or(false),
|
||||||
suppress_dummy_args: options.suppress_dummy_args.unwrap_or_default(),
|
suppress_dummy_args: options.suppress_dummy_args.unwrap_or(false),
|
||||||
suppress_none_returning: options.suppress_none_returning.unwrap_or_default(),
|
suppress_none_returning: options.suppress_none_returning.unwrap_or(false),
|
||||||
allow_star_arg_any: options.allow_star_arg_any.unwrap_or_default(),
|
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};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(
|
#[derive(
|
||||||
Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, JsonSchema,
|
Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions, JsonSchema,
|
||||||
)]
|
)]
|
||||||
#[serde(
|
#[serde(
|
||||||
deny_unknown_fields,
|
deny_unknown_fields,
|
||||||
|
@ -26,15 +26,23 @@ pub struct Options {
|
||||||
pub extend_immutable_calls: Option<Vec<String>>,
|
pub extend_immutable_calls: Option<Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Hash, Default)]
|
#[derive(Debug, Default, Hash)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
pub extend_immutable_calls: Vec<String>,
|
pub extend_immutable_calls: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl From<Options> for Settings {
|
||||||
pub fn from_options(options: Options) -> Self {
|
fn from(options: Options) -> Self {
|
||||||
Self {
|
Self {
|
||||||
extend_immutable_calls: options.extend_immutable_calls.unwrap_or_default(),
|
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,
|
pub max_string_length: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl From<Options> for Settings {
|
||||||
#[allow(clippy::needless_pass_by_value)]
|
fn from(options: Options) -> Self {
|
||||||
pub fn from_options(options: Options) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
max_string_length: options.max_string_length.unwrap_or_default(),
|
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(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/flake8_import_conventions/custom.py"),
|
Path::new("./resources/test/fixtures/flake8_import_conventions/custom.py"),
|
||||||
&Settings {
|
&Settings {
|
||||||
flake8_import_conventions:
|
flake8_import_conventions: flake8_import_conventions::settings::Options {
|
||||||
flake8_import_conventions::settings::Settings::from_options(
|
|
||||||
flake8_import_conventions::settings::Options {
|
|
||||||
aliases: None,
|
aliases: None,
|
||||||
extend_aliases: Some(FxHashMap::from_iter([
|
extend_aliases: Some(FxHashMap::from_iter([
|
||||||
("dask.array".to_string(), "da".to_string()),
|
("dask.array".to_string(), "da".to_string()),
|
||||||
("dask.dataframe".to_string(), "dd".to_string()),
|
("dask.dataframe".to_string(), "dd".to_string()),
|
||||||
])),
|
])),
|
||||||
},
|
}
|
||||||
),
|
.into(),
|
||||||
..Settings::for_rule(CheckCode::ICN001)
|
..Settings::for_rule(CheckCode::ICN001)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
@ -52,9 +50,7 @@ mod tests {
|
||||||
let mut checks = test_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/flake8_import_conventions/remove_default.py"),
|
Path::new("./resources/test/fixtures/flake8_import_conventions/remove_default.py"),
|
||||||
&Settings {
|
&Settings {
|
||||||
flake8_import_conventions:
|
flake8_import_conventions: flake8_import_conventions::settings::Options {
|
||||||
flake8_import_conventions::settings::Settings::from_options(
|
|
||||||
flake8_import_conventions::settings::Options {
|
|
||||||
aliases: Some(FxHashMap::from_iter([
|
aliases: Some(FxHashMap::from_iter([
|
||||||
("altair".to_string(), "alt".to_string()),
|
("altair".to_string(), "alt".to_string()),
|
||||||
("matplotlib.pyplot".to_string(), "plt".to_string()),
|
("matplotlib.pyplot".to_string(), "plt".to_string()),
|
||||||
|
@ -62,8 +58,8 @@ mod tests {
|
||||||
("seaborn".to_string(), "sns".to_string()),
|
("seaborn".to_string(), "sns".to_string()),
|
||||||
])),
|
])),
|
||||||
extend_aliases: None,
|
extend_aliases: None,
|
||||||
},
|
}
|
||||||
),
|
.into(),
|
||||||
..Settings::for_rule(CheckCode::ICN001)
|
..Settings::for_rule(CheckCode::ICN001)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
@ -77,16 +73,14 @@ mod tests {
|
||||||
let mut checks = test_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/flake8_import_conventions/override_default.py"),
|
Path::new("./resources/test/fixtures/flake8_import_conventions/override_default.py"),
|
||||||
&Settings {
|
&Settings {
|
||||||
flake8_import_conventions:
|
flake8_import_conventions: flake8_import_conventions::settings::Options {
|
||||||
flake8_import_conventions::settings::Settings::from_options(
|
|
||||||
flake8_import_conventions::settings::Options {
|
|
||||||
aliases: None,
|
aliases: None,
|
||||||
extend_aliases: Some(FxHashMap::from_iter([(
|
extend_aliases: Some(FxHashMap::from_iter([(
|
||||||
"numpy".to_string(),
|
"numpy".to_string(),
|
||||||
"nmp".to_string(),
|
"nmp".to_string(),
|
||||||
)])),
|
)])),
|
||||||
},
|
}
|
||||||
),
|
.into(),
|
||||||
..Settings::for_rule(CheckCode::ICN001)
|
..Settings::for_rule(CheckCode::ICN001)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
|
@ -84,14 +84,6 @@ fn resolve_aliases(options: Options) -> FxHashMap<String, String> {
|
||||||
aliases
|
aliases
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
|
||||||
pub fn from_options(options: Options) -> Self {
|
|
||||||
Self {
|
|
||||||
aliases: resolve_aliases(options),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Settings {
|
impl Default for Settings {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
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,
|
Double,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Quote {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::Double
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(
|
#[derive(
|
||||||
Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, JsonSchema,
|
Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, JsonSchema,
|
||||||
)]
|
)]
|
||||||
|
@ -74,24 +80,35 @@ pub struct Settings {
|
||||||
pub avoid_escape: bool,
|
pub avoid_escape: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl Default for Settings {
|
||||||
pub fn from_options(options: Options) -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
inline_quotes: options.inline_quotes.unwrap_or(Quote::Double),
|
inline_quotes: Quote::default(),
|
||||||
multiline_quotes: options.multiline_quotes.unwrap_or(Quote::Double),
|
multiline_quotes: Quote::default(),
|
||||||
docstring_quotes: options.docstring_quotes.unwrap_or(Quote::Double),
|
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),
|
avoid_escape: options.avoid_escape.unwrap_or(true),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Settings {
|
impl From<Settings> for Options {
|
||||||
fn default() -> Self {
|
fn from(settings: Settings) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inline_quotes: Quote::Double,
|
inline_quotes: Some(settings.inline_quotes),
|
||||||
multiline_quotes: Quote::Double,
|
multiline_quotes: Some(settings.multiline_quotes),
|
||||||
docstring_quotes: Quote::Double,
|
docstring_quotes: Some(settings.docstring_quotes),
|
||||||
avoid_escape: true,
|
avoid_escape: Some(settings.avoid_escape),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,14 +40,6 @@ pub struct Settings {
|
||||||
pub ban_relative_imports: Strictness,
|
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 {
|
impl Default for Settings {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
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>,
|
pub ignore_variadic_names: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Hash, Default)]
|
#[derive(Debug, Default, Hash)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
pub ignore_variadic_names: bool,
|
pub ignore_variadic_names: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl From<Options> for Settings {
|
||||||
#[allow(clippy::needless_pass_by_value)]
|
fn from(options: Options) -> Self {
|
||||||
pub fn from_options(options: Options) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
ignore_variadic_names: options.ignore_variadic_names.unwrap_or_default(),
|
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>,
|
pub extra_standard_library: BTreeSet<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl Default for Settings {
|
||||||
pub fn from_options(options: Options) -> Self {
|
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 {
|
Self {
|
||||||
combine_as_imports: options.combine_as_imports.unwrap_or(false),
|
combine_as_imports: options.combine_as_imports.unwrap_or(false),
|
||||||
force_wrap_aliases: options.force_wrap_aliases.unwrap_or(false),
|
force_wrap_aliases: options.force_wrap_aliases.unwrap_or(false),
|
||||||
|
@ -142,17 +157,17 @@ impl Settings {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Settings {
|
impl From<Settings> for Options {
|
||||||
fn default() -> Self {
|
fn from(settings: Settings) -> Self {
|
||||||
Self {
|
Self {
|
||||||
combine_as_imports: false,
|
combine_as_imports: Some(settings.combine_as_imports),
|
||||||
force_wrap_aliases: false,
|
force_wrap_aliases: Some(settings.force_wrap_aliases),
|
||||||
split_on_trailing_comma: true,
|
split_on_trailing_comma: Some(settings.split_on_trailing_comma),
|
||||||
force_single_line: false,
|
force_single_line: Some(settings.force_single_line),
|
||||||
single_line_exclusions: BTreeSet::new(),
|
single_line_exclusions: Some(settings.single_line_exclusions.into_iter().collect()),
|
||||||
known_first_party: BTreeSet::new(),
|
known_first_party: Some(settings.known_first_party.into_iter().collect()),
|
||||||
known_third_party: BTreeSet::new(),
|
known_third_party: Some(settings.known_third_party.into_iter().collect()),
|
||||||
extra_standard_library: BTreeSet::new(),
|
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::autofix::Fix;
|
||||||
use crate::checks::CheckCode;
|
use crate::checks::CheckCode;
|
||||||
use crate::directives;
|
use crate::checks_gen::CheckCodePrefix;
|
||||||
use crate::linter::check_path;
|
use crate::linter::check_path;
|
||||||
use crate::rustpython_helpers::tokenize;
|
use crate::rustpython_helpers::tokenize;
|
||||||
use crate::settings::configuration::Configuration;
|
use crate::settings::configuration::Configuration;
|
||||||
use crate::settings::options::Options;
|
use crate::settings::options::Options;
|
||||||
|
use crate::settings::types::PythonVersion;
|
||||||
use crate::settings::{flags, Settings};
|
use crate::settings::{flags, Settings};
|
||||||
use crate::source_code_locator::SourceCodeLocator;
|
use crate::source_code_locator::SourceCodeLocator;
|
||||||
use crate::source_code_style::SourceCodeStyleDetector;
|
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");
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
|
@ -62,11 +68,65 @@ pub fn run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn current_version() -> JsValue {
|
#[allow(non_snake_case)]
|
||||||
|
pub fn currentVersion() -> JsValue {
|
||||||
JsValue::from(VERSION)
|
JsValue::from(VERSION)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[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> {
|
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 options: Options = serde_wasm_bindgen::from_value(options).map_err(|e| e.to_string())?;
|
||||||
let configuration =
|
let configuration =
|
||||||
|
|
|
@ -30,16 +30,24 @@ pub struct Settings {
|
||||||
pub max_complexity: usize,
|
pub max_complexity: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl Default for Settings {
|
||||||
pub fn from_options(options: &Options) -> Self {
|
fn default() -> Self {
|
||||||
|
Self { max_complexity: 10 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Options> for Settings {
|
||||||
|
fn from(options: Options) -> Self {
|
||||||
Self {
|
Self {
|
||||||
max_complexity: options.max_complexity.unwrap_or_default(),
|
max_complexity: options.max_complexity.unwrap_or_default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Settings {
|
impl From<Settings> for Options {
|
||||||
fn default() -> Self {
|
fn from(settings: Settings) -> Self {
|
||||||
Self { max_complexity: 10 }
|
Self {
|
||||||
|
max_complexity: Some(settings.max_complexity),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,8 +76,18 @@ pub struct Settings {
|
||||||
pub staticmethod_decorators: Vec<String>,
|
pub staticmethod_decorators: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl Default for Settings {
|
||||||
pub fn from_options(options: Options) -> Self {
|
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 {
|
Self {
|
||||||
ignore_names: options
|
ignore_names: options
|
||||||
.ignore_names
|
.ignore_names
|
||||||
|
@ -92,12 +102,12 @@ impl Settings {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Settings {
|
impl From<Settings> for Options {
|
||||||
fn default() -> Self {
|
fn from(settings: Settings) -> Self {
|
||||||
Self {
|
Self {
|
||||||
ignore_names: IGNORE_NAMES.map(String::from).to_vec(),
|
ignore_names: Some(settings.ignore_names),
|
||||||
classmethod_decorators: CLASSMETHOD_DECORATORS.map(String::from).to_vec(),
|
classmethod_decorators: Some(settings.classmethod_decorators),
|
||||||
staticmethod_decorators: STATICMETHOD_DECORATORS.map(String::from).to_vec(),
|
staticmethod_decorators: Some(settings.staticmethod_decorators),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ use ruff_macros::ConfigurationOptions;
|
||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
use serde::{Deserialize, Serialize};
|
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")]
|
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
|
||||||
pub enum Convention {
|
pub enum Convention {
|
||||||
/// Use Google-style docstrings.
|
/// Use Google-style docstrings.
|
||||||
|
@ -37,10 +37,18 @@ pub struct Settings {
|
||||||
pub convention: Option<Convention>,
|
pub convention: Option<Convention>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl From<Options> for Settings {
|
||||||
pub fn from_options(options: Options) -> Self {
|
fn from(options: Options) -> Self {
|
||||||
Self {
|
Self {
|
||||||
convention: options.convention,
|
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>,
|
pub keep_runtime_typing: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Hash, Default)]
|
#[derive(Debug, Default, Hash)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
pub keep_runtime_typing: bool,
|
pub keep_runtime_typing: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl From<Options> for Settings {
|
||||||
pub fn from_options(options: &Options) -> Self {
|
fn from(options: Options) -> Self {
|
||||||
Self {
|
Self {
|
||||||
keep_runtime_typing: options.keep_runtime_typing.unwrap_or_default(),
|
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: config
|
||||||
.src
|
.src
|
||||||
.unwrap_or_else(|| vec![project_root.to_path_buf()]),
|
.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),
|
update_check: config.update_check.unwrap_or(true),
|
||||||
// Plugins
|
// Plugins
|
||||||
flake8_annotations: config
|
flake8_annotations: config
|
||||||
.flake8_annotations
|
.flake8_annotations
|
||||||
.map(flake8_annotations::settings::Settings::from_options)
|
.map(std::convert::Into::into)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
flake8_bugbear: config
|
flake8_bugbear: config
|
||||||
.flake8_bugbear
|
.flake8_bugbear
|
||||||
.map(flake8_bugbear::settings::Settings::from_options)
|
.map(std::convert::Into::into)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
flake8_errmsg: config
|
flake8_errmsg: config
|
||||||
.flake8_errmsg
|
.flake8_errmsg
|
||||||
.map(flake8_errmsg::settings::Settings::from_options)
|
.map(std::convert::Into::into)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
flake8_import_conventions: config
|
flake8_import_conventions: config
|
||||||
.flake8_import_conventions
|
.flake8_import_conventions
|
||||||
.map(flake8_import_conventions::settings::Settings::from_options)
|
.map(std::convert::Into::into)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
flake8_quotes: config
|
flake8_quotes: config
|
||||||
.flake8_quotes
|
.flake8_quotes
|
||||||
.map(flake8_quotes::settings::Settings::from_options)
|
.map(std::convert::Into::into)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
flake8_tidy_imports: config
|
flake8_tidy_imports: config
|
||||||
.flake8_tidy_imports
|
.flake8_tidy_imports
|
||||||
.map(flake8_tidy_imports::settings::Settings::from_options)
|
.map(std::convert::Into::into)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
flake8_unused_arguments: config
|
flake8_unused_arguments: config
|
||||||
.flake8_unused_arguments
|
.flake8_unused_arguments
|
||||||
.map(flake8_unused_arguments::settings::Settings::from_options)
|
.map(std::convert::Into::into)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
isort: config
|
isort: config
|
||||||
.isort
|
.isort
|
||||||
.map(isort::settings::Settings::from_options)
|
.map(std::convert::Into::into)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
mccabe: config
|
mccabe: config
|
||||||
.mccabe
|
.mccabe
|
||||||
.as_ref()
|
.map(std::convert::Into::into)
|
||||||
.map(mccabe::settings::Settings::from_options)
|
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
pep8_naming: config
|
pep8_naming: config
|
||||||
.pep8_naming
|
.pep8_naming
|
||||||
.map(pep8_naming::settings::Settings::from_options)
|
.map(std::convert::Into::into)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
pydocstyle: config
|
pydocstyle: config
|
||||||
.pydocstyle
|
.pydocstyle
|
||||||
.map(pydocstyle::settings::Settings::from_options)
|
.map(std::convert::Into::into)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
pyupgrade: config
|
pyupgrade: config
|
||||||
.pyupgrade
|
.pyupgrade
|
||||||
.as_ref()
|
.map(std::convert::Into::into)
|
||||||
.map(pyupgrade::settings::Settings::from_options)
|
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,12 @@ pub enum PythonVersion {
|
||||||
Py311,
|
Py311,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for PythonVersion {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::Py310
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FromStr for PythonVersion {
|
impl FromStr for PythonVersion {
|
||||||
type Err = anyhow::Error;
|
type Err = anyhow::Error;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue