Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(lint/useNamingConvention): allow import namespace in PascalCase #174

Merged
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
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ inherits = "release"

[workspace.dependencies]
# Internal crates
biome_lsp = { path = "./crates/biome_lsp" }
biome_markup = { version = "0.0.1", path = "./crates/biome_markup" }
biome_test_utils = { path = "./crates/biome_test_utils" }
rome_analyze = { path = "./crates/rome_analyze" }
rome_aria = { path = "./crates/rome_aria" }
rome_aria_metadata = { path = "./crates/rome_aria_metadata" }
Expand Down Expand Up @@ -54,14 +57,11 @@ rome_json_factory = { version = "0.2.0", path = "./crates/rome_json_fa
rome_json_formatter = { path = "./crates/rome_json_formatter" }
rome_json_parser = { path = "./crates/rome_json_parser" }
rome_json_syntax = { version = "0.2.0", path = "./crates/rome_json_syntax" }
biome_lsp = { path = "./crates/biome_lsp" }
biome_markup = { version = "0.0.1", path = "./crates/biome_markup" }
rome_migrate = { path = "./crates/rome_migrate" }
rome_parser = { version = "0.2.0", path = "./crates/rome_parser" }
rome_rowan = { version = "0.2.0", path = "./crates/rome_rowan" }
rome_service = { path = "./crates/rome_service" }
rome_suppression = { version = "0.2.0", path = "./crates/rome_suppression" }
biome_test_utils = { path = "./crates/biome_test_utils" }
rome_text_edit = { version = "0.0.1", path = "./crates/rome_text_edit" }
rome_text_size = { version = "0.0.1", path = "./crates/rome_text_size" }
tests_macros = { path = "./crates/tests_macros" }
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ path = "src/main.rs"

[dependencies]
anyhow = "1.0.52"
biome_lsp = { workspace = true }
bpaf = { workspace = true }
crossbeam = "0.8.1"
dashmap = { workspace = true }
Expand All @@ -29,7 +30,6 @@ rome_fs = { workspace = true }
rome_json_formatter = { workspace = true }
rome_json_parser = { workspace = true }
rome_json_syntax = { workspace = true }
biome_lsp = { workspace = true }
rome_migrate = { workspace = true }
rome_rowan = { workspace = true }
rome_service = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_console/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ version = "0.0.1"

[dependencies]
atty = { workspace = true }
biome_markup = { workspace = true }
biome_markup = { workspace = true }
rome_text_size = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, optional = true, features = ["derive"] }
Expand Down
10 changes: 5 additions & 5 deletions crates/rome_js_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ serde_json = { workspace = true }
smallvec = { workspace = true }

[dev-dependencies]
countme = { workspace = true, features = ["enable"] }
insta = { workspace = true, features = ["glob"] }
rome_js_parser = { workspace = true, features = ["tests"] }
biome_test_utils = { workspace = true }
rome_text_edit = { workspace = true }
tests_macros = { workspace = true }
countme = { workspace = true, features = ["enable"] }
insta = { workspace = true, features = ["glob"] }
rome_js_parser = { workspace = true, features = ["tests"] }
rome_text_edit = { workspace = true }
tests_macros = { workspace = true }

[features]
schema = ["schemars", "rome_deserialize/schema"]
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 ""
```


2 changes: 1 addition & 1 deletion crates/rome_js_semantic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rust-lapper = "1.0.1"
rustc-hash = { workspace = true }

[dev-dependencies]
biome_markup = { workspace = true }
rome_console = { workspace = true }
rome_diagnostics = { workspace = true }
rome_js_parser = { workspace = true }
biome_markup = { workspace = true }
2 changes: 1 addition & 1 deletion crates/rome_js_transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ rome_rowan = { workspace = true }


[dev-dependencies]
biome_test_utils = { workspace = true }
insta = { workspace = true }
rome_analyze = { workspace = true }
rome_js_formatter = { workspace = true }
rome_js_parser = { workspace = true }
biome_test_utils = { workspace = true }
tests_macros = { workspace = true }
2 changes: 1 addition & 1 deletion crates/rome_json_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ rome_json_syntax = { workspace = true }
rome_rowan = { workspace = true }

[dev-dependencies]
biome_test_utils = { workspace = true }
insta = { workspace = true, features = ["glob"] }
rome_json_factory = { workspace = true }
rome_json_parser = { workspace = true }
rome_service = { workspace = true }
biome_test_utils = { workspace = true }
tests_macros = { workspace = true }
Loading