218 lines
26 KiB
JavaScript
218 lines
26 KiB
JavaScript
|
'use strict';var _declaredScope = require('eslint-module-utils/declaredScope');var _declaredScope2 = _interopRequireDefault(_declaredScope);
|
||
|
var _ExportMap = require('../ExportMap');var _ExportMap2 = _interopRequireDefault(_ExportMap);
|
||
|
var _importDeclaration = require('../importDeclaration');var _importDeclaration2 = _interopRequireDefault(_importDeclaration);
|
||
|
var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
|
||
|
|
||
|
function processBodyStatement(context, namespaces, declaration) {
|
||
|
if (declaration.type !== 'ImportDeclaration') {return;}
|
||
|
|
||
|
if (declaration.specifiers.length === 0) {return;}
|
||
|
|
||
|
var imports = _ExportMap2['default'].get(declaration.source.value, context);
|
||
|
if (imports == null) {return null;}
|
||
|
|
||
|
if (imports.errors.length > 0) {
|
||
|
imports.reportErrors(context, declaration);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
declaration.specifiers.forEach(function (specifier) {
|
||
|
switch (specifier.type) {
|
||
|
case 'ImportNamespaceSpecifier':
|
||
|
if (!imports.size) {
|
||
|
context.report(
|
||
|
specifier, 'No exported names found in module \'' + String(
|
||
|
declaration.source.value) + '\'.');
|
||
|
|
||
|
}
|
||
|
namespaces.set(specifier.local.name, imports);
|
||
|
break;
|
||
|
case 'ImportDefaultSpecifier':
|
||
|
case 'ImportSpecifier':{
|
||
|
var meta = imports.get(
|
||
|
// default to 'default' for default https://i.imgur.com/nj6qAWy.jpg
|
||
|
specifier.imported ? specifier.imported.name || specifier.imported.value : 'default');
|
||
|
|
||
|
if (!meta || !meta.namespace) {break;}
|
||
|
namespaces.set(specifier.local.name, meta.namespace);
|
||
|
break;
|
||
|
}
|
||
|
default:}
|
||
|
|
||
|
});
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
meta: {
|
||
|
type: 'problem',
|
||
|
docs: {
|
||
|
category: 'Static analysis',
|
||
|
description: 'Ensure imported namespaces contain dereferenced properties as they are dereferenced.',
|
||
|
url: (0, _docsUrl2['default'])('namespace') },
|
||
|
|
||
|
|
||
|
schema: [
|
||
|
{
|
||
|
type: 'object',
|
||
|
properties: {
|
||
|
allowComputed: {
|
||
|
description: 'If `false`, will report computed (and thus, un-lintable) references to namespace members.',
|
||
|
type: 'boolean',
|
||
|
'default': false } },
|
||
|
|
||
|
|
||
|
additionalProperties: false }] },
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
create: function () {function namespaceRule(context) {
|
||
|
// read options
|
||
|
var _ref =
|
||
|
|
||
|
context.options[0] || {},_ref$allowComputed = _ref.allowComputed,allowComputed = _ref$allowComputed === undefined ? false : _ref$allowComputed;
|
||
|
|
||
|
var namespaces = new Map();
|
||
|
|
||
|
function makeMessage(last, namepath) {
|
||
|
return '\'' + String(last.name) + '\' not found in ' + (namepath.length > 1 ? 'deeply ' : '') + 'imported namespace \'' + String(namepath.join('.')) + '\'.';
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
// pick up all imports at body entry time, to properly respect hoisting
|
||
|
Program: function () {function Program(_ref2) {var body = _ref2.body;
|
||
|
body.forEach(function (x) {processBodyStatement(context, namespaces, x);});
|
||
|
}return Program;}(),
|
||
|
|
||
|
// same as above, but does not add names to local map
|
||
|
ExportNamespaceSpecifier: function () {function ExportNamespaceSpecifier(namespace) {
|
||
|
var declaration = (0, _importDeclaration2['default'])(context);
|
||
|
|
||
|
var imports = _ExportMap2['default'].get(declaration.source.value, context);
|
||
|
if (imports == null) {return null;}
|
||
|
|
||
|
if (imports.errors.length) {
|
||
|
imports.reportErrors(context, declaration);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!imports.size) {
|
||
|
context.report(
|
||
|
namespace, 'No exported names found in module \'' + String(
|
||
|
declaration.source.value) + '\'.');
|
||
|
|
||
|
}
|
||
|
}return ExportNamespaceSpecifier;}(),
|
||
|
|
||
|
// todo: check for possible redefinition
|
||
|
|
||
|
MemberExpression: function () {function MemberExpression(dereference) {
|
||
|
if (dereference.object.type !== 'Identifier') {return;}
|
||
|
if (!namespaces.has(dereference.object.name)) {return;}
|
||
|
if ((0, _declaredScope2['default'])(context, dereference.object.name) !== 'module') {return;}
|
||
|
|
||
|
if (dereference.parent.type === 'AssignmentExpression' && dereference.parent.left === dereference) {
|
||
|
context.report(
|
||
|
dereference.parent, 'Assignment to member of namespace \'' + String(
|
||
|
dereference.object.name) + '\'.');
|
||
|
|
||
|
}
|
||
|
|
||
|
// go deep
|
||
|
var namespace = namespaces.get(dereference.object.name);
|
||
|
var namepath = [dereference.object.name];
|
||
|
// while property is namespace and parent is member expression, keep validating
|
||
|
while (namespace instanceof _ExportMap2['default'] && dereference.type === 'MemberExpression') {
|
||
|
if (dereference.computed) {
|
||
|
if (!allowComputed) {
|
||
|
context.report(
|
||
|
dereference.property, 'Unable to validate computed reference to imported namespace \'' + String(
|
||
|
dereference.object.name) + '\'.');
|
||
|
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!namespace.has(dereference.property.name)) {
|
||
|
context.report(
|
||
|
dereference.property,
|
||
|
makeMessage(dereference.property, namepath));
|
||
|
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
var exported = namespace.get(dereference.property.name);
|
||
|
if (exported == null) {return;}
|
||
|
|
||
|
// stash and pop
|
||
|
namepath.push(dereference.property.name);
|
||
|
namespace = exported.namespace;
|
||
|
dereference = dereference.parent;
|
||
|
}
|
||
|
}return MemberExpression;}(),
|
||
|
|
||
|
VariableDeclarator: function () {function VariableDeclarator(_ref3) {var id = _ref3.id,init = _ref3.init;
|
||
|
if (init == null) {return;}
|
||
|
if (init.type !== 'Identifier') {return;}
|
||
|
if (!namespaces.has(init.name)) {return;}
|
||
|
|
||
|
// check for redefinition in intermediate scopes
|
||
|
if ((0, _declaredScope2['default'])(context, init.name) !== 'module') {return;}
|
||
|
|
||
|
// DFS traverse child namespaces
|
||
|
function testKey(pattern, namespace) {var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [init.name];
|
||
|
if (!(namespace instanceof _ExportMap2['default'])) {return;}
|
||
|
|
||
|
if (pattern.type !== 'ObjectPattern') {return;}var _iteratorNormalCompletion = true;var _didIteratorError = false;var _iteratorError = undefined;try {
|
||
|
|
||
|
for (var _iterator = pattern.properties[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {var property = _step.value;
|
||
|
if (
|
||
|
property.type === 'ExperimentalRestProperty' ||
|
||
|
property.type === 'RestElement' ||
|
||
|
!property.key)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if (property.key.type !== 'Identifier') {
|
||
|
context.report({
|
||
|
node: property,
|
||
|
message: 'Only destructure top-level names.' });
|
||
|
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if (!namespace.has(property.key.name)) {
|
||
|
context.report({
|
||
|
node: property,
|
||
|
message: makeMessage(property.key, path) });
|
||
|
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
path.push(property.key.name);
|
||
|
var dependencyExportMap = namespace.get(property.key.name);
|
||
|
// could be null when ignored or ambiguous
|
||
|
if (dependencyExportMap !== null) {
|
||
|
testKey(property.value, dependencyExportMap.namespace, path);
|
||
|
}
|
||
|
path.pop();
|
||
|
}} catch (err) {_didIteratorError = true;_iteratorError = err;} finally {try {if (!_iteratorNormalCompletion && _iterator['return']) {_iterator['return']();}} finally {if (_didIteratorError) {throw _iteratorError;}}}
|
||
|
}
|
||
|
|
||
|
testKey(id, namespaces.get(init.name));
|
||
|
}return VariableDeclarator;}(),
|
||
|
|
||
|
JSXMemberExpression: function () {function JSXMemberExpression(_ref4) {var object = _ref4.object,property = _ref4.property;
|
||
|
if (!namespaces.has(object.name)) {return;}
|
||
|
var namespace = namespaces.get(object.name);
|
||
|
if (!namespace.has(property.name)) {
|
||
|
context.report({
|
||
|
node: property,
|
||
|
message: makeMessage(property, [object.name]) });
|
||
|
|
||
|
}
|
||
|
}return JSXMemberExpression;}() };
|
||
|
|
||
|
}return namespaceRule;}() };
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uYW1lc3BhY2UuanMiXSwibmFtZXMiOlsicHJvY2Vzc0JvZHlTdGF0ZW1lbnQiLCJjb250ZXh0IiwibmFtZXNwYWNlcyIsImRlY2xhcmF0aW9uIiwidHlwZSIsInNwZWNpZmllcnMiLCJsZW5ndGgiLCJpbXBvcnRzIiwiRXhwb3J0cyIsImdldCIsInNvdXJjZSIsInZhbHVlIiwiZXJyb3JzIiwicmVwb3J0RXJyb3JzIiwiZm9yRWFjaCIsInNwZWNpZmllciIsInNpemUiLCJyZXBvcnQiLCJzZXQiLCJsb2NhbCIsIm5hbWUiLCJtZXRhIiwiaW1wb3J0ZWQiLCJuYW1lc3BhY2UiLCJtb2R1bGUiLCJleHBvcnRzIiwiZG9jcyIsImNhdGVnb3J5IiwiZGVzY3JpcHRpb24iLCJ1cmwiLCJzY2hlbWEiLCJwcm9wZXJ0aWVzIiwiYWxsb3dDb21wdXRlZCIsImFkZGl0aW9uYWxQcm9wZXJ0aWVzIiwiY3JlYXRlIiwibmFtZXNwYWNlUnVsZSIsIm9wdGlvbnMiLCJNYXAiLCJtYWtlTWVzc2FnZSIsImxhc3QiLCJuYW1lcGF0aCIsImpvaW4iLCJQcm9ncmFtIiwiYm9keSIsIngiLCJFeHBvcnROYW1lc3BhY2VTcGVjaWZpZXIiLCJNZW1iZXJFeHByZXNzaW9uIiwiZGVyZWZlcmVuY2UiLCJvYmplY3QiLCJoYXMiLCJwYXJlbnQiLCJsZWZ0IiwiY29tcHV0ZWQiLCJwcm9wZXJ0eSIsImV4cG9ydGVkIiwicHVzaCIsIlZhcmlhYmxlRGVjbGFyYXRvciIsImlkIiwiaW5pdCIsInRlc3RLZXkiLCJwYXR0ZXJuIiwicGF0aCIsImtleSIsI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