{"size": 4332, "ext": "js", "max_stars_repo_path": "node_modules/gatsby/dist/schema/type-conflict-reporter.js", "max_stars_repo_name": "hughpearse/gatsby-starter-netlify-cms", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "node_modules/gatsby/dist/schema/type-conflict-reporter.js", "max_issues_repo_name": "hughpearse/gatsby-starter-netlify-cms", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "node_modules/gatsby/dist/schema/type-conflict-reporter.js", "max_forks_repo_name": "hughpearse/gatsby-starter-netlify-cms", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.7692307692, "max_line_length": 118, "alphanum_fraction": 0.6650507849}
"use strict";

var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var _ = require(`lodash`);
var report = require(`gatsby-cli/lib/reporter`);
var typeOf = require(`type-of`);
var util = require(`util`);

var _require = require(`./node-tracking`),
    findRootNodeAncestor = _require.findRootNodeAncestor;

var isNodeWithDescription = function isNodeWithDescription(node) {
  return node && node.internal && node.internal.description;
};

var findNodeDescription = function findNodeDescription(obj) {
  if (obj) {
    var node = findRootNodeAncestor(obj, isNodeWithDescription);
    if (isNodeWithDescription(node)) {
      return node.internal.description;
    }
  }
  return ``;
};

var formatValue = function formatValue(value) {
  if (!_.isArray(value)) {
    return util.inspect(value, {
      colors: true,
      depth: 0,
      breakLength: Infinity
    });
  }

  var wasElipsisLast = false;
  var usedTypes = [];
  var output = [];

  value.forEach(function (item) {
    var type = typeOf(item);
    if (usedTypes.indexOf(type) !== -1) {
      if (!wasElipsisLast) {
        output.push(`...`);
        wasElipsisLast = true;
      }
    } else {
      output.push(formatValue(item));
      wasElipsisLast = false;
      usedTypes.push(type);
    }
  });

  return `[ ${output.join(`, `)} ]`;
};

var TypeConflictEntry = function () {
  function TypeConflictEntry(selector) {
    (0, _classCallCheck3.default)(this, TypeConflictEntry);

    this.selector = selector;
    this.types = new Map();
  }

  TypeConflictEntry.prototype.addExample = function addExample(_ref) {
    var value = _ref.value,
        type = _ref.type,
        parent = _ref.parent;

    this.types.set(type, {
      value,
      description: findNodeDescription(parent)
    });
  };

  TypeConflictEntry.prototype.printEntry = function printEntry() {
    var sortedByTypeName = _.sortBy(this.types.entries(), function (_ref2) {
      var typeName = _ref2[0],
          value = _ref2[1];
      return typeName;
    });

    report.log(`${this.selector}:${sortedByTypeName.map(function (_ref3) {
      var typeName = _ref3[0],
          _ref3$ = _ref3[1],
          value = _ref3$.value,
          description = _ref3$.description;
      return `\n - type: ${typeName}\n   value: ${formatValue(value)}${description && `\n   source: ${description}`}`;
    }).join(``)}`);
  };

  return TypeConflictEntry;
}();

var TypeConflictReporter = function () {
  function TypeConflictReporter() {
    (0, _classCallCheck3.default)(this, TypeConflictReporter);

    this.entries = new Map();
  }

  TypeConflictReporter.prototype.clearConflicts = function clearConflicts() {
    this.entries.clear();
  };

  TypeConflictReporter.prototype.getEntryFromSelector = function getEntryFromSelector(selector) {
    var dataEntry = this.entries.get(selector);

    if (!dataEntry) {
      dataEntry = new TypeConflictEntry(selector);
      this.entries.set(selector, dataEntry);
    }

    return dataEntry;
  };

  TypeConflictReporter.prototype.addConflict = function addConflict(selector, examples) {
    if (selector.substring(0, 11) === `SitePlugin.`) {
      // Don't store and print out type conflicts in plugins.
      // This is out of user control so he can't do anything
      // to hide those.
      return;
    }

    var entry = this.getEntryFromSelector(selector);
    examples.filter(function (example) {
      return example.value != null;
    }).forEach(function (example) {
      return entry.addExample(example);
    });
  };

  TypeConflictReporter.prototype.printConflicts = function printConflicts() {
    if (this.entries.size > 0) {
      report.warn(`There are conflicting field types in your data. GraphQL schema will omit those fields.`);
      this.entries.forEach(function (entry) {
        return entry.printEntry();
      });
    }
  };

  return TypeConflictReporter;
}();

var typeConflictReporter = new TypeConflictReporter();

var printConflicts = function printConflicts() {
  typeConflictReporter.printConflicts();
};

module.exports = { typeConflictReporter, printConflicts, TypeConflictEntry };
//# sourceMappingURL=type-conflict-reporter.js.map