Skip to content

Commit

Permalink
Merge branch 'main' into lint-role
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Dec 29, 2023
2 parents d2c0fe6 + 4bbc977 commit 5e6b7cd
Show file tree
Hide file tree
Showing 36 changed files with 54 additions and 52 deletions.
4 changes: 2 additions & 2 deletions crates/oxc_diagnostics/src/graphic_reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ impl GraphicalReportHandler {
/// Render a [`Diagnostic`]. This function is mostly internal and meant to
/// be called by the toplevel [`ReportHandler`] handler, but is made public
/// to make it easier (possible) to test in isolation from global state.
pub fn render_report(
pub fn render_report<T: fmt::Write>(
&self,
f: &mut impl fmt::Write,
f: &mut T,
diagnostic: &(dyn Diagnostic),
) -> fmt::Result {
self.render_header(f, diagnostic)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ fn resolve_rule_value(value: &serde_json::Value) -> Result<(AllowWarnDeny, Optio
config.push(item.clone());
}
let config = if config.is_empty() { None } else { Some(Value::Array(config)) };
if let Some(v_idx_0) = v.get(0) {
if let Some(v_idx_0) = v.first() {
return Ok((AllowWarnDeny::try_from(v_idx_0)?, config));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Rule for BadArrayMethodOnArguments {
// only check template string like "arguments[`METHOD_NAME`]" for Deepscan compatible
if template.expressions.is_empty() && template.quasis.len() == 1 {
if let Some(name) =
template.quasis.get(0).and_then(|template_element| {
template.quasis.first().and_then(|template_element| {
template_element.value.cooked.as_deref()
})
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Rule for UninvokedArrayCallback {
return;
}
if !matches!(
new_expr.arguments.get(0),
new_expr.arguments.first(),
Some(Argument::Expression(Expression::NumberLiteral(_)))
) {
return;
Expand All @@ -69,7 +69,7 @@ impl Rule for UninvokedArrayCallback {
else {
return;
};
if !matches!(call_expr.arguments.get(0), Some(Argument::Expression(arg_expr)) if arg_expr.is_function())
if !matches!(call_expr.arguments.first(), Some(Argument::Expression(arg_expr)) if arg_expr.is_function())
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_regex_spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl NoRegexSpaces {
}
}

if let Some(Argument::Expression(Expression::StringLiteral(pattern))) = args.get(0) {
if let Some(Argument::Expression(Expression::StringLiteral(pattern))) = args.first() {
if Self::has_exempted_char_class(&pattern.value) {
return None; // skip spaces inside char class, e.g. RegExp('[ ]')
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jest/no_done_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ fn find_argument_of_callback<'a>(
}

if matches!(kind, JestFnKind::General(JestGeneralFnKind::Hook)) {
return call_expr.arguments.get(0);
return call_expr.arguments.first();
}

if matches!(kind, JestFnKind::General(JestGeneralFnKind::Test)) {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jest/no_identical_title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn filter_and_process_jest_result<'a>(
return None;
};

match call_expr.arguments.get(0) {
match call_expr.arguments.first() {
Some(Argument::Expression(Expression::StringLiteral(string_lit))) => {
Some((string_lit.span, &string_lit.value, kind, parent_id))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jest/no_mocks_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Rule for NoMocksImport {
};

let Some(Argument::Expression(Expression::StringLiteral(string_literal))) =
call_expr.arguments.get(0)
call_expr.arguments.first()
else {
return;
};
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/jest/valid_title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl ValidTitle {
return;
}

let Some(Argument::Expression(expr)) = call_expr.arguments.get(0) else {
let Some(Argument::Expression(expr)) = call_expr.arguments.first() else {
return;
};

Expand Down Expand Up @@ -263,7 +263,7 @@ fn compile_matcher_pattern(pattern: MatcherPattern) -> Option<CompiledMatcherAnd
Some((reg, None))
}
MatcherPattern::Vec(pattern) => {
let reg_str = pattern.get(0).and_then(|v| v.as_str()).map(|v| format!("(?u){v}"))?;
let reg_str = pattern.first().and_then(|v| v.as_str()).map(|v| format!("(?u){v}"))?;
let reg = Regex::new(&reg_str).ok()?;
let message = pattern.get(1).map(std::string::ToString::to_string);
Some((reg, message))
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Rule for NoAccumulatingSpread {
// We're only looking for the first parameter, since that's where acc is.
// Skip non-parameter or non-first-parameter declarations.
let first_param_symbol_id =
params.items.get(0).and_then(|item| get_identifier_symbol_id(&item.pattern.kind));
params.items.first().and_then(|item| get_identifier_symbol_id(&item.pattern.kind));
if !first_param_symbol_id.is_some_and(|id| id == referenced_symbol_id) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/react/button_has_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Rule for ButtonHasType {
AstKind::CallExpression(call_expr) => {
if is_create_element_call(call_expr) {
let Some(Argument::Expression(Expression::StringLiteral(str))) =
call_expr.arguments.get(0)
call_expr.arguments.first()
else {
return;
};
Expand Down Expand Up @@ -144,7 +144,7 @@ impl Rule for ButtonHasType {
}

fn from_configuration(value: serde_json::Value) -> Self {
let value = value.as_array().and_then(|arr| arr.get(0)).and_then(|val| val.as_object());
let value = value.as_array().and_then(|arr| arr.first()).and_then(|val| val.as_object());

Self {
button: value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn is_fragment_with_only_text_and_is_not_child<'a>(
return false;
}

if let Some(JSXChild::Text(_)) = node.get(0) {
if let Some(JSXChild::Text(_)) = node.first() {
let Some(parent) = ctx.nodes().parent_kind(id) else { return false };
return !matches!(parent, AstKind::JSXElement(_) | AstKind::JSXFragment(_));
}
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/unicorn/catch_error_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Rule for CatchErrorName {
if let AstKind::CallExpression(call_expr) = node.kind() {
if let Expression::MemberExpression(member_expr) = &call_expr.callee {
if member_expr.static_property_name() == Some("catch") {
if let Some(arg0) = call_expr.arguments.get(0) {
if let Some(arg0) = call_expr.arguments.first() {
if let Some(diagnostic) = self.check_function_arguments(arg0, ctx) {
ctx.diagnostic(diagnostic);
}
Expand Down Expand Up @@ -149,7 +149,7 @@ impl CatchErrorName {
let expr = expr.without_parenthesized();

if let Expression::ArrowExpression(arrow_expr) = expr {
if let Some(arg0) = arrow_expr.params.items.get(0) {
if let Some(arg0) = arrow_expr.params.items.first() {
if let BindingPatternKind::BindingIdentifier(v) = &arg0.pattern.kind {
if self.is_name_allowed(&v.name) {
return None;
Expand Down Expand Up @@ -177,7 +177,7 @@ impl CatchErrorName {
}

if let Expression::FunctionExpression(fn_expr) = expr {
if let Some(arg0) = fn_expr.params.items.get(0) {
if let Some(arg0) = fn_expr.params.items.first() {
if let BindingPatternKind::BindingIdentifier(binding_ident) = &arg0.pattern.kind {
if self.is_name_allowed(&binding_ident.name) {
return None;
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/unicorn/escape_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ declare_oxc_lint!(
);

fn is_hex_char(c: char) -> bool {
matches!(c, '0'..='9' | 'a'..='f' | 'A'..='F')
c.is_ascii_hexdigit()
}
fn is_hex(iter: &Chars, count: i32) -> bool {
let mut iter = iter.clone();
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/unicorn/filename_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{context::LintContext, rule::Rule};
struct FilenameCaseDiagnostic(#[label] pub Span, &'static str);

#[derive(Debug, Clone)]
#[allow(clippy::struct_field_names)]
pub struct FilenameCase {
kebab_case: bool,
camel_case: bool,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/unicorn/no_array_reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Rule for NoArrayReduce {
};

if is_method_call(call_expr, None, Some(&["reduce", "reduceRight"]), Some(1), Some(2))
&& !matches!(call_expr.arguments.get(0), Some(Argument::SpreadElement(_)))
&& !matches!(call_expr.arguments.first(), Some(Argument::SpreadElement(_)))
&& !call_expr.optional
&& !member_expr.is_computed()
{
Expand All @@ -99,7 +99,7 @@ impl Rule for NoArrayReduce {
}

fn is_simple_operation(node: &CallExpression) -> bool {
let Some(Argument::Expression(callback_arg)) = node.arguments.get(0) else {
let Some(Argument::Expression(callback_arg)) = node.arguments.first() else {
return false;
};
let function_body = match callback_arg {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Rule for NoInvalidRemoveEventListener {
return;
}

if matches!(call_expr.arguments.get(0), Some(Argument::SpreadElement(_))) {
if matches!(call_expr.arguments.first(), Some(Argument::SpreadElement(_))) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/unicorn/prefer_array_flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn check_array_flat_map_case<'a>(call_expr: &CallExpression<'a>, ctx: &LintConte
return;
}

let Argument::Expression(first_argument) = call_expr.arguments.get(0).unwrap() else {
let Argument::Expression(first_argument) = call_expr.arguments.first().unwrap() else {
return;
};

Expand Down Expand Up @@ -113,7 +113,7 @@ fn check_array_reduce_case<'a>(call_expr: &CallExpression<'a>, ctx: &LintContext
return;
}
let Argument::Expression(Expression::ArrowExpression(first_argument)) =
call_expr.arguments.get(0).unwrap()
call_expr.arguments.first().unwrap()
else {
return;
};
Expand Down Expand Up @@ -144,7 +144,7 @@ fn check_array_reduce_case<'a>(call_expr: &CallExpression<'a>, ctx: &LintContext
return;
};

let Some(Statement::ExpressionStatement(expr_stmt)) = first_argument.body.statements.get(0)
let Some(Statement::ExpressionStatement(expr_stmt)) = first_argument.body.statements.first()
else {
return;
};
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/unicorn/prefer_date_now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Rule for PreferDateNow {
if matches!(ident.name.as_str(), "Number" | "BigInt")
&& call_expr.arguments.len() == 1
{
if let Some(Argument::Expression(expr)) = call_expr.arguments.get(0) {
if let Some(Argument::Expression(expr)) = call_expr.arguments.first() {
if is_new_date(expr.without_parenthesized()) {
ctx.diagnostic(
PreferDateNowDiagnostic::PreferDateNowOverNumberDateObject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Rule for PreferNativeCoercionFunctions {
}

fn get_first_parameter_name<'a>(arg: &'a FormalParameters) -> Option<&'a str> {
let first_func_param = arg.items.get(0)?;
let first_func_param = arg.items.first()?;
let BindingPatternKind::BindingIdentifier(first_func_param) = &first_func_param.pattern.kind
else {
return None;
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/unicorn/prefer_query_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Rule for PreferQuerySelector {
return;
}

let Argument::Expression(argument_expr) = call_expr.arguments.get(0).unwrap() else {
let Argument::Expression(argument_expr) = call_expr.arguments.first().unwrap() else {
return;
};

Expand All @@ -97,7 +97,7 @@ impl Rule for PreferQuerySelector {
Expression::StringLiteral(literal) => Some(literal.value.trim()),
Expression::TemplateLiteral(literal) => {
if literal.expressions.len() == 0 {
literal.quasis.get(0).unwrap().value.cooked.as_deref().map(str::trim)
literal.quasis.first().unwrap().value.cooked.as_deref().map(str::trim)
} else {
None
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/unicorn/prefer_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Rule for PreferSpread {
}
}

if let Some(first_arg) = call_expr.arguments.get(0) {
if let Some(first_arg) = call_expr.arguments.first() {
let Argument::Expression(first_arg) = first_arg else { return };
if let Expression::NumberLiteral(num_lit) = first_arg.without_parenthesized() {
if num_lit.value != 0.0 {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/utils/react.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub fn parse_jsx_value(value: &JSXAttributeValue) -> Result<f64, ()> {
}) => match expression {
Expression::StringLiteral(str) => str.value.parse().or(Err(())),
Expression::TemplateLiteral(tmpl) => {
tmpl.quasis.get(0).unwrap().value.raw.parse().or(Err(()))
tmpl.quasis.first().unwrap().value.raw.parse().or(Err(()))
}
Expression::NumberLiteral(num) => Ok(num.value),
_ => Err(()),
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/utils/unicorn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn is_logical_expression(node: &AstNode) -> bool {

// gets the name of the first parameter of a function
pub fn get_first_parameter_name<'a>(arg: &'a FormalParameters) -> Option<&'a str> {
let first_func_param = arg.items.get(0)?;
let first_func_param = arg.items.first()?;
let BindingPatternKind::BindingIdentifier(first_func_param) = &first_func_param.pattern.kind
else {
return None;
Expand All @@ -117,9 +117,9 @@ pub fn get_first_parameter_name<'a>(arg: &'a FormalParameters) -> Option<&'a str
}

pub fn get_return_identifier_name<'a>(body: &'a FunctionBody<'_>) -> Option<&'a str> {
match body.statements.get(0)? {
match body.statements.first()? {
Statement::BlockStatement(block_stmt) => {
let Statement::ReturnStatement(return_stmt) = block_stmt.body.get(0)? else {
let Statement::ReturnStatement(return_stmt) = block_stmt.body.first()? else {
return None;
};

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_minifier/src/compressor/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ pub fn get_boolean_value(expr: &Expression) -> Option<bool> {
// only for ``
template_literal
.quasis
.get(0)
.first()
.filter(|quasi| quasi.tail)
.and_then(|quasi| quasi.value.cooked.as_ref())
.map(|cooked| !cooked.is_empty())
Expand Down Expand Up @@ -550,7 +550,7 @@ pub fn get_string_value<'a>(expr: &'a Expression) -> Option<Cow<'a, str>> {
// Closure-compiler do more: [case TEMPLATELIT](https://github.com/google/closure-compiler/blob/e13f5cd0a5d3d35f2db1e6c03fdf67ef02946009/src/com/google/javascript/jscomp/NodeUtil.java#L241-L256).
template_literal
.quasis
.get(0)
.first()
.filter(|quasi| quasi.tail)
.and_then(|quasi| quasi.value.cooked.as_ref())
.map(|cooked| Cow::Borrowed(cooked.as_str()))
Expand Down
5 changes: 1 addition & 4 deletions crates/oxc_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,6 @@ impl<'a> Lexer<'a> {
/// Read each char and set the current token
/// Whitespace and line terminators are skipped
fn read_next_token(&mut self) -> Kind {
self.current.token.start = self.offset();

loop {
let offset = self.offset();
self.current.token.start = offset;
Expand Down Expand Up @@ -1587,8 +1585,7 @@ const BTO: ByteHandler = |lexer| {
// \
const ESC: ByteHandler = |lexer| {
let mut builder = AutoCow::new(lexer);
let c = lexer.consume_char();
builder.push_matching(c);
lexer.consume_char();
builder.force_allocation_without_current_ascii_char(lexer);
lexer.identifier_unicode_escape_sequence(&mut builder, true);
let text = lexer.identifier_name(builder);
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_parser/src/ts/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,7 @@ impl<'a> Parser<'a> {
break;
}

#[allow(clippy::unnecessary_fallible_conversions)]
if let Ok(modifier_flag) = self.cur_kind().try_into() {
flags.set(modifier_flag, true);
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_prettier/src/format/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn choose_layout<'a>(
return Layout::Chain;
} else if let Expression::ArrowExpression(arrow_expr) = right_expr {
if let Some(Statement::ExpressionStatement(expr_stmt)) =
arrow_expr.body.statements.get(0)
arrow_expr.body.statements.first()
{
if let Expression::ArrowExpression(_) = expr_stmt.expression {
return Layout::ChainTailArrowChain;
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,15 +993,15 @@ impl<'a> Format<'a> for ImportDeclaration<'a> {
parts.push(ss!(" type"));
}
if let Some(specifiers) = &self.specifiers {
let is_default = specifiers.get(0).is_some_and(|x| {
let is_default = specifiers.first().is_some_and(|x| {
matches!(x, ImportDeclarationSpecifier::ImportDefaultSpecifier(_))
});

let validate_namespace = |x: &ImportDeclarationSpecifier| {
matches!(x, ImportDeclarationSpecifier::ImportNamespaceSpecifier(_))
};

let is_namespace = specifiers.get(0).is_some_and(validate_namespace)
let is_namespace = specifiers.first().is_some_and(validate_namespace)
|| specifiers.get(1).is_some_and(validate_namespace);

parts.push(module::print_module_specifiers(p, specifiers, is_default, is_namespace));
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_prettier/src/needs_parens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ impl<'a> Prettier<'a> {
}
Expression::SequenceExpression(e) => e
.expressions
.get(0)
.first()
.map_or(false, |e| Self::starts_with_no_lookahead_token(e, span)),
Expression::ChainExpression(e) => match &e.expression {
ChainElement::CallExpression(e) => {
Expand Down
Loading

0 comments on commit 5e6b7cd

Please sign in to comment.