Skip to content

Commit

Permalink
refactor: extract common function
Browse files Browse the repository at this point in the history
  • Loading branch information
luhc228 committed Feb 4, 2024
1 parent 94ab857 commit f7e4234
Showing 1 changed file with 47 additions and 97 deletions.
144 changes: 47 additions & 97 deletions crates/oxc_linter/src/rules/typescript/array_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,114 +101,64 @@ impl Rule for ArrayType {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let default_config = &self.default;
let readonly_config: &ArrayOption = &self.readonly.clone().unwrap_or(default_config.clone());

match node.kind() {
AstKind::TSTypeAnnotation(ts_type_annotation) => {
if let TSType::TSArrayType(array_type) = &ts_type_annotation.type_annotation {
check_and_report_error_generic(
default_config,
array_type.span,
&array_type.element_type,
ctx,
false,
);
}

if let TSType::TSTypeOperatorType(ts_operator_type) = &ts_type_annotation.type_annotation {
if let TSTypeOperator::Readonly = &ts_operator_type.operator {
if let TSType::TSArrayType(array_type) = &ts_operator_type.type_annotation {
check_and_report_error_generic(
readonly_config,
ts_operator_type.span,
&array_type.element_type,
ctx,
true,
);
}
}
}

if let TSType::TSTypeReference(ts_type_reference) = &ts_type_annotation.type_annotation {
check_and_report_error_array(
default_config,
readonly_config,
ts_type_reference,
ctx,
);
}
check(&ts_type_annotation.type_annotation, default_config, readonly_config, ctx);
},
AstKind::TSTypeAliasDeclaration(ts_type_annotation) => {
if let TSType::TSArrayType(array_type) = &ts_type_annotation.type_annotation {
check_and_report_error_generic(
default_config,
array_type.span,
&array_type.element_type,
ctx,
false,
);
}

if let TSType::TSTypeOperatorType(ts_operator_type) = &ts_type_annotation.type_annotation {
if let TSTypeOperator::Readonly = &ts_operator_type.operator {
if let TSType::TSArrayType(array_type) = &ts_operator_type.type_annotation {
check_and_report_error_generic(
readonly_config,
ts_operator_type.span,
&array_type.element_type,
ctx,
true,
);
}
}
}

if let TSType::TSTypeReference(ts_type_reference) = &ts_type_annotation.type_annotation {
check_and_report_error_array(
default_config,
readonly_config,
ts_type_reference,
ctx,
);
}
// for example: type barUnion = (string | number | boolean)[];
AstKind::TSTypeAliasDeclaration(ts_alias_annotation) => {
check(&ts_alias_annotation.type_annotation, default_config, readonly_config, ctx);
},
// for example: let ya = [[1, '2']] as [number, string][];
AstKind::TSAsExpression(ts_as_expression) => {
if let TSType::TSArrayType(array_type) = &ts_as_expression.type_annotation {
check_and_report_error_generic(
default_config,
array_type.span,
&array_type.element_type,
ctx,
false,
);
}

if let TSType::TSTypeOperatorType(ts_operator_type) = &ts_as_expression.type_annotation {
if let TSTypeOperator::Readonly = &ts_operator_type.operator {
if let TSType::TSArrayType(array_type) = &ts_operator_type.type_annotation {
check_and_report_error_generic(
readonly_config,
ts_operator_type.span,
&array_type.element_type,
ctx,
true,
);
}
}
}

if let TSType::TSTypeReference(ts_type_reference) = &ts_as_expression.type_annotation {
check_and_report_error_array(
default_config,
readonly_config,
ts_type_reference,
ctx,
);
}
check(&ts_as_expression.type_annotation, default_config, readonly_config, ctx);
},
_ => {}
}
}
}

fn check(
type_annotation: &TSType,
default_config: &ArrayOption,
readonly_config: &ArrayOption,
ctx: &LintContext,
) {
if let TSType::TSArrayType(array_type) = &type_annotation {
check_and_report_error_generic(
default_config,
array_type.span,
&array_type.element_type,
ctx,
false,
);
}

if let TSType::TSTypeOperatorType(ts_operator_type) = &type_annotation {
if let TSTypeOperator::Readonly = &ts_operator_type.operator {
if let TSType::TSArrayType(array_type) = &ts_operator_type.type_annotation {
check_and_report_error_generic(
readonly_config,
ts_operator_type.span,
&array_type.element_type,
ctx,
true,
);
}
}
}

if let TSType::TSTypeReference(ts_type_reference) = &type_annotation {
check_and_report_error_array(
default_config,
readonly_config,
ts_type_reference,
ctx,
);
}
}

fn type_needs_parentheses(type_param: &TSType) -> bool {
match type_param {
TSType::TSTypeReference(node) => {
Expand Down

0 comments on commit f7e4234

Please sign in to comment.