Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 12 additions & 6 deletions crates/oxc_linter/src/generated/rule_runner_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ impl RuleRunner for crate::rules::eslint::no_empty_function::NoEmptyFunction {
}

impl RuleRunner for crate::rules::eslint::no_empty_pattern::NoEmptyPattern {
const NODE_TYPES: Option<&AstTypesBitset> = None;
const NODE_TYPES: Option<&AstTypesBitset> =
Some(&AstTypesBitset::from_types(&[AstType::ArrayPattern, AstType::ObjectPattern]));
}

impl RuleRunner for crate::rules::eslint::no_empty_static_block::NoEmptyStaticBlock {
Expand All @@ -288,7 +289,8 @@ impl RuleRunner for crate::rules::eslint::no_eval::NoEval {
}

impl RuleRunner for crate::rules::eslint::no_ex_assign::NoExAssign {
const NODE_TYPES: Option<&AstTypesBitset> = None;
const NODE_TYPES: Option<&AstTypesBitset> =
Some(&AstTypesBitset::from_types(&[AstType::CatchParameter]));
}

impl RuleRunner for crate::rules::eslint::no_extend_native::NoExtendNative {
Expand All @@ -305,7 +307,8 @@ impl RuleRunner for crate::rules::eslint::no_extra_boolean_cast::NoExtraBooleanC
}

impl RuleRunner for crate::rules::eslint::no_extra_label::NoExtraLabel {
const NODE_TYPES: Option<&AstTypesBitset> = None;
const NODE_TYPES: Option<&AstTypesBitset> =
Some(&AstTypesBitset::from_types(&[AstType::BreakStatement, AstType::ContinueStatement]));
}

impl RuleRunner for crate::rules::eslint::no_fallthrough::NoFallthrough {
Expand All @@ -314,19 +317,22 @@ impl RuleRunner for crate::rules::eslint::no_fallthrough::NoFallthrough {
}

impl RuleRunner for crate::rules::eslint::no_func_assign::NoFuncAssign {
const NODE_TYPES: Option<&AstTypesBitset> = None;
const NODE_TYPES: Option<&AstTypesBitset> =
Some(&AstTypesBitset::from_types(&[AstType::Function]));
}

impl RuleRunner for crate::rules::eslint::no_global_assign::NoGlobalAssign {
const NODE_TYPES: Option<&AstTypesBitset> = None;
}

impl RuleRunner for crate::rules::eslint::no_import_assign::NoImportAssign {
const NODE_TYPES: Option<&AstTypesBitset> = None;
const NODE_TYPES: Option<&AstTypesBitset> =
Some(&AstTypesBitset::from_types(&[AstType::ImportDeclaration]));
}

impl RuleRunner for crate::rules::eslint::no_inner_declarations::NoInnerDeclarations {
const NODE_TYPES: Option<&AstTypesBitset> = None;
const NODE_TYPES: Option<&AstTypesBitset> =
Some(&AstTypesBitset::from_types(&[AstType::Function, AstType::VariableDeclaration]));
}

impl RuleRunner for crate::rules::eslint::no_invalid_regexp::NoInvalidRegexp {
Expand Down
19 changes: 13 additions & 6 deletions crates/oxc_linter/src/rules/eslint/no_empty_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,19 @@ declare_oxc_lint!(

impl Rule for NoEmptyPattern {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let (pattern_type, span) = match node.kind() {
AstKind::ArrayPattern(array) if array.is_empty() => ("array", array.span),
AstKind::ObjectPattern(object) if object.is_empty() => ("object", object.span),
_ => return,
};
ctx.diagnostic(no_empty_pattern_diagnostic(pattern_type, span));
match node.kind() {
AstKind::ArrayPattern(array) => {
if array.is_empty() {
ctx.diagnostic(no_empty_pattern_diagnostic("array", array.span));
}
}
AstKind::ObjectPattern(object) => {
if object.is_empty() {
ctx.diagnostic(no_empty_pattern_diagnostic("object", object.span));
}
}
_ => {}
}
}
}

Expand Down
26 changes: 17 additions & 9 deletions crates/oxc_linter/src/rules/eslint/no_ex_assign.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::SymbolId;
use oxc_semantic::AstNode;
use oxc_span::Span;

use crate::{context::LintContext, rule::Rule};
Expand Down Expand Up @@ -50,14 +51,21 @@ declare_oxc_lint!(
);

impl Rule for NoExAssign {
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext<'_>) {
let symbol_table = ctx.scoping();
if symbol_table.symbol_flags(symbol_id).is_catch_variable() {
for reference in symbol_table.get_resolved_references(symbol_id) {
if reference.is_write() {
ctx.diagnostic(no_ex_assign_diagnostic(
ctx.semantic().reference_span(reference),
));
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::CatchParameter(catch_param) = node.kind() {
let idents = catch_param.pattern.get_binding_identifiers();
let symbol_table = ctx.scoping();
for ident in idents {
let symbol_id = ident.symbol_id();
// This symbol _should_ always be considered a catch variable (since we got it from a catch param),
// but we check in debug mode just to be sure.
debug_assert!(symbol_table.symbol_flags(symbol_id).is_catch_variable());
for reference in symbol_table.get_resolved_references(symbol_id) {
if reference.is_write() {
ctx.diagnostic(no_ex_assign_diagnostic(
ctx.semantic().reference_span(reference),
));
}
}
}
}
Expand Down
21 changes: 12 additions & 9 deletions crates/oxc_linter/src/rules/eslint/no_extra_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,18 @@ declare_oxc_lint!(

impl Rule for NoExtraLabel {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::BreakStatement(break_stmt) = node.kind()
&& let Some(label) = &break_stmt.label
{
report_label_if_extra(label, node, ctx);
}
if let AstKind::ContinueStatement(cont_stmt) = node.kind()
&& let Some(label) = &cont_stmt.label
{
report_label_if_extra(label, node, ctx);
match node.kind() {
AstKind::BreakStatement(break_stmt) => {
if let Some(label) = &break_stmt.label {
report_label_if_extra(label, node, ctx);
}
}
AstKind::ContinueStatement(cont_stmt) => {
if let Some(label) = &cont_stmt.label {
report_label_if_extra(label, node, ctx);
}
}
_ => {}
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions crates/oxc_linter/src/rules/eslint/no_func_assign.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::SymbolId;
use oxc_semantic::AstNode;
use oxc_span::Span;

use crate::{context::LintContext, rule::Rule};
Expand Down Expand Up @@ -67,14 +67,17 @@ declare_oxc_lint!(
);

impl Rule for NoFuncAssign {
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext<'_>) {
let symbol_table = ctx.scoping();
let decl = symbol_table.symbol_declaration(symbol_id);
if let AstKind::Function(_) = ctx.nodes().kind(decl) {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::Function(func) = node.kind() {
let (func_name, symbol_id) = match &func.id {
Some(id) => (id.name.as_str(), id.symbol_id()),
None => return,
};
let symbol_table = ctx.scoping();
for reference in symbol_table.get_resolved_references(symbol_id) {
if reference.is_write() {
ctx.diagnostic(no_func_assign_diagnostic(
symbol_table.symbol_name(symbol_id),
func_name,
ctx.semantic().reference_span(reference),
));
}
Expand Down
147 changes: 82 additions & 65 deletions crates/oxc_linter/src/rules/eslint/no_import_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use oxc_ast::{
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::{AstNode, NodeId, Reference, SymbolId};
use oxc_semantic::{AstNode, NodeId, Reference};
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::UnaryOperator;

Expand Down Expand Up @@ -54,78 +54,95 @@ const REFLECT_MUTATION_METHODS: [&str; 4] =
["defineProperty", "deleteProperty", "set", "setPrototypeOf"];

impl Rule for NoImportAssign {
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext<'_>) {
let symbol_table = ctx.scoping();
if symbol_table.symbol_flags(symbol_id).is_import() {
let kind = ctx.nodes().kind(symbol_table.symbol_declaration(symbol_id));
let is_namespace_specifier = matches!(kind, AstKind::ImportNamespaceSpecifier(_));
for reference in symbol_table.get_resolved_references(symbol_id) {
if is_namespace_specifier {
let parent_node = ctx.nodes().parent_node(reference.node_id());
if parent_node.kind().is_member_expression_kind() {
let expr = parent_node.kind();
let parent_parent_node = ctx.nodes().parent_node(parent_node.id());
let is_unary_expression_with_delete_operator = |kind| matches!(kind, AstKind::UnaryExpression(expr) if expr.operator == UnaryOperator::Delete);
let parent_parent_kind = parent_parent_node.kind();
if (matches!(parent_parent_kind, AstKind::IdentifierReference(_))
|| is_unary_expression_with_delete_operator(parent_parent_kind)
|| matches!(parent_parent_kind, AstKind::ChainExpression(_) if is_unary_expression_with_delete_operator(ctx.nodes().parent_kind(parent_parent_node.id()))))
&& let Some((span, _)) = match expr {
AstKind::StaticMemberExpression(expr) => {
Some(expr.static_property_info())
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::ImportDeclaration(import_decl) = node.kind() {
let symbol_table = ctx.scoping();
if let Some(specifiers) = &import_decl.specifiers {
for specifier in specifiers {
let symbol_id = specifier.local().symbol_id();
let is_namespace_specifier = matches!(
specifier,
oxc_ast::ast::ImportDeclarationSpecifier::ImportNamespaceSpecifier(_)
);
for reference in symbol_table.get_resolved_references(symbol_id) {
if is_namespace_specifier {
let parent_node = ctx.nodes().parent_node(reference.node_id());
if parent_node.kind().is_member_expression_kind() {
let expr = parent_node.kind();
let parent_parent_node = ctx.nodes().parent_node(parent_node.id());
let is_unary_expression_with_delete_operator = |kind| {
matches!(
kind,
AstKind::UnaryExpression(expr)
if expr.operator == UnaryOperator::Delete
)
};
let parent_parent_kind = parent_parent_node.kind();
if (matches!(parent_parent_kind, AstKind::IdentifierReference(_))
|| is_unary_expression_with_delete_operator(parent_parent_kind)
|| matches!(parent_parent_kind, AstKind::ChainExpression(_) if is_unary_expression_with_delete_operator(ctx.nodes().parent_kind(parent_parent_node.id()))))
&& let Some((span, _)) = match expr {
AstKind::StaticMemberExpression(expr) => {
Some(expr.static_property_info())
}
AstKind::ComputedMemberExpression(expr) => {
expr.static_property_info()
}
_ => return,
}
&& span != ctx.semantic().reference_span(reference)
{
return ctx
.diagnostic(no_import_assign_diagnostic(expr.span()));
}
AstKind::ComputedMemberExpression(expr) => {
expr.static_property_info()
// Check for assignment to namespace property
match expr {
AstKind::StaticMemberExpression(member_expr) => {
let condition_met = is_assignment_condition_met(
&parent_parent_kind,
parent_node.span(),
true, // is_static
);
check_namespace_member_assignment(
&member_expr.object,
parent_node,
reference,
ctx,
condition_met,
);
}
AstKind::ComputedMemberExpression(member_expr) => {
let condition_met = is_assignment_condition_met(
&parent_parent_kind,
parent_node.span(),
false, // is_static
);
check_namespace_member_assignment(
&member_expr.object,
parent_node,
reference,
ctx,
condition_met,
);
}
_ => {}
}
_ => return,
}
&& span != ctx.semantic().reference_span(reference)
{
return ctx.diagnostic(no_import_assign_diagnostic(expr.span()));
}
// Check for assignment to namespace property
match expr {
AstKind::StaticMemberExpression(member_expr) => {
let condition_met = is_assignment_condition_met(
&parent_parent_kind,
parent_node.span(),
true, // is_static
);
check_namespace_member_assignment(
&member_expr.object,
parent_node,
reference,
ctx,
condition_met,
);
}
AstKind::ComputedMemberExpression(member_expr) => {
let condition_met = is_assignment_condition_met(
&parent_parent_kind,
parent_node.span(),
false, // is_static
);
check_namespace_member_assignment(
&member_expr.object,
parent_node,
reference,

if reference.is_write()
|| (is_namespace_specifier
&& is_argument_of_well_known_mutation_function(
reference.node_id(),
ctx,
condition_met,
);
}
_ => {}
))
{
ctx.diagnostic(no_import_assign_diagnostic(
ctx.semantic().reference_span(reference),
));
}
}
}

if reference.is_write()
|| (is_namespace_specifier
&& is_argument_of_well_known_mutation_function(reference.node_id(), ctx))
{
ctx.diagnostic(no_import_assign_diagnostic(
ctx.semantic().reference_span(reference),
));
}
}
}
}
Expand Down
Loading
Loading