webhook/node_modules/eslint-plugin-import/lib/rules/no-relative-parent-imports.js
2020-08-26 09:57:08 +10:00

49 lines
No EOL
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]);
} };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1yZWxhdGl2ZS1wYXJlbnQtaW1wb3J0cy5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsInR5cGUiLCJkb2NzIiwidXJsIiwic2NoZW1hIiwiY3JlYXRlIi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