63 lines
5.5 KiB
JavaScript
63 lines
5.5 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: {
|
|
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]);
|
|
}
|
|
};
|
|
//# sourceMappingURL=data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