importStatement method

String? importStatement(
  1. String path, {
  2. bool includeIgnores = false,
  3. ImportStyle style = ImportStyle.granular,
  4. BarrelImportCache barrelCache = const BarrelImportCache.empty(),
})

includeIgnores will add any ignore comments to above any show combinator

Implementation

String? importStatement(
  String path, {
  bool includeIgnores = false,
  ImportStyle style = ImportStyle.granular,
  BarrelImportCache barrelCache = const BarrelImportCache.empty(),
}) {
  if (optional) {
    return null;
  }

  var import = this.import.resolved(path);
  if (import == null) {
    return null;
  }

  var usesBarrel = false;
  if (style == ImportStyle.barrel && Import(import).isPackage) {
    final barrel = barrelCache.resolve(import, associatedElement);
    if (barrel != null) {
      import = barrel;
      usesBarrel = true;
    }
  }

  final show = usesBarrel
      ? null
      : showCombinator(includeIgnores: includeIgnores);

  final statement = [
    'import',
    "'$import'",
    if (prefix case final String prefix) 'as $prefix',
    if (show case final String combinator) combinator,
  ].join(' ').trim();

  return '$statement;';
}