Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5da0c85
chore(linter): no-unused-vars boilerplate
DonIsaac Jul 23, 2023
c7a1958
feat(linter): add no-unused-vars options parsing
DonIsaac Jul 23, 2023
bfa819a
refactor(linter): regex option parsing, clippy fixes
DonIsaac Jul 23, 2023
03d3358
feat: start reference resolution steps
DonIsaac Jul 23, 2023
fef74ce
fix(linter): start var impl
DonIsaac Jul 24, 2023
56b4c41
Merge branch 'main' of https://github.com/Boshen/oxc into don/feat/no…
DonIsaac Jul 25, 2023
3603c40
test(linter): more no-unused-vars test cases
DonIsaac Jul 25, 2023
a974932
test(linter): start enabling eslint test cases
DonIsaac Jul 26, 2023
d196582
feat(linter/no-unused-vars): support exports
DonIsaac Jul 27, 2023
f2b8019
test(linter/no-unused-vars): add more fail cases
DonIsaac Jul 27, 2023
6e413b5
feat(linter/no-unused-vars): support class decls
DonIsaac Jul 27, 2023
7a3d490
feat(linter/no-unused-vars): start supporting args
DonIsaac Jul 27, 2023
b3177d7
Merge branch 'main' of https://github.com/Boshen/oxc into don/feat/no…
DonIsaac Jul 27, 2023
23562d0
refactor(linter/no-unused-vars): remove reliance on experimental feat…
DonIsaac Jul 27, 2023
e6a6bf6
feat(linter/no-unused-args): support after-used
DonIsaac Jul 28, 2023
a473a28
refactor(linter/no-unused-vars): cleanup
DonIsaac Jul 28, 2023
5084705
feat(linter/no-unused-vars): support caught errors
DonIsaac Jul 28, 2023
477c61e
test(linter/no-unused-vars): enable more passing tests
DonIsaac Jul 28, 2023
36c9dd5
fix(linter/no-unused-vars): support var: local option
DonIsaac Jul 28, 2023
f34e746
fix(linter/no-unused-vars): support exported variables
DonIsaac Jul 28, 2023
ed59097
feat(linter/no-unused-vars): start recursive binding check
DonIsaac Jul 28, 2023
f284f56
Merge branch 'main' of https://github.com/Boshen/oxc into don/feat/no…
DonIsaac Jul 28, 2023
410d739
feat(linter/no-unused-vars): arr/obj unpacking
DonIsaac Jul 29, 2023
3730312
fix(linter/no-unused-vars): arr unpacking in function args
DonIsaac Jul 29, 2023
e5a9007
style(linter): fix clippy lints
DonIsaac Jul 29, 2023
8966874
style(linter/no-unused-vars): fix clippy lints
DonIsaac Jul 29, 2023
a886407
test(linter/no-unused-vars): report unused imports, add a lot of fail…
DonIsaac Jul 29, 2023
dcf2e40
test(linter/no-unused-vars): more testing
DonIsaac Jul 29, 2023
def7d6b
feat(linter/no-unused-vars): support ignore rest siblings
DonIsaac Jul 29, 2023
0267425
Merge branch 'main' of https://github.com/Boshen/oxc into don/feat/no…
DonIsaac Jul 29, 2023
8c6d70f
fix(linter/no-unused-vars): more progress
DonIsaac Jul 31, 2023
dd16c82
Merge branch 'main' of https://github.com/Boshen/oxc into don/feat/no…
DonIsaac Jul 31, 2023
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
Prev Previous commit
Next Next commit
refactor(linter): regex option parsing, clippy fixes
  • Loading branch information
DonIsaac committed Jul 23, 2023
commit bfa819a01365230576a76839b91e43fc23871d5b
88 changes: 47 additions & 41 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use oxc_diagnostics::{
use oxc_macros::declare_oxc_lint;
use oxc_span::{Atom, Span};
use regex::Regex;
use serde_json::Value;

use crate::{context::LintContext, rule::Rule, AstNode};

Expand All @@ -22,24 +23,26 @@ pub enum VarsOption {
/// global variables to be unused.
Local,
}

impl TryFrom<&String> for VarsOption {
type Error = String;

fn try_from(value: &String) -> Result<Self, Self::Error> {
match value.as_str() {
"all" => Ok(Self::All),
"local" => Ok(Self::Local),
_ => Err(format!("Expected 'all' or 'local', got {}", value)),
_ => Err(format!("Expected 'all' or 'local', got {value}")),
}
}
}
impl TryFrom<&serde_json::Value> for VarsOption {

impl TryFrom<&Value> for VarsOption {
type Error = String;

fn try_from(value: &serde_json::Value) -> Result<Self, Self::Error> {
fn try_from(value: &Value) -> Result<Self, Self::Error> {
match value {
serde_json::Value::String(s) => Self::try_from(s),
_ => Err(format!("Expected a string, got {}", value)),
Value::String(s) => Self::try_from(s),
_ => Err(format!("Expected a string, got {value}")),
}
}
}
Expand All @@ -56,18 +59,19 @@ pub enum ArgsOption {
/// Do not check arguments
None,
}
impl TryFrom<&serde_json::Value> for ArgsOption {

impl TryFrom<&Value> for ArgsOption {
type Error = String;

fn try_from(value: &serde_json::Value) -> Result<Self, Self::Error> {
fn try_from(value: &Value) -> Result<Self, Self::Error> {
match value {
serde_json::Value::String(s) => match s.as_str() {
Value::String(s) => match s.as_str() {
"after-used" => Ok(Self::AfterUsed),
"all" => Ok(Self::All),
"none" => Ok(Self::None),
_ => Err(format!("Expected 'after-used', 'all', or 'none', got '{:}", s)),
_ => Err(format!("Expected 'after-used', 'all', or 'none', got '{s}")),
},
_ => Err(format!("Expected a string, got {}", value)),
_ => Err(format!("Expected a string, got {value}")),
}
}
}
Expand Down Expand Up @@ -284,18 +288,29 @@ declare_oxc_lint!(
correctness
);

/// Parses a potential pattern into a [`Regex`] that accepts unicode characters.
fn parse_unicode_rule(value: Option<&Value>, name: &str) -> Option<Regex> {
// fn parse_unicode_rule(config: &serde_json::map::Map<String, &Value>, name: &str) -> Option<Regex>
value
.and_then(Value::as_str)
.map(|pattern| regex::RegexBuilder::new(pattern).unicode(true).build())
.transpose()
.map_err(|err| panic!("Invalid '{}' option for no-unused-vars: {}", name, err))
.unwrap()
}

impl Rule for NoUnusedVars {
fn from_configuration(value: serde_json::Value) -> Self {
fn from_configuration(value: Value) -> Self {
let Some(config) = value.get(0) else { return Self::default() };
match config {
serde_json::Value::String(vars) => {
Value::String(vars) => {
let vars: VarsOption = vars
.try_into()
.map_err(|err| format!("Invalid 'vars' option for no-unused-vars: {:}", err))
.unwrap();
Self { vars, ..Default::default() }
}
serde_json::Value::Object(config) => {
Value::Object(config) => {
let vars = config
.get("vars")
.map(|vars| {
Expand All @@ -310,10 +325,7 @@ impl Rule for NoUnusedVars {
.unwrap_or_default();

let vars_ignore_pattern: Option<Regex> =
config.get("varsIgnorePattern").map(|pattern| {
let pattern = pattern.as_str().unwrap();
Regex::new(pattern).unwrap()
});
parse_unicode_rule(config.get("varsIgnorePattern"), "varsIgnorePattern");

let args: ArgsOption = config
.get("args")
Expand All @@ -329,16 +341,13 @@ impl Rule for NoUnusedVars {
.unwrap_or_default();

let args_ignore_pattern: Option<Regex> =
config.get("argsIgnorePattern").map(|pattern| {
let pattern = pattern.as_str().unwrap();
Regex::new(pattern).unwrap()
});
parse_unicode_rule(config.get("argsIgnorePattern"), "argsIgnorePattern");

let caught_errors: bool = config
.get("caughtErrors")
.map(|caught_errors| {
match caught_errors {
serde_json::Value::String(s) => match s.as_str() {
Value::String(s) => match s.as_str() {
"all" => true,
"none" => false,
_ => panic!("Invalid 'caughtErrors' option for no-unused-vars: Expected 'all' or 'none', got {}", s),
Expand All @@ -347,22 +356,19 @@ impl Rule for NoUnusedVars {
}
}).unwrap_or_default();

let caught_errors_ignore_pattern =
config.get("caughtErrorsIgnorePattern").map(|pattern| {
let pattern = pattern.as_str().unwrap();
Regex::new(pattern).unwrap()
});
let caught_errors_ignore_pattern = parse_unicode_rule(
config.get("caughtErrorsIgnorePattern"),
"caughtErrorsIgnorePattern",
);

let destructured_array_ignore_pattern =
config.get("destructuredArrayIgnorePattern").map(|pattern| {
let pattern = pattern.as_str().unwrap();
Regex::new(pattern).unwrap()
});
let destructured_array_ignore_pattern: Option<Regex> = parse_unicode_rule(
config.get("destructuredArrayIgnorePattern"),
"destructuredArrayIgnorePattern",
);

let ignore_rest_siblings: bool = config
.get("ignoreRestSiblings")
.map(serde_json::Value::as_bool)
.unwrap_or(Some(false))
.map_or(Some(false), Value::as_bool)
.unwrap_or(false);

Self {
Expand All @@ -376,10 +382,9 @@ impl Rule for NoUnusedVars {
ignore_rest_siblings,
}
}
serde_json::Value::Null => Self::default(),
Value::Null => Self::default(),
_ => panic!(
"Invalid 'vars' option for no-unused-vars: Expected a string or an object, got {}",
config
"Invalid 'vars' option for no-unused-vars: Expected a string or an object, got {config}"
),
}
}
Expand All @@ -389,9 +394,10 @@ impl Rule for NoUnusedVars {

#[cfg(test)]
mod tests {
use serde_json::{json, Value};

use super::*;
use crate::tester::Tester;
use serde_json::{json, Value};

// test-only trait implementations to make testing easier
impl PartialEq for VarsOption {
Expand Down Expand Up @@ -457,8 +463,9 @@ mod tests {
"destructuredArrayIgnorePattern": "^_",
"ignoreRestSiblings": true
}
]).into();

])
.into();

assert_eq!(rule.vars, VarsOption::Local);
assert_eq!(rule.vars_ignore_pattern.unwrap().as_str(), "^_");
assert_eq!(rule.args, ArgsOption::AfterUsed);
Expand All @@ -471,7 +478,6 @@ mod tests {

#[test]
fn test() {

let pass = vec![
("var foo = 5;\n\nlabel: while (true) {\n console.log(foo);\n break label;\n}", None),
("var foo = 5;\n\nwhile (true) {\n console.log(foo);\n break;\n}", None),
Expand Down