Skip to content

Commit

Permalink
refactor(lint/useNamingConvention): allow import namespace in PascalC…
Browse files Browse the repository at this point in the history
…ase (#174)
  • Loading branch information
Conaclos committed Sep 7, 2023
1 parent a947fc9 commit ef3af45
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 66 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom
### Formatter
### JavaScript APIs
### Linter

#### Enhancements

- [useNamingConvention](https://biomejs.dev/linter/rules/use-naming-convention/) now accepts import namespaces in _PascalCase_ and rejects export namespaces in _CONSTANT\_CASE_.

The following code is now valid:

```js
import * as React from "react";
```

And the following code is now invalid:

```js
export * as MY_NAMESPACE from "./lib.js";
```

#### Bug fixes


### Parser
### VSCode

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,14 @@ declare_rule! {
///
/// ### Imported and exported module aliases
///
/// Imported and exported module aliases are in [`camelCase`].
/// Imported and exported module aliases are in [`camelCase`] or [`PascalCase`].
///
/// ```js
/// import * as myLib from "my-lib";
/// import * as Framework from "framework";
///
/// export * as myLib from "my-lib";
/// export * as Framework from "framework";
/// ```
///
/// `import` and `export` aliases are in [`camelCase`], [`PascalCase`], or [`CONSTANT_CASE`]:
Expand All @@ -205,7 +207,7 @@ declare_rule! {
/// Examples of an incorrect name:
///
/// ```ts,expect_diagnostic
/// import * as MyLib from "my-lib";
/// import * as MY_LIB from "my-lib";
/// ```
///
/// ### TypeScript type parameter names
Expand Down Expand Up @@ -596,6 +598,7 @@ enum Named {
Enum,
EnumMember,
ExportAlias,
ExportNamespace,
ExportSource,
Function,
FunctionParameter,
Expand Down Expand Up @@ -652,11 +655,17 @@ impl Named {
Named::from_class_member(&member_name.parent::<AnyJsClassMember>()?)
}
AnyIdentifierBindingLike::JsLiteralExportName(export_name) => {
match export_name.syntax().parent()?.kind() {
let parent = export_name.syntax().parent()?;
match parent.kind() {
JsSyntaxKind::JS_NAMED_IMPORT_SPECIFIER => Some(Named::ImportSource),
JsSyntaxKind::JS_EXPORT_NAMED_FROM_SPECIFIER => Some(Named::ExportSource),
JsSyntaxKind::JS_EXPORT_NAMED_SPECIFIER | JsSyntaxKind::JS_EXPORT_AS_CLAUSE => {
Some(Named::ExportAlias)
JsSyntaxKind::JS_EXPORT_NAMED_SPECIFIER => Some(Named::ExportAlias),
JsSyntaxKind::JS_EXPORT_AS_CLAUSE => {
if parent.parent()?.kind() == JsSyntaxKind::JS_EXPORT_FROM_CLAUSE {
Some(Named::ExportNamespace)
} else {
Some(Named::ExportAlias)
}
}
_ => None,
}
Expand Down Expand Up @@ -879,7 +888,6 @@ impl Named {
| Named::ClassStaticMethod
| Named::ClassStaticSetter
| Named::FunctionParameter
| Named::ImportNamespace
| Named::IndexParameter
| Named::LocalConst
| Named::LocalLet
Expand Down Expand Up @@ -908,9 +916,10 @@ impl Named {
SmallVec::from_slice(&[Case::Camel, Case::Pascal, Case::Constant])
}
Named::ExportSource | Named::ImportSource => SmallVec::new(),
Named::Function | Named::Namespace => {
SmallVec::from_slice(&[Case::Camel, Case::Pascal])
}
Named::ExportNamespace
| Named::Function
| Named::ImportNamespace
| Named::Namespace => SmallVec::from_slice(&[Case::Camel, Case::Pascal]),
}
}
}
Expand All @@ -931,6 +940,7 @@ impl std::fmt::Display for Named {
Named::Enum => "enum",
Named::EnumMember => "enum member",
Named::ExportAlias => "export alias",
Named::ExportNamespace => "export namespace",
Named::ExportSource => "export source",
Named::Function => "function",
Named::FunctionParameter => "function parameter",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export * as X from ""

export * as PascalCase from ""

export * as CONSTANT_CASE from ""

export * as snake_case from ""

export * as Unknown_Style from ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: invalidExportNamespace.js
---
# Input
```js
export * as X from ""

export * as PascalCase from ""

export * as CONSTANT_CASE from ""

export * as snake_case from ""

export * as Unknown_Style from ""

```

# Diagnostics
```
invalidExportNamespace.js:5:13 lint/nursery/useNamingConvention ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This export namespace name should be in camelCase or PascalCase.
3 │ export * as PascalCase from ""
4 │
> 5 │ export * as CONSTANT_CASE from ""
│ ^^^^^^^^^^^^^
6 │
7 │ export * as snake_case from ""
i The name could be renamed to `constantCase`.
```

```
invalidExportNamespace.js:7:13 lint/nursery/useNamingConvention ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This export namespace name should be in camelCase or PascalCase.
5 │ export * as CONSTANT_CASE from ""
6 │
> 7 │ export * as snake_case from ""
│ ^^^^^^^^^^
8 │
9 │ export * as Unknown_Style from ""
i The name could be renamed to `snakeCase`.
```

```
invalidExportNamespace.js:9:13 lint/nursery/useNamingConvention ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This export namespace name should be in camelCase or PascalCase.
7 │ export * as snake_case from ""
8 │
> 9 │ export * as Unknown_Style from ""
│ ^^^^^^^^^^^^^
10 │
i The name could be renamed to `unknownStyle`.
```


Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,10 @@ import * as Unknown_Style from ""
```

# Diagnostics
```
invalidImportNamespace.js:1:13 lint/nursery/useNamingConvention FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
! This import namespace name should be in camelCase.
> 1 │ import * as X from ""
│ ^
2 │
3 │ import * as PascalCase from ""
i The name could be renamed to `x`.
i Safe fix: Rename this symbol in camelCase.
1 │ - import·*·as·X·from·""
1 │ + import·*·as·x·from·""
2 2 │
3 3 │ import * as PascalCase from ""
```

```
invalidImportNamespace.js:3:13 lint/nursery/useNamingConvention FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
! This import namespace name should be in camelCase.
1 │ import * as X from ""
2 │
> 3 │ import * as PascalCase from ""
│ ^^^^^^^^^^
4 │
5 │ import * as CONSTANT_CASE from ""
i The name could be renamed to `pascalCase`.
i Safe fix: Rename this symbol in camelCase.
1 1 │ import * as X from ""
2 2 │
3 │ - import·*·as·PascalCase·from·""
3 │ + import·*·as·pascalCase·from·""
4 4 │
5 5 │ import * as CONSTANT_CASE from ""
```

```
invalidImportNamespace.js:5:13 lint/nursery/useNamingConvention FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
! This import namespace name should be in camelCase.
! This import namespace name should be in camelCase or PascalCase.
3 │ import * as PascalCase from ""
4 │
Expand All @@ -94,7 +46,7 @@ invalidImportNamespace.js:5:13 lint/nursery/useNamingConvention FIXABLE ━━
```
invalidImportNamespace.js:7:13 lint/nursery/useNamingConvention FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
! This import namespace name should be in camelCase.
! This import namespace name should be in camelCase or PascalCase.
5 │ import * as CONSTANT_CASE from ""
6 │
Expand All @@ -120,7 +72,7 @@ invalidImportNamespace.js:7:13 lint/nursery/useNamingConvention FIXABLE ━━
```
invalidImportNamespace.js:9:13 lint/nursery/useNamingConvention FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
! This import namespace name should be in camelCase.
! This import namespace name should be in camelCase or PascalCase.
7 │ import * as snake_case from ""
8 │
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * as x from ""

export * as camelCase from ""

export * as $ from ""

export * as _ from ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: validExportNamespace.js
---
# Input
```js
export * as x from ""

export * as camelCase from ""

export * as $ from ""

export * as _ from ""
```


20 changes: 20 additions & 0 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom
### Formatter
### JavaScript APIs
### Linter

#### Enhancements

- [useNamingConvention](https://biomejs.dev/linter/rules/use-naming-convention/) now accepts import namespaces in _PascalCase_ and rejects export namespaces in _CONSTANT\_CASE_.

The following code is now valid:

```js
import * as React from "react";
```

And the following code is now invalid:

```js
export * as MY_NAMESPACE from "./lib.js";
```

#### Bug fixes


### Parser
### VSCode

Expand Down
14 changes: 8 additions & 6 deletions website/src/content/docs/linter/rules/use-naming-convention.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,14 @@ const alice = {

### Imported and exported module aliases

Imported and exported module aliases are in [`camelCase`](https://en.wikipedia.org/wiki/Camel_case).
Imported and exported module aliases are in [`camelCase`](https://en.wikipedia.org/wiki/Camel_case) or [`PascalCase`](https://en.wikipedia.org/wiki/Camel_case).

```jsx
import * as myLib from "my-lib";
import * as Framework from "framework";

export * as myLib from "my-lib";
export * as Framework from "framework";
```

`import` and `export` aliases are in [`camelCase`](https://en.wikipedia.org/wiki/Camel_case), [`PascalCase`](https://en.wikipedia.org/wiki/Camel_case), or [`CONSTANT_CASE`](https://en.wikipedia.org/wiki/Snake_case):
Expand All @@ -250,22 +252,22 @@ import assert, {
Examples of an incorrect name:

```ts
import * as MyLib from "my-lib";
import * as MY_LIB from "my-lib";
```

<pre class="language-text"><code class="language-text">nursery/useNamingConvention.js:1:13 <a href="https://biomejs.dev/linter/rules/use-naming-convention">lint/nursery/useNamingConvention</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━

<strong><span style="color: Orange;"> </span></strong><strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">This </span><span style="color: Orange;"><strong>import namespace</strong></span><span style="color: Orange;"> name should be in </span><span style="color: Orange;"><strong>camelCase</strong></span><span style="color: Orange;">.</span>
<strong><span style="color: Orange;"> </span></strong><strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">This </span><span style="color: Orange;"><strong>import namespace</strong></span><span style="color: Orange;"> name should be in </span><span style="color: Orange;"><strong>camelCase or PascalCase</strong></span><span style="color: Orange;">.</span>

<strong><span style="color: Tomato;"> </span></strong><strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>import * as MyLib from &quot;my-lib&quot;;
<strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong>
<strong><span style="color: Tomato;"> </span></strong><strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>import * as MY_LIB from &quot;my-lib&quot;;
<strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong>
<strong>2 │ </strong>

<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">The name could be renamed to `myLib`.</span>

<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">Safe fix</span><span style="color: rgb(38, 148, 255);">: </span><span style="color: rgb(38, 148, 255);">Rename this symbol in </span><span style="color: rgb(38, 148, 255);"><strong>camelCase</strong></span><span style="color: rgb(38, 148, 255);">.</span>

<strong>1</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;">i</span><span style="color: Tomato;">m</span><span style="color: Tomato;">p</span><span style="color: Tomato;">o</span><span style="color: Tomato;">r</span><span style="color: Tomato;">t</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">*</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">a</span><span style="color: Tomato;">s</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;"><strong>M</strong></span><span style="color: Tomato;"><strong>y</strong></span><span style="color: Tomato;"><strong>L</strong></span><span style="color: Tomato;"><strong>i</strong></span><span style="color: Tomato;"><strong>b</strong></span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">f</span><span style="color: Tomato;">r</span><span style="color: Tomato;">o</span><span style="color: Tomato;">m</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;">m</span><span style="color: Tomato;">y</span><span style="color: Tomato;">-</span><span style="color: Tomato;">l</span><span style="color: Tomato;">i</span><span style="color: Tomato;">b</span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;">;</span>
<strong>1</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;">i</span><span style="color: Tomato;">m</span><span style="color: Tomato;">p</span><span style="color: Tomato;">o</span><span style="color: Tomato;">r</span><span style="color: Tomato;">t</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">*</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">a</span><span style="color: Tomato;">s</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;"><strong>M</strong></span><span style="color: Tomato;"><strong>Y</strong></span><span style="color: Tomato;"><strong>_</strong></span><span style="color: Tomato;"><strong>L</strong></span><span style="color: Tomato;"><strong>I</strong></span><span style="color: Tomato;"><strong>B</strong></span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">f</span><span style="color: Tomato;">r</span><span style="color: Tomato;">o</span><span style="color: Tomato;">m</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;">m</span><span style="color: Tomato;">y</span><span style="color: Tomato;">-</span><span style="color: Tomato;">l</span><span style="color: Tomato;">i</span><span style="color: Tomato;">b</span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;">;</span>
<strong>1</strong><strong> │ </strong><span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;">i</span><span style="color: MediumSeaGreen;">m</span><span style="color: MediumSeaGreen;">p</span><span style="color: MediumSeaGreen;">o</span><span style="color: MediumSeaGreen;">r</span><span style="color: MediumSeaGreen;">t</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">*</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">a</span><span style="color: MediumSeaGreen;">s</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;"><strong>m</strong></span><span style="color: MediumSeaGreen;"><strong>y</strong></span><span style="color: MediumSeaGreen;"><strong>L</strong></span><span style="color: MediumSeaGreen;"><strong>i</strong></span><span style="color: MediumSeaGreen;"><strong>b</strong></span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">f</span><span style="color: MediumSeaGreen;">r</span><span style="color: MediumSeaGreen;">o</span><span style="color: MediumSeaGreen;">m</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">&quot;</span><span style="color: MediumSeaGreen;">m</span><span style="color: MediumSeaGreen;">y</span><span style="color: MediumSeaGreen;">-</span><span style="color: MediumSeaGreen;">l</span><span style="color: MediumSeaGreen;">i</span><span style="color: MediumSeaGreen;">b</span><span style="color: MediumSeaGreen;">&quot;</span><span style="color: MediumSeaGreen;">;</span>
<strong>2</strong> <strong>2</strong><strong> │ </strong>

Expand Down

0 comments on commit ef3af45

Please sign in to comment.