'use strict';

var _containsPath = require('contains-path');

var _containsPath2 = _interopRequireDefault(_containsPath);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _resolve = require('../core/resolve');

var _resolve2 = _interopRequireDefault(_resolve);

var _staticRequire = require('../core/staticRequire');

var _staticRequire2 = _interopRequireDefault(_staticRequire);

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

module.exports = function noRestrictedPaths(context) {
  var options = context.options[0] || {};
  var restrictedPaths = options.zones || [];
  var basePath = options.basePath || process.cwd();
  var currentFilename = context.getFilename();
  var matchingZones = restrictedPaths.filter(function (zone) {
    var targetPath = _path2.default.resolve(basePath, zone.target);

    return (0, _containsPath2.default)(currentFilename, targetPath);
  });

  function checkForRestrictedImportPath(importPath, node) {
    var absoluteImportPath = (0, _resolve2.default)(importPath, context);

    if (!absoluteImportPath) {
      return;
    }

    matchingZones.forEach(function (zone) {
      var absoluteFrom = _path2.default.resolve(basePath, zone.from);

      if ((0, _containsPath2.default)(absoluteImportPath, absoluteFrom)) {
        context.report({
          node: node,
          message: 'Unexpected path "' + importPath + '" imported in restricted zone.'
        });
      }
    });
  }

  return {
    ImportDeclaration: function ImportDeclaration(node) {
      checkForRestrictedImportPath(node.source.value, node.source);
    },
    CallExpression: function CallExpression(node) {
      if ((0, _staticRequire2.default)(node)) {
        var _node$arguments = node.arguments;
        var firstArgument = _node$arguments[0];


        checkForRestrictedImportPath(firstArgument.value, firstArgument);
      }
    }
  };
};

module.exports.schema = [{
  type: 'object',
  properties: {
    zones: {
      type: 'array',
      minItems: 1,
      items: {
        type: 'object',
        properties: {
          target: { type: 'string' },
          from: { type: 'string' }
        },
        additionalProperties: false
      }
    },
    basePath: { type: 'string' }
  },
  additionalProperties: false
}];
//# sourceMappingURL=data:application/json;base64,[encoded data, 4544 characters]