143 lines
17 KiB
JavaScript
143 lines
17 KiB
JavaScript
|
'use strict';var _slicedToArray = function () {function sliceIterator(arr, i) {var _arr = [];var _n = true;var _d = false;var _e = undefined;try {for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {_arr.push(_s.value);if (i && _arr.length === i) break;}} catch (err) {_d = true;_e = err;} finally {try {if (!_n && _i["return"]) _i["return"]();} finally {if (_d) throw _e;}}return _arr;}return function (arr, i) {if (Array.isArray(arr)) {return arr;} else if (Symbol.iterator in Object(arr)) {return sliceIterator(arr, i);} else {throw new TypeError("Invalid attempt to destructure non-iterable instance");}};}();var _path = require('path');var path = _interopRequireWildcard(_path);
|
||
|
var _ExportMap = require('../ExportMap');var _ExportMap2 = _interopRequireDefault(_ExportMap);
|
||
|
var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}function _interopRequireWildcard(obj) {if (obj && obj.__esModule) {return obj;} else {var newObj = {};if (obj != null) {for (var key in obj) {if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key];}}newObj['default'] = obj;return newObj;}}
|
||
|
|
||
|
module.exports = {
|
||
|
meta: {
|
||
|
type: 'problem',
|
||
|
docs: {
|
||
|
category: 'Static analysis',
|
||
|
description: 'Ensure named imports correspond to a named export in the remote file.',
|
||
|
url: (0, _docsUrl2['default'])('named') },
|
||
|
|
||
|
schema: [
|
||
|
{
|
||
|
type: 'object',
|
||
|
properties: {
|
||
|
commonjs: {
|
||
|
type: 'boolean' } },
|
||
|
|
||
|
|
||
|
additionalProperties: false }] },
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
create: function () {function create(context) {
|
||
|
var options = context.options[0] || {};
|
||
|
|
||
|
function checkSpecifiers(key, type, node) {
|
||
|
// ignore local exports and type imports/exports
|
||
|
if (
|
||
|
node.source == null ||
|
||
|
node.importKind === 'type' ||
|
||
|
node.importKind === 'typeof' ||
|
||
|
node.exportKind === 'type')
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!node.specifiers.some(function (im) {return im.type === type;})) {
|
||
|
return; // no named imports/exports
|
||
|
}
|
||
|
|
||
|
var imports = _ExportMap2['default'].get(node.source.value, context);
|
||
|
if (imports == null || imports.parseGoal === 'ambiguous') {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (imports.errors.length) {
|
||
|
imports.reportErrors(context, node);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
node.specifiers.forEach(function (im) {
|
||
|
if (
|
||
|
im.type !== type
|
||
|
// ignore type imports
|
||
|
|| im.importKind === 'type' || im.importKind === 'typeof')
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var name = im[key].name || im[key].value;
|
||
|
|
||
|
var deepLookup = imports.hasDeep(name);
|
||
|
|
||
|
if (!deepLookup.found) {
|
||
|
if (deepLookup.path.length > 1) {
|
||
|
var deepPath = deepLookup.path.
|
||
|
map(function (i) {return path.relative(path.dirname(context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename()), i.path);}).
|
||
|
join(' -> ');
|
||
|
|
||
|
context.report(im[key], String(name) + ' not found via ' + String(deepPath));
|
||
|
} else {
|
||
|
context.report(im[key], String(name) + ' not found in \'' + String(node.source.value) + '\'');
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function checkRequire(node) {
|
||
|
if (
|
||
|
!options.commonjs ||
|
||
|
node.type !== 'VariableDeclarator'
|
||
|
// return if it's not an object destructure or it's an empty object destructure
|
||
|
|| !node.id || node.id.type !== 'ObjectPattern' || node.id.properties.length === 0
|
||
|
// return if there is no call expression on the right side
|
||
|
|| !node.init || node.init.type !== 'CallExpression')
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var call = node.init;var _call$arguments = _slicedToArray(
|
||
|
call.arguments, 1),source = _call$arguments[0];
|
||
|
var variableImports = node.id.properties;
|
||
|
var variableExports = _ExportMap2['default'].get(source.value, context);
|
||
|
|
||
|
if (
|
||
|
// return if it's not a commonjs require statement
|
||
|
call.callee.type !== 'Identifier' || call.callee.name !== 'require' || call.arguments.length !== 1
|
||
|
// return if it's not a string source
|
||
|
|| source.type !== 'Literal' ||
|
||
|
variableExports == null ||
|
||
|
variableExports.parseGoal === 'ambiguous')
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (variableExports.errors.length) {
|
||
|
variableExports.reportErrors(context, node);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
variableImports.forEach(function (im) {
|
||
|
if (im.type !== 'Property' || !im.key || im.key.type !== 'Identifier') {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var deepLookup = variableExports.hasDeep(im.key.name);
|
||
|
|
||
|
if (!deepLookup.found) {
|
||
|
if (deepLookup.path.length > 1) {
|
||
|
var deepPath = deepLookup.path.
|
||
|
map(function (i) {return path.relative(path.dirname(context.getFilename()), i.path);}).
|
||
|
join(' -> ');
|
||
|
|
||
|
context.report(im.key, String(im.key.name) + ' not found via ' + String(deepPath));
|
||
|
} else {
|
||
|
context.report(im.key, String(im.key.name) + ' not found in \'' + String(source.value) + '\'');
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
ImportDeclaration: checkSpecifiers.bind(null, 'imported', 'ImportSpecifier'),
|
||
|
|
||
|
ExportNamedDeclaration: checkSpecifiers.bind(null, 'local', 'ExportSpecifier'),
|
||
|
|
||
|
VariableDeclarator: checkRequire };
|
||
|
|
||
|
}return create;}() };
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uYW1lZC5qcyJdLCJuYW1lcyI6WyJwYXRoIiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsImNhdGVnb3J5IiwiZGVzY3JpcHRpb24i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