Skip to content
Merged
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
36 changes: 19 additions & 17 deletions crates/oxc_linter/src/rules/eslint/no_inner_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@ fn no_inner_declarations_diagnostic(decl_type: &str, body: &str, span: Span) ->
.with_label(span)
}

#[derive(Debug, Default, Clone, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", default)]
pub struct NoInnerDeclarations {
/// Determines what type of declarations to check.
config: NoInnerDeclarationsConfig,
/// Controls whether function declarations in nested blocks are allowed in strict mode (ES6+ behavior).
#[schemars(with = "BlockScopedFunctions")]
block_scoped_functions: Option<BlockScopedFunctions>,
}

/// Determines what type of declarations to check.
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
enum NoInnerDeclarationsConfig {
Expand All @@ -33,6 +24,14 @@ enum NoInnerDeclarationsConfig {
Both,
}

#[derive(Debug, Default, Clone, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
struct NoInnerDeclarationsOptions {
/// Controls whether function declarations in nested blocks are allowed in strict mode (ES6+ behavior).
#[schemars(with = "BlockScopedFunctions")]
block_scoped_functions: Option<BlockScopedFunctions>,
}

#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
enum BlockScopedFunctions {
Expand All @@ -43,10 +42,14 @@ enum BlockScopedFunctions {
Disallow,
}

#[derive(Debug, Default, Clone, Deserialize, JsonSchema)]
#[serde(default)]
pub struct NoInnerDeclarations(NoInnerDeclarationsConfig, NoInnerDeclarationsOptions);

declare_oxc_lint!(
/// ### What it does
///
/// Disallow variable or function declarations in nested blocks
/// Disallow variable or function declarations in nested blocks.
///
/// ### Why is this bad?
///
Expand All @@ -59,7 +62,7 @@ declare_oxc_lint!(
/// Examples of **incorrect** code for this rule:
/// ```javascript
/// if (test) {
/// function doSomethingElse () { }
/// function doSomethingElse () { }
/// }
/// ```
///
Expand Down Expand Up @@ -100,13 +103,13 @@ impl Rule for NoInnerDeclarations {
None
};

Ok(Self { config, block_scoped_functions })
Ok(Self(config, NoInnerDeclarationsOptions { block_scoped_functions }))
}

fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
match node.kind() {
AstKind::VariableDeclaration(decl) => {
if self.config == NoInnerDeclarationsConfig::Functions || !decl.kind.is_var() {
if self.0 == NoInnerDeclarationsConfig::Functions || !decl.kind.is_var() {
return;
}

Expand All @@ -117,9 +120,8 @@ impl Rule for NoInnerDeclarations {
return;
}

if self.config == NoInnerDeclarationsConfig::Functions
&& let Some(block_scoped_functions) = self.block_scoped_functions
&& block_scoped_functions == BlockScopedFunctions::Allow
if self.0 == NoInnerDeclarationsConfig::Functions
&& self.1.block_scoped_functions == Some(BlockScopedFunctions::Allow)
{
// Modules are always strict mode.
// This check is redundant, because in modules, the scope will have strict mode flag set,
Expand Down
Loading