no-relative-parent-imports.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';var _moduleVisitor = require('eslint-module-utils/moduleVisitor');var _moduleVisitor2 = _interopRequireDefault(_moduleVisitor);
  2. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);
  3. var _path = require('path');
  4. var _resolve = require('eslint-module-utils/resolve');var _resolve2 = _interopRequireDefault(_resolve);
  5. var _importType = require('../core/importType');var _importType2 = _interopRequireDefault(_importType);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
  6. module.exports = {
  7. meta: {
  8. type: 'suggestion',
  9. docs: {
  10. category: 'Static analysis',
  11. description: 'Forbid importing modules from parent directories.',
  12. url: (0, _docsUrl2['default'])('no-relative-parent-imports') },
  13. schema: [(0, _moduleVisitor.makeOptionsSchema)()] },
  14. create: function () {function noRelativePackages(context) {
  15. var myPath = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
  16. if (myPath === '<text>') {return {};} // can't check a non-file
  17. function checkSourceValue(sourceNode) {
  18. var depPath = sourceNode.value;
  19. if ((0, _importType2['default'])(depPath, context) === 'external') {// ignore packages
  20. return;
  21. }
  22. var absDepPath = (0, _resolve2['default'])(depPath, context);
  23. if (!absDepPath) {// unable to resolve path
  24. return;
  25. }
  26. var relDepPath = (0, _path.relative)((0, _path.dirname)(myPath), absDepPath);
  27. if ((0, _importType2['default'])(relDepPath, context) === 'parent') {
  28. context.report({
  29. node: sourceNode,
  30. message: 'Relative imports from parent directories are not allowed. Please either pass what you\'re importing through at runtime (dependency injection), move `' + String((0, _path.basename)(myPath)) + '` to same directory as `' + String(depPath) + '` or consider making `' + String(depPath) + '` a package.' });
  31. }
  32. }
  33. return (0, _moduleVisitor2['default'])(checkSourceValue, context.options[0]);
  34. }return noRelativePackages;}() };
  35. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4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