dart-lang / linter

Linter for Dart.
https://dart.dev/tools/linter-rules
BSD 3-Clause "New" or "Revised" License
628 stars 172 forks source link

proposal: `file_import_inside_lib_references_file_outside` #4103

Open srawlins opened 1 year ago

srawlins commented 1 year ago

file_import_inside_lib_references_file_outside

Description

This rule was originally proposed (and implemented?) as an analyzer Hint, but the implementation is either gone or never existed, and this is more a style recommendation so should be a Lint rule.

Details

The YAML definition of the Hint was the following:

  FILE_IMPORT_INSIDE_LIB_REFERENCES_FILE_OUTSIDE:
    problemMessage: "A file in the 'lib' directory shouldn't import a file outside the 'lib' directory."
    correctionMessage: "Try removing the import, or moving the imported file inside the 'lib' directory."
    comment: |-
      It is a bad practice for a source file in a package "lib" directory
      hierarchy to traverse outside that directory hierarchy. For example, a
      source file in the "lib" directory should not contain a directive such as
      `import '../web/some.dart'` which references a file outside the lib
      directory.

Kind

Style

Bad Examples

Relative imports from lib/ that go outside lib/.

// in lib/foo.dart:

import '../bin/bar.dart';

Good Examples

Relative imports from lib/ that don't go outside lib/.

// in lib/foo.dart:

import 'bar.dart';

Relative imports from outside lib/.

// in test/foo.dart:

import '../tool/bar.dart';

Discussion

Discussion checklist

srawlins commented 1 year ago

FWIW, I am proposing this primarily as due diligence in deleting the (not implemented) analyzer Hint. But also I would support this rule. I don't see it happen much, but it's a legitimate request, helpful for new users (as our lib/ setup is bespoke).