odin-javascript-exercises/node_modules/eslint-plugin-import/lib/rules/namespace.js

193 lines
21 KiB
JavaScript
Raw Normal View History

2017-12-15 10:56:14 -08:00
'use strict';
var _ExportMap = require('../ExportMap');
var _ExportMap2 = _interopRequireDefault(_ExportMap);
var _importDeclaration = require('../importDeclaration');
var _importDeclaration2 = _interopRequireDefault(_importDeclaration);
var _declaredScope = require('eslint-module-utils/declaredScope');
var _declaredScope2 = _interopRequireDefault(_declaredScope);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
module.exports = {
meta: {
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 (let 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 (let property of pattern.properties) {
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);
testKey(property.value, namespace.get(property.key.name).namespace, path);
path.pop();
}
}
testKey(id, namespaces.get(init.name));
}
};
}
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJ1bGVzL25hbWVzcGFjZS5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsInNjaGVtYSIsImNyZWF0ZSIsIm5hbWVzcGFjZVJ1bGUiLCJjb250ZXh0Iiwib3B0aW9ucyIsImFsbG93Q29tcHV0ZWQiLCJuYW1lc3BhY2VzIiwiTWFwIiwibWFrZU1lc3NhZ2UiLCJsYXN0IiwibmFtZXBhdGgiLCJuYW1lIiwibGVuZ3RoIiwiam9pbiIsImJvZHkiLCJwcm9jZXNzQm9keVN0YXRlbWVudCIsImRlY2xhcmF0aW9uIiwidHlwZSIsInNwZWNpZmllcnMiLCJpbXBvcnRzIiwiZ2V0Iiwic291cmNlIiwidmFsdWUiLCJlcnJvcnMiLCJyZXBvcnRFcnJvcnMiLCJzcGVjaWZpZXIiLCJzaXplIiwicmVwb3J0Iiwic2V0IiwibG9jYWwiLCJpbXBvcnRlZCIsIm5hbWVzcGFjZSIsImZvckVhY2giLCJkZXJlZmVyZW5jZSIsIm9iamVjdCIsImhhcyIsInBhcmVudCIsImxlZnQiLCJjb21wdXRlZCIsInByb3BlcnR5IiwiZXhwb3J0ZWQiLCJwdXNoIiwiaWQiLCJpbml0IiwidGVzdEtleSIsInBhdHRlcm4iLCJwYXRoIiwicHJvcGVydGllcyIsImtleSIsIm5vZGUiLCJtZXNzYWdlIiwicG9wIl0sIm1hcHBpbmdzIjoiOztBQUFBOzs7O0FBQ0E7Ozs7QUFDQTs7Ozs7O0FBRUFBLE9BQU9DLE9BQVAsR0FBaUI7QUFDZkMsUUFBTTtBQUNKQyxZQUFRLENBQ047QUFDRSxjQUFRLFFBRFY7QUFFRSxvQkFBYztBQUNaLHlCQUFpQjtBQUNmLHlCQUNFLHlFQUNBLHVCQUhhO0FBSWYsa0JB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