223 lines
24 KiB
JavaScript
223 lines
24 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 }; }
|
||
|
|
||
|
module.exports = {
|
||
|
meta: {
|
||
|
type: 'problem',
|
||
|
docs: {
|
||
|
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 namespaceRule(context) {
|
||
|
|
||
|
// read options
|
||
|
var _ref = context.options[0] || {},
|
||
|
_ref$allowComputed = _ref.allowComputed;
|
||
|
|
||
|
const allowComputed = _ref$allowComputed === undefined ? false : _ref$allowComputed;
|
||
|
|
||
|
|
||
|
const namespaces = new Map();
|
||
|
|
||
|
function makeMessage(last, namepath) {
|
||
|
return `'${last.name}' not found in` + (namepath.length > 1 ? ' deeply ' : ' ') + `imported namespace '${namepath.join('.')}'.`;
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
|
||
|
// pick up all imports at body entry time, to properly respect hoisting
|
||
|
Program: function (_ref2) {
|
||
|
let body = _ref2.body;
|
||
|
|
||
|
function processBodyStatement(declaration) {
|
||
|
if (declaration.type !== 'ImportDeclaration') return;
|
||
|
|
||
|
if (declaration.specifiers.length === 0) return;
|
||
|
|
||
|
const imports = _ExportMap2.default.get(declaration.source.value, context);
|
||
|
if (imports == null) return null;
|
||
|
|
||
|
if (imports.errors.length) {
|
||
|
imports.reportErrors(context, declaration);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
for (const specifier of declaration.specifiers) {
|
||
|
switch (specifier.type) {
|
||
|
case 'ImportNamespaceSpecifier':
|
||
|
if (!imports.size) {
|
||
|
context.report(specifier, `No exported names found in module '${declaration.source.value}'.`);
|
||
|
}
|
||
|
namespaces.set(specifier.local.name, imports);
|
||
|
break;
|
||
|
case 'ImportDefaultSpecifier':
|
||
|
case 'ImportSpecifier':
|
||
|
{
|
||
|
const meta = imports.get(
|
||
|
// default to 'default' for default http://i.imgur.com/nj6qAWy.jpg
|
||
|
specifier.imported ? specifier.imported.name : 'default');
|
||
|
if (!meta || !meta.namespace) break;
|
||
|
namespaces.set(specifier.local.name, meta.namespace);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
body.forEach(processBodyStatement);
|
||
|
},
|
||
|
|
||
|
// same as above, but does not add names to local map
|
||
|
ExportNamespaceSpecifier: function (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 '${declaration.source.value}'.`);
|
||
|
}
|
||
|
},
|
||
|
|
||
|
// todo: check for possible redefinition
|
||
|
|
||
|
MemberExpression: function (dereference) {
|
||
|
if (dereference.object.type !== 'Identifier') return;
|
||
|
if (!namespaces.has(dereference.object.name)) return;
|
||
|
|
||
|
if (dereference.parent.type === 'AssignmentExpression' && dereference.parent.left === dereference) {
|
||
|
context.report(dereference.parent, `Assignment to member of namespace '${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 \'' + dereference.object.name + '\'.');
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!namespace.has(dereference.property.name)) {
|
||
|
context.report(dereference.property, makeMessage(dereference.property, namepath));
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
const exported = namespace.get(dereference.property.name);
|
||
|
if (exported == null) return;
|
||
|
|
||
|
// stash and pop
|
||
|
namepath.push(dereference.property.name);
|
||
|
namespace = exported.namespace;
|
||
|
dereference = dereference.parent;
|
||
|
}
|
||
|
},
|
||
|
|
||
|
VariableDeclarator: function (_ref3) {
|
||
|
let 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) {
|
||
|
let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [init.name];
|
||
|
|
||
|
if (!(namespace instanceof _ExportMap2.default)) return;
|
||
|
|
||
|
if (pattern.type !== 'ObjectPattern') return;
|
||
|
|
||
|
for (const property of pattern.properties) {
|
||
|
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);
|
||
|
const dependencyExportMap = namespace.get(property.key.name);
|
||
|
// could be null when ignored or ambiguous
|
||
|
if (dependencyExportMap !== null) {
|
||
|
testKey(property.value, dependencyExportMap.namespace, path);
|
||
|
}
|
||
|
path.pop();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
testKey(id, namespaces.get(init.name));
|
||
|
},
|
||
|
|
||
|
JSXMemberExpression: function (_ref4) {
|
||
|
let 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])
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
};
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uYW1lc3BhY2UuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsInNjaGVtYSIsImNyZWF0ZSIsIm5hbWVzcGFjZVJ1bGUiLCJjb250ZXh0Iiwib3B0aW9ucyIsImFsbG93Q29tcHV0ZWQiLCJuYW1lc3BhY2VzIiwiTWFwIiwibWFrZU1lc3NhZ2UiLCJsYXN0IiwibmFtZXBhdGgiLCJuYW1lIiwibGVuZ3RoIiwiam9pbiIsIlByb2dyYW0iLCJib2R5IiwicHJvY2Vzc0JvZHlTdGF0ZW1lbnQiLCJkZWNsYXJ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