<commit_before>// Copyright (c) 2017, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:collection';

import 'package:pub_semver/pub_semver.dart';

import 'analyzer_output.dart';
import 'license.dart';
import 'platform.dart';
import 'pub_summary.dart';
import 'pubspec.dart';
import 'utils.dart' show toRelativePath;

class DartFileSummary {
  final String uri;
  final int size;

  /// If this file is formatted with `dartfmt`.
  ///
  /// `true` if it is.
  /// `false` if it is not.
  /// `null` if `dartfmt` failed while running.
  final bool isFormatted;
  final List<AnalyzerOutput> analyzerItems;
  final List<String> directLibs;
  final List<String> transitiveLibs;
  final PlatformInfo platform;

  DartFileSummary(
    this.uri,
    this.size,
    this.isFormatted,
    this.analyzerItems,
    this.directLibs,
    this.transitiveLibs,
    this.platform,
  );

  factory DartFileSummary.fromJson(Map<String, dynamic> json) =>
      new DartFileSummary(
        json['uri'],
        json['size'],
        json['isFormatted'],
        (json['analyzerItems'] as List)
            ?.map((Map m) => new AnalyzerOutput.fromJson(m))
            ?.toList(growable: false),
        json['directLibs'],
        json['transitiveLibs'],
        json['platform'] == null
            ? null
            : new PlatformInfo.fromJson(json['platform']),
      );

  /// The relative path in the package archive.
  String get path => toRelativePath(uri);
  bool get isInBin => path.startsWith('bin/');
  bool get isInLib => path.startsWith('lib/');
  bool get isInLibSrc => path.startsWith('lib/src/');

  /// Whether the file provides a public API for the package users.
  bool get isPublicApi => isInLib && !isInLibSrc;

  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{
      'uri': uri,
      'size': size,
      'isFormatted': isFormatted,
      'analyzerItems': analyzerItems?.map((item) => item.toJson())?.toList()
    };

    var other = {
      'directLibs': directLibs,
      'transitiveLibs': transitiveLibs,
      'platform': platform,
    };

    other.forEach((k, v) {
      if (v != null) {
        map[k] = v;
      }
    });

    return map;
  }
}<commit_msg>Detect import that point outside of the lib. Fixes #44. (#46)<commit_after>// Copyright (c) 2017, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:collection';

import 'package:pub_semver/pub_semver.dart';

import 'analyzer_output.dart';
import 'license.dart';
import 'platform.dart';
import 'pub_summary.dart';
import 'pubspec.dart';
import 'utils.dart' show toRelativePath;

class DartFileSummary {
  final String uri;
  final int size;

  /// If this file is formatted with `dartfmt`.
  ///
  /// `true` if it is.
  /// `false` if it is not.
  /// `null` if `dartfmt` failed while running.
  final bool isFormatted;
  final List<AnalyzerOutput> analyzerItems;
  final List<String> directLibs;
  final List<String> transitiveLibs;
  final PlatformInfo platform;

  DartFileSummary(
    this.uri,
    this.size,
    this.isFormatted,
    this.analyzerItems,
    this.directLibs,
    this.transitiveLibs,
    this.platform,
  );

  factory DartFileSummary.fromJson(Map<String, dynamic> json) =>
      new DartFileSummary(
        json['uri'],
        json['size'],
        json['isFormatted'],
        (json['analyzerItems'] as List)
            ?.map((Map m) => new AnalyzerOutput.fromJson(m))
            ?.toList(growable: false),
        json['directLibs'],
        json['transitiveLibs'],
        json['platform'] == null
            ? null
            : new PlatformInfo.fromJson(json['platform']),
      );

  /// The relative path in the package archive.
  String get path => toRelativePath(uri);
  bool get isInBin => path.startsWith('bin/');
  bool get isInLib => path.startsWith('lib/');
  bool get isInLibSrc => path.startsWith('lib/src/');

  /// Whether the file provides a public API for the package users.
  bool get isPublicApi => isInLib && !isInLibSrc;

  /// Whether the file has any local import that point outside of the lib/
  bool get hasOutsideLibDependency =>
      directLibs != null &&
      directLibs.any((String lib) => lib.startsWith('path:'));

  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{
      'uri': uri,
      'size': size,
      'isFormatted': isFormatted,
      'analyzerItems': analyzerItems?.map((item) => item.toJson())?.toList()
    };

    var other = {
      'directLibs': directLibs,
      'transitiveLibs': transitiveLibs,
      'platform': platform,
    };

    other.forEach((k, v) {
      if (v != null) {
        map[k] = v;
      }
    });

    return map;
  }
}
{'fraction_non_alphanumeric': 0.13333333333333333, 'fraction_numerical': 0.0024316109422492403, 'mean_word_length': 2.8683385579937304, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6303259', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1543, 'n_words': 441}