Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_js_analyze): noParameterProperties #3874

Merged
merged 1 commit into from
Feb 13, 2023
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
1 change: 1 addition & 0 deletions crates/rome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ define_dategories! {
"lint/nursery/noInvalidConstructorSuper": "https://docs.rome.tools/lint/rules/noInvalidConstructorSuper",
"lint/nursery/noConfusingLabels": "https://docs.rome.tools/lint/rules/noConfusingLabels",
"lint/nursery/noNonNullAssertion": "https://docs.rome.tools/lint/rules/noNonNullAssertion",
"lint/nursery/noParameterProperties": "https://docs.rome.tools/lint/rules/noParameterProperties",
"lint/nursery/noPrecisionLoss": "https://docs.rome.tools/lint/rules/noPrecisionLoss",
"lint/nursery/noRedundantAlt": "https://docs.rome.tools/lint/rules/noRedundantAlt",
"lint/nursery/noRedundantUseStrict": "https://docs.rome.tools/lint/rules/noRedundantUseStrict",
Expand Down
3 changes: 2 additions & 1 deletion crates/rome_js_analyze/src/analyzers/nursery.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use rome_analyze::context::RuleContext;
use rome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_js_syntax::TsPropertyParameter;
use rome_rowan::AstNode;

declare_rule! {
/// Disallow the use of parameter properties in class constructors.
///
/// TypeScript includes a "parameter properties" shorthand for declaring a class constructor parameter and class property in one location.
/// Parameter properties can confuse those new to TypeScript as they are less explicit than other ways of declaring and initializing class members.
/// Moreover, private class properties, starting with `#`, cannot be turned into "parameter properties".
/// This questions the future of this feature.
///
/// Source: https://typescript-eslint.io/rules/parameter-properties
///
/// ## Examples
///
/// ### Invalid
///
/// ```ts,expect_diagnostic
/// class A {
/// constructor(readonly name: string) {}
/// }
/// ```
///
/// ### Valid
///
/// ```ts
/// class A {
/// constructor(name: string) {}
/// }
/// ```
///
pub(crate) NoParameterProperties {
version: "next",
name: "noParameterProperties",
recommended: false,
}
}

impl Rule for NoParameterProperties {
type Query = Ast<TsPropertyParameter>;
type State = ();
type Signals = Option<Self::State>;
type Options = ();

fn run(_: &RuleContext<Self>) -> Self::Signals {
Some(())
}

fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> {
let param_prop = ctx.query();
Some(RuleDiagnostic::new(
rule_category!(),
param_prop.range(),
markup! {
"Use a more explicit "<Emphasis>"class property"</Emphasis>" instead of a "<Emphasis>"parameter property"</Emphasis>"."
},
).note(
markup! {
<Emphasis>"Parameter properties"</Emphasis>" are less explicit than other ways of declaring and initializing "<Emphasis>"class properties"</Emphasis>"."
}
))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export class Foo1 {
constructor(readonly name: string) {}
}

export class Foo2 {
constructor(private name: string) {}
}

export class Foo3 {
constructor(protected name: string) {}
}

export class Foo4 {
constructor(public name: string) {}
}

export class Foo5 {
constructor(private readonly name: string) {}
}

export class Foo6 {
constructor(protected readonly name: string) {}
}

export class Foo7 {
constructor(public readonly name: string) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
assertion_line: 92
expression: invalid.ts
---
# Input
```js
export class Foo1 {
constructor(readonly name: string) {}
}

export class Foo2 {
constructor(private name: string) {}
}

export class Foo3 {
constructor(protected name: string) {}
}

export class Foo4 {
constructor(public name: string) {}
}

export class Foo5 {
constructor(private readonly name: string) {}
}

export class Foo6 {
constructor(protected readonly name: string) {}
}

export class Foo7 {
constructor(public readonly name: string) {}
}
```

# Diagnostics
```
invalid.ts:2:15 lint/nursery/noParameterProperties ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Use a more explicit class property instead of a parameter property.
1 │ export class Foo1 {
> 2 │ constructor(readonly name: string) {}
│ ^^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │
i Parameter properties are less explicit than other ways of declaring and initializing class properties.
```

```
invalid.ts:6:15 lint/nursery/noParameterProperties ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Use a more explicit class property instead of a parameter property.
5 │ export class Foo2 {
> 6 │ constructor(private name: string) {}
│ ^^^^^^^^^^^^^^^^^^^^
7 │ }
8 │
i Parameter properties are less explicit than other ways of declaring and initializing class properties.
```

```
invalid.ts:10:15 lint/nursery/noParameterProperties ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Use a more explicit class property instead of a parameter property.
9 │ export class Foo3 {
> 10 │ constructor(protected name: string) {}
│ ^^^^^^^^^^^^^^^^^^^^^^
11 │ }
12 │
i Parameter properties are less explicit than other ways of declaring and initializing class properties.
```

```
invalid.ts:14:15 lint/nursery/noParameterProperties ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Use a more explicit class property instead of a parameter property.
13 │ export class Foo4 {
> 14 │ constructor(public name: string) {}
│ ^^^^^^^^^^^^^^^^^^^
15 │ }
16 │
i Parameter properties are less explicit than other ways of declaring and initializing class properties.
```

```
invalid.ts:18:15 lint/nursery/noParameterProperties ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Use a more explicit class property instead of a parameter property.
17 │ export class Foo5 {
> 18 │ constructor(private readonly name: string) {}
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19 │ }
20 │
i Parameter properties are less explicit than other ways of declaring and initializing class properties.
```

```
invalid.ts:22:15 lint/nursery/noParameterProperties ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Use a more explicit class property instead of a parameter property.
21 │ export class Foo6 {
> 22 │ constructor(protected readonly name: string) {}
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23 │ }
24 │
i Parameter properties are less explicit than other ways of declaring and initializing class properties.
```

```
invalid.ts:26:15 lint/nursery/noParameterProperties ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Use a more explicit class property instead of a parameter property.
25 │ export class Foo7 {
> 26 │ constructor(public readonly name: string) {}
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27 │ }
i Parameter properties are less explicit than other ways of declaring and initializing class properties.
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Foo {
constructor(name: string) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
assertion_line: 73
expression: valid.ts
---
# Input
```js
export class Foo {
constructor(name: string) {}
}
```


Loading