Skip to content

Commit

Permalink
ready
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Jul 17, 2024
1 parent c98e1fe commit b2f6b65
Show file tree
Hide file tree
Showing 30 changed files with 605 additions and 74 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 20 additions & 11 deletions apps/oxlint/src/command/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,35 @@ pub struct FixOptions {
/// Fix as many issues as possible. Only unfixed issues are reported in the output
#[bpaf(switch)]
pub fix: bool,
/// Apply all safe fixes and more-risky suggestions. Overrides `--fix`.
/// Apply auto-fixable suggestions. May change program behavior.
#[bpaf(switch)]
pub fix_suggestions: bool,

/// Apply all fixes and suggestions, including dangerous ones. Overrides
/// `--fix` and `--fix-suggestions`. These fixes may create incorrect code.
/// Apply dangerous fixes and suggestions.
#[bpaf(switch)]
pub fix_dangerously: bool,
}

impl FixOptions {
pub fn fix_kind(&self) -> Option<FixKind> {
pub fn fix_kind(&self) -> FixKind {
let mut kind = FixKind::None;

if self.fix {
kind.set(FixKind::SafeFix, true);
}

if self.fix_suggestions {
kind.set(FixKind::Suggestion, true);
}

if self.fix_dangerously {
Some(FixKind::DangerousFix)
} else if self.fix_suggestions {
Some(FixKind::Suggestion)
} else if self.fix {
Some(FixKind::SafeFix)
} else {
None
if kind.is_none() {
kind.set(FixKind::Fix, true);
}
kind.set(FixKind::Dangerous, true);
}

kind
}

pub fn is_enabled(&self) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_language_server/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ pub struct ServerLinter {

impl ServerLinter {
pub fn new() -> Self {
let linter = Linter::default().with_fix(Some(FixKind::SafeFix));
let linter = Linter::default().with_fix(FixKind::SafeFix);
Self { linter: Arc::new(linter) }
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_language_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl Backend {
*linter = ServerLinter::new_with_linter(
Linter::from_options(
LintOptions::default()
.with_fix(Some(FixKind::SafeFix))
.with_fix(FixKind::SafeFix)
.with_config_path(Some(config_path)),
)
.expect("should have initialized linter with new options"),
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ oxc_codegen = { workspace = true }
oxc_resolver = { workspace = true }

rayon = { workspace = true }
bitflags = { workspace = true }
lazy_static = { workspace = true }
serde_json = { workspace = true }
serde = { workspace = true, features = ["derive"] }
Expand Down
50 changes: 32 additions & 18 deletions crates/oxc_linter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ pub struct LintContext<'a> {

disable_directives: Rc<DisableDirectives<'a>>,

/// Whether or not to apply code fixes during linting. Defaults to `false`.
/// Whether or not to apply code fixes during linting. Defaults to
/// [`FixKind::None`] (no fixing).
///
/// Set via the `--fix` CLI flag.
fix: Option<FixKind>,
/// Set via the `--fix`, `--fix-suggestions`, and `--fix-dangerously` CLI
/// flags.
fix: FixKind,

file_path: Rc<Path>,

Expand Down Expand Up @@ -69,7 +71,7 @@ impl<'a> LintContext<'a> {
semantic,
diagnostics: RefCell::new(Vec::with_capacity(DIAGNOSTICS_INITIAL_CAPACITY)),
disable_directives: Rc::new(disable_directives),
fix: None,
fix: FixKind::None,
file_path: file_path.into(),
eslint_config: Arc::new(OxlintConfig::default()),
current_rule_name: "",
Expand All @@ -79,7 +81,7 @@ impl<'a> LintContext<'a> {

/// Enable/disable automatic code fixes.
#[must_use]
pub fn with_fix(mut self, fix: Option<FixKind>) -> Self {
pub fn with_fix(mut self, fix: FixKind) -> Self {
self.fix = fix;
self
}
Expand Down Expand Up @@ -274,19 +276,31 @@ impl<'a> LintContext<'a> {
C: Into<RuleFix<'a>>,
F: FnOnce(RuleFixer<'_, 'a>) -> C,
{
if let Some(accepted_fix_kind) = self.fix {
let fixer = RuleFixer::new(fix_kind, self);
let rule_fix: RuleFix<'a> = fix(fixer).into();
let diagnostic = match (rule_fix.message(), &diagnostic.help) {
(Some(message), None) => diagnostic.with_help(message.to_owned()),
_ => diagnostic,
};
if rule_fix.kind() <= accepted_fix_kind {
let fix = rule_fix.into_fix(self.source_text());
self.add_diagnostic(Message::new(diagnostic, Some(fix)));
} else {
self.diagnostic(diagnostic);
}
// if let Some(accepted_fix_kind) = self.fix {
// let fixer = RuleFixer::new(fix_kind, self);
// let rule_fix: RuleFix<'a> = fix(fixer).into();
// let diagnostic = match (rule_fix.message(), &diagnostic.help) {
// (Some(message), None) => diagnostic.with_help(message.to_owned()),
// _ => diagnostic,
// };
// if rule_fix.kind() <= accepted_fix_kind {
// let fix = rule_fix.into_fix(self.source_text());
// self.add_diagnostic(Message::new(diagnostic, Some(fix)));
// } else {
// self.diagnostic(diagnostic);
// }
// } else {
// self.diagnostic(diagnostic);
// }
let fixer = RuleFixer::new(fix_kind, self);
let rule_fix: RuleFix<'a> = fix(fixer).into();
let diagnostic = match (rule_fix.message(), &diagnostic.help) {
(Some(message), None) => diagnostic.with_help(message.to_owned()),
_ => diagnostic,
};
if self.fix.can_apply(rule_fix.kind()) {
let fix = rule_fix.into_fix(self.source_text());
self.add_diagnostic(Message::new(diagnostic, Some(fix)));
} else {
self.diagnostic(diagnostic);
}
Expand Down
Loading

0 comments on commit b2f6b65

Please sign in to comment.