This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rome_js_analyze):
noParameterProperties
- Loading branch information
Showing
14 changed files
with
446 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
crates/rome_js_analyze/src/analyzers/nursery/no_parameter_properties.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"." | ||
} | ||
)) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
crates/rome_js_analyze/tests/specs/nursery/noParameterProperties/invalid.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
149 changes: 149 additions & 0 deletions
149
crates/rome_js_analyze/tests/specs/nursery/noParameterProperties/invalid.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
``` | ||
|
||
|
3 changes: 3 additions & 0 deletions
3
crates/rome_js_analyze/tests/specs/nursery/noParameterProperties/valid.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class Foo { | ||
constructor(name: string) {} | ||
} |
13 changes: 13 additions & 0 deletions
13
crates/rome_js_analyze/tests/specs/nursery/noParameterProperties/valid.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} | ||
``` | ||
|
||
|
Oops, something went wrong.