64 lines
5.6 KiB
JavaScript
64 lines
5.6 KiB
JavaScript
'use strict';
|
|
|
|
var _moduleVisitor = require('eslint-module-utils/moduleVisitor');
|
|
|
|
var _moduleVisitor2 = _interopRequireDefault(_moduleVisitor);
|
|
|
|
var _docsUrl = require('../docsUrl');
|
|
|
|
var _docsUrl2 = _interopRequireDefault(_docsUrl);
|
|
|
|
var _path = require('path');
|
|
|
|
var _resolve = require('eslint-module-utils/resolve');
|
|
|
|
var _resolve2 = _interopRequireDefault(_resolve);
|
|
|
|
var _importType = require('../core/importType');
|
|
|
|
var _importType2 = _interopRequireDefault(_importType);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
module.exports = {
|
|
meta: {
|
|
type: 'suggestion',
|
|
docs: {
|
|
url: (0, _docsUrl2.default)('no-relative-parent-imports')
|
|
},
|
|
schema: [(0, _moduleVisitor.makeOptionsSchema)()]
|
|
},
|
|
|
|
create: function noRelativePackages(context) {
|
|
const myPath = context.getFilename();
|
|
if (myPath === '<text>') return {}; // can't check a non-file
|
|
|
|
function checkSourceValue(sourceNode) {
|
|
const depPath = sourceNode.value;
|
|
|
|
if ((0, _importType2.default)(depPath, context) === 'external') {
|
|
// ignore packages
|
|
return;
|
|
}
|
|
|
|
const absDepPath = (0, _resolve2.default)(depPath, context);
|
|
|
|
if (!absDepPath) {
|
|
// unable to resolve path
|
|
return;
|
|
}
|
|
|
|
const relDepPath = (0, _path.relative)((0, _path.dirname)(myPath), absDepPath);
|
|
|
|
if ((0, _importType2.default)(relDepPath, context) === 'parent') {
|
|
context.report({
|
|
node: sourceNode,
|
|
message: 'Relative imports from parent directories are not allowed. ' + `Please either pass what you're importing through at runtime ` + `(dependency injection), move \`${(0, _path.basename)(myPath)}\` to same ` + `directory as \`${depPath}\` or consider making \`${depPath}\` a package.`
|
|
});
|
|
}
|
|
}
|
|
|
|
return (0, _moduleVisitor2.default)(checkSourceValue, context.options[0]);
|
|
}
|
|
};
|
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