no-relative-packages.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';var _path = require('path');var _path2 = _interopRequireDefault(_path);
  2. var _readPkgUp = require('eslint-module-utils/readPkgUp');var _readPkgUp2 = _interopRequireDefault(_readPkgUp);
  3. var _resolve = require('eslint-module-utils/resolve');var _resolve2 = _interopRequireDefault(_resolve);
  4. var _moduleVisitor = require('eslint-module-utils/moduleVisitor');var _moduleVisitor2 = _interopRequireDefault(_moduleVisitor);
  5. var _importType = require('../core/importType');var _importType2 = _interopRequireDefault(_importType);
  6. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
  7. /** @param {string} filePath */
  8. function toPosixPath(filePath) {
  9. return filePath.replace(/\\/g, '/');
  10. }
  11. function findNamedPackage(filePath) {
  12. var found = (0, _readPkgUp2['default'])({ cwd: filePath });
  13. if (found.pkg && !found.pkg.name) {
  14. return findNamedPackage(_path2['default'].join(found.path, '../..'));
  15. }
  16. return found;
  17. }
  18. function checkImportForRelativePackage(context, importPath, node) {
  19. var potentialViolationTypes = ['parent', 'index', 'sibling'];
  20. if (potentialViolationTypes.indexOf((0, _importType2['default'])(importPath, context)) === -1) {
  21. return;
  22. }
  23. var resolvedImport = (0, _resolve2['default'])(importPath, context);
  24. var resolvedContext = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
  25. if (!resolvedImport || !resolvedContext) {
  26. return;
  27. }
  28. var importPkg = findNamedPackage(resolvedImport);
  29. var contextPkg = findNamedPackage(resolvedContext);
  30. if (importPkg.pkg && contextPkg.pkg && importPkg.pkg.name !== contextPkg.pkg.name) {
  31. var importBaseName = _path2['default'].basename(importPath);
  32. var importRoot = _path2['default'].dirname(importPkg.path);
  33. var properPath = _path2['default'].relative(importRoot, resolvedImport);
  34. var properImport = _path2['default'].join(
  35. importPkg.pkg.name,
  36. _path2['default'].dirname(properPath),
  37. importBaseName === _path2['default'].basename(importRoot) ? '' : importBaseName);
  38. context.report({
  39. node: node,
  40. message: 'Relative import from another package is not allowed. Use `' + String(properImport) + '` instead of `' + String(importPath) + '`',
  41. fix: function () {function fix(fixer) {return fixer.replaceText(node, JSON.stringify(toPosixPath(properImport)));}return fix;}() });
  42. }
  43. }
  44. module.exports = {
  45. meta: {
  46. type: 'suggestion',
  47. docs: {
  48. url: (0, _docsUrl2['default'])('no-relative-packages') },
  49. fixable: 'code',
  50. schema: [(0, _moduleVisitor.makeOptionsSchema)()] },
  51. create: function () {function create(context) {
  52. return (0, _moduleVisitor2['default'])(function (source) {return checkImportForRelativePackage(context, source.value, source);}, context.options[0]);
  53. }return create;}() };
  54. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1yZWxhdGl2ZS1wYWNrYWdlcy5qcyJdLCJuYW1lcyI6WyJ0b1Bvc2l4UGF0aCIsImZpbGVQYXRoIiwicmVwbGFjZSIsImZpbmROYW1lZFBhY2thZ2UiLCJmb3VuZCIsImN3ZCIsInBrZyIsIm5hbWUiLCJwYXRoIiwiam9pbiIsImNoZWNrSW1wb3J0Rm9yUmVsYXRpdmVQYWNrYWdlIiwiY29udGV4dCIsImltcG9ydFBhdGgiLCJub2RlIiwicG90ZW50aWFsVmlvbGF0aW9uVHlwZXMiLCJpbmRleE9mIiwicmVzb2x2ZWRJbXBvcnQiLCJyZXNvbHZlZENvbnRleHQiLCJnZXRQaHlzaWNhbEZpbGVuYW1lIiwiZ2V0RmlsZW5hbWUiLCJpbXBvcnRQa2ciLCJjb250ZXh0UGtnIiwiaW1wb3J0QmFzZU5hbWUiLCJiYXNlbmFtZSIsImltcG9ydFJvb3QiLCJkaXJuYW1lIiwicHJvcGVyUGF0aCIsInJlbGF0aXZlIiwicHJvcGVySW1wb3J0IiwicmVwb3J0IiwibWVzc2FnZSIsImZpe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