dart-lang / dart-lang/language

Enhancing `library` and `part of` Declarations in Dart

Open
#3,908 1 comment 0 reactions 0 assignees View on GitHub
feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

The current usage of `library` and `part of` declarations in Dart has certain limitations, particularly when using the `library` name as a `part of`, which is discouraged. The `flutter_lints` package suggests using URI or relative directory structures instead. However, this approach can be less descriptive and harder to manage, especially in larger projects with complex directory structures like Clean Architecture.

### Problem:

1. **Lack of Clarity:** Using URI or relative directories for `part of` is not as descriptive as using the library name. It makes it difficult to quickly understand the relationship between different `.dart` files.

2. **Difficulty in Management:** Tracking and managing file relationships using URI or relative paths in large projects can be cumbersome. This is particularly problematic in architectural patterns emphasizing modularity and clear separation of concerns.

### Proposed Changes:

1. **Lints Update for Declaring Libraries:** Disallow using relative paths in `part of` directives and allow capitalized library name (e.g., `library Product`).

2. **Enhanced Syntax for Library Calls:** Allow `part of` declarations to use updated library name syntax with distinct color highlighting support for the library name. Possible syntaxes include:

- `Product.CatalogPage()` (dot notation)

- `Product:CatalogPage()` (colon notation)

- `Product::CatalogPage()` (double colon notation, similar to C++ namespace)

### Benefits:

1. **Improved Readability:** The new syntax immediately clarifies which library a part belongs to, improving code readability and maintainability.

2. **Better Project Organization:** Helps organize and manage large projects by making the relationships between files more explicit and easier to follow.

3. **Reduced Redundancy:** Eliminates the need for redundant naming conventions and shorten class names, such as:

- `ProductCatalogPage` to `Product::CatalogPage`
- `ProductDetailsPage` to `Product::DetailsPage`
- `ShopCatalogPage` to `Shop::CatalogPage`
- `ShopDetailsPage` to `Shop::DetailsPage`

### Example:

product.dart
```dart
library Product;

part 'presentation/pages/catalog_page.dart';
part 'presentation/pages/details_page.dart';
```

catalog_page.dart
```dart
part of Product;

class CatalogPage extends StatelessWidget {
// some code
}

// alternate syntax
class Product::CatalogPage extends StatelessWidget {
// some code
}
```

main.dart
```dart
void main() {
Product.CatalogPage(); // dot notation
Product:CatalogPage(); // colon notation
Product::CatalogPage(); // double colon notation
}
```

Any insights, concerns, or suggestions for improvement would be greatly appreciated.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.